xref: /trunk/main/rsc/source/tools/rsctools.cxx (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
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 
32 // C and C++ Includes.
33 #include <stdlib.h>
34 #include <stdio.h>
35 #if defined (WNT )
36 #include <direct.h>
37 #endif
38 #if defined ( OS2 ) && !defined ( GCC )
39 #include <direct.h>
40 #endif
41 #include <string.h>
42 #include <ctype.h>
43 
44 #include <tools/fsys.hxx>
45 
46 // Include
47 #include <rscdef.hxx>
48 #include <rsctools.hxx>
49 
50 #include <osl/file.h>
51 #include <rtl/alloc.h>
52 #include <rtl/memory.h>
53 
54 using namespace rtl;
55 
56 /****************** C o d e **********************************************/
57 /*************************************************************************
58 |*
59 |*    rsc_strnicmp()
60 |*
61 |*    Beschreibung      Vergleicht zwei Strings Case-Unabhaengig bis zu
62 |*                      einer bestimmten Laenge
63 |*    Ersterstellung    MM 13.02.91
64 |*    Letzte Aenderung  MM 13.02.91
65 |*
66 *************************************************************************/
67 int rsc_strnicmp( const char *string1, const char *string2, size_t count )
68 {
69     size_t i;
70 
71     for( i = 0; ( i < count ) && string1[ i ] && string2[ i ] ; i++ )
72     {
73         if( tolower( string1[ i ] ) < tolower( string2[ i ] ) )
74             return( -1 );
75         else if( tolower( string1[ i ] ) > tolower( string2[ i ] ) )
76             return( 1 );
77     }
78     if( i == count )
79         return( 0 );
80     else if( tolower( string1[ i ] ) < tolower( string2[ i ] ) )
81         return( -1 );
82     else if( tolower( string1[ i ] ) > tolower( string2[ i ] ) )
83         return( 1 );
84     return( 0 );
85 }
86 
87 /*************************************************************************
88 |*
89 |*    rsc_strnicmp()
90 |*
91 |*    Beschreibung      Vergleicht zwei Strings Case-Unabhaengig
92 |*    Ersterstellung    MM 13.02.91
93 |*    Letzte Aenderung  MM 13.02.91
94 |*
95 *************************************************************************/
96 int rsc_stricmp( const char *string1, const char *string2 ){
97     int i;
98 
99     for( i = 0; string1[ i ] && string2[ i ]; i++ ){
100         if( tolower( string1[ i ] ) < tolower( string2[ i ] ) )
101             return( -1 );
102         else if( tolower( string1[ i ] ) > tolower( string2[ i ] ) )
103             return( 1 );
104     }
105     if( tolower( string1[ i ] ) < tolower( string2[ i ] ) )
106         return( -1 );
107     else if( tolower( string1[ i ] ) > tolower( string2[ i ] ) )
108         return( 1 );
109     return( 0 );
110 }
111 
112 char* rsc_strdup( const char* pStr )
113 {
114     int nLen = strlen( pStr );
115     char* pBuffer = (char*)rtl_allocateMemory( nLen+1 );
116     rtl_copyMemory( pBuffer, pStr, nLen+1 );
117     return pBuffer;
118 }
119 
120 /*************************************************************************
121 |*
122 |*    GetTmpFileName()
123 |*
124 |*    Beschreibung      Gibt einen String eines eindeutigen Dateinamens
125 |*                      zurueck. Der Speicher fuer den String wird mit
126 |*                      malloc allokiert
127 |*    Ersterstellung    MM 13.02.91
128 |*    Letzte Aenderung  MH 13.10.97
129 |*
130 *************************************************************************/
131 ByteString GetTmpFileName()
132 {
133     OUString aTmpURL, aTmpFile;
134     osl_createTempFile( NULL, NULL, &aTmpURL.pData );
135     osl_getSystemPathFromFileURL( aTmpURL.pData, &aTmpFile.pData );
136     return OUStringToOString( aTmpFile, RTL_TEXTENCODING_MS_1252 );
137 }
138 
139 /********************************************************************/
140 /*                                                                  */
141 /*  Function    :   Append( )                                       */
142 /*                                                                  */
143 /*  Parameters  :   psw     - pointer to a preprocessor switch      */
144 /*                                                                  */
145 /*  Description :   appends text files                              */
146 /********************************************************************/
147 sal_Bool Append( FILE * fDest, ByteString aTmpFile )
148 {
149 #define MAX_BUF 4096
150     char    szBuf[ MAX_BUF ];
151     int     nItems;
152     FILE    *fSource;
153 
154     fSource = fopen( aTmpFile.GetBuffer(), "rb" );
155     if( !fDest || !fSource ){
156         if( fSource )
157             fclose( fSource );
158         return sal_False;
159     }
160     else{
161         do{ // append
162             nItems = fread( szBuf, sizeof( char ), MAX_BUF, fSource );
163             fwrite( szBuf, sizeof( char ), nItems, fDest );
164         } while( MAX_BUF == nItems );
165 
166         fclose( fSource );
167     };
168     return sal_True;
169 }
170 
171 sal_Bool Append( ByteString aOutputSrs, ByteString aTmpFile )
172 {
173     FILE * fDest   = fopen( aOutputSrs.GetBuffer(), "ab" );
174 
175     sal_Bool bRet = Append( fDest, aTmpFile );
176 
177     if( fDest )
178         fclose( fDest );
179 
180     return bRet;
181 }
182 
183 /*************************************************************************
184 |*
185 |*    InputFile
186 |*
187 |*    Beschreibung      Haengt Extension an, wenn keine da ist
188 |*    Parameter:        pInput, der Input-Dateiname.
189 |*                      pExt, die Extension des Ausgabenamens
190 |*    Ersterstellung    MM 13.02.91
191 |*    Letzte Aenderung  MM 28.06.91
192 |*
193 *************************************************************************/
194 ByteString InputFile ( const char * pInput, const char * pExt )
195 {
196     UniString aUniInput( pInput, RTL_TEXTENCODING_ASCII_US );
197     DirEntry aFileName( aUniInput );
198 
199     if ( 0 == aFileName.GetExtension().Len() )
200     {
201         UniString aExt( pExt, RTL_TEXTENCODING_ASCII_US );
202         aFileName.SetExtension( aExt );
203     }
204 
205     return ByteString( aFileName.GetFull(), RTL_TEXTENCODING_ASCII_US );
206 }
207 
208 /*************************************************************************
209 |*
210 |*    OutputFile
211 |*
212 |*    Beschreibung      Ersetzt Extension durch eine andere
213 |*    Parameter:        input, der Input-Dateiname.
214 |*                      pExt, die Extension des Ausgabenamens
215 |*    Ersterstellung    MM 13.02.91
216 |*    Letzte Aenderung  MM 28.06.91
217 |*
218 *************************************************************************/
219 ByteString OutputFile ( ByteString aInput, const char * pExt )
220 {
221     UniString   aUniInput( aInput, RTL_TEXTENCODING_ASCII_US );
222     DirEntry    aFileName( aUniInput );
223 
224     UniString aExt( pExt, RTL_TEXTENCODING_ASCII_US );
225     aFileName.SetExtension( aExt );
226 
227     return ByteString( aFileName.GetFull(), RTL_TEXTENCODING_ASCII_US );
228 }
229 
230 /*************************************************************************
231 |*
232 |*    ::ResonseFile()
233 |*
234 |*    Beschreibung      Kommandozeile aufbereiten
235 |*    Ersterstellung    MM 05.09.91
236 |*    Letzte Aenderung  MM 05.09.91
237 |*
238 *************************************************************************/
239 char * ResponseFile( RscPtrPtr * ppCmd, char ** ppArgv, sal_uInt32 nArgc )
240 {
241     FILE    *fFile;
242     int     nItems;
243     char    szBuffer[4096];       // file buffer
244     sal_uInt32  i;
245     bool bInQuotes = false;
246 
247     // Programmname
248     ppCmd->Append( rsc_strdup( *ppArgv ) );
249     for( i = 1; i < nArgc; i++ )
250     {
251         if( '@' == **(ppArgv +i) ){ // wenn @, dann Response-Datei
252             if( NULL == (fFile = fopen( (*(ppArgv +i)) +1, "r" )) )
253                 return( (*(ppArgv +i)) );
254             nItems = fread( &szBuffer[ 0 ], 1, sizeof( char ), fFile );
255             while( nItems )
256             {
257                 if( !isspace( szBuffer[ 0 ] ) )
258                 {
259                     /*
260                      *  #i27914# double ticks '"' now have a duplicate function:
261                      *  1. they define a string ( e.g. -DFOO="baz" )
262                      *  2. a string can contain spaces, so -DFOO="baz zum" defines one
263                      *  argument no two !
264                      */
265                     unsigned int n = 0;
266                     while( nItems && (!isspace( szBuffer[ n ] ) || bInQuotes) &&
267                            n +1 < sizeof( szBuffer )  )
268                     {
269                         n++;
270                         nItems = fread( &szBuffer[ n ], 1,
271                                         sizeof( char ), fFile );
272                         if( szBuffer[n] == '"' )
273                             bInQuotes = !bInQuotes;
274                     }
275                     szBuffer[ n ] = '\0';
276                     ppCmd->Append( rsc_strdup( szBuffer ) );
277                 }
278                 nItems = fread( &szBuffer[ 0 ], 1, sizeof( char ), fFile );
279             };
280 
281             fclose( fFile );
282         }
283         else
284             ppCmd->Append( rsc_strdup( *(ppArgv +i) ) );
285     };
286     ppCmd->Append( (void *)0 );
287     return( NULL );
288 }
289 
290 
291 /*************** R s c P t r P t r **************************************/
292 /*************************************************************************
293 |*
294 |*    RscPtrPtr :: RscPtrPtr()
295 |*
296 |*    Beschreibung      Eine Tabelle mit Zeigern
297 |*    Ersterstellung    MM 13.02.91
298 |*    Letzte Aenderung  MM 13.02.91
299 |*
300 *************************************************************************/
301 RscPtrPtr :: RscPtrPtr(){
302     nCount = 0;
303     pMem = NULL;
304 }
305 
306 /*************************************************************************
307 |*
308 |*    RscPtrPtr :: ~RscPtrPtr()
309 |*
310 |*    Beschreibung      Zerst�rt eine Tabelle mit Zeigern, die Zeiger werde
311 |*                      ebenfalls freigegebn
312 |*    Ersterstellung    MM 13.02.91
313 |*    Letzte Aenderung  MM 13.02.91
314 |*
315 *************************************************************************/
316 RscPtrPtr :: ~RscPtrPtr(){
317     Reset();
318 }
319 
320 /*************************************************************************
321 |*
322 |*    RscPtrPtr :: Reset()
323 |*
324 |*    Beschreibung
325 |*    Ersterstellung    MM 03.05.91
326 |*    Letzte Aenderung  MM 03.05.91
327 |*
328 *************************************************************************/
329 void RscPtrPtr :: Reset(){
330     sal_uInt32 i;
331 
332     if( pMem ){
333         for( i = 0; i < nCount; i++ ){
334             if( pMem[ i ] )
335                rtl_freeMemory( pMem[ i ] );
336         }
337         rtl_freeMemory( (void *)pMem );
338     };
339     nCount = 0;
340     pMem = NULL;
341 }
342 
343 /*************************************************************************
344 |*
345 |*    RscPtrPtr :: Append()
346 |*
347 |*    Beschreibung      Haengt einen Eintrag an.
348 |*    Ersterstellung    MM 13.02.91
349 |*    Letzte Aenderung  MM 13.02.91
350 |*
351 *************************************************************************/
352 sal_uInt32 RscPtrPtr :: Append( void * pBuffer ){
353     if( !pMem )
354         pMem = (void **)rtl_allocateMemory( (nCount +1) * sizeof( void * ) );
355     else
356         pMem = (void **)rtl_reallocateMemory( (void *)pMem,
357                          ((nCount +1) * sizeof( void * )
358                        ) );
359     pMem[ nCount ] = pBuffer;
360     return( nCount++ );
361 }
362 
363 /*************************************************************************
364 |*
365 |*    RscPtrPtr :: GetEntry()
366 |*
367 |*    Beschreibung      Liefert einen Eintrag, NULL wenn nicht vorhanden.
368 |*    Ersterstellung    MM 13.02.91
369 |*    Letzte Aenderung  MM 13.02.91
370 |*
371 *************************************************************************/
372 void * RscPtrPtr :: GetEntry( sal_uInt32 nEntry ){
373     if( nEntry < nCount )
374         return( pMem[ nEntry ] );
375     return( NULL );
376 }
377 
378 /****************** R S C W R I T E R C **********************************/
379 /*************************************************************************
380 |*
381 |*    RscWriteRc :: RscWriteRc()
382 |*
383 |*    Beschreibung
384 |*    Ersterstellung    MM 15.04.91
385 |*    Letzte Aenderung  MM 15.04.91
386 |*
387 *************************************************************************/
388 RscWriteRc::RscWriteRc( RSCBYTEORDER_TYPE nOrder )
389 {
390     short               nSwapTest = 1;
391     RSCBYTEORDER_TYPE   nMachineOrder;
392 
393     bSwap = sal_False;
394     if( nOrder != RSC_SYSTEMENDIAN )
395     {
396         if( (sal_uInt8)*(sal_uInt8 *)&nSwapTest )
397             nMachineOrder = RSC_LITTLEENDIAN;
398         else
399             nMachineOrder = RSC_BIGENDIAN;
400         bSwap = nOrder != nMachineOrder;
401     }
402     nByteOrder = nOrder;
403     nLen = 0;
404     pMem = NULL;
405 }
406 
407 /*************************************************************************
408 |*
409 |*    RscWriteRc :: ~RscWriteRc()
410 |*
411 |*    Beschreibung
412 |*    Ersterstellung    MM 15.04.91
413 |*    Letzte Aenderung  MM 15.04.91
414 |*
415 *************************************************************************/
416 RscWriteRc :: ~RscWriteRc()
417 {
418     if( pMem )
419         rtl_freeMemory( pMem );
420 }
421 
422 /*************************************************************************
423 |*
424 |*    RscWriteRc :: IncSize()
425 |*
426 |*    Beschreibung
427 |*    Ersterstellung    MM 15.04.91
428 |*    Letzte Aenderung  MM 15.04.91
429 |*
430 *************************************************************************/
431 sal_uInt32 RscWriteRc :: IncSize( sal_uInt32 nSize )
432 {
433     nLen += nSize;
434     if( pMem )
435         pMem = (char*)rtl_reallocateMemory( pMem, nLen );
436     return( nLen - nSize );
437 }
438 
439 /*************************************************************************
440 |*
441 |*    RscWriteRc :: GetPointer()
442 |*
443 |*    Beschreibung
444 |*    Ersterstellung    MM 15.04.91
445 |*    Letzte Aenderung  MM 15.04.91
446 |*
447 *************************************************************************/
448 char * RscWriteRc :: GetPointer( sal_uInt32 nSize )
449 {
450     if( !pMem )
451         pMem = (char *)rtl_allocateMemory( nLen );
452     return( pMem + nSize );
453 }
454 
455 
456 /*************************************************************************
457 |*
458 |*    RscWriteRc :: Put()
459 |*
460 |*    Beschreibung
461 |*    Ersterstellung    MM 15.04.91
462 |*    Letzte Aenderung  MM 15.04.91
463 |*
464 *************************************************************************/
465 void RscWriteRc :: Put( sal_uInt16 nVal )
466 {
467     sal_uInt32  nOldLen;
468 
469     nOldLen = IncSize( sizeof( nVal ) );
470     PutAt( nOldLen, nVal );
471 }
472 
473 void RscWriteRc :: PutUTF8( char * pStr )
474 {
475     sal_uInt32 nStrLen = 0;
476     if( pStr )
477         nStrLen = strlen( pStr );
478 
479     sal_uInt32  n = nStrLen +1;
480     if( n % 2 )
481         // align to 2
482         n++;
483 
484     sal_uInt32  nOldLen = IncSize( n );
485     rtl_copyMemory( GetPointer( nOldLen ), pStr, nStrLen );
486     // 0 terminated
487     pMem[ nOldLen + nStrLen ] = '\0';
488 }
489