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>
24 bool isSpace( sal_Unicode cChar
)
27 cChar
== ' ' || cChar
== '\t' ||
28 cChar
== '\r' || cChar
== '\n' ||
29 cChar
== 0x0c || cChar
== 0x0b;
32 bool isProtect( sal_Unicode cChar
)
34 return cChar
== '`' || cChar
== '\'' || cChar
== '"';
37 void CopyUntil( char*& pTo
, const char*& pFrom
, char cUntil
, bool bIncludeUntil
= false )
50 else if( bIncludeUntil
|| ! isProtect( *pFrom
) )
56 } while( *pFrom
&& *pFrom
!= cUntil
);
57 // copy the terminating character unless zero or protector
58 if( ! isProtect( *pFrom
) || bIncludeUntil
)
68 void CopyUntil( sal_Unicode
*& pTo
, const sal_Unicode
*& pFrom
, sal_Unicode cUntil
, bool bIncludeUntil
= false )
81 else if( bIncludeUntil
|| ! isProtect( *pFrom
) )
87 } while( *pFrom
&& *pFrom
!= cUntil
);
88 // copy the terminating character unless zero or protector
89 if( ! isProtect( *pFrom
) || bIncludeUntil
)
103 OUString
GetCommandLineToken( int nToken
, const OUString
& rLine
)
105 sal_Int32 nLen
= rLine
.getLength();
109 int nActualToken
= 0;
110 sal_Unicode
* pBuffer
= static_cast<sal_Unicode
*>(alloca( sizeof(sal_Unicode
)*( nLen
+ 1 ) ));
111 const sal_Unicode
* pRun
= rLine
.getStr();
112 sal_Unicode
* pLeap
= nullptr;
114 while( *pRun
&& nActualToken
<= nToken
)
116 while( *pRun
&& isSpace( *pRun
) )
119 while( *pRun
&& ! isSpace( *pRun
) )
130 else if( *pRun
== '`' )
131 CopyUntil( pLeap
, pRun
, '`' );
132 else if( *pRun
== '\'' )
133 CopyUntil( pLeap
, pRun
, '\'' );
134 else if( *pRun
== '"' )
135 CopyUntil( pLeap
, pRun
, '"' );
143 if( nActualToken
!= nToken
)
150 return OUString(pBuffer
);
153 OString
GetCommandLineToken(int nToken
, const OString
& rLine
)
155 sal_Int32 nLen
= rLine
.getLength();
159 int nActualToken
= 0;
160 char* pBuffer
= static_cast<char*>(alloca( nLen
+ 1 ));
161 const char* pRun
= rLine
.getStr();
162 char* pLeap
= nullptr;
164 while( *pRun
&& nActualToken
<= nToken
)
166 while( *pRun
&& isSpace( *pRun
) )
169 while( *pRun
&& ! isSpace( *pRun
) )
180 else if( *pRun
== '`' )
181 CopyUntil( pLeap
, pRun
, '`' );
182 else if( *pRun
== '\'' )
183 CopyUntil( pLeap
, pRun
, '\'' );
184 else if( *pRun
== '"' )
185 CopyUntil( pLeap
, pRun
, '"' );
193 if( nActualToken
!= nToken
)
203 int GetCommandLineTokenCount(const OUString
& rLine
)
209 const sal_Unicode
*pRun
= rLine
.getStr();
213 while( *pRun
&& isSpace( *pRun
) )
217 while( *pRun
&& ! isSpace( *pRun
) )
226 else if( *pRun
== '`' )
228 do pRun
++; while( *pRun
&& *pRun
!= '`' );
232 else if( *pRun
== '\'' )
234 do pRun
++; while( *pRun
&& *pRun
!= '\'' );
238 else if( *pRun
== '"' )
240 do pRun
++; while( *pRun
&& *pRun
!= '"' );
253 OUString
WhitespaceToSpace( const OUString
& rLine
, bool bProtect
)
255 sal_Int32 nLen
= rLine
.getLength();
259 sal_Unicode
*pBuffer
= static_cast<sal_Unicode
*>(alloca( sizeof(sal_Unicode
)*(nLen
+ 1) ));
260 const sal_Unicode
*pRun
= rLine
.getStr();
261 sal_Unicode
*pLeap
= pBuffer
;
265 if( *pRun
&& isSpace( *pRun
) )
271 while( *pRun
&& isSpace( *pRun
) )
273 while( *pRun
&& ! isSpace( *pRun
) )
284 else if( bProtect
&& *pRun
== '`' )
285 CopyUntil( pLeap
, pRun
, '`', true );
286 else if( bProtect
&& *pRun
== '\'' )
287 CopyUntil( pLeap
, pRun
, '\'', true );
288 else if( bProtect
&& *pRun
== '"' )
289 CopyUntil( pLeap
, pRun
, '"', true );
301 // there might be a space at beginning or end
309 return OUString(*pBuffer
== ' ' ? pBuffer
+1 : pBuffer
);
312 OString
WhitespaceToSpace(const OString
& rLine
)
314 sal_Int32 nLen
= rLine
.getLength();
318 char *pBuffer
= static_cast<char*>(alloca( nLen
+ 1 ));
319 const char *pRun
= rLine
.getStr();
320 char *pLeap
= pBuffer
;
324 if( *pRun
&& isSpace( *pRun
) )
330 while( *pRun
&& isSpace( *pRun
) )
332 while( *pRun
&& ! isSpace( *pRun
) )
343 else if( *pRun
== '`' )
344 CopyUntil( pLeap
, pRun
, '`', true );
345 else if( *pRun
== '\'' )
346 CopyUntil( pLeap
, pRun
, '\'', true );
347 else if( *pRun
== '"' )
348 CopyUntil( pLeap
, pRun
, '"', true );
360 // there might be a space at beginning or end
365 return *pBuffer
== ' ' ? pBuffer
+1 : pBuffer
;
370 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */