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 .
20 #include "vcl/strhelper.hxx"
21 #include "sal/alloca.h"
25 inline bool isSpace( sal_Unicode cChar
)
28 cChar
== ' ' || cChar
== '\t' ||
29 cChar
== '\r' || cChar
== '\n' ||
30 cChar
== 0x0c || cChar
== 0x0b;
33 inline bool isProtect( sal_Unicode cChar
)
35 return cChar
== '`' || cChar
== '\'' || cChar
== '"';
38 inline void CopyUntil( char*& pTo
, const char*& pFrom
, char cUntil
, bool bIncludeUntil
= false )
51 else if( bIncludeUntil
|| ! isProtect( *pFrom
) )
57 } while( *pFrom
&& *pFrom
!= cUntil
);
58 // copy the terminating character unless zero or protector
59 if( ! isProtect( *pFrom
) || bIncludeUntil
)
69 inline void CopyUntil( sal_Unicode
*& pTo
, const sal_Unicode
*& pFrom
, sal_Unicode cUntil
, bool bIncludeUntil
= false )
82 else if( bIncludeUntil
|| ! isProtect( *pFrom
) )
88 } while( *pFrom
&& *pFrom
!= cUntil
);
89 // copy the terminating character unless zero or protector
90 if( ! isProtect( *pFrom
) || bIncludeUntil
)
100 OUString
GetCommandLineToken( int nToken
, const OUString
& rLine
)
102 sal_Int32 nLen
= rLine
.getLength();
106 int nActualToken
= 0;
107 sal_Unicode
* pBuffer
= static_cast<sal_Unicode
*>(alloca( sizeof(sal_Unicode
)*( nLen
+ 1 ) ));
108 const sal_Unicode
* pRun
= rLine
.getStr();
109 sal_Unicode
* pLeap
= NULL
;
111 while( *pRun
&& nActualToken
<= nToken
)
113 while( *pRun
&& isSpace( *pRun
) )
116 while( *pRun
&& ! isSpace( *pRun
) )
127 else if( *pRun
== '`' )
128 CopyUntil( pLeap
, pRun
, '`' );
129 else if( *pRun
== '\'' )
130 CopyUntil( pLeap
, pRun
, '\'' );
131 else if( *pRun
== '"' )
132 CopyUntil( pLeap
, pRun
, '"' );
140 if( nActualToken
!= nToken
)
147 return OUString(pBuffer
);
150 OString
GetCommandLineToken(int nToken
, const OString
& rLine
)
152 sal_Int32 nLen
= rLine
.getLength();
156 int nActualToken
= 0;
157 char* pBuffer
= static_cast<char*>(alloca( nLen
+ 1 ));
158 const char* pRun
= rLine
.getStr();
161 while( *pRun
&& nActualToken
<= nToken
)
163 while( *pRun
&& isSpace( *pRun
) )
166 while( *pRun
&& ! isSpace( *pRun
) )
177 else if( *pRun
== '`' )
178 CopyUntil( pLeap
, pRun
, '`' );
179 else if( *pRun
== '\'' )
180 CopyUntil( pLeap
, pRun
, '\'' );
181 else if( *pRun
== '"' )
182 CopyUntil( pLeap
, pRun
, '"' );
190 if( nActualToken
!= nToken
)
197 return OString(pBuffer
);
200 int GetCommandLineTokenCount(const OUString
& rLine
)
206 const sal_Unicode
*pRun
= rLine
.getStr();
210 while( *pRun
&& isSpace( *pRun
) )
214 while( *pRun
&& ! isSpace( *pRun
) )
223 else if( *pRun
== '`' )
225 do pRun
++; while( *pRun
&& *pRun
!= '`' );
229 else if( *pRun
== '\'' )
231 do pRun
++; while( *pRun
&& *pRun
!= '\'' );
235 else if( *pRun
== '"' )
237 do pRun
++; while( *pRun
&& *pRun
!= '"' );
250 OUString
WhitespaceToSpace( const OUString
& rLine
, bool bProtect
)
252 sal_Int32 nLen
= rLine
.getLength();
256 sal_Unicode
*pBuffer
= static_cast<sal_Unicode
*>(alloca( sizeof(sal_Unicode
)*(nLen
+ 1) ));
257 const sal_Unicode
*pRun
= rLine
.getStr();
258 sal_Unicode
*pLeap
= pBuffer
;
262 if( *pRun
&& isSpace( *pRun
) )
268 while( *pRun
&& isSpace( *pRun
) )
270 while( *pRun
&& ! isSpace( *pRun
) )
281 else if( bProtect
&& *pRun
== '`' )
282 CopyUntil( pLeap
, pRun
, '`', true );
283 else if( bProtect
&& *pRun
== '\'' )
284 CopyUntil( pLeap
, pRun
, '\'', true );
285 else if( bProtect
&& *pRun
== '"' )
286 CopyUntil( pLeap
, pRun
, '"', true );
298 // there might be a space at beginning or end
303 return OUString(*pBuffer
== ' ' ? pBuffer
+1 : pBuffer
);
306 OString
WhitespaceToSpace(const OString
& rLine
, bool bProtect
)
308 sal_Int32 nLen
= rLine
.getLength();
312 char *pBuffer
= static_cast<char*>(alloca( nLen
+ 1 ));
313 const char *pRun
= rLine
.getStr();
314 char *pLeap
= pBuffer
;
318 if( *pRun
&& isSpace( *pRun
) )
324 while( *pRun
&& isSpace( *pRun
) )
326 while( *pRun
&& ! isSpace( *pRun
) )
337 else if( bProtect
&& *pRun
== '`' )
338 CopyUntil( pLeap
, pRun
, '`', true );
339 else if( bProtect
&& *pRun
== '\'' )
340 CopyUntil( pLeap
, pRun
, '\'', true );
341 else if( bProtect
&& *pRun
== '"' )
342 CopyUntil( pLeap
, pRun
, '"', true );
354 // there might be a space at beginning or end
359 return OString(*pBuffer
== ' ' ? pBuffer
+1 : pBuffer
);
364 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */