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