xref: /aoo41x/main/rsc/source/parser/rsckey.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_rsc.hxx"
30 /****************** I N C L U D E S **************************************/
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <ctype.h>
34 #include <string.h>
35 #include <rscall.h>
36 #include <rsctools.hxx>
37 #include <rschash.hxx>
38 #include <rsckey.hxx>
39 
40 #if defined(_MSC_VER) && (_MSC_VER >= 1200 )
41 #define _cdecl __cdecl
42 #endif
43 
44 /****************** C o d e **********************************************/
45 /****************** keyword sort function ********************************/
46 extern "C" {
47 #if defined( ZTC ) && defined( PM2 )
48     int __CLIB KeyCompare( const void * pFirst, const void * pSecond );
49 #else
50 #if defined( WNT ) && !defined( WTC ) && !defined (ICC)
51     int _cdecl KeyCompare( const void * pFirst, const void * pSecond );
52 #else
53     int KeyCompare( const void * pFirst, const void * pSecond );
54 #endif
55 #endif
56 }
57 
58 #if defined( WNT ) && !defined( WTC ) && !defined(ICC)
59 int _cdecl KeyCompare( const void * pFirst, const void * pSecond ){
60 #else
61 int KeyCompare( const void * pFirst, const void * pSecond ){
62 #endif
63     if( ((KEY_STRUCT *)pFirst)->nName > ((KEY_STRUCT *)pSecond)->nName )
64         return( 1 );
65     else if( ((KEY_STRUCT *)pFirst)->nName < ((KEY_STRUCT *)pSecond)->nName )
66         return( -1 );
67     else
68         return( 0 );
69 }
70 
71 /*************************************************************************
72 |*
73 |*    RscNameTable::RscNameTable()
74 |*
75 |*    Beschreibung      RES.DOC
76 |*    Ersterstellung    MM 28.02.91
77 |*    Letzte Aenderung  MM 28.02.91
78 |*
79 *************************************************************************/
80 RscNameTable::RscNameTable() {
81     bSort    = sal_True;
82     nEntries = 0;
83     pTable   = NULL;
84 };
85 
86 /*************************************************************************
87 |*
88 |*    RscNameTable::~RscNameTable()
89 |*
90 |*    Beschreibung
91 |*    Ersterstellung    MM 15.05.91
92 |*    Letzte Aenderung  MM 15.05.91
93 |*
94 *************************************************************************/
95 RscNameTable::~RscNameTable() {
96     if( pTable )
97         rtl_freeMemory( pTable );
98 };
99 
100 
101 /*************************************************************************
102 |*
103 |*    RscNameTable::SetSort()
104 |*
105 |*    Beschreibung      RES.DOC
106 |*    Ersterstellung    MM 28.02.91
107 |*    Letzte Aenderung  MM 28.02.91
108 |*
109 *************************************************************************/
110 void RscNameTable::SetSort( sal_Bool bSorted ){
111     bSort = bSorted;
112     if( bSort && pTable){
113         // Schluesselwort Feld sortieren
114         qsort( (void *)pTable, nEntries,
115                sizeof( KEY_STRUCT ), KeyCompare );
116     };
117 };
118 
119 /*************************************************************************
120 |*
121 |*    RscNameTable::Put()
122 |*
123 |*    Beschreibung      RES.DOC
124 |*    Ersterstellung    MM 28.02.91
125 |*    Letzte Aenderung  MM 28.02.91
126 |*
127 *************************************************************************/
128 Atom RscNameTable::Put( Atom nName, sal_uInt32 nTyp, long nValue ){
129     if( pTable )
130         pTable = (KEY_STRUCT *)
131                  rtl_reallocateMemory( (void *)pTable,
132                  ((nEntries +1) * sizeof( KEY_STRUCT )) );
133     else
134         pTable = (KEY_STRUCT *)
135                  rtl_allocateMemory( ((nEntries +1)
136                                  * sizeof( KEY_STRUCT )) );
137     pTable[ nEntries ].nName  = nName;
138     pTable[ nEntries ].nTyp   = nTyp;
139     pTable[ nEntries ].yylval = nValue;
140     nEntries++;
141     if( bSort )
142         SetSort();
143     return( nName );
144 };
145 
146 Atom RscNameTable::Put( const char * pName, sal_uInt32 nTyp, long nValue )
147 {
148     return( Put( pHS->getID( pName ), nTyp, nValue ) );
149 };
150 
151 Atom RscNameTable::Put( Atom nName, sal_uInt32 nTyp )
152 {
153     return( Put( nName, nTyp, (long)nName ) );
154 };
155 
156 Atom RscNameTable::Put( const char * pName, sal_uInt32 nTyp )
157 {
158     Atom  nId;
159 
160     nId = pHS->getID( pName );
161     return( Put( nId, nTyp, (long)nId ) );
162 };
163 
164 Atom RscNameTable::Put( Atom nName, sal_uInt32 nTyp, RscTop * pClass )
165 {
166     return( Put( nName, nTyp, (long)pClass ) );
167 };
168 
169 Atom RscNameTable::Put( const char * pName, sal_uInt32 nTyp, RscTop * pClass )
170 {
171     return( Put( pHS->getID( pName ), nTyp, (long)pClass ) );
172 };
173 
174 /*************************************************************************
175 |*
176 |*    RscNameTable::Get()
177 |*
178 |*    Beschreibung      RES.DOC
179 |*    Ersterstellung    MM 28.02.91
180 |*    Letzte Aenderung  MM 28.02.91
181 |*
182 *************************************************************************/
183 sal_Bool RscNameTable::Get( Atom nName, KEY_STRUCT * pEle ){
184     KEY_STRUCT * pKey = NULL;
185     KEY_STRUCT  aSearchName;
186     sal_uInt32  i;
187 
188     if( bSort ){
189         // Suche nach dem Schluesselwort
190         aSearchName.nName = nName;
191         pKey = (KEY_STRUCT *)bsearch(
192 #ifdef UNX
193                    (const char *) &aSearchName, (char *)pTable,
194 #else
195                    (const void *) &aSearchName, (const void *)pTable,
196 #endif
197                    nEntries, sizeof( KEY_STRUCT ), KeyCompare );
198     }
199     else{
200         i = 0;
201         while( i < nEntries && !pKey ){
202             if( pTable[ i ].nName == nName )
203                 pKey = &pTable[ i ];
204             i++;
205         };
206     };
207 
208     if( pKey ){ // Schluesselwort gefunden
209         *pEle = *pKey;
210         return( sal_True );
211     };
212     return( sal_False );
213 };
214 
215