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