Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include "vcl/strhelper.hxx"
21 : #include "sal/alloca.h"
22 :
23 : namespace psp {
24 :
25 1055132 : inline bool isSpace( char cChar )
26 : {
27 : return
28 936000 : cChar == ' ' || cChar == '\t' ||
29 936000 : cChar == '\r' || cChar == '\n' ||
30 1991132 : cChar == 0x0c || cChar == 0x0b;
31 : }
32 :
33 549109 : inline bool isSpace( sal_Unicode cChar )
34 : {
35 : return
36 501901 : cChar == ' ' || cChar == '\t' ||
37 501901 : cChar == '\r' || cChar == '\n' ||
38 1051010 : cChar == 0x0c || cChar == 0x0b;
39 : }
40 :
41 17212 : inline bool isProtect( char cChar )
42 : {
43 17212 : return cChar == '`' || cChar == '\'' || cChar == '"';
44 : }
45 :
46 0 : inline bool isProtect( sal_Unicode cChar )
47 : {
48 0 : return cChar == '`' || cChar == '\'' || cChar == '"';
49 : }
50 :
51 471224 : inline void CopyUntil( char*& pTo, const char*& pFrom, char cUntil, bool bIncludeUntil = false )
52 : {
53 471224 : do
54 : {
55 471224 : if( *pFrom == '\\' )
56 : {
57 104 : pFrom++;
58 104 : if( *pFrom )
59 : {
60 104 : *pTo = *pFrom;
61 104 : pTo++;
62 : }
63 : }
64 471120 : else if( bIncludeUntil || ! isProtect( *pFrom ) )
65 : {
66 471120 : *pTo = *pFrom;
67 471120 : pTo++;
68 : }
69 471224 : pFrom++;
70 471224 : } while( *pFrom && *pFrom != cUntil );
71 : // copy the terminating character unless zero or protector
72 17212 : if( ! isProtect( *pFrom ) || bIncludeUntil )
73 : {
74 17212 : *pTo = *pFrom;
75 17212 : if( *pTo )
76 17212 : pTo++;
77 : }
78 17212 : if( *pFrom )
79 17212 : pFrom++;
80 17212 : }
81 :
82 0 : inline void CopyUntil( sal_Unicode*& pTo, const sal_Unicode*& pFrom, sal_Unicode cUntil, bool bIncludeUntil = false )
83 : {
84 0 : do
85 : {
86 0 : if( *pFrom == '\\' )
87 : {
88 0 : pFrom++;
89 0 : if( *pFrom )
90 : {
91 0 : *pTo = *pFrom;
92 0 : pTo++;
93 : }
94 : }
95 0 : else if( bIncludeUntil || ! isProtect( *pFrom ) )
96 : {
97 0 : *pTo = *pFrom;
98 0 : pTo++;
99 : }
100 0 : pFrom++;
101 0 : } while( *pFrom && *pFrom != cUntil );
102 : // copy the terminating character unless zero or protector
103 0 : if( ! isProtect( *pFrom ) || bIncludeUntil )
104 : {
105 0 : *pTo = *pFrom;
106 0 : if( *pTo )
107 0 : pTo++;
108 : }
109 0 : if( *pFrom )
110 0 : pFrom++;
111 0 : }
112 :
113 21046 : OUString GetCommandLineToken( int nToken, const OUString& rLine )
114 : {
115 21046 : sal_Int32 nLen = rLine.getLength();
116 21046 : if( ! nLen )
117 0 : return OUString();
118 :
119 21046 : int nActualToken = 0;
120 21046 : sal_Unicode* pBuffer = (sal_Unicode*)alloca( sizeof(sal_Unicode)*( nLen + 1 ) );
121 21046 : const sal_Unicode* pRun = rLine.getStr();
122 21046 : sal_Unicode* pLeap = NULL;
123 :
124 80743 : while( *pRun && nActualToken <= nToken )
125 : {
126 94907 : while( *pRun && isSpace( *pRun ) )
127 17605 : pRun++;
128 38651 : pLeap = pBuffer;
129 524640 : while( *pRun && ! isSpace( *pRun ) )
130 : {
131 447338 : if( *pRun == '\\' )
132 : {
133 : // escapement
134 0 : pRun++;
135 0 : *pLeap = *pRun;
136 0 : pLeap++;
137 0 : if( *pRun )
138 0 : pRun++;
139 : }
140 447338 : else if( *pRun == '`' )
141 0 : CopyUntil( pLeap, pRun, '`' );
142 447338 : else if( *pRun == '\'' )
143 0 : CopyUntil( pLeap, pRun, '\'' );
144 447338 : else if( *pRun == '"' )
145 0 : CopyUntil( pLeap, pRun, '"' );
146 : else
147 : {
148 447338 : *pLeap = *pRun;
149 447338 : pLeap++;
150 447338 : pRun++;
151 : }
152 : }
153 38651 : if( nActualToken != nToken )
154 20725 : pBuffer[0] = 0;
155 38651 : nActualToken++;
156 : }
157 :
158 21046 : *pLeap = 0;
159 :
160 21046 : return OUString(pBuffer);
161 : }
162 :
163 20436 : OString GetCommandLineToken(int nToken, const OString& rLine)
164 : {
165 20436 : sal_Int32 nLen = rLine.getLength();
166 20436 : if (!nLen)
167 0 : return rLine;
168 :
169 20436 : int nActualToken = 0;
170 20436 : char* pBuffer = (char*)alloca( nLen + 1 );
171 20436 : const char* pRun = rLine.getStr();
172 20436 : char* pLeap = NULL;
173 :
174 62140 : while( *pRun && nActualToken <= nToken )
175 : {
176 43992 : while( *pRun && isSpace( *pRun ) )
177 1456 : pRun++;
178 21268 : pLeap = pBuffer;
179 305500 : while( *pRun && ! isSpace( *pRun ) )
180 : {
181 262964 : if( *pRun == '\\' )
182 : {
183 : // escapement
184 0 : pRun++;
185 0 : *pLeap = *pRun;
186 0 : pLeap++;
187 0 : if( *pRun )
188 0 : pRun++;
189 : }
190 262964 : else if( *pRun == '`' )
191 0 : CopyUntil( pLeap, pRun, '`' );
192 262964 : else if( *pRun == '\'' )
193 0 : CopyUntil( pLeap, pRun, '\'' );
194 262964 : else if( *pRun == '"' )
195 0 : CopyUntil( pLeap, pRun, '"' );
196 : else
197 : {
198 262964 : *pLeap = *pRun;
199 262964 : pLeap++;
200 262964 : pRun++;
201 : }
202 : }
203 21268 : if( nActualToken != nToken )
204 832 : pBuffer[0] = 0;
205 21268 : nActualToken++;
206 : }
207 :
208 20436 : *pLeap = 0;
209 :
210 20436 : return OString(pBuffer);
211 : }
212 :
213 0 : int GetCommandLineTokenCount(const OUString& rLine)
214 : {
215 0 : if (rLine.isEmpty())
216 0 : return 0;
217 :
218 0 : int nTokenCount = 0;
219 0 : const sal_Unicode *pRun = rLine.getStr();
220 :
221 0 : while( *pRun )
222 : {
223 0 : while( *pRun && isSpace( *pRun ) )
224 0 : pRun++;
225 0 : if( ! *pRun )
226 0 : break;
227 0 : while( *pRun && ! isSpace( *pRun ) )
228 : {
229 0 : if( *pRun == '\\' )
230 : {
231 : // escapement
232 0 : pRun++;
233 0 : if( *pRun )
234 0 : pRun++;
235 : }
236 0 : else if( *pRun == '`' )
237 : {
238 0 : do pRun++; while( *pRun && *pRun != '`' );
239 0 : if( *pRun )
240 0 : pRun++;
241 : }
242 0 : else if( *pRun == '\'' )
243 : {
244 0 : do pRun++; while( *pRun && *pRun != '\'' );
245 0 : if( *pRun )
246 0 : pRun++;
247 : }
248 0 : else if( *pRun == '"' )
249 : {
250 0 : do pRun++; while( *pRun && *pRun != '"' );
251 0 : if( *pRun )
252 0 : pRun++;
253 : }
254 : else
255 0 : pRun++;
256 : }
257 0 : nTokenCount++;
258 : }
259 :
260 0 : return nTokenCount;
261 : }
262 :
263 1692 : OUString WhitespaceToSpace( const OUString& rLine, bool bProtect )
264 : {
265 1692 : sal_Int32 nLen = rLine.getLength();
266 1692 : if( ! nLen )
267 108 : return OUString();
268 :
269 1584 : sal_Unicode *pBuffer = (sal_Unicode*)alloca( sizeof(sal_Unicode)*(nLen + 1) );
270 1584 : const sal_Unicode *pRun = rLine.getStr();
271 1584 : sal_Unicode *pLeap = pBuffer;
272 :
273 5616 : while( *pRun )
274 : {
275 2448 : if( *pRun && isSpace( *pRun ) )
276 : {
277 2448 : *pLeap = ' ';
278 2448 : pLeap++;
279 2448 : pRun++;
280 : }
281 4896 : while( *pRun && isSpace( *pRun ) )
282 0 : pRun++;
283 18396 : while( *pRun && ! isSpace( *pRun ) )
284 : {
285 13500 : if( *pRun == '\\' )
286 : {
287 : // escapement
288 0 : pRun++;
289 0 : *pLeap = *pRun;
290 0 : pLeap++;
291 0 : if( *pRun )
292 0 : pRun++;
293 : }
294 13500 : else if( bProtect && *pRun == '`' )
295 0 : CopyUntil( pLeap, pRun, '`', true );
296 13500 : else if( bProtect && *pRun == '\'' )
297 0 : CopyUntil( pLeap, pRun, '\'', true );
298 13500 : else if( bProtect && *pRun == '"' )
299 0 : CopyUntil( pLeap, pRun, '"', true );
300 : else
301 : {
302 13500 : *pLeap = *pRun;
303 13500 : ++pLeap;
304 13500 : ++pRun;
305 : }
306 : }
307 : }
308 :
309 1584 : *pLeap = 0;
310 :
311 : // there might be a space at beginning or end
312 1584 : pLeap--;
313 1584 : if( *pLeap == ' ' )
314 36 : *pLeap = 0;
315 :
316 1584 : return OUString(*pBuffer == ' ' ? pBuffer+1 : pBuffer);
317 : }
318 :
319 36556 : OString WhitespaceToSpace(const OString& rLine, bool bProtect)
320 : {
321 36556 : sal_Int32 nLen = rLine.getLength();
322 36556 : if (!nLen)
323 0 : return rLine;
324 :
325 36556 : char *pBuffer = (char*)alloca( nLen + 1 );
326 36556 : const char *pRun = rLine.getStr();
327 36556 : char *pLeap = pBuffer;
328 :
329 151112 : while( *pRun )
330 : {
331 78000 : if( *pRun && isSpace( *pRun ) )
332 : {
333 60060 : *pLeap = ' ';
334 60060 : pLeap++;
335 60060 : pRun++;
336 : }
337 156000 : while( *pRun && isSpace( *pRun ) )
338 0 : pRun++;
339 711828 : while( *pRun && ! isSpace( *pRun ) )
340 : {
341 555828 : if( *pRun == '\\' )
342 : {
343 : // escapement
344 0 : pRun++;
345 0 : *pLeap = *pRun;
346 0 : pLeap++;
347 0 : if( *pRun )
348 0 : pRun++;
349 : }
350 555828 : else if( bProtect && *pRun == '`' )
351 0 : CopyUntil( pLeap, pRun, '`', true );
352 555828 : else if( bProtect && *pRun == '\'' )
353 0 : CopyUntil( pLeap, pRun, '\'', true );
354 555828 : else if( bProtect && *pRun == '"' )
355 17212 : CopyUntil( pLeap, pRun, '"', true );
356 : else
357 : {
358 538616 : *pLeap = *pRun;
359 538616 : ++pLeap;
360 538616 : ++pRun;
361 : }
362 : }
363 : }
364 :
365 36556 : *pLeap = 0;
366 :
367 : // there might be a space at beginning or end
368 36556 : pLeap--;
369 36556 : if( *pLeap == ' ' )
370 0 : *pLeap = 0;
371 :
372 36556 : return OString(*pBuffer == ' ' ? pBuffer+1 : pBuffer);
373 : }
374 :
375 : } // namespace
376 :
377 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|