fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / rsc / source / tools / rsctools.cxx
blob5a486e0eecbfaca3e38e4d022e9d6b35107758fc
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include <stdlib.h>
22 #include <stdio.h>
23 #if defined (WNT )
24 #include <direct.h>
25 #endif
26 #include <string.h>
27 #include <ctype.h>
29 #include <rscdef.hxx>
30 #include <rsctools.hxx>
32 #include <osl/file.h>
33 #include <rtl/alloc.h>
34 #include <sal/log.hxx>
36 /* case insensitive compare of two strings up to a given length */
37 int rsc_strnicmp( const char *string1, const char *string2, size_t count )
39 size_t i;
41 for( i = 0; ( i < count ) && string1[ i ] && string2[ i ] ; i++ )
43 if( tolower( string1[ i ] ) < tolower( string2[ i ] ) )
44 return -1;
45 else if( tolower( string1[ i ] ) > tolower( string2[ i ] ) )
46 return 1;
48 if( i == count )
49 return 0;
50 else if( tolower( string1[ i ] ) < tolower( string2[ i ] ) )
51 return -1;
52 else if( tolower( string1[ i ] ) > tolower( string2[ i ] ) )
53 return 1;
54 return 0;
57 /* case insensitive compare of two strings */
58 int rsc_stricmp( const char *string1, const char *string2 ){
59 int i;
61 for( i = 0; string1[ i ] && string2[ i ]; i++ )
63 if( tolower( string1[ i ] ) < tolower( string2[ i ] ) )
64 return -1;
65 else if( tolower( string1[ i ] ) > tolower( string2[ i ] ) )
66 return 1;
68 if( tolower( string1[ i ] ) < tolower( string2[ i ] ) )
69 return -1;
70 else if( tolower( string1[ i ] ) > tolower( string2[ i ] ) )
71 return 1;
72 return 0;
75 char* rsc_strdup( const char* pStr )
77 int nLen = strlen( pStr );
78 char* pBuffer = static_cast<char*>(rtl_allocateMemory( nLen+1 ));
79 memcpy( pBuffer, pStr, nLen+1 );
80 return pBuffer;
83 OString GetTmpFileName()
85 OUString aTmpURL, aTmpFile;
86 osl_createTempFile( NULL, NULL, &aTmpURL.pData );
87 osl_getSystemPathFromFileURL( aTmpURL.pData, &aTmpFile.pData );
88 return OUStringToOString( aTmpFile, RTL_TEXTENCODING_MS_1252 );
91 bool Append(FILE * fDest, const OString &rTmpFile)
93 #define MAX_BUF 4096
94 FILE *fSource = fopen(rTmpFile.getStr(), "rb");
95 if( !fDest || !fSource )
97 if( fSource )
98 fclose( fSource );
99 return false;
102 bool bSuccess = true;
103 char szBuf[ MAX_BUF ];
104 size_t nItems;
106 do //append
108 nItems = fread( szBuf, 1, MAX_BUF, fSource );
109 bSuccess = (nItems == fwrite(szBuf, 1, nItems, fDest));
110 SAL_WARN_IF(!bSuccess, "rsc", "short write");
112 while (MAX_BUF == nItems && bSuccess);
114 fclose( fSource );
115 return bSuccess;
118 bool Append(const OString &rOutputSrs, const OString &rTmpFile)
120 FILE * fDest = fopen(rOutputSrs.getStr(), "ab");
122 bool bRet = Append(fDest, rTmpFile);
124 if( fDest )
125 fclose( fDest );
127 return bRet;
130 /* replaces extension of a file name */
131 OString OutputFile(const OString &rInput, const char * pExt)
133 sal_Int32 nSepInd = rInput.lastIndexOf('.');
135 if( nSepInd != -1 )
137 return rInput.copy(0, nSepInd + 1).concat(OString(pExt));
140 return rInput.concat(OString(".")).concat(OString(pExt));
143 char * ResponseFile( RscPtrPtr * ppCmd, char ** ppArgv, sal_uInt32 nArgc )
145 FILE *fFile;
146 int nItems;
147 char szBuffer[4096]; // file buffer
148 sal_uInt32 i;
149 bool bInQuotes = false;
151 // program name
152 ppCmd->Append( rsc_strdup( *ppArgv ) );
153 for( i = 1; i < nArgc; i++ )
155 if( '@' == **(ppArgv +i) ){ // when @, then response file
156 if( NULL == (fFile = fopen( (*(ppArgv +i)) +1, "r" )) )
157 return( (*(ppArgv +i)) );
158 nItems = fread( &szBuffer[ 0 ], 1, sizeof( char ), fFile );
159 while( nItems )
161 if( !isspace( szBuffer[ 0 ] ) )
164 * #i27914# double ticks '"' now have a duplicate function:
165 * 1. they define a string ( e.g. -DFOO="baz" )
166 * 2. a string can contain spaces, so -DFOO="baz zum" defines one
167 * argument no two !
169 unsigned int n = 0;
170 while( nItems && (!isspace( szBuffer[ n ] ) || bInQuotes) &&
171 n +1 < sizeof( szBuffer ) )
173 n++;
174 nItems = fread( &szBuffer[ n ], 1,
175 sizeof( char ), fFile );
176 if( szBuffer[n] == '"' )
177 bInQuotes = !bInQuotes;
179 szBuffer[ n ] = '\0';
180 ppCmd->Append( rsc_strdup( szBuffer ) );
182 nItems = fread( &szBuffer[ 0 ], 1, sizeof( char ), fFile );
185 fclose( fFile );
187 else
188 ppCmd->Append( rsc_strdup( *(ppArgv +i) ) );
190 ppCmd->Append( (void *)0 );
191 return NULL;
195 RscPtrPtr :: RscPtrPtr()
197 nCount = 0;
198 pMem = NULL;
201 RscPtrPtr :: ~RscPtrPtr()
203 Reset();
206 void RscPtrPtr :: Reset()
208 sal_uInt32 i;
210 if( pMem )
212 for( i = 0; i < nCount; i++ )
214 if( pMem[ i ] )
215 rtl_freeMemory( pMem[ i ] );
217 rtl_freeMemory( (void *)pMem );
219 nCount = 0;
220 pMem = NULL;
223 sal_uInt32 RscPtrPtr :: Append( void * pBuffer )
225 if( !pMem )
226 pMem = static_cast<void **>(rtl_allocateMemory( (nCount +1) * sizeof( void * ) ));
227 else
228 pMem = static_cast<void **>(rtl_reallocateMemory( (void *)pMem,
229 ((nCount +1) * sizeof( void * )
230 ) ));
231 pMem[ nCount ] = pBuffer;
232 return nCount++;
235 void * RscPtrPtr :: GetEntry( sal_uInt32 nEntry )
237 if( nEntry < nCount )
238 return pMem[ nEntry ];
239 return NULL;
242 RscWriteRc::RscWriteRc( RSCBYTEORDER_TYPE nOrder )
244 bSwap = false;
245 if( nOrder != RSC_SYSTEMENDIAN )
247 RSCBYTEORDER_TYPE nMachineOrder;
248 #if defined OSL_LITENDIAN
249 nMachineOrder = RSC_LITTLEENDIAN;
250 #else
251 nMachineOrder = RSC_BIGENDIAN;
252 #endif
253 bSwap = nOrder != nMachineOrder;
255 nByteOrder = nOrder;
256 nLen = 0;
257 pMem = NULL;
260 RscWriteRc :: ~RscWriteRc()
262 if( pMem )
263 rtl_freeMemory( pMem );
266 sal_uInt32 RscWriteRc :: IncSize( sal_uInt32 nSize )
268 sal_uInt32 nOrigPos = nLen;
269 nLen += nSize;
270 if( pMem )
271 pMem = static_cast<char*>(rtl_reallocateMemory( pMem, nLen ));
272 if( pMem )
273 memset( pMem + nOrigPos, 0, nSize );
274 return nOrigPos;
277 char * RscWriteRc :: GetPointer( sal_uInt32 nSize )
279 if( !pMem )
281 pMem = static_cast<char *>(rtl_allocateMemory( nLen ));
282 memset( pMem, 0, nLen );
284 return pMem + nSize;
288 void RscWriteRc :: Put( sal_uInt16 nVal )
290 sal_uInt32 nOldLen;
292 nOldLen = IncSize( sizeof( nVal ) );
293 PutAt( nOldLen, nVal );
296 void RscWriteRc :: PutUTF8( char * pStr )
298 sal_uInt32 nStrLen = 0;
299 if( pStr )
300 nStrLen = strlen( pStr );
302 sal_uInt32 n = nStrLen +1;
303 if( n % 2 )
304 // align to 2
305 n++;
307 sal_uInt32 nOldLen = IncSize( n );
308 memcpy( GetPointer( nOldLen ), pStr, nStrLen );
309 // 0 terminated
310 pMem[ nOldLen + nStrLen ] = '\0';
313 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */