1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: strhelper.cxx,v $
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_vcl.hxx"
34 #include "vcl/strhelper.hxx"
35 #include "sal/alloca.h"
39 inline int isSpace( char cChar
)
42 cChar
== ' ' || cChar
== '\t' ||
43 cChar
== '\r' || cChar
== '\n' ||
44 cChar
== 0x0c || cChar
== 0x0b;
47 inline int isSpace( sal_Unicode cChar
)
50 cChar
== ' ' || cChar
== '\t' ||
51 cChar
== '\r' || cChar
== '\n' ||
52 cChar
== 0x0c || cChar
== 0x0b;
55 inline int isProtect( char cChar
)
57 return cChar
== '`' || cChar
== '\'' || cChar
== '"';
60 inline int isProtect( sal_Unicode cChar
)
62 return cChar
== '`' || cChar
== '\'' || cChar
== '"';
65 inline void CopyUntil( char*& pTo
, const char*& pFrom
, char cUntil
, int bIncludeUntil
= 0 )
78 else if( bIncludeUntil
|| ! isProtect( *pFrom
) )
84 } while( *pFrom
&& *pFrom
!= cUntil
);
85 // copy the terminating character unless zero or protector
86 if( ! isProtect( *pFrom
) || bIncludeUntil
)
96 inline void CopyUntil( sal_Unicode
*& pTo
, const sal_Unicode
*& pFrom
, sal_Unicode cUntil
, int bIncludeUntil
= 0 )
109 else if( bIncludeUntil
|| ! isProtect( *pFrom
) )
115 } while( *pFrom
&& *pFrom
!= cUntil
);
116 // copy the terminating character unless zero or protector
117 if( ! isProtect( *pFrom
) || bIncludeUntil
)
127 String
GetCommandLineToken( int nToken
, const String
& rLine
)
129 int nLen
= rLine
.Len();
133 int nActualToken
= 0;
134 sal_Unicode
* pBuffer
= (sal_Unicode
*)alloca( sizeof(sal_Unicode
)*( nLen
+ 1 ) );
135 const sal_Unicode
* pRun
= rLine
.GetBuffer();
136 sal_Unicode
* pLeap
= NULL
;
138 while( *pRun
&& nActualToken
<= nToken
)
140 while( *pRun
&& isSpace( *pRun
) )
143 while( *pRun
&& ! isSpace( *pRun
) )
154 else if( *pRun
== '`' )
155 CopyUntil( pLeap
, pRun
, '`' );
156 else if( *pRun
== '\'' )
157 CopyUntil( pLeap
, pRun
, '\'' );
158 else if( *pRun
== '"' )
159 CopyUntil( pLeap
, pRun
, '"' );
167 if( nActualToken
!= nToken
)
174 String
aRet( pBuffer
);
178 ByteString
GetCommandLineToken( int nToken
, const ByteString
& rLine
)
180 int nLen
= rLine
.Len();
184 int nActualToken
= 0;
185 char* pBuffer
= (char*)alloca( nLen
+ 1 );
186 const char* pRun
= rLine
.GetBuffer();
189 while( *pRun
&& nActualToken
<= nToken
)
191 while( *pRun
&& isSpace( *pRun
) )
194 while( *pRun
&& ! isSpace( *pRun
) )
205 else if( *pRun
== '`' )
206 CopyUntil( pLeap
, pRun
, '`' );
207 else if( *pRun
== '\'' )
208 CopyUntil( pLeap
, pRun
, '\'' );
209 else if( *pRun
== '"' )
210 CopyUntil( pLeap
, pRun
, '"' );
218 if( nActualToken
!= nToken
)
225 ByteString
aRet( pBuffer
);
229 int GetCommandLineTokenCount( const String
& rLine
)
235 const sal_Unicode
*pRun
= rLine
.GetBuffer();
240 while( *pRun
&& isSpace( *pRun
) )
244 while( *pRun
&& ! isSpace( *pRun
) )
253 else if( *pRun
== '`' )
255 do pRun
++; while( *pRun
&& *pRun
!= '`' );
259 else if( *pRun
== '\'' )
261 do pRun
++; while( *pRun
&& *pRun
!= '\'' );
265 else if( *pRun
== '"' )
267 do pRun
++; while( *pRun
&& *pRun
!= '"' );
280 int GetCommandLineTokenCount( const ByteString
& rLine
)
286 const char *pRun
= rLine
.GetBuffer();
291 while( *pRun
&& isSpace( *pRun
) )
295 while( *pRun
&& ! isSpace( *pRun
) )
304 else if( *pRun
== '`' )
306 do pRun
++; while( *pRun
&& *pRun
!= '`' );
310 else if( *pRun
== '\'' )
312 do pRun
++; while( *pRun
&& *pRun
!= '\'' );
316 else if( *pRun
== '"' )
318 do pRun
++; while( *pRun
&& *pRun
!= '"' );
331 String
WhitespaceToSpace( const String
& rLine
, BOOL bProtect
)
333 int nLen
= rLine
.Len();
337 sal_Unicode
*pBuffer
= (sal_Unicode
*)alloca( sizeof(sal_Unicode
)*(nLen
+ 1) );
338 const sal_Unicode
*pRun
= rLine
.GetBuffer();
339 sal_Unicode
*pLeap
= pBuffer
;
343 if( *pRun
&& isSpace( *pRun
) )
349 while( *pRun
&& isSpace( *pRun
) )
351 while( *pRun
&& ! isSpace( *pRun
) )
362 else if( bProtect
&& *pRun
== '`' )
363 CopyUntil( pLeap
, pRun
, '`', TRUE
);
364 else if( bProtect
&& *pRun
== '\'' )
365 CopyUntil( pLeap
, pRun
, '\'', TRUE
);
366 else if( bProtect
&& *pRun
== '"' )
367 CopyUntil( pLeap
, pRun
, '"', TRUE
);
379 // there might be a space at beginning or end
384 String
aRet( *pBuffer
== ' ' ? pBuffer
+1 : pBuffer
);
388 ByteString
WhitespaceToSpace( const ByteString
& rLine
, BOOL bProtect
)
390 int nLen
= rLine
.Len();
394 char *pBuffer
= (char*)alloca( nLen
+ 1 );
395 const char *pRun
= rLine
.GetBuffer();
396 char *pLeap
= pBuffer
;
400 if( *pRun
&& isSpace( *pRun
) )
406 while( *pRun
&& isSpace( *pRun
) )
408 while( *pRun
&& ! isSpace( *pRun
) )
419 else if( bProtect
&& *pRun
== '`' )
420 CopyUntil( pLeap
, pRun
, '`', TRUE
);
421 else if( bProtect
&& *pRun
== '\'' )
422 CopyUntil( pLeap
, pRun
, '\'', TRUE
);
423 else if( bProtect
&& *pRun
== '"' )
424 CopyUntil( pLeap
, pRun
, '"', TRUE
);
436 // there might be a space at beginning or end
441 ByteString
aRet( *pBuffer
== ' ' ? pBuffer
+1 : pBuffer
);