fdo#74697 Add Bluez 5 support for impress remote.
[LibreOffice.git] / rsc / source / tools / rsctools.cxx
blob1310a915b6fca544fa267f6e2a6f70f0e3007666
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>
35 /* case insensitive compare of two strings up to a given length */
36 int rsc_strnicmp( const char *string1, const char *string2, size_t count )
38 size_t i;
40 for( i = 0; ( i < count ) && string1[ i ] && string2[ i ] ; i++ )
42 if( tolower( string1[ i ] ) < tolower( string2[ i ] ) )
43 return( -1 );
44 else if( tolower( string1[ i ] ) > tolower( string2[ i ] ) )
45 return( 1 );
47 if( i == count )
48 return( 0 );
49 else if( tolower( string1[ i ] ) < tolower( string2[ i ] ) )
50 return( -1 );
51 else if( tolower( string1[ i ] ) > tolower( string2[ i ] ) )
52 return( 1 );
53 return( 0 );
56 /* case insensitive compare of two strings */
57 int rsc_stricmp( const char *string1, const char *string2 ){
58 int i;
60 for( i = 0; string1[ i ] && string2[ i ]; i++ ){
61 if( tolower( string1[ i ] ) < tolower( string2[ i ] ) )
62 return( -1 );
63 else if( tolower( string1[ i ] ) > tolower( string2[ i ] ) )
64 return( 1 );
66 if( tolower( string1[ i ] ) < tolower( string2[ i ] ) )
67 return( -1 );
68 else if( tolower( string1[ i ] ) > tolower( string2[ i ] ) )
69 return( 1 );
70 return( 0 );
73 char* rsc_strdup( const char* pStr )
75 int nLen = strlen( pStr );
76 char* pBuffer = (char*)rtl_allocateMemory( nLen+1 );
77 memcpy( pBuffer, pStr, nLen+1 );
78 return pBuffer;
81 OString GetTmpFileName()
83 OUString aTmpURL, aTmpFile;
84 osl_createTempFile( NULL, NULL, &aTmpURL.pData );
85 osl_getSystemPathFromFileURL( aTmpURL.pData, &aTmpFile.pData );
86 return OUStringToOString( aTmpFile, RTL_TEXTENCODING_MS_1252 );
89 sal_Bool Append(FILE * fDest, const OString &rTmpFile)
91 #define MAX_BUF 4096
92 FILE *fSource = fopen(rTmpFile.getStr(), "rb");
93 if( !fDest || !fSource )
95 if( fSource )
96 fclose( fSource );
97 return sal_False;
100 bool bSuccess = true;
101 char szBuf[ MAX_BUF ];
102 size_t nItems;
104 do //append
106 nItems = fread( szBuf, 1, MAX_BUF, fSource );
107 bSuccess = (nItems == fwrite(szBuf, 1, nItems, fDest));
108 SAL_WARN_IF(!bSuccess, "rsc", "short write");
109 } while (MAX_BUF == nItems && bSuccess);
111 fclose( fSource );
112 return bSuccess;
115 sal_Bool Append(const OString &rOutputSrs, const OString &rTmpFile)
117 FILE * fDest = fopen(rOutputSrs.getStr(), "ab");
119 sal_Bool bRet = Append(fDest, rTmpFile);
121 if( fDest )
122 fclose( fDest );
124 return bRet;
127 /* replaces extension of a file name */
128 OString OutputFile(const OString &rInput, const char * pExt)
130 sal_Int32 nSepInd = rInput.lastIndexOf(".");
132 if( nSepInd != -1 )
134 return rInput.copy(0, nSepInd + 1).concat(OString(pExt));
137 return rInput.concat(OString(".")).concat(OString(pExt));
140 char * ResponseFile( RscPtrPtr * ppCmd, char ** ppArgv, sal_uInt32 nArgc )
142 FILE *fFile;
143 int nItems;
144 char szBuffer[4096]; // file buffer
145 sal_uInt32 i;
146 bool bInQuotes = false;
148 // program name
149 ppCmd->Append( rsc_strdup( *ppArgv ) );
150 for( i = 1; i < nArgc; i++ )
152 if( '@' == **(ppArgv +i) ){ // when @, then response file
153 if( NULL == (fFile = fopen( (*(ppArgv +i)) +1, "r" )) )
154 return( (*(ppArgv +i)) );
155 nItems = fread( &szBuffer[ 0 ], 1, sizeof( char ), fFile );
156 while( nItems )
158 if( !isspace( szBuffer[ 0 ] ) )
161 * #i27914# double ticks '"' now have a duplicate function:
162 * 1. they define a string ( e.g. -DFOO="baz" )
163 * 2. a string can contain spaces, so -DFOO="baz zum" defines one
164 * argument no two !
166 unsigned int n = 0;
167 while( nItems && (!isspace( szBuffer[ n ] ) || bInQuotes) &&
168 n +1 < sizeof( szBuffer ) )
170 n++;
171 nItems = fread( &szBuffer[ n ], 1,
172 sizeof( char ), fFile );
173 if( szBuffer[n] == '"' )
174 bInQuotes = !bInQuotes;
176 szBuffer[ n ] = '\0';
177 ppCmd->Append( rsc_strdup( szBuffer ) );
179 nItems = fread( &szBuffer[ 0 ], 1, sizeof( char ), fFile );
182 fclose( fFile );
184 else
185 ppCmd->Append( rsc_strdup( *(ppArgv +i) ) );
187 ppCmd->Append( (void *)0 );
188 return( NULL );
192 RscPtrPtr :: RscPtrPtr(){
193 nCount = 0;
194 pMem = NULL;
197 RscPtrPtr :: ~RscPtrPtr(){
198 Reset();
201 void RscPtrPtr :: Reset(){
202 sal_uInt32 i;
204 if( pMem ){
205 for( i = 0; i < nCount; i++ ){
206 if( pMem[ i ] )
207 rtl_freeMemory( pMem[ i ] );
209 rtl_freeMemory( (void *)pMem );
211 nCount = 0;
212 pMem = NULL;
215 sal_uInt32 RscPtrPtr :: Append( void * pBuffer ){
216 if( !pMem )
217 pMem = (void **)rtl_allocateMemory( (nCount +1) * sizeof( void * ) );
218 else
219 pMem = (void **)rtl_reallocateMemory( (void *)pMem,
220 ((nCount +1) * sizeof( void * )
221 ) );
222 pMem[ nCount ] = pBuffer;
223 return( nCount++ );
226 void * RscPtrPtr :: GetEntry( sal_uInt32 nEntry ){
227 if( nEntry < nCount )
228 return( pMem[ nEntry ] );
229 return( NULL );
232 RscWriteRc::RscWriteRc( RSCBYTEORDER_TYPE nOrder )
234 short nSwapTest = 1;
235 RSCBYTEORDER_TYPE nMachineOrder;
237 bSwap = sal_False;
238 if( nOrder != RSC_SYSTEMENDIAN )
240 if( (sal_uInt8)*(sal_uInt8 *)&nSwapTest )
241 nMachineOrder = RSC_LITTLEENDIAN;
242 else
243 nMachineOrder = RSC_BIGENDIAN;
244 bSwap = nOrder != nMachineOrder;
246 nByteOrder = nOrder;
247 nLen = 0;
248 pMem = NULL;
251 RscWriteRc :: ~RscWriteRc()
253 if( pMem )
254 rtl_freeMemory( pMem );
257 sal_uInt32 RscWriteRc :: IncSize( sal_uInt32 nSize )
259 sal_uInt32 nOrigPos = nLen;
260 nLen += nSize;
261 if( pMem )
262 pMem = (char*)rtl_reallocateMemory( pMem, nLen );
263 if( pMem )
264 memset( pMem + nOrigPos, 0, nSize );
265 return nOrigPos;
268 char * RscWriteRc :: GetPointer( sal_uInt32 nSize )
270 if( !pMem )
272 pMem = (char *)rtl_allocateMemory( nLen );
273 memset( pMem, 0, nLen );
275 return( pMem + nSize );
279 void RscWriteRc :: Put( sal_uInt16 nVal )
281 sal_uInt32 nOldLen;
283 nOldLen = IncSize( sizeof( nVal ) );
284 PutAt( nOldLen, nVal );
287 void RscWriteRc :: PutUTF8( char * pStr )
289 sal_uInt32 nStrLen = 0;
290 if( pStr )
291 nStrLen = strlen( pStr );
293 sal_uInt32 n = nStrLen +1;
294 if( n % 2 )
295 // align to 2
296 n++;
298 sal_uInt32 nOldLen = IncSize( n );
299 memcpy( GetPointer( nOldLen ), pStr, nStrLen );
300 // 0 terminated
301 pMem[ nOldLen + nStrLen ] = '\0';
304 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */