Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : :
30 : : #include "vcl/strhelper.hxx"
31 : : #include "sal/alloca.h"
32 : :
33 : : namespace psp {
34 : :
35 : 1215300 : inline int isSpace( char cChar )
36 : : {
37 : : return
38 : : cChar == ' ' || cChar == '\t' ||
39 : : cChar == '\r' || cChar == '\n' ||
40 [ + + ][ + - ]: 1215300 : cChar == 0x0c || cChar == 0x0b;
[ + - ][ + - ]
[ + - ][ - + ]
41 : : }
42 : :
43 : 633430 : inline int isSpace( sal_Unicode cChar )
44 : : {
45 : : return
46 : : cChar == ' ' || cChar == '\t' ||
47 : : cChar == '\r' || cChar == '\n' ||
48 [ + + ][ + - ]: 633430 : cChar == 0x0c || cChar == 0x0b;
[ + - ][ + - ]
[ + - ][ - + ]
49 : : }
50 : :
51 : 19860 : inline int isProtect( char cChar )
52 : : {
53 [ + - ][ + - ]: 19860 : return cChar == '`' || cChar == '\'' || cChar == '"';
[ + - ]
54 : : }
55 : :
56 : 0 : inline int isProtect( sal_Unicode cChar )
57 : : {
58 [ # # ][ # # ]: 0 : return cChar == '`' || cChar == '\'' || cChar == '"';
[ # # ]
59 : : }
60 : :
61 : 543720 : inline void CopyUntil( char*& pTo, const char*& pFrom, char cUntil, int bIncludeUntil = 0 )
62 : : {
63 [ + - ][ + + ]: 543720 : do
[ + + ]
64 : : {
65 [ + + ]: 543720 : if( *pFrom == '\\' )
66 : : {
67 : 120 : pFrom++;
68 [ + - ]: 120 : if( *pFrom )
69 : : {
70 : 120 : *pTo = *pFrom;
71 : 120 : pTo++;
72 : : }
73 : : }
74 [ - + ][ # # ]: 543600 : else if( bIncludeUntil || ! isProtect( *pFrom ) )
[ + - ]
75 : : {
76 : 543600 : *pTo = *pFrom;
77 : 543600 : pTo++;
78 : : }
79 : 543720 : pFrom++;
80 : : } while( *pFrom && *pFrom != cUntil );
81 : : // copy the terminating character unless zero or protector
82 [ + - ][ + - ]: 19860 : if( ! isProtect( *pFrom ) || bIncludeUntil )
[ + - ]
83 : : {
84 : 19860 : *pTo = *pFrom;
85 [ + - ]: 19860 : if( *pTo )
86 : 19860 : pTo++;
87 : : }
88 [ + - ]: 19860 : if( *pFrom )
89 : 19860 : pFrom++;
90 : 19860 : }
91 : :
92 : 0 : inline void CopyUntil( sal_Unicode*& pTo, const sal_Unicode*& pFrom, sal_Unicode cUntil, int bIncludeUntil = 0 )
93 : : {
94 [ # # ][ # # ]: 0 : do
[ # # ]
95 : : {
96 [ # # ]: 0 : if( *pFrom == '\\' )
97 : : {
98 : 0 : pFrom++;
99 [ # # ]: 0 : if( *pFrom )
100 : : {
101 : 0 : *pTo = *pFrom;
102 : 0 : pTo++;
103 : : }
104 : : }
105 [ # # ][ # # ]: 0 : else if( bIncludeUntil || ! isProtect( *pFrom ) )
[ # # ]
106 : : {
107 : 0 : *pTo = *pFrom;
108 : 0 : pTo++;
109 : : }
110 : 0 : pFrom++;
111 : : } while( *pFrom && *pFrom != cUntil );
112 : : // copy the terminating character unless zero or protector
113 [ # # ][ # # ]: 0 : if( ! isProtect( *pFrom ) || bIncludeUntil )
[ # # ]
114 : : {
115 : 0 : *pTo = *pFrom;
116 [ # # ]: 0 : if( *pTo )
117 : 0 : pTo++;
118 : : }
119 [ # # ]: 0 : if( *pFrom )
120 : 0 : pFrom++;
121 : 0 : }
122 : :
123 : 21640 : String GetCommandLineToken( int nToken, const String& rLine )
124 : : {
125 : 21640 : int nLen = rLine.Len();
126 [ - + ]: 21640 : if( ! nLen )
127 [ # # ]: 0 : return String();
128 : :
129 : 21640 : int nActualToken = 0;
130 : 21640 : sal_Unicode* pBuffer = (sal_Unicode*)alloca( sizeof(sal_Unicode)*( nLen + 1 ) );
131 : 21640 : const sal_Unicode* pRun = rLine.GetBuffer();
132 : 21640 : sal_Unicode* pLeap = NULL;
133 : :
134 [ + + ][ + + ]: 61268 : while( *pRun && nActualToken <= nToken )
[ + + ]
135 : : {
136 [ + - ][ + + ]: 57616 : while( *pRun && isSpace( *pRun ) )
[ + + ]
137 : 17988 : pRun++;
138 : 39628 : pLeap = pBuffer;
139 [ + + ][ + + ]: 542148 : while( *pRun && ! isSpace( *pRun ) )
[ + + ]
140 : : {
141 [ - + ]: 502520 : if( *pRun == '\\' )
142 : : {
143 : : // escapement
144 : 0 : pRun++;
145 : 0 : *pLeap = *pRun;
146 : 0 : pLeap++;
147 [ # # ]: 0 : if( *pRun )
148 : 0 : pRun++;
149 : : }
150 [ - + ]: 502520 : else if( *pRun == '`' )
151 : 0 : CopyUntil( pLeap, pRun, '`' );
152 [ - + ]: 502520 : else if( *pRun == '\'' )
153 : 0 : CopyUntil( pLeap, pRun, '\'' );
154 [ - + ]: 502520 : else if( *pRun == '"' )
155 : 0 : CopyUntil( pLeap, pRun, '"' );
156 : : else
157 : : {
158 : 502520 : *pLeap = *pRun;
159 : 502520 : pLeap++;
160 : 502520 : pRun++;
161 : : }
162 : : }
163 [ + + ]: 39628 : if( nActualToken != nToken )
164 : 21528 : pBuffer[0] = 0;
165 : 39628 : nActualToken++;
166 : : }
167 : :
168 : 21640 : *pLeap = 0;
169 : :
170 [ + - ]: 21640 : return rtl::OUString(pBuffer);
171 : : }
172 : :
173 : 23520 : rtl::OString GetCommandLineToken(int nToken, const rtl::OString& rLine)
174 : : {
175 : 23520 : sal_Int32 nLen = rLine.getLength();
176 [ - + ]: 23520 : if (!nLen)
177 : 0 : return rLine;
178 : :
179 : 23520 : int nActualToken = 0;
180 : 23520 : char* pBuffer = (char*)alloca( nLen + 1 );
181 : 23520 : const char* pRun = rLine.getStr();
182 : 23520 : char* pLeap = NULL;
183 : :
184 [ + + ][ + + ]: 48000 : while( *pRun && nActualToken <= nToken )
[ + + ]
185 : : {
186 [ + - ][ + + ]: 26160 : while( *pRun && isSpace( *pRun ) )
[ + + ]
187 : 1680 : pRun++;
188 : 24480 : pLeap = pBuffer;
189 [ + + ][ + + ]: 327120 : while( *pRun && ! isSpace( *pRun ) )
[ + + ]
190 : : {
191 [ - + ]: 302640 : if( *pRun == '\\' )
192 : : {
193 : : // escapement
194 : 0 : pRun++;
195 : 0 : *pLeap = *pRun;
196 : 0 : pLeap++;
197 [ # # ]: 0 : if( *pRun )
198 : 0 : pRun++;
199 : : }
200 [ - + ]: 302640 : else if( *pRun == '`' )
201 : 0 : CopyUntil( pLeap, pRun, '`' );
202 [ - + ]: 302640 : else if( *pRun == '\'' )
203 : 0 : CopyUntil( pLeap, pRun, '\'' );
204 [ - + ]: 302640 : else if( *pRun == '"' )
205 : 0 : CopyUntil( pLeap, pRun, '"' );
206 : : else
207 : : {
208 : 302640 : *pLeap = *pRun;
209 : 302640 : pLeap++;
210 : 302640 : pRun++;
211 : : }
212 : : }
213 [ + + ]: 24480 : if( nActualToken != nToken )
214 : 960 : pBuffer[0] = 0;
215 : 24480 : nActualToken++;
216 : : }
217 : :
218 : 23520 : *pLeap = 0;
219 : :
220 : 23520 : return rtl::OString(pBuffer);
221 : : }
222 : :
223 : 0 : int GetCommandLineTokenCount(const rtl::OUString& rLine)
224 : : {
225 [ # # ]: 0 : if (rLine.isEmpty())
226 : 0 : return 0;
227 : :
228 : 0 : int nTokenCount = 0;
229 : 0 : const sal_Unicode *pRun = rLine.getStr();
230 : :
231 [ # # ]: 0 : while( *pRun )
232 : : {
233 [ # # ][ # # ]: 0 : while( *pRun && isSpace( *pRun ) )
[ # # ]
234 : 0 : pRun++;
235 [ # # ]: 0 : if( ! *pRun )
236 : 0 : break;
237 [ # # ][ # # ]: 0 : while( *pRun && ! isSpace( *pRun ) )
[ # # ]
238 : : {
239 [ # # ]: 0 : if( *pRun == '\\' )
240 : : {
241 : : // escapement
242 : 0 : pRun++;
243 [ # # ]: 0 : if( *pRun )
244 : 0 : pRun++;
245 : : }
246 [ # # ]: 0 : else if( *pRun == '`' )
247 : : {
248 [ # # ][ # # ]: 0 : do pRun++; while( *pRun && *pRun != '`' );
[ # # ]
249 [ # # ]: 0 : if( *pRun )
250 : 0 : pRun++;
251 : : }
252 [ # # ]: 0 : else if( *pRun == '\'' )
253 : : {
254 [ # # ][ # # ]: 0 : do pRun++; while( *pRun && *pRun != '\'' );
[ # # ]
255 [ # # ]: 0 : if( *pRun )
256 : 0 : pRun++;
257 : : }
258 [ # # ]: 0 : else if( *pRun == '"' )
259 : : {
260 [ # # ][ # # ]: 0 : do pRun++; while( *pRun && *pRun != '"' );
[ # # ]
261 [ # # ]: 0 : if( *pRun )
262 : 0 : pRun++;
263 : : }
264 : : else
265 : 0 : pRun++;
266 : : }
267 : 0 : nTokenCount++;
268 : : }
269 : :
270 : 0 : return nTokenCount;
271 : : }
272 : :
273 : 4302 : String WhitespaceToSpace( const String& rLine, sal_Bool bProtect )
274 : : {
275 : 4302 : int nLen = rLine.Len();
276 [ + + ]: 4302 : if( ! nLen )
277 [ + - ]: 316 : return String();
278 : :
279 : 3986 : sal_Unicode *pBuffer = (sal_Unicode*)alloca( sizeof(sal_Unicode)*(nLen + 1) );
280 : 3986 : const sal_Unicode *pRun = rLine.GetBuffer();
281 : 3986 : sal_Unicode *pLeap = pBuffer;
282 : :
283 [ + + ]: 10020 : while( *pRun )
284 : : {
285 [ + - ][ + - ]: 6034 : if( *pRun && isSpace( *pRun ) )
[ + - ]
286 : : {
287 : 6034 : *pLeap = ' ';
288 : 6034 : pLeap++;
289 : 6034 : pRun++;
290 : : }
291 [ + + ][ - + ]: 6034 : while( *pRun && isSpace( *pRun ) )
[ - + ]
292 : 0 : pRun++;
293 [ + + ][ + + ]: 38820 : while( *pRun && ! isSpace( *pRun ) )
[ + + ]
294 : : {
295 [ - + ]: 32786 : if( *pRun == '\\' )
296 : : {
297 : : // escapement
298 : 0 : pRun++;
299 : 0 : *pLeap = *pRun;
300 : 0 : pLeap++;
301 [ # # ]: 0 : if( *pRun )
302 : 0 : pRun++;
303 : : }
304 [ + - ][ - + ]: 32786 : else if( bProtect && *pRun == '`' )
305 : 0 : CopyUntil( pLeap, pRun, '`', sal_True );
306 [ + - ][ - + ]: 32786 : else if( bProtect && *pRun == '\'' )
307 : 0 : CopyUntil( pLeap, pRun, '\'', sal_True );
308 [ + - ][ - + ]: 32786 : else if( bProtect && *pRun == '"' )
309 : 0 : CopyUntil( pLeap, pRun, '"', sal_True );
310 : : else
311 : : {
312 : 32786 : *pLeap = *pRun;
313 : 32786 : ++pLeap;
314 : 32786 : ++pRun;
315 : : }
316 : : }
317 : : }
318 : :
319 : 3986 : *pLeap = 0;
320 : :
321 : : // there might be a space at beginning or end
322 : 3986 : pLeap--;
323 [ + + ]: 3986 : if( *pLeap == ' ' )
324 : 68 : *pLeap = 0;
325 : :
326 [ + + ][ + - ]: 4302 : return rtl::OUString(*pBuffer == ' ' ? pBuffer+1 : pBuffer);
327 : : }
328 : :
329 : 42060 : rtl::OString WhitespaceToSpace(const rtl::OString& rLine, sal_Bool bProtect)
330 : : {
331 : 42060 : sal_Int32 nLen = rLine.getLength();
332 [ - + ]: 42060 : if (!nLen)
333 : 0 : return rLine;
334 : :
335 : 42060 : char *pBuffer = (char*)alloca( nLen + 1 );
336 : 42060 : const char *pRun = rLine.getStr();
337 : 42060 : char *pLeap = pBuffer;
338 : :
339 [ + + ]: 131940 : while( *pRun )
340 : : {
341 [ + - ][ + + ]: 89880 : if( *pRun && isSpace( *pRun ) )
[ + + ]
342 : : {
343 : 69240 : *pLeap = ' ';
344 : 69240 : pLeap++;
345 : 69240 : pRun++;
346 : : }
347 [ + - ][ - + ]: 89880 : while( *pRun && isSpace( *pRun ) )
[ - + ]
348 : 0 : pRun++;
349 [ + + ][ + + ]: 730140 : while( *pRun && ! isSpace( *pRun ) )
[ + + ]
350 : : {
351 [ - + ]: 640260 : if( *pRun == '\\' )
352 : : {
353 : : // escapement
354 : 0 : pRun++;
355 : 0 : *pLeap = *pRun;
356 : 0 : pLeap++;
357 [ # # ]: 0 : if( *pRun )
358 : 0 : pRun++;
359 : : }
360 [ + - ][ - + ]: 640260 : else if( bProtect && *pRun == '`' )
361 : 0 : CopyUntil( pLeap, pRun, '`', sal_True );
362 [ + - ][ - + ]: 640260 : else if( bProtect && *pRun == '\'' )
363 : 0 : CopyUntil( pLeap, pRun, '\'', sal_True );
364 [ + - ][ + + ]: 640260 : else if( bProtect && *pRun == '"' )
365 : 19860 : CopyUntil( pLeap, pRun, '"', sal_True );
366 : : else
367 : : {
368 : 620400 : *pLeap = *pRun;
369 : 620400 : ++pLeap;
370 : 620400 : ++pRun;
371 : : }
372 : : }
373 : : }
374 : :
375 : 42060 : *pLeap = 0;
376 : :
377 : : // there might be a space at beginning or end
378 : 42060 : pLeap--;
379 [ - + ]: 42060 : if( *pLeap == ' ' )
380 : 0 : *pLeap = 0;
381 : :
382 [ + + ]: 42060 : return rtl::OString(*pBuffer == ' ' ? pBuffer+1 : pBuffer);
383 : : }
384 : :
385 : : } // namespace
386 : :
387 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|