Version 4.0.0.1, tag libreoffice-4.0.0.1
[LibreOffice.git] / vcl / source / helper / strhelper.cxx
blob954413b603a208154d11efd3448739e8ff796796
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 .
21 #include "vcl/strhelper.hxx"
22 #include "sal/alloca.h"
24 namespace psp {
26 inline int isSpace( char cChar )
28 return
29 cChar == ' ' || cChar == '\t' ||
30 cChar == '\r' || cChar == '\n' ||
31 cChar == 0x0c || cChar == 0x0b;
34 inline int isSpace( sal_Unicode cChar )
36 return
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 )
56 if( *pFrom == '\\' )
58 pFrom++;
59 if( *pFrom )
61 *pTo = *pFrom;
62 pTo++;
65 else if( bIncludeUntil || ! isProtect( *pFrom ) )
67 *pTo = *pFrom;
68 pTo++;
70 pFrom++;
71 } while( *pFrom && *pFrom != cUntil );
72 // copy the terminating character unless zero or protector
73 if( ! isProtect( *pFrom ) || bIncludeUntil )
75 *pTo = *pFrom;
76 if( *pTo )
77 pTo++;
79 if( *pFrom )
80 pFrom++;
83 inline void CopyUntil( sal_Unicode*& pTo, const sal_Unicode*& pFrom, sal_Unicode cUntil, int bIncludeUntil = 0 )
87 if( *pFrom == '\\' )
89 pFrom++;
90 if( *pFrom )
92 *pTo = *pFrom;
93 pTo++;
96 else if( bIncludeUntil || ! isProtect( *pFrom ) )
98 *pTo = *pFrom;
99 pTo++;
101 pFrom++;
102 } while( *pFrom && *pFrom != cUntil );
103 // copy the terminating character unless zero or protector
104 if( ! isProtect( *pFrom ) || bIncludeUntil )
106 *pTo = *pFrom;
107 if( *pTo )
108 pTo++;
110 if( *pFrom )
111 pFrom++;
114 String GetCommandLineToken( int nToken, const String& rLine )
116 int nLen = rLine.Len();
117 if( ! nLen )
118 return String();
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 ) )
128 pRun++;
129 pLeap = pBuffer;
130 while( *pRun && ! isSpace( *pRun ) )
132 if( *pRun == '\\' )
134 // escapement
135 pRun++;
136 *pLeap = *pRun;
137 pLeap++;
138 if( *pRun )
139 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, '"' );
147 else
149 *pLeap = *pRun;
150 pLeap++;
151 pRun++;
154 if( nActualToken != nToken )
155 pBuffer[0] = 0;
156 nActualToken++;
159 *pLeap = 0;
161 return rtl::OUString(pBuffer);
164 rtl::OString GetCommandLineToken(int nToken, const rtl::OString& rLine)
166 sal_Int32 nLen = rLine.getLength();
167 if (!nLen)
168 return rLine;
170 int nActualToken = 0;
171 char* pBuffer = (char*)alloca( nLen + 1 );
172 const char* pRun = rLine.getStr();
173 char* pLeap = NULL;
175 while( *pRun && nActualToken <= nToken )
177 while( *pRun && isSpace( *pRun ) )
178 pRun++;
179 pLeap = pBuffer;
180 while( *pRun && ! isSpace( *pRun ) )
182 if( *pRun == '\\' )
184 // escapement
185 pRun++;
186 *pLeap = *pRun;
187 pLeap++;
188 if( *pRun )
189 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, '"' );
197 else
199 *pLeap = *pRun;
200 pLeap++;
201 pRun++;
204 if( nActualToken != nToken )
205 pBuffer[0] = 0;
206 nActualToken++;
209 *pLeap = 0;
211 return rtl::OString(pBuffer);
214 int GetCommandLineTokenCount(const rtl::OUString& rLine)
216 if (rLine.isEmpty())
217 return 0;
219 int nTokenCount = 0;
220 const sal_Unicode *pRun = rLine.getStr();
222 while( *pRun )
224 while( *pRun && isSpace( *pRun ) )
225 pRun++;
226 if( ! *pRun )
227 break;
228 while( *pRun && ! isSpace( *pRun ) )
230 if( *pRun == '\\' )
232 // escapement
233 pRun++;
234 if( *pRun )
235 pRun++;
237 else if( *pRun == '`' )
239 do pRun++; while( *pRun && *pRun != '`' );
240 if( *pRun )
241 pRun++;
243 else if( *pRun == '\'' )
245 do pRun++; while( *pRun && *pRun != '\'' );
246 if( *pRun )
247 pRun++;
249 else if( *pRun == '"' )
251 do pRun++; while( *pRun && *pRun != '"' );
252 if( *pRun )
253 pRun++;
255 else
256 pRun++;
258 nTokenCount++;
261 return nTokenCount;
264 String WhitespaceToSpace( const String& rLine, sal_Bool bProtect )
266 int nLen = rLine.Len();
267 if( ! nLen )
268 return String();
270 sal_Unicode *pBuffer = (sal_Unicode*)alloca( sizeof(sal_Unicode)*(nLen + 1) );
271 const sal_Unicode *pRun = rLine.GetBuffer();
272 sal_Unicode *pLeap = pBuffer;
274 while( *pRun )
276 if( *pRun && isSpace( *pRun ) )
278 *pLeap = ' ';
279 pLeap++;
280 pRun++;
282 while( *pRun && isSpace( *pRun ) )
283 pRun++;
284 while( *pRun && ! isSpace( *pRun ) )
286 if( *pRun == '\\' )
288 // escapement
289 pRun++;
290 *pLeap = *pRun;
291 pLeap++;
292 if( *pRun )
293 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 );
301 else
303 *pLeap = *pRun;
304 ++pLeap;
305 ++pRun;
310 *pLeap = 0;
312 // there might be a space at beginning or end
313 pLeap--;
314 if( *pLeap == ' ' )
315 *pLeap = 0;
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();
323 if (!nLen)
324 return rLine;
326 char *pBuffer = (char*)alloca( nLen + 1 );
327 const char *pRun = rLine.getStr();
328 char *pLeap = pBuffer;
330 while( *pRun )
332 if( *pRun && isSpace( *pRun ) )
334 *pLeap = ' ';
335 pLeap++;
336 pRun++;
338 while( *pRun && isSpace( *pRun ) )
339 pRun++;
340 while( *pRun && ! isSpace( *pRun ) )
342 if( *pRun == '\\' )
344 // escapement
345 pRun++;
346 *pLeap = *pRun;
347 pLeap++;
348 if( *pRun )
349 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 );
357 else
359 *pLeap = *pRun;
360 ++pLeap;
361 ++pRun;
366 *pLeap = 0;
368 // there might be a space at beginning or end
369 pLeap--;
370 if( *pLeap == ' ' )
371 *pLeap = 0;
373 return rtl::OString(*pBuffer == ' ' ? pBuffer+1 : pBuffer);
376 } // namespace
378 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */