merge the formfield patch from ooo-build
[ooovba.git] / rsc / source / tools / rsctools.cxx
blobbddb43a0d97dab95026a9e5359de642b9db0a85e
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: rsctools.cxx,v $
10 * $Revision: 1.17 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_rsc.hxx"
33 /****************** I N C L U D E S **************************************/
35 // C and C++ Includes.
36 #include <stdlib.h>
37 #include <stdio.h>
38 #if defined ( DOS ) || defined ( WIN ) || defined (WNT )
39 #include <direct.h>
40 #endif
41 #if defined ( OS2 ) && !defined ( GCC )
42 #include <direct.h>
43 #endif
44 #include <string.h>
45 #include <ctype.h>
47 #include <tools/fsys.hxx>
49 // Include
50 #include <rscdef.hxx>
51 #include <rsctools.hxx>
53 #include <osl/file.h>
54 #include <rtl/alloc.h>
55 #include <rtl/memory.h>
57 #if defined (WIN)
58 #define ONLY_NEW
59 #endif
61 using namespace rtl;
63 /****************** C o d e **********************************************/
64 /*************************************************************************
66 |* rsc_strnicmp()
68 |* Beschreibung Vergleicht zwei Strings Case-Unabhaengig bis zu
69 |* einer bestimmten Laenge
70 |* Ersterstellung MM 13.02.91
71 |* Letzte Aenderung MM 13.02.91
73 *************************************************************************/
74 int rsc_strnicmp( const char *string1, const char *string2, size_t count )
76 size_t i;
78 for( i = 0; ( i < count ) && string1[ i ] && string2[ i ] ; i++ )
80 if( tolower( string1[ i ] ) < tolower( string2[ i ] ) )
81 return( -1 );
82 else if( tolower( string1[ i ] ) > tolower( string2[ i ] ) )
83 return( 1 );
85 if( i == count )
86 return( 0 );
87 else if( tolower( string1[ i ] ) < tolower( string2[ i ] ) )
88 return( -1 );
89 else if( tolower( string1[ i ] ) > tolower( string2[ i ] ) )
90 return( 1 );
91 return( 0 );
94 /*************************************************************************
96 |* rsc_strnicmp()
98 |* Beschreibung Vergleicht zwei Strings Case-Unabhaengig
99 |* Ersterstellung MM 13.02.91
100 |* Letzte Aenderung MM 13.02.91
102 *************************************************************************/
103 int rsc_stricmp( const char *string1, const char *string2 ){
104 int i;
106 for( i = 0; string1[ i ] && string2[ i ]; i++ ){
107 if( tolower( string1[ i ] ) < tolower( string2[ i ] ) )
108 return( -1 );
109 else if( tolower( string1[ i ] ) > tolower( string2[ i ] ) )
110 return( 1 );
112 if( tolower( string1[ i ] ) < tolower( string2[ i ] ) )
113 return( -1 );
114 else if( tolower( string1[ i ] ) > tolower( string2[ i ] ) )
115 return( 1 );
116 return( 0 );
119 char* rsc_strdup( const char* pStr )
121 int nLen = strlen( pStr );
122 char* pBuffer = (char*)rtl_allocateMemory( nLen+1 );
123 rtl_copyMemory( pBuffer, pStr, nLen+1 );
124 return pBuffer;
127 /*************************************************************************
129 |* GetTmpFileName()
131 |* Beschreibung Gibt einen String eines eindeutigen Dateinamens
132 |* zurueck. Der Speicher fuer den String wird mit
133 |* malloc allokiert
134 |* Ersterstellung MM 13.02.91
135 |* Letzte Aenderung MH 13.10.97
137 *************************************************************************/
138 ByteString GetTmpFileName()
140 OUString aTmpURL, aTmpFile;
141 osl_createTempFile( NULL, NULL, &aTmpURL.pData );
142 osl_getSystemPathFromFileURL( aTmpURL.pData, &aTmpFile.pData );
143 return OUStringToOString( aTmpFile, RTL_TEXTENCODING_MS_1252 );
146 /********************************************************************/
147 /* */
148 /* Function : Append( ) */
149 /* */
150 /* Parameters : psw - pointer to a preprocessor switch */
151 /* */
152 /* Description : appends text files */
153 /********************************************************************/
154 BOOL Append( FILE * fDest, ByteString aTmpFile )
156 #define MAX_BUF 4096
157 char szBuf[ MAX_BUF ];
158 int nItems;
159 FILE *fSource;
161 fSource = fopen( aTmpFile.GetBuffer(), "rb" );
162 if( !fDest || !fSource ){
163 if( fSource )
164 fclose( fSource );
165 return FALSE;
167 else{
168 do{ // append
169 nItems = fread( szBuf, sizeof( char ), MAX_BUF, fSource );
170 fwrite( szBuf, sizeof( char ), nItems, fDest );
171 } while( MAX_BUF == nItems );
173 fclose( fSource );
175 return TRUE;
178 BOOL Append( ByteString aOutputSrs, ByteString aTmpFile )
180 FILE * fDest = fopen( aOutputSrs.GetBuffer(), "ab" );
182 BOOL bRet = Append( fDest, aTmpFile );
184 if( fDest )
185 fclose( fDest );
187 return bRet;
190 /*************************************************************************
192 |* InputFile
194 |* Beschreibung Haengt Extension an, wenn keine da ist
195 |* Parameter: pInput, der Input-Dateiname.
196 |* pExt, die Extension des Ausgabenamens
197 |* Ersterstellung MM 13.02.91
198 |* Letzte Aenderung MM 28.06.91
200 *************************************************************************/
201 ByteString InputFile ( const char * pInput, const char * pExt )
203 UniString aUniInput( pInput, RTL_TEXTENCODING_ASCII_US );
204 DirEntry aFileName( aUniInput );
206 if ( 0 == aFileName.GetExtension().Len() )
208 UniString aExt( pExt, RTL_TEXTENCODING_ASCII_US );
209 aFileName.SetExtension( aExt );
212 return ByteString( aFileName.GetFull(), RTL_TEXTENCODING_ASCII_US );
215 /*************************************************************************
217 |* OutputFile
219 |* Beschreibung Ersetzt Extension durch eine andere
220 |* Parameter: input, der Input-Dateiname.
221 |* pExt, die Extension des Ausgabenamens
222 |* Ersterstellung MM 13.02.91
223 |* Letzte Aenderung MM 28.06.91
225 *************************************************************************/
226 ByteString OutputFile ( ByteString aInput, const char * pExt )
228 UniString aUniInput( aInput, RTL_TEXTENCODING_ASCII_US );
229 DirEntry aFileName( aUniInput );
231 UniString aExt( pExt, RTL_TEXTENCODING_ASCII_US );
232 aFileName.SetExtension( aExt );
234 return ByteString( aFileName.GetFull(), RTL_TEXTENCODING_ASCII_US );
237 /*************************************************************************
239 |* ::ResonseFile()
241 |* Beschreibung Kommandozeile aufbereiten
242 |* Ersterstellung MM 05.09.91
243 |* Letzte Aenderung MM 05.09.91
245 *************************************************************************/
246 char * ResponseFile( RscPtrPtr * ppCmd, char ** ppArgv, sal_uInt32 nArgc )
248 FILE *fFile;
249 int nItems;
250 char szBuffer[4096]; // file buffer
251 sal_uInt32 i;
252 bool bInQuotes = false;
254 // Programmname
255 ppCmd->Append( rsc_strdup( *ppArgv ) );
256 for( i = 1; i < nArgc; i++ )
258 if( '@' == **(ppArgv +i) ){ // wenn @, dann Response-Datei
259 if( NULL == (fFile = fopen( (*(ppArgv +i)) +1, "r" )) )
260 return( (*(ppArgv +i)) );
261 nItems = fread( &szBuffer[ 0 ], 1, sizeof( char ), fFile );
262 while( nItems )
264 if( !isspace( szBuffer[ 0 ] ) )
267 * #i27914# double ticks '"' now have a duplicate function:
268 * 1. they define a string ( e.g. -DFOO="baz" )
269 * 2. a string can contain spaces, so -DFOO="baz zum" defines one
270 * argument no two !
272 unsigned int n = 0;
273 while( nItems && (!isspace( szBuffer[ n ] ) || bInQuotes) &&
274 n +1 < sizeof( szBuffer ) )
276 n++;
277 nItems = fread( &szBuffer[ n ], 1,
278 sizeof( char ), fFile );
279 if( szBuffer[n] == '"' )
280 bInQuotes = !bInQuotes;
282 szBuffer[ n ] = '\0';
283 ppCmd->Append( rsc_strdup( szBuffer ) );
285 nItems = fread( &szBuffer[ 0 ], 1, sizeof( char ), fFile );
288 fclose( fFile );
290 else
291 ppCmd->Append( rsc_strdup( *(ppArgv +i) ) );
293 ppCmd->Append( (void *)0 );
294 return( NULL );
298 /*************** R s c P t r P t r **************************************/
299 /*************************************************************************
301 |* RscPtrPtr :: RscPtrPtr()
303 |* Beschreibung Eine Tabelle mit Zeigern
304 |* Ersterstellung MM 13.02.91
305 |* Letzte Aenderung MM 13.02.91
307 *************************************************************************/
308 RscPtrPtr :: RscPtrPtr(){
309 nCount = 0;
310 pMem = NULL;
313 /*************************************************************************
315 |* RscPtrPtr :: ~RscPtrPtr()
317 |* Beschreibung Zerst�rt eine Tabelle mit Zeigern, die Zeiger werde
318 |* ebenfalls freigegebn
319 |* Ersterstellung MM 13.02.91
320 |* Letzte Aenderung MM 13.02.91
322 *************************************************************************/
323 RscPtrPtr :: ~RscPtrPtr(){
324 Reset();
327 /*************************************************************************
329 |* RscPtrPtr :: Reset()
331 |* Beschreibung
332 |* Ersterstellung MM 03.05.91
333 |* Letzte Aenderung MM 03.05.91
335 *************************************************************************/
336 void RscPtrPtr :: Reset(){
337 sal_uInt32 i;
339 if( pMem ){
340 for( i = 0; i < nCount; i++ ){
341 if( pMem[ i ] )
342 rtl_freeMemory( pMem[ i ] );
344 rtl_freeMemory( (void *)pMem );
346 nCount = 0;
347 pMem = NULL;
350 /*************************************************************************
352 |* RscPtrPtr :: Append()
354 |* Beschreibung Haengt einen Eintrag an.
355 |* Ersterstellung MM 13.02.91
356 |* Letzte Aenderung MM 13.02.91
358 *************************************************************************/
359 sal_uInt32 RscPtrPtr :: Append( void * pBuffer ){
360 if( !pMem )
361 pMem = (void **)rtl_allocateMemory( (nCount +1) * sizeof( void * ) );
362 else
363 pMem = (void **)rtl_reallocateMemory( (void *)pMem,
364 ((nCount +1) * sizeof( void * )
365 ) );
366 pMem[ nCount ] = pBuffer;
367 return( nCount++ );
370 /*************************************************************************
372 |* RscPtrPtr :: GetEntry()
374 |* Beschreibung Liefert einen Eintrag, NULL wenn nicht vorhanden.
375 |* Ersterstellung MM 13.02.91
376 |* Letzte Aenderung MM 13.02.91
378 *************************************************************************/
379 void * RscPtrPtr :: GetEntry( sal_uInt32 nEntry ){
380 if( nEntry < nCount )
381 return( pMem[ nEntry ] );
382 return( NULL );
385 /****************** R S C W R I T E R C **********************************/
386 /*************************************************************************
388 |* RscWriteRc :: RscWriteRc()
390 |* Beschreibung
391 |* Ersterstellung MM 15.04.91
392 |* Letzte Aenderung MM 15.04.91
394 *************************************************************************/
395 RscWriteRc::RscWriteRc( RSCBYTEORDER_TYPE nOrder )
397 short nSwapTest = 1;
398 RSCBYTEORDER_TYPE nMachineOrder;
400 bSwap = FALSE;
401 if( nOrder != RSC_SYSTEMENDIAN )
403 if( (BYTE)*(BYTE *)&nSwapTest )
404 nMachineOrder = RSC_LITTLEENDIAN;
405 else
406 nMachineOrder = RSC_BIGENDIAN;
407 bSwap = nOrder != nMachineOrder;
409 nByteOrder = nOrder;
410 nLen = 0;
411 pMem = NULL;
414 /*************************************************************************
416 |* RscWriteRc :: ~RscWriteRc()
418 |* Beschreibung
419 |* Ersterstellung MM 15.04.91
420 |* Letzte Aenderung MM 15.04.91
422 *************************************************************************/
423 RscWriteRc :: ~RscWriteRc()
425 if( pMem )
426 rtl_freeMemory( pMem );
429 /*************************************************************************
431 |* RscWriteRc :: IncSize()
433 |* Beschreibung
434 |* Ersterstellung MM 15.04.91
435 |* Letzte Aenderung MM 15.04.91
437 *************************************************************************/
438 sal_uInt32 RscWriteRc :: IncSize( sal_uInt32 nSize )
440 nLen += nSize;
441 if( pMem )
442 pMem = (char*)rtl_reallocateMemory( pMem, nLen );
443 return( nLen - nSize );
446 /*************************************************************************
448 |* RscWriteRc :: GetPointer()
450 |* Beschreibung
451 |* Ersterstellung MM 15.04.91
452 |* Letzte Aenderung MM 15.04.91
454 *************************************************************************/
455 char * RscWriteRc :: GetPointer( sal_uInt32 nSize )
457 if( !pMem )
458 pMem = (char *)rtl_allocateMemory( nLen );
459 return( pMem + nSize );
463 /*************************************************************************
465 |* RscWriteRc :: Put()
467 |* Beschreibung
468 |* Ersterstellung MM 15.04.91
469 |* Letzte Aenderung MM 15.04.91
471 *************************************************************************/
472 void RscWriteRc :: Put( sal_uInt16 nVal )
474 sal_uInt32 nOldLen;
476 nOldLen = IncSize( sizeof( nVal ) );
477 PutAt( nOldLen, nVal );
480 void RscWriteRc :: PutUTF8( char * pStr )
482 sal_uInt32 nStrLen = 0;
483 if( pStr )
484 nStrLen = strlen( pStr );
486 sal_uInt32 n = nStrLen +1;
487 if( n % 2 )
488 // align to 2
489 n++;
491 sal_uInt32 nOldLen = IncSize( n );
492 rtl_copyMemory( GetPointer( nOldLen ), pStr, nStrLen );
493 // 0 terminated
494 pMem[ nOldLen + nStrLen ] = '\0';