Branch libreoffice-5-0-4
[LibreOffice.git] / vcl / source / helper / strhelper.cxx
blobea08f22b83171508ea4f5ef176afa978fc6aec26
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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"
23 namespace psp {
25 inline bool isSpace( sal_Unicode cChar )
27 return
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 )
42 if( *pFrom == '\\' )
44 pFrom++;
45 if( *pFrom )
47 *pTo = *pFrom;
48 pTo++;
51 else if( bIncludeUntil || ! isProtect( *pFrom ) )
53 *pTo = *pFrom;
54 pTo++;
56 pFrom++;
57 } while( *pFrom && *pFrom != cUntil );
58 // copy the terminating character unless zero or protector
59 if( ! isProtect( *pFrom ) || bIncludeUntil )
61 *pTo = *pFrom;
62 if( *pTo )
63 pTo++;
65 if( *pFrom )
66 pFrom++;
69 inline void CopyUntil( sal_Unicode*& pTo, const sal_Unicode*& pFrom, sal_Unicode cUntil, bool bIncludeUntil = false )
73 if( *pFrom == '\\' )
75 pFrom++;
76 if( *pFrom )
78 *pTo = *pFrom;
79 pTo++;
82 else if( bIncludeUntil || ! isProtect( *pFrom ) )
84 *pTo = *pFrom;
85 pTo++;
87 pFrom++;
88 } while( *pFrom && *pFrom != cUntil );
89 // copy the terminating character unless zero or protector
90 if( ! isProtect( *pFrom ) || bIncludeUntil )
92 *pTo = *pFrom;
93 if( *pTo )
94 pTo++;
96 if( *pFrom )
97 pFrom++;
100 OUString GetCommandLineToken( int nToken, const OUString& rLine )
102 sal_Int32 nLen = rLine.getLength();
103 if( ! nLen )
104 return OUString();
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 ) )
114 pRun++;
115 pLeap = pBuffer;
116 while( *pRun && ! isSpace( *pRun ) )
118 if( *pRun == '\\' )
120 // escapement
121 pRun++;
122 *pLeap = *pRun;
123 pLeap++;
124 if( *pRun )
125 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, '"' );
133 else
135 *pLeap = *pRun;
136 pLeap++;
137 pRun++;
140 if( nActualToken != nToken )
141 pBuffer[0] = 0;
142 nActualToken++;
145 *pLeap = 0;
147 return OUString(pBuffer);
150 OString GetCommandLineToken(int nToken, const OString& rLine)
152 sal_Int32 nLen = rLine.getLength();
153 if (!nLen)
154 return rLine;
156 int nActualToken = 0;
157 char* pBuffer = static_cast<char*>(alloca( nLen + 1 ));
158 const char* pRun = rLine.getStr();
159 char* pLeap = NULL;
161 while( *pRun && nActualToken <= nToken )
163 while( *pRun && isSpace( *pRun ) )
164 pRun++;
165 pLeap = pBuffer;
166 while( *pRun && ! isSpace( *pRun ) )
168 if( *pRun == '\\' )
170 // escapement
171 pRun++;
172 *pLeap = *pRun;
173 pLeap++;
174 if( *pRun )
175 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, '"' );
183 else
185 *pLeap = *pRun;
186 pLeap++;
187 pRun++;
190 if( nActualToken != nToken )
191 pBuffer[0] = 0;
192 nActualToken++;
195 *pLeap = 0;
197 return OString(pBuffer);
200 int GetCommandLineTokenCount(const OUString& rLine)
202 if (rLine.isEmpty())
203 return 0;
205 int nTokenCount = 0;
206 const sal_Unicode *pRun = rLine.getStr();
208 while( *pRun )
210 while( *pRun && isSpace( *pRun ) )
211 pRun++;
212 if( ! *pRun )
213 break;
214 while( *pRun && ! isSpace( *pRun ) )
216 if( *pRun == '\\' )
218 // escapement
219 pRun++;
220 if( *pRun )
221 pRun++;
223 else if( *pRun == '`' )
225 do pRun++; while( *pRun && *pRun != '`' );
226 if( *pRun )
227 pRun++;
229 else if( *pRun == '\'' )
231 do pRun++; while( *pRun && *pRun != '\'' );
232 if( *pRun )
233 pRun++;
235 else if( *pRun == '"' )
237 do pRun++; while( *pRun && *pRun != '"' );
238 if( *pRun )
239 pRun++;
241 else
242 pRun++;
244 nTokenCount++;
247 return nTokenCount;
250 OUString WhitespaceToSpace( const OUString& rLine, bool bProtect )
252 sal_Int32 nLen = rLine.getLength();
253 if( ! nLen )
254 return OUString();
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;
260 while( *pRun )
262 if( *pRun && isSpace( *pRun ) )
264 *pLeap = ' ';
265 pLeap++;
266 pRun++;
268 while( *pRun && isSpace( *pRun ) )
269 pRun++;
270 while( *pRun && ! isSpace( *pRun ) )
272 if( *pRun == '\\' )
274 // escapement
275 pRun++;
276 *pLeap = *pRun;
277 pLeap++;
278 if( *pRun )
279 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 );
287 else
289 *pLeap = *pRun;
290 ++pLeap;
291 ++pRun;
296 *pLeap = 0;
298 // there might be a space at beginning or end
299 pLeap--;
300 if( *pLeap == ' ' )
301 *pLeap = 0;
303 return OUString(*pBuffer == ' ' ? pBuffer+1 : pBuffer);
306 OString WhitespaceToSpace(const OString& rLine, bool bProtect)
308 sal_Int32 nLen = rLine.getLength();
309 if (!nLen)
310 return rLine;
312 char *pBuffer = static_cast<char*>(alloca( nLen + 1 ));
313 const char *pRun = rLine.getStr();
314 char *pLeap = pBuffer;
316 while( *pRun )
318 if( *pRun && isSpace( *pRun ) )
320 *pLeap = ' ';
321 pLeap++;
322 pRun++;
324 while( *pRun && isSpace( *pRun ) )
325 pRun++;
326 while( *pRun && ! isSpace( *pRun ) )
328 if( *pRun == '\\' )
330 // escapement
331 pRun++;
332 *pLeap = *pRun;
333 pLeap++;
334 if( *pRun )
335 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 );
343 else
345 *pLeap = *pRun;
346 ++pLeap;
347 ++pRun;
352 *pLeap = 0;
354 // there might be a space at beginning or end
355 pLeap--;
356 if( *pLeap == ' ' )
357 *pLeap = 0;
359 return OString(*pBuffer == ' ' ? pBuffer+1 : pBuffer);
362 } // namespace
364 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */