CWS-TOOLING: integrate CWS os146
[LibreOffice.git] / rsc / source / tools / rsctools.cxx
blob57345d0aa8938f6453b450965d6f82a9718ea8a2
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
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.
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).
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.
26 ************************************************************************/
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 **************************************/
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>
44 #include <tools/fsys.hxx>
46 // Include
47 #include <rscdef.hxx>
48 #include <rsctools.hxx>
50 #include <osl/file.h>
51 #include <rtl/alloc.h>
52 #include <rtl/memory.h>
54 using namespace rtl;
56 /****************** C o d e **********************************************/
57 /*************************************************************************
59 |* rsc_strnicmp()
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
66 *************************************************************************/
67 int rsc_strnicmp( const char *string1, const char *string2, size_t count )
69 size_t i;
71 for( i = 0; ( i < count ) && string1[ i ] && string2[ i ] ; i++ )
73 if( tolower( string1[ i ] ) < tolower( string2[ i ] ) )
74 return( -1 );
75 else if( tolower( string1[ i ] ) > tolower( string2[ i ] ) )
76 return( 1 );
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 );
87 /*************************************************************************
89 |* rsc_strnicmp()
91 |* Beschreibung Vergleicht zwei Strings Case-Unabhaengig
92 |* Ersterstellung MM 13.02.91
93 |* Letzte Aenderung MM 13.02.91
95 *************************************************************************/
96 int rsc_stricmp( const char *string1, const char *string2 ){
97 int i;
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 );
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 );
112 char* rsc_strdup( const char* pStr )
114 int nLen = strlen( pStr );
115 char* pBuffer = (char*)rtl_allocateMemory( nLen+1 );
116 rtl_copyMemory( pBuffer, pStr, nLen+1 );
117 return pBuffer;
120 /*************************************************************************
122 |* GetTmpFileName()
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
130 *************************************************************************/
131 ByteString GetTmpFileName()
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 );
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 )
149 #define MAX_BUF 4096
150 char szBuf[ MAX_BUF ];
151 int nItems;
152 FILE *fSource;
154 fSource = fopen( aTmpFile.GetBuffer(), "rb" );
155 if( !fDest || !fSource ){
156 if( fSource )
157 fclose( fSource );
158 return sal_False;
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 );
166 fclose( fSource );
168 return sal_True;
171 sal_Bool Append( ByteString aOutputSrs, ByteString aTmpFile )
173 FILE * fDest = fopen( aOutputSrs.GetBuffer(), "ab" );
175 sal_Bool bRet = Append( fDest, aTmpFile );
177 if( fDest )
178 fclose( fDest );
180 return bRet;
183 /*************************************************************************
185 |* InputFile
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
193 *************************************************************************/
194 ByteString InputFile ( const char * pInput, const char * pExt )
196 UniString aUniInput( pInput, RTL_TEXTENCODING_ASCII_US );
197 DirEntry aFileName( aUniInput );
199 if ( 0 == aFileName.GetExtension().Len() )
201 UniString aExt( pExt, RTL_TEXTENCODING_ASCII_US );
202 aFileName.SetExtension( aExt );
205 return ByteString( aFileName.GetFull(), RTL_TEXTENCODING_ASCII_US );
208 /*************************************************************************
210 |* OutputFile
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
218 *************************************************************************/
219 ByteString OutputFile ( ByteString aInput, const char * pExt )
221 UniString aUniInput( aInput, RTL_TEXTENCODING_ASCII_US );
222 DirEntry aFileName( aUniInput );
224 UniString aExt( pExt, RTL_TEXTENCODING_ASCII_US );
225 aFileName.SetExtension( aExt );
227 return ByteString( aFileName.GetFull(), RTL_TEXTENCODING_ASCII_US );
230 /*************************************************************************
232 |* ::ResonseFile()
234 |* Beschreibung Kommandozeile aufbereiten
235 |* Ersterstellung MM 05.09.91
236 |* Letzte Aenderung MM 05.09.91
238 *************************************************************************/
239 char * ResponseFile( RscPtrPtr * ppCmd, char ** ppArgv, sal_uInt32 nArgc )
241 FILE *fFile;
242 int nItems;
243 char szBuffer[4096]; // file buffer
244 sal_uInt32 i;
245 bool bInQuotes = false;
247 // Programmname
248 ppCmd->Append( rsc_strdup( *ppArgv ) );
249 for( i = 1; i < nArgc; i++ )
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 )
257 if( !isspace( szBuffer[ 0 ] ) )
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 !
265 unsigned int n = 0;
266 while( nItems && (!isspace( szBuffer[ n ] ) || bInQuotes) &&
267 n +1 < sizeof( szBuffer ) )
269 n++;
270 nItems = fread( &szBuffer[ n ], 1,
271 sizeof( char ), fFile );
272 if( szBuffer[n] == '"' )
273 bInQuotes = !bInQuotes;
275 szBuffer[ n ] = '\0';
276 ppCmd->Append( rsc_strdup( szBuffer ) );
278 nItems = fread( &szBuffer[ 0 ], 1, sizeof( char ), fFile );
281 fclose( fFile );
283 else
284 ppCmd->Append( rsc_strdup( *(ppArgv +i) ) );
286 ppCmd->Append( (void *)0 );
287 return( NULL );
291 /*************** R s c P t r P t r **************************************/
292 /*************************************************************************
294 |* RscPtrPtr :: RscPtrPtr()
296 |* Beschreibung Eine Tabelle mit Zeigern
297 |* Ersterstellung MM 13.02.91
298 |* Letzte Aenderung MM 13.02.91
300 *************************************************************************/
301 RscPtrPtr :: RscPtrPtr(){
302 nCount = 0;
303 pMem = NULL;
306 /*************************************************************************
308 |* RscPtrPtr :: ~RscPtrPtr()
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
315 *************************************************************************/
316 RscPtrPtr :: ~RscPtrPtr(){
317 Reset();
320 /*************************************************************************
322 |* RscPtrPtr :: Reset()
324 |* Beschreibung
325 |* Ersterstellung MM 03.05.91
326 |* Letzte Aenderung MM 03.05.91
328 *************************************************************************/
329 void RscPtrPtr :: Reset(){
330 sal_uInt32 i;
332 if( pMem ){
333 for( i = 0; i < nCount; i++ ){
334 if( pMem[ i ] )
335 rtl_freeMemory( pMem[ i ] );
337 rtl_freeMemory( (void *)pMem );
339 nCount = 0;
340 pMem = NULL;
343 /*************************************************************************
345 |* RscPtrPtr :: Append()
347 |* Beschreibung Haengt einen Eintrag an.
348 |* Ersterstellung MM 13.02.91
349 |* Letzte Aenderung MM 13.02.91
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++ );
363 /*************************************************************************
365 |* RscPtrPtr :: GetEntry()
367 |* Beschreibung Liefert einen Eintrag, NULL wenn nicht vorhanden.
368 |* Ersterstellung MM 13.02.91
369 |* Letzte Aenderung MM 13.02.91
371 *************************************************************************/
372 void * RscPtrPtr :: GetEntry( sal_uInt32 nEntry ){
373 if( nEntry < nCount )
374 return( pMem[ nEntry ] );
375 return( NULL );
378 /****************** R S C W R I T E R C **********************************/
379 /*************************************************************************
381 |* RscWriteRc :: RscWriteRc()
383 |* Beschreibung
384 |* Ersterstellung MM 15.04.91
385 |* Letzte Aenderung MM 15.04.91
387 *************************************************************************/
388 RscWriteRc::RscWriteRc( RSCBYTEORDER_TYPE nOrder )
390 short nSwapTest = 1;
391 RSCBYTEORDER_TYPE nMachineOrder;
393 bSwap = sal_False;
394 if( nOrder != RSC_SYSTEMENDIAN )
396 if( (sal_uInt8)*(sal_uInt8 *)&nSwapTest )
397 nMachineOrder = RSC_LITTLEENDIAN;
398 else
399 nMachineOrder = RSC_BIGENDIAN;
400 bSwap = nOrder != nMachineOrder;
402 nByteOrder = nOrder;
403 nLen = 0;
404 pMem = NULL;
407 /*************************************************************************
409 |* RscWriteRc :: ~RscWriteRc()
411 |* Beschreibung
412 |* Ersterstellung MM 15.04.91
413 |* Letzte Aenderung MM 15.04.91
415 *************************************************************************/
416 RscWriteRc :: ~RscWriteRc()
418 if( pMem )
419 rtl_freeMemory( pMem );
422 /*************************************************************************
424 |* RscWriteRc :: IncSize()
426 |* Beschreibung
427 |* Ersterstellung MM 15.04.91
428 |* Letzte Aenderung MM 15.04.91
430 *************************************************************************/
431 sal_uInt32 RscWriteRc :: IncSize( sal_uInt32 nSize )
433 nLen += nSize;
434 if( pMem )
435 pMem = (char*)rtl_reallocateMemory( pMem, nLen );
436 return( nLen - nSize );
439 /*************************************************************************
441 |* RscWriteRc :: GetPointer()
443 |* Beschreibung
444 |* Ersterstellung MM 15.04.91
445 |* Letzte Aenderung MM 15.04.91
447 *************************************************************************/
448 char * RscWriteRc :: GetPointer( sal_uInt32 nSize )
450 if( !pMem )
451 pMem = (char *)rtl_allocateMemory( nLen );
452 return( pMem + nSize );
456 /*************************************************************************
458 |* RscWriteRc :: Put()
460 |* Beschreibung
461 |* Ersterstellung MM 15.04.91
462 |* Letzte Aenderung MM 15.04.91
464 *************************************************************************/
465 void RscWriteRc :: Put( sal_uInt16 nVal )
467 sal_uInt32 nOldLen;
469 nOldLen = IncSize( sizeof( nVal ) );
470 PutAt( nOldLen, nVal );
473 void RscWriteRc :: PutUTF8( char * pStr )
475 sal_uInt32 nStrLen = 0;
476 if( pStr )
477 nStrLen = strlen( pStr );
479 sal_uInt32 n = nStrLen +1;
480 if( n % 2 )
481 // align to 2
482 n++;
484 sal_uInt32 nOldLen = IncSize( n );
485 rtl_copyMemory( GetPointer( nOldLen ), pStr, nStrLen );
486 // 0 terminated
487 pMem[ nOldLen + nStrLen ] = '\0';