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 .
21 #include "vcl/strhelper.hxx"
22 #include "sal/alloca.h"
26 inline int isSpace( char cChar
)
29 cChar
== ' ' || cChar
== '\t' ||
30 cChar
== '\r' || cChar
== '\n' ||
31 cChar
== 0x0c || cChar
== 0x0b;
34 inline int isSpace( sal_Unicode cChar
)
37 cChar
== ' ' || cChar
== '\t' ||
38 cChar
== '\r' || cChar
== '\n' ||
39 cChar
== 0x0c || cChar
== 0x0b;
42 inline int isProtect( char cChar
)
44 return cChar
== '`' || cChar
== '\'' || cChar
== '"';
47 inline int isProtect( sal_Unicode cChar
)
49 return cChar
== '`' || cChar
== '\'' || cChar
== '"';
52 inline void CopyUntil( char*& pTo
, const char*& pFrom
, char cUntil
, int bIncludeUntil
= 0 )
65 else if( bIncludeUntil
|| ! isProtect( *pFrom
) )
71 } while( *pFrom
&& *pFrom
!= cUntil
);
72 // copy the terminating character unless zero or protector
73 if( ! isProtect( *pFrom
) || bIncludeUntil
)
83 inline void CopyUntil( sal_Unicode
*& pTo
, const sal_Unicode
*& pFrom
, sal_Unicode cUntil
, int bIncludeUntil
= 0 )
96 else if( bIncludeUntil
|| ! isProtect( *pFrom
) )
102 } while( *pFrom
&& *pFrom
!= cUntil
);
103 // copy the terminating character unless zero or protector
104 if( ! isProtect( *pFrom
) || bIncludeUntil
)
114 String
GetCommandLineToken( int nToken
, const String
& rLine
)
116 int nLen
= rLine
.Len();
120 int nActualToken
= 0;
121 sal_Unicode
* pBuffer
= (sal_Unicode
*)alloca( sizeof(sal_Unicode
)*( nLen
+ 1 ) );
122 const sal_Unicode
* pRun
= rLine
.GetBuffer();
123 sal_Unicode
* pLeap
= NULL
;
125 while( *pRun
&& nActualToken
<= nToken
)
127 while( *pRun
&& isSpace( *pRun
) )
130 while( *pRun
&& ! isSpace( *pRun
) )
141 else if( *pRun
== '`' )
142 CopyUntil( pLeap
, pRun
, '`' );
143 else if( *pRun
== '\'' )
144 CopyUntil( pLeap
, pRun
, '\'' );
145 else if( *pRun
== '"' )
146 CopyUntil( pLeap
, pRun
, '"' );
154 if( nActualToken
!= nToken
)
161 return rtl::OUString(pBuffer
);
164 rtl::OString
GetCommandLineToken(int nToken
, const rtl::OString
& rLine
)
166 sal_Int32 nLen
= rLine
.getLength();
170 int nActualToken
= 0;
171 char* pBuffer
= (char*)alloca( nLen
+ 1 );
172 const char* pRun
= rLine
.getStr();
175 while( *pRun
&& nActualToken
<= nToken
)
177 while( *pRun
&& isSpace( *pRun
) )
180 while( *pRun
&& ! isSpace( *pRun
) )
191 else if( *pRun
== '`' )
192 CopyUntil( pLeap
, pRun
, '`' );
193 else if( *pRun
== '\'' )
194 CopyUntil( pLeap
, pRun
, '\'' );
195 else if( *pRun
== '"' )
196 CopyUntil( pLeap
, pRun
, '"' );
204 if( nActualToken
!= nToken
)
211 return rtl::OString(pBuffer
);
214 int GetCommandLineTokenCount(const rtl::OUString
& rLine
)
220 const sal_Unicode
*pRun
= rLine
.getStr();
224 while( *pRun
&& isSpace( *pRun
) )
228 while( *pRun
&& ! isSpace( *pRun
) )
237 else if( *pRun
== '`' )
239 do pRun
++; while( *pRun
&& *pRun
!= '`' );
243 else if( *pRun
== '\'' )
245 do pRun
++; while( *pRun
&& *pRun
!= '\'' );
249 else if( *pRun
== '"' )
251 do pRun
++; while( *pRun
&& *pRun
!= '"' );
264 String
WhitespaceToSpace( const String
& rLine
, sal_Bool bProtect
)
266 int nLen
= rLine
.Len();
270 sal_Unicode
*pBuffer
= (sal_Unicode
*)alloca( sizeof(sal_Unicode
)*(nLen
+ 1) );
271 const sal_Unicode
*pRun
= rLine
.GetBuffer();
272 sal_Unicode
*pLeap
= pBuffer
;
276 if( *pRun
&& isSpace( *pRun
) )
282 while( *pRun
&& isSpace( *pRun
) )
284 while( *pRun
&& ! isSpace( *pRun
) )
295 else if( bProtect
&& *pRun
== '`' )
296 CopyUntil( pLeap
, pRun
, '`', sal_True
);
297 else if( bProtect
&& *pRun
== '\'' )
298 CopyUntil( pLeap
, pRun
, '\'', sal_True
);
299 else if( bProtect
&& *pRun
== '"' )
300 CopyUntil( pLeap
, pRun
, '"', sal_True
);
312 // there might be a space at beginning or end
317 return rtl::OUString(*pBuffer
== ' ' ? pBuffer
+1 : pBuffer
);
320 rtl::OString
WhitespaceToSpace(const rtl::OString
& rLine
, sal_Bool bProtect
)
322 sal_Int32 nLen
= rLine
.getLength();
326 char *pBuffer
= (char*)alloca( nLen
+ 1 );
327 const char *pRun
= rLine
.getStr();
328 char *pLeap
= pBuffer
;
332 if( *pRun
&& isSpace( *pRun
) )
338 while( *pRun
&& isSpace( *pRun
) )
340 while( *pRun
&& ! isSpace( *pRun
) )
351 else if( bProtect
&& *pRun
== '`' )
352 CopyUntil( pLeap
, pRun
, '`', sal_True
);
353 else if( bProtect
&& *pRun
== '\'' )
354 CopyUntil( pLeap
, pRun
, '\'', sal_True
);
355 else if( bProtect
&& *pRun
== '"' )
356 CopyUntil( pLeap
, pRun
, '"', sal_True
);
368 // there might be a space at beginning or end
373 return rtl::OString(*pBuffer
== ' ' ? pBuffer
+1 : pBuffer
);
378 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */