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
)
104 OUString
GetCommandLineToken( int nToken
, const OUString
& rLine
)
106 sal_Int32 nLen
= rLine
.getLength();
110 int nActualToken
= 0;
111 sal_Unicode
* pBuffer
= static_cast<sal_Unicode
*>(alloca( sizeof(sal_Unicode
)*( nLen
+ 1 ) ));
112 const sal_Unicode
* pRun
= rLine
.getStr();
113 sal_Unicode
* pLeap
= nullptr;
115 while( *pRun
&& nActualToken
<= nToken
)
117 while( *pRun
&& isSpace( *pRun
) )
120 while( *pRun
&& ! isSpace( *pRun
) )
131 else if( *pRun
== '`' )
132 CopyUntil( pLeap
, pRun
, '`' );
133 else if( *pRun
== '\'' )
134 CopyUntil( pLeap
, pRun
, '\'' );
135 else if( *pRun
== '"' )
136 CopyUntil( pLeap
, pRun
, '"' );
144 if( nActualToken
!= nToken
)
151 return OUString(pBuffer
);
154 OString
GetCommandLineToken(int nToken
, const OString
& rLine
)
156 sal_Int32 nLen
= rLine
.getLength();
160 int nActualToken
= 0;
161 char* pBuffer
= static_cast<char*>(alloca( nLen
+ 1 ));
162 const char* pRun
= rLine
.getStr();
163 char* pLeap
= nullptr;
165 while( *pRun
&& nActualToken
<= nToken
)
167 while( *pRun
&& isSpace( *pRun
) )
170 while( *pRun
&& ! isSpace( *pRun
) )
181 else if( *pRun
== '`' )
182 CopyUntil( pLeap
, pRun
, '`' );
183 else if( *pRun
== '\'' )
184 CopyUntil( pLeap
, pRun
, '\'' );
185 else if( *pRun
== '"' )
186 CopyUntil( pLeap
, pRun
, '"' );
194 if( nActualToken
!= nToken
)
201 return OString(pBuffer
);
204 int GetCommandLineTokenCount(const OUString
& rLine
)
210 const sal_Unicode
*pRun
= rLine
.getStr();
214 while( *pRun
&& isSpace( *pRun
) )
218 while( *pRun
&& ! isSpace( *pRun
) )
227 else if( *pRun
== '`' )
229 do pRun
++; while( *pRun
&& *pRun
!= '`' );
233 else if( *pRun
== '\'' )
235 do pRun
++; while( *pRun
&& *pRun
!= '\'' );
239 else if( *pRun
== '"' )
241 do pRun
++; while( *pRun
&& *pRun
!= '"' );
254 OUString
WhitespaceToSpace( const OUString
& rLine
, bool bProtect
)
256 sal_Int32 nLen
= rLine
.getLength();
260 sal_Unicode
*pBuffer
= static_cast<sal_Unicode
*>(alloca( sizeof(sal_Unicode
)*(nLen
+ 1) ));
261 const sal_Unicode
*pRun
= rLine
.getStr();
262 sal_Unicode
*pLeap
= pBuffer
;
266 if( *pRun
&& isSpace( *pRun
) )
272 while( *pRun
&& isSpace( *pRun
) )
274 while( *pRun
&& ! isSpace( *pRun
) )
285 else if( bProtect
&& *pRun
== '`' )
286 CopyUntil( pLeap
, pRun
, '`', true );
287 else if( bProtect
&& *pRun
== '\'' )
288 CopyUntil( pLeap
, pRun
, '\'', true );
289 else if( bProtect
&& *pRun
== '"' )
290 CopyUntil( pLeap
, pRun
, '"', true );
302 // there might be a space at beginning or end
310 return OUString(*pBuffer
== ' ' ? pBuffer
+1 : pBuffer
);
313 OString
WhitespaceToSpace(const OString
& rLine
)
315 sal_Int32 nLen
= rLine
.getLength();
319 char *pBuffer
= static_cast<char*>(alloca( nLen
+ 1 ));
320 const char *pRun
= rLine
.getStr();
321 char *pLeap
= pBuffer
;
325 if( *pRun
&& isSpace( *pRun
) )
331 while( *pRun
&& isSpace( *pRun
) )
333 while( *pRun
&& ! isSpace( *pRun
) )
344 else if( *pRun
== '`' )
345 CopyUntil( pLeap
, pRun
, '`', true );
346 else if( *pRun
== '\'' )
347 CopyUntil( pLeap
, pRun
, '\'', true );
348 else if( *pRun
== '"' )
349 CopyUntil( pLeap
, pRun
, '"', true );
361 // there might be a space at beginning or end
366 return OString(*pBuffer
== ' ' ? pBuffer
+1 : pBuffer
);
371 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */