merge the formfield patch from ooo-build
[ooovba.git] / vcl / source / helper / strhelper.cxx
blob3752bdd494b8ea43de4046f4ca0b59871303b8b0
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: strhelper.cxx,v $
10 * $Revision: 1.16 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_vcl.hxx"
34 #include "vcl/strhelper.hxx"
35 #include "sal/alloca.h"
37 namespace psp {
39 inline int isSpace( char cChar )
41 return
42 cChar == ' ' || cChar == '\t' ||
43 cChar == '\r' || cChar == '\n' ||
44 cChar == 0x0c || cChar == 0x0b;
47 inline int isSpace( sal_Unicode cChar )
49 return
50 cChar == ' ' || cChar == '\t' ||
51 cChar == '\r' || cChar == '\n' ||
52 cChar == 0x0c || cChar == 0x0b;
55 inline int isProtect( char cChar )
57 return cChar == '`' || cChar == '\'' || cChar == '"';
60 inline int isProtect( sal_Unicode cChar )
62 return cChar == '`' || cChar == '\'' || cChar == '"';
65 inline void CopyUntil( char*& pTo, const char*& pFrom, char cUntil, int bIncludeUntil = 0 )
69 if( *pFrom == '\\' )
71 pFrom++;
72 if( *pFrom )
74 *pTo = *pFrom;
75 pTo++;
78 else if( bIncludeUntil || ! isProtect( *pFrom ) )
80 *pTo = *pFrom;
81 pTo++;
83 pFrom++;
84 } while( *pFrom && *pFrom != cUntil );
85 // copy the terminating character unless zero or protector
86 if( ! isProtect( *pFrom ) || bIncludeUntil )
88 *pTo = *pFrom;
89 if( *pTo )
90 pTo++;
92 if( *pFrom )
93 pFrom++;
96 inline void CopyUntil( sal_Unicode*& pTo, const sal_Unicode*& pFrom, sal_Unicode cUntil, int bIncludeUntil = 0 )
100 if( *pFrom == '\\' )
102 pFrom++;
103 if( *pFrom )
105 *pTo = *pFrom;
106 pTo++;
109 else if( bIncludeUntil || ! isProtect( *pFrom ) )
111 *pTo = *pFrom;
112 pTo++;
114 pFrom++;
115 } while( *pFrom && *pFrom != cUntil );
116 // copy the terminating character unless zero or protector
117 if( ! isProtect( *pFrom ) || bIncludeUntil )
119 *pTo = *pFrom;
120 if( *pTo )
121 pTo++;
123 if( *pFrom )
124 pFrom++;
127 String GetCommandLineToken( int nToken, const String& rLine )
129 int nLen = rLine.Len();
130 if( ! nLen )
131 return String();
133 int nActualToken = 0;
134 sal_Unicode* pBuffer = (sal_Unicode*)alloca( sizeof(sal_Unicode)*( nLen + 1 ) );
135 const sal_Unicode* pRun = rLine.GetBuffer();
136 sal_Unicode* pLeap = NULL;
138 while( *pRun && nActualToken <= nToken )
140 while( *pRun && isSpace( *pRun ) )
141 pRun++;
142 pLeap = pBuffer;
143 while( *pRun && ! isSpace( *pRun ) )
145 if( *pRun == '\\' )
147 // escapement
148 pRun++;
149 *pLeap = *pRun;
150 pLeap++;
151 if( *pRun )
152 pRun++;
154 else if( *pRun == '`' )
155 CopyUntil( pLeap, pRun, '`' );
156 else if( *pRun == '\'' )
157 CopyUntil( pLeap, pRun, '\'' );
158 else if( *pRun == '"' )
159 CopyUntil( pLeap, pRun, '"' );
160 else
162 *pLeap = *pRun;
163 pLeap++;
164 pRun++;
167 if( nActualToken != nToken )
168 pBuffer[0] = 0;
169 nActualToken++;
172 *pLeap = 0;
174 String aRet( pBuffer );
175 return aRet;
178 ByteString GetCommandLineToken( int nToken, const ByteString& rLine )
180 int nLen = rLine.Len();
181 if( ! nLen )
182 return ByteString();
184 int nActualToken = 0;
185 char* pBuffer = (char*)alloca( nLen + 1 );
186 const char* pRun = rLine.GetBuffer();
187 char* pLeap = NULL;
189 while( *pRun && nActualToken <= nToken )
191 while( *pRun && isSpace( *pRun ) )
192 pRun++;
193 pLeap = pBuffer;
194 while( *pRun && ! isSpace( *pRun ) )
196 if( *pRun == '\\' )
198 // escapement
199 pRun++;
200 *pLeap = *pRun;
201 pLeap++;
202 if( *pRun )
203 pRun++;
205 else if( *pRun == '`' )
206 CopyUntil( pLeap, pRun, '`' );
207 else if( *pRun == '\'' )
208 CopyUntil( pLeap, pRun, '\'' );
209 else if( *pRun == '"' )
210 CopyUntil( pLeap, pRun, '"' );
211 else
213 *pLeap = *pRun;
214 pLeap++;
215 pRun++;
218 if( nActualToken != nToken )
219 pBuffer[0] = 0;
220 nActualToken++;
223 *pLeap = 0;
225 ByteString aRet( pBuffer );
226 return aRet;
229 int GetCommandLineTokenCount( const String& rLine )
231 if( ! rLine.Len() )
232 return 0;
234 int nTokenCount = 0;
235 const sal_Unicode *pRun = rLine.GetBuffer();
238 while( *pRun )
240 while( *pRun && isSpace( *pRun ) )
241 pRun++;
242 if( ! *pRun )
243 break;
244 while( *pRun && ! isSpace( *pRun ) )
246 if( *pRun == '\\' )
248 // escapement
249 pRun++;
250 if( *pRun )
251 pRun++;
253 else if( *pRun == '`' )
255 do pRun++; while( *pRun && *pRun != '`' );
256 if( *pRun )
257 pRun++;
259 else if( *pRun == '\'' )
261 do pRun++; while( *pRun && *pRun != '\'' );
262 if( *pRun )
263 pRun++;
265 else if( *pRun == '"' )
267 do pRun++; while( *pRun && *pRun != '"' );
268 if( *pRun )
269 pRun++;
271 else
272 pRun++;
274 nTokenCount++;
277 return nTokenCount;
280 int GetCommandLineTokenCount( const ByteString& rLine )
282 if( ! rLine.Len() )
283 return 0;
285 int nTokenCount = 0;
286 const char *pRun = rLine.GetBuffer();
289 while( *pRun )
291 while( *pRun && isSpace( *pRun ) )
292 pRun++;
293 if( ! *pRun )
294 break;
295 while( *pRun && ! isSpace( *pRun ) )
297 if( *pRun == '\\' )
299 // escapement
300 pRun++;
301 if( *pRun )
302 pRun++;
304 else if( *pRun == '`' )
306 do pRun++; while( *pRun && *pRun != '`' );
307 if( *pRun )
308 pRun++;
310 else if( *pRun == '\'' )
312 do pRun++; while( *pRun && *pRun != '\'' );
313 if( *pRun )
314 pRun++;
316 else if( *pRun == '"' )
318 do pRun++; while( *pRun && *pRun != '"' );
319 if( *pRun )
320 pRun++;
322 else
323 pRun++;
325 nTokenCount++;
328 return nTokenCount;
331 String WhitespaceToSpace( const String& rLine, BOOL bProtect )
333 int nLen = rLine.Len();
334 if( ! nLen )
335 return String();
337 sal_Unicode *pBuffer = (sal_Unicode*)alloca( sizeof(sal_Unicode)*(nLen + 1) );
338 const sal_Unicode *pRun = rLine.GetBuffer();
339 sal_Unicode *pLeap = pBuffer;
341 while( *pRun )
343 if( *pRun && isSpace( *pRun ) )
345 *pLeap = ' ';
346 pLeap++;
347 pRun++;
349 while( *pRun && isSpace( *pRun ) )
350 pRun++;
351 while( *pRun && ! isSpace( *pRun ) )
353 if( *pRun == '\\' )
355 // escapement
356 pRun++;
357 *pLeap = *pRun;
358 pLeap++;
359 if( *pRun )
360 pRun++;
362 else if( bProtect && *pRun == '`' )
363 CopyUntil( pLeap, pRun, '`', TRUE );
364 else if( bProtect && *pRun == '\'' )
365 CopyUntil( pLeap, pRun, '\'', TRUE );
366 else if( bProtect && *pRun == '"' )
367 CopyUntil( pLeap, pRun, '"', TRUE );
368 else
370 *pLeap = *pRun;
371 *pLeap++;
372 *pRun++;
377 *pLeap = 0;
379 // there might be a space at beginning or end
380 pLeap--;
381 if( *pLeap == ' ' )
382 *pLeap = 0;
384 String aRet( *pBuffer == ' ' ? pBuffer+1 : pBuffer );
385 return aRet;
388 ByteString WhitespaceToSpace( const ByteString& rLine, BOOL bProtect )
390 int nLen = rLine.Len();
391 if( ! nLen )
392 return ByteString();
394 char *pBuffer = (char*)alloca( nLen + 1 );
395 const char *pRun = rLine.GetBuffer();
396 char *pLeap = pBuffer;
398 while( *pRun )
400 if( *pRun && isSpace( *pRun ) )
402 *pLeap = ' ';
403 pLeap++;
404 pRun++;
406 while( *pRun && isSpace( *pRun ) )
407 pRun++;
408 while( *pRun && ! isSpace( *pRun ) )
410 if( *pRun == '\\' )
412 // escapement
413 pRun++;
414 *pLeap = *pRun;
415 pLeap++;
416 if( *pRun )
417 pRun++;
419 else if( bProtect && *pRun == '`' )
420 CopyUntil( pLeap, pRun, '`', TRUE );
421 else if( bProtect && *pRun == '\'' )
422 CopyUntil( pLeap, pRun, '\'', TRUE );
423 else if( bProtect && *pRun == '"' )
424 CopyUntil( pLeap, pRun, '"', TRUE );
425 else
427 *pLeap = *pRun;
428 *pLeap++;
429 *pRun++;
434 *pLeap = 0;
436 // there might be a space at beginning or end
437 pLeap--;
438 if( *pLeap == ' ' )
439 *pLeap = 0;
441 ByteString aRet( *pBuffer == ' ' ? pBuffer+1 : pBuffer );
442 return aRet;
445 } // namespace