xref: /trunk/main/rsc/source/parser/rsckey.cxx (revision 477794c1)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_rsc.hxx"
26 /****************** I N C L U D E S **************************************/
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <ctype.h>
30 #include <string.h>
31 #include <rscall.h>
32 #include <rsctools.hxx>
33 #include <rschash.hxx>
34 #include <rsckey.hxx>
35 
36 #if defined(_MSC_VER) && (_MSC_VER >= 1200 )
37 #define _cdecl __cdecl
38 #endif
39 
40 /****************** C o d e **********************************************/
41 /****************** keyword sort function ********************************/
42 extern "C" {
43 #if defined( ZTC ) && defined( PM2 )
44     int __CLIB KeyCompare( const void * pFirst, const void * pSecond );
45 #else
46 #if defined( WNT ) && !defined( WTC ) && !defined (ICC)
47     int _cdecl KeyCompare( const void * pFirst, const void * pSecond );
48 #else
49     int KeyCompare( const void * pFirst, const void * pSecond );
50 #endif
51 #endif
52 }
53 
54 #if defined( WNT ) && !defined( WTC ) && !defined(ICC)
KeyCompare(const void * pFirst,const void * pSecond)55 int _cdecl KeyCompare( const void * pFirst, const void * pSecond ){
56 #else
57 int KeyCompare( const void * pFirst, const void * pSecond ){
58 #endif
59     if( ((KEY_STRUCT *)pFirst)->nName > ((KEY_STRUCT *)pSecond)->nName )
60         return( 1 );
61     else if( ((KEY_STRUCT *)pFirst)->nName < ((KEY_STRUCT *)pSecond)->nName )
62         return( -1 );
63     else
64         return( 0 );
65 }
66 
67 /*************************************************************************
68 |*
69 |*    RscNameTable::RscNameTable()
70 |*
71 |*    Beschreibung      RES.DOC
72 |*    Ersterstellung    MM 28.02.91
73 |*    Letzte Aenderung  MM 28.02.91
74 |*
75 *************************************************************************/
76 RscNameTable::RscNameTable() {
77     bSort    = sal_True;
78     nEntries = 0;
79     pTable   = NULL;
80 };
81 
82 /*************************************************************************
83 |*
84 |*    RscNameTable::~RscNameTable()
85 |*
86 |*    Beschreibung
87 |*    Ersterstellung    MM 15.05.91
88 |*    Letzte Aenderung  MM 15.05.91
89 |*
90 *************************************************************************/
91 RscNameTable::~RscNameTable() {
92     if( pTable )
93         rtl_freeMemory( pTable );
94 };
95 
96 
97 /*************************************************************************
98 |*
99 |*    RscNameTable::SetSort()
100 |*
101 |*    Beschreibung      RES.DOC
102 |*    Ersterstellung    MM 28.02.91
103 |*    Letzte Aenderung  MM 28.02.91
104 |*
105 *************************************************************************/
106 void RscNameTable::SetSort( sal_Bool bSorted ){
107     bSort = bSorted;
108     if( bSort && pTable){
109         // Schluesselwort Feld sortieren
110         qsort( (void *)pTable, nEntries,
111                sizeof( KEY_STRUCT ), KeyCompare );
112     };
113 };
114 
115 /*************************************************************************
116 |*
117 |*    RscNameTable::Put()
118 |*
119 |*    Beschreibung      RES.DOC
120 |*    Ersterstellung    MM 28.02.91
121 |*    Letzte Aenderung  MM 28.02.91
122 |*
123 *************************************************************************/
124 Atom RscNameTable::Put( Atom nName, sal_uInt32 nTyp, long nValue ){
125     if( pTable )
126         pTable = (KEY_STRUCT *)
127                  rtl_reallocateMemory( (void *)pTable,
128                  ((nEntries +1) * sizeof( KEY_STRUCT )) );
129     else
130         pTable = (KEY_STRUCT *)
131                  rtl_allocateMemory( ((nEntries +1)
132                                  * sizeof( KEY_STRUCT )) );
133     pTable[ nEntries ].nName  = nName;
134     pTable[ nEntries ].nTyp   = nTyp;
135     pTable[ nEntries ].yylval = nValue;
136     nEntries++;
137     if( bSort )
138         SetSort();
139     return( nName );
140 };
141 
142 Atom RscNameTable::Put( const char * pName, sal_uInt32 nTyp, long nValue )
143 {
144     return( Put( pHS->getID( pName ), nTyp, nValue ) );
145 };
146 
147 Atom RscNameTable::Put( Atom nName, sal_uInt32 nTyp )
148 {
149     return( Put( nName, nTyp, (long)nName ) );
150 };
151 
152 Atom RscNameTable::Put( const char * pName, sal_uInt32 nTyp )
153 {
154     Atom  nId;
155 
156     nId = pHS->getID( pName );
157     return( Put( nId, nTyp, (long)nId ) );
158 };
159 
160 Atom RscNameTable::Put( Atom nName, sal_uInt32 nTyp, RscTop * pClass )
161 {
162     return( Put( nName, nTyp, (long)pClass ) );
163 };
164 
165 Atom RscNameTable::Put( const char * pName, sal_uInt32 nTyp, RscTop * pClass )
166 {
167     return( Put( pHS->getID( pName ), nTyp, (long)pClass ) );
168 };
169 
170 /*************************************************************************
171 |*
172 |*    RscNameTable::Get()
173 |*
174 |*    Beschreibung      RES.DOC
175 |*    Ersterstellung    MM 28.02.91
176 |*    Letzte Aenderung  MM 28.02.91
177 |*
178 *************************************************************************/
179 sal_Bool RscNameTable::Get( Atom nName, KEY_STRUCT * pEle ){
180     KEY_STRUCT * pKey = NULL;
181     KEY_STRUCT  aSearchName;
182     sal_uInt32  i;
183 
184     if( bSort ){
185         // Suche nach dem Schluesselwort
186         aSearchName.nName = nName;
187         pKey = (KEY_STRUCT *)bsearch(
188 #ifdef UNX
189                    (const char *) &aSearchName, (char *)pTable,
190 #else
191                    (const void *) &aSearchName, (const void *)pTable,
192 #endif
193                    nEntries, sizeof( KEY_STRUCT ), KeyCompare );
194     }
195     else{
196         i = 0;
197         while( i < nEntries && !pKey ){
198             if( pTable[ i ].nName == nName )
199                 pKey = &pTable[ i ];
200             i++;
201         };
202     };
203 
204     if( pKey ){ // Schluesselwort gefunden
205         *pEle = *pKey;
206         return( sal_True );
207     };
208     return( sal_False );
209 };
210 
211