1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
30 #include <rsctools.hxx>
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
)
41 for( i
= 0; ( i
< count
) && string1
[ i
] && string2
[ i
] ; i
++ )
43 if( tolower( string1
[ i
] ) < tolower( string2
[ i
] ) )
45 else if( tolower( string1
[ i
] ) > tolower( string2
[ i
] ) )
50 else if( tolower( string1
[ i
] ) < tolower( string2
[ i
] ) )
52 else if( tolower( string1
[ i
] ) > tolower( string2
[ i
] ) )
57 /* case insensitive compare of two strings */
58 int rsc_stricmp( const char *string1
, const char *string2
){
61 for( i
= 0; string1
[ i
] && string2
[ i
]; i
++ )
63 if( tolower( string1
[ i
] ) < tolower( string2
[ i
] ) )
65 else if( tolower( string1
[ i
] ) > tolower( string2
[ i
] ) )
68 if( tolower( string1
[ i
] ) < tolower( string2
[ i
] ) )
70 else if( tolower( string1
[ i
] ) > tolower( string2
[ i
] ) )
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 );
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
)
94 FILE *fSource
= fopen(rTmpFile
.getStr(), "rb");
95 if( !fDest
|| !fSource
)
102 bool bSuccess
= true;
103 char szBuf
[ MAX_BUF
];
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
);
118 bool Append(const OString
&rOutputSrs
, const OString
&rTmpFile
)
120 FILE * fDest
= fopen(rOutputSrs
.getStr(), "ab");
122 bool bRet
= Append(fDest
, rTmpFile
);
130 /* replaces extension of a file name */
131 OString
OutputFile(const OString
&rInput
, const char * pExt
)
133 sal_Int32 nSepInd
= rInput
.lastIndexOf('.');
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
)
147 char szBuffer
[4096]; // file buffer
149 bool bInQuotes
= false;
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
);
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
170 while( nItems
&& (!isspace( szBuffer
[ n
] ) || bInQuotes
) &&
171 n
+1 < sizeof( szBuffer
) )
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
);
188 ppCmd
->Append( rsc_strdup( *(ppArgv
+i
) ) );
190 ppCmd
->Append( (void *)0 );
195 RscPtrPtr :: RscPtrPtr()
201 RscPtrPtr :: ~RscPtrPtr()
206 void RscPtrPtr :: Reset()
212 for( i
= 0; i
< nCount
; i
++ )
215 rtl_freeMemory( pMem
[ i
] );
217 rtl_freeMemory( (void *)pMem
);
223 sal_uInt32
RscPtrPtr :: Append( void * pBuffer
)
226 pMem
= static_cast<void **>(rtl_allocateMemory( (nCount
+1) * sizeof( void * ) ));
228 pMem
= static_cast<void **>(rtl_reallocateMemory( (void *)pMem
,
229 ((nCount
+1) * sizeof( void * )
231 pMem
[ nCount
] = pBuffer
;
235 void * RscPtrPtr :: GetEntry( sal_uInt32 nEntry
)
237 if( nEntry
< nCount
)
238 return pMem
[ nEntry
];
242 RscWriteRc::RscWriteRc( RSCBYTEORDER_TYPE nOrder
)
245 if( nOrder
!= RSC_SYSTEMENDIAN
)
247 RSCBYTEORDER_TYPE nMachineOrder
;
248 #if defined OSL_LITENDIAN
249 nMachineOrder
= RSC_LITTLEENDIAN
;
251 nMachineOrder
= RSC_BIGENDIAN
;
253 bSwap
= nOrder
!= nMachineOrder
;
260 RscWriteRc :: ~RscWriteRc()
263 rtl_freeMemory( pMem
);
266 sal_uInt32
RscWriteRc :: IncSize( sal_uInt32 nSize
)
268 sal_uInt32 nOrigPos
= nLen
;
271 pMem
= static_cast<char*>(rtl_reallocateMemory( pMem
, nLen
));
273 memset( pMem
+ nOrigPos
, 0, nSize
);
277 char * RscWriteRc :: GetPointer( sal_uInt32 nSize
)
281 pMem
= static_cast<char *>(rtl_allocateMemory( nLen
));
282 memset( pMem
, 0, nLen
);
288 void RscWriteRc :: Put( sal_uInt16 nVal
)
292 nOldLen
= IncSize( sizeof( nVal
) );
293 PutAt( nOldLen
, nVal
);
296 void RscWriteRc :: PutUTF8( char * pStr
)
298 sal_uInt32 nStrLen
= 0;
300 nStrLen
= strlen( pStr
);
302 sal_uInt32 n
= nStrLen
+1;
307 sal_uInt32 nOldLen
= IncSize( n
);
308 memcpy( GetPointer( nOldLen
), pStr
, nStrLen
);
310 pMem
[ nOldLen
+ nStrLen
] = '\0';
313 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */