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 :
21 : #include "vcl/strhelper.hxx"
22 : #include "sal/alloca.h"
23 :
24 : namespace psp {
25 :
26 627905 : inline int isSpace( char cChar )
27 : {
28 : return
29 556915 : cChar == ' ' || cChar == '\t' ||
30 556915 : cChar == '\r' || cChar == '\n' ||
31 1184820 : cChar == 0x0c || cChar == 0x0b;
32 : }
33 :
34 394983 : inline int isSpace( sal_Unicode cChar )
35 : {
36 : return
37 356039 : cChar == ' ' || cChar == '\t' ||
38 356039 : cChar == '\r' || cChar == '\n' ||
39 751022 : cChar == 0x0c || cChar == 0x0b;
40 : }
41 :
42 10261 : inline int isProtect( char cChar )
43 : {
44 10261 : return cChar == '`' || cChar == '\'' || cChar == '"';
45 : }
46 :
47 0 : inline int isProtect( sal_Unicode cChar )
48 : {
49 0 : return cChar == '`' || cChar == '\'' || cChar == '"';
50 : }
51 :
52 280922 : inline void CopyUntil( char*& pTo, const char*& pFrom, char cUntil, int bIncludeUntil = 0 )
53 : {
54 280922 : do
55 : {
56 280922 : if( *pFrom == '\\' )
57 : {
58 62 : pFrom++;
59 62 : if( *pFrom )
60 : {
61 62 : *pTo = *pFrom;
62 62 : pTo++;
63 : }
64 : }
65 280860 : else if( bIncludeUntil || ! isProtect( *pFrom ) )
66 : {
67 280860 : *pTo = *pFrom;
68 280860 : pTo++;
69 : }
70 280922 : pFrom++;
71 280922 : } while( *pFrom && *pFrom != cUntil );
72 : // copy the terminating character unless zero or protector
73 10261 : if( ! isProtect( *pFrom ) || bIncludeUntil )
74 : {
75 10261 : *pTo = *pFrom;
76 10261 : if( *pTo )
77 10261 : pTo++;
78 : }
79 10261 : if( *pFrom )
80 10261 : pFrom++;
81 10261 : }
82 :
83 0 : inline void CopyUntil( sal_Unicode*& pTo, const sal_Unicode*& pFrom, sal_Unicode cUntil, int bIncludeUntil = 0 )
84 : {
85 0 : do
86 : {
87 0 : if( *pFrom == '\\' )
88 : {
89 0 : pFrom++;
90 0 : if( *pFrom )
91 : {
92 0 : *pTo = *pFrom;
93 0 : pTo++;
94 : }
95 : }
96 0 : else if( bIncludeUntil || ! isProtect( *pFrom ) )
97 : {
98 0 : *pTo = *pFrom;
99 0 : pTo++;
100 : }
101 0 : pFrom++;
102 0 : } while( *pFrom && *pFrom != cUntil );
103 : // copy the terminating character unless zero or protector
104 0 : if( ! isProtect( *pFrom ) || bIncludeUntil )
105 : {
106 0 : *pTo = *pFrom;
107 0 : if( *pTo )
108 0 : pTo++;
109 : }
110 0 : if( *pFrom )
111 0 : pFrom++;
112 0 : }
113 :
114 11402 : String GetCommandLineToken( int nToken, const String& rLine )
115 : {
116 11402 : int nLen = rLine.Len();
117 11402 : if( ! nLen )
118 0 : return String();
119 :
120 11402 : int nActualToken = 0;
121 11402 : sal_Unicode* pBuffer = (sal_Unicode*)alloca( sizeof(sal_Unicode)*( nLen + 1 ) );
122 11402 : const sal_Unicode* pRun = rLine.GetBuffer();
123 11402 : sal_Unicode* pLeap = NULL;
124 :
125 43699 : while( *pRun && nActualToken <= nToken )
126 : {
127 51283 : while( *pRun && isSpace( *pRun ) )
128 9493 : pRun++;
129 20895 : pLeap = pBuffer;
130 302532 : while( *pRun && ! isSpace( *pRun ) )
131 : {
132 260742 : if( *pRun == '\\' )
133 : {
134 : // escapement
135 0 : pRun++;
136 0 : *pLeap = *pRun;
137 0 : pLeap++;
138 0 : if( *pRun )
139 0 : pRun++;
140 : }
141 260742 : else if( *pRun == '`' )
142 0 : CopyUntil( pLeap, pRun, '`' );
143 260742 : else if( *pRun == '\'' )
144 0 : CopyUntil( pLeap, pRun, '\'' );
145 260742 : else if( *pRun == '"' )
146 0 : CopyUntil( pLeap, pRun, '"' );
147 : else
148 : {
149 260742 : *pLeap = *pRun;
150 260742 : pLeap++;
151 260742 : pRun++;
152 : }
153 : }
154 20895 : if( nActualToken != nToken )
155 11322 : pBuffer[0] = 0;
156 20895 : nActualToken++;
157 : }
158 :
159 11402 : *pLeap = 0;
160 :
161 11402 : return OUString(pBuffer);
162 : }
163 :
164 12152 : OString GetCommandLineToken(int nToken, const OString& rLine)
165 : {
166 12152 : sal_Int32 nLen = rLine.getLength();
167 12152 : if (!nLen)
168 0 : return rLine;
169 :
170 12152 : int nActualToken = 0;
171 12152 : char* pBuffer = (char*)alloca( nLen + 1 );
172 12152 : const char* pRun = rLine.getStr();
173 12152 : char* pLeap = NULL;
174 :
175 36952 : while( *pRun && nActualToken <= nToken )
176 : {
177 26164 : while( *pRun && isSpace( *pRun ) )
178 868 : pRun++;
179 12648 : pLeap = pBuffer;
180 181660 : while( *pRun && ! isSpace( *pRun ) )
181 : {
182 156364 : if( *pRun == '\\' )
183 : {
184 : // escapement
185 0 : pRun++;
186 0 : *pLeap = *pRun;
187 0 : pLeap++;
188 0 : if( *pRun )
189 0 : pRun++;
190 : }
191 156364 : else if( *pRun == '`' )
192 0 : CopyUntil( pLeap, pRun, '`' );
193 156364 : else if( *pRun == '\'' )
194 0 : CopyUntil( pLeap, pRun, '\'' );
195 156364 : else if( *pRun == '"' )
196 0 : CopyUntil( pLeap, pRun, '"' );
197 : else
198 : {
199 156364 : *pLeap = *pRun;
200 156364 : pLeap++;
201 156364 : pRun++;
202 : }
203 : }
204 12648 : if( nActualToken != nToken )
205 496 : pBuffer[0] = 0;
206 12648 : nActualToken++;
207 : }
208 :
209 12152 : *pLeap = 0;
210 :
211 12152 : return OString(pBuffer);
212 : }
213 :
214 0 : int GetCommandLineTokenCount(const OUString& rLine)
215 : {
216 0 : if (rLine.isEmpty())
217 0 : return 0;
218 :
219 0 : int nTokenCount = 0;
220 0 : const sal_Unicode *pRun = rLine.getStr();
221 :
222 0 : while( *pRun )
223 : {
224 0 : while( *pRun && isSpace( *pRun ) )
225 0 : pRun++;
226 0 : if( ! *pRun )
227 0 : break;
228 0 : while( *pRun && ! isSpace( *pRun ) )
229 : {
230 0 : if( *pRun == '\\' )
231 : {
232 : // escapement
233 0 : pRun++;
234 0 : if( *pRun )
235 0 : pRun++;
236 : }
237 0 : else if( *pRun == '`' )
238 : {
239 0 : do pRun++; while( *pRun && *pRun != '`' );
240 0 : if( *pRun )
241 0 : pRun++;
242 : }
243 0 : else if( *pRun == '\'' )
244 : {
245 0 : do pRun++; while( *pRun && *pRun != '\'' );
246 0 : if( *pRun )
247 0 : pRun++;
248 : }
249 0 : else if( *pRun == '"' )
250 : {
251 0 : do pRun++; while( *pRun && *pRun != '"' );
252 0 : if( *pRun )
253 0 : pRun++;
254 : }
255 : else
256 0 : pRun++;
257 : }
258 0 : nTokenCount++;
259 : }
260 :
261 0 : return nTokenCount;
262 : }
263 :
264 8479 : String WhitespaceToSpace( const String& rLine, bool bProtect )
265 : {
266 8479 : int nLen = rLine.Len();
267 8479 : if( ! nLen )
268 711 : return String();
269 :
270 7768 : sal_Unicode *pBuffer = (sal_Unicode*)alloca( sizeof(sal_Unicode)*(nLen + 1) );
271 7768 : const sal_Unicode *pRun = rLine.GetBuffer();
272 7768 : sal_Unicode *pLeap = pBuffer;
273 :
274 27144 : while( *pRun )
275 : {
276 11608 : if( *pRun && isSpace( *pRun ) )
277 : {
278 11608 : *pLeap = ' ';
279 11608 : pLeap++;
280 11608 : pRun++;
281 : }
282 23216 : while( *pRun && isSpace( *pRun ) )
283 0 : pRun++;
284 86135 : while( *pRun && ! isSpace( *pRun ) )
285 : {
286 62919 : if( *pRun == '\\' )
287 : {
288 : // escapement
289 0 : pRun++;
290 0 : *pLeap = *pRun;
291 0 : pLeap++;
292 0 : if( *pRun )
293 0 : pRun++;
294 : }
295 62919 : else if( bProtect && *pRun == '`' )
296 0 : CopyUntil( pLeap, pRun, '`', sal_True );
297 62919 : else if( bProtect && *pRun == '\'' )
298 0 : CopyUntil( pLeap, pRun, '\'', sal_True );
299 62919 : else if( bProtect && *pRun == '"' )
300 0 : CopyUntil( pLeap, pRun, '"', sal_True );
301 : else
302 : {
303 62919 : *pLeap = *pRun;
304 62919 : ++pLeap;
305 62919 : ++pRun;
306 : }
307 : }
308 : }
309 :
310 7768 : *pLeap = 0;
311 :
312 : // there might be a space at beginning or end
313 7768 : pLeap--;
314 7768 : if( *pLeap == ' ' )
315 125 : *pLeap = 0;
316 :
317 7768 : return OUString(*pBuffer == ' ' ? pBuffer+1 : pBuffer);
318 : }
319 :
320 21731 : OString WhitespaceToSpace(const OString& rLine, bool bProtect)
321 : {
322 21731 : sal_Int32 nLen = rLine.getLength();
323 21731 : if (!nLen)
324 0 : return rLine;
325 :
326 21731 : char *pBuffer = (char*)alloca( nLen + 1 );
327 21731 : const char *pRun = rLine.getStr();
328 21731 : char *pLeap = pBuffer;
329 :
330 89900 : while( *pRun )
331 : {
332 46438 : if( *pRun && isSpace( *pRun ) )
333 : {
334 35774 : *pLeap = ' ';
335 35774 : pLeap++;
336 35774 : pRun++;
337 : }
338 92876 : while( *pRun && isSpace( *pRun ) )
339 0 : pRun++;
340 423677 : while( *pRun && ! isSpace( *pRun ) )
341 : {
342 330801 : if( *pRun == '\\' )
343 : {
344 : // escapement
345 0 : pRun++;
346 0 : *pLeap = *pRun;
347 0 : pLeap++;
348 0 : if( *pRun )
349 0 : pRun++;
350 : }
351 330801 : else if( bProtect && *pRun == '`' )
352 0 : CopyUntil( pLeap, pRun, '`', sal_True );
353 330801 : else if( bProtect && *pRun == '\'' )
354 0 : CopyUntil( pLeap, pRun, '\'', sal_True );
355 330801 : else if( bProtect && *pRun == '"' )
356 10261 : CopyUntil( pLeap, pRun, '"', sal_True );
357 : else
358 : {
359 320540 : *pLeap = *pRun;
360 320540 : ++pLeap;
361 320540 : ++pRun;
362 : }
363 : }
364 : }
365 :
366 21731 : *pLeap = 0;
367 :
368 : // there might be a space at beginning or end
369 21731 : pLeap--;
370 21731 : if( *pLeap == ' ' )
371 0 : *pLeap = 0;
372 :
373 21731 : return OString(*pBuffer == ' ' ? pBuffer+1 : pBuffer);
374 : }
375 :
376 : } // namespace
377 :
378 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|