Newer
Older
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2011 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
\*---------------------------------------------------------------------------*/
#include "stringOps.H"
#include "OSspecific.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
Foam::string Foam::stringOps::expand
(
const string& original,
const HashTable<string, word, string::hash>& mapping,
const char sigil
)
{
string s(original);
return inplaceExpand(s, mapping);
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
}
Foam::string& Foam::stringOps::inplaceExpand
(
string& s,
const HashTable<string, word, string::hash>& mapping,
const char sigil
)
{
string::size_type begVar = 0;
// Expand $VAR or ${VAR}
// Repeat until nothing more is found
while
(
(begVar = s.find(sigil, begVar)) != string::npos
&& begVar < s.size()-1
)
{
if (begVar == 0 || s[begVar-1] != '\\')
{
// Find end of first occurrence
string::size_type endVar = begVar;
string::size_type delim = 0;
if (s[begVar+1] == '{')
{
endVar = s.find('}', begVar);
delim = 1;
}
else
{
string::iterator iter = s.begin() + begVar + 1;
// more generous in accepting keywords than for env variables
while
(
iter != s.end()
&&
(
isalnum(*iter)
|| *iter == '.'
|| *iter == ':'
|| *iter == '_'
)
)
{
++iter;
++endVar;
}
}
if (endVar != string::npos && endVar != begVar)
{
s.substr
(
begVar + 1 + delim,
endVar - begVar - 2*delim
),
false
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
);
HashTable<string, word, string::hash>::const_iterator fnd =
mapping.find(varName);
if (fnd != HashTable<string, word, string::hash>::end())
{
s.std::string::replace
(
begVar,
endVar - begVar + 1,
*fnd
);
begVar += (*fnd).size();
}
else
{
s.std::string::replace
(
begVar,
endVar - begVar + 1,
""
);
}
}
else
{
break;
}
}
else
{
++begVar;
}
}
return s;
}
Foam::string Foam::stringOps::expand
(
const string& original,
const dictionary& dict,
const char sigil
)
{
string s(original);
return inplaceExpand(s, dict, sigil);
Foam::string& Foam::stringOps::inplaceExpand
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
(
string& s,
const dictionary& dict,
const char sigil
)
{
string::size_type begVar = 0;
// Expand $VAR or ${VAR}
// Repeat until nothing more is found
while
(
(begVar = s.find(sigil, begVar)) != string::npos
&& begVar < s.size()-1
)
{
if (begVar == 0 || s[begVar-1] != '\\')
{
// Find end of first occurrence
string::size_type endVar = begVar;
string::size_type delim = 0;
if (s[begVar+1] == '{')
{
endVar = s.find('}', begVar);
delim = 1;
}
else
{
string::iterator iter = s.begin() + begVar + 1;
// more generous in accepting keywords than for env variables
while
(
iter != s.end()
&&
(
isalnum(*iter)
|| *iter == '.'
|| *iter == ':'
|| *iter == '_'
)
)
{
++iter;
++endVar;
}
}
if (endVar != string::npos && endVar != begVar)
{
const word varName
(
s.substr
(
begVar + 1 + delim,
endVar - begVar - 2*delim
),
false
);
const entry* ePtr = dict.lookupEntryPtr(varName, true, true);
// if defined - copy its entries
{
OStringStream buf;
if (ePtr->isDict())
{
ePtr->dict().write(buf, false);
}
else
{
// fail for other types
dynamicCast<const primitiveEntry>
(
*ePtr
).write(buf, true);
}
s.std::string::replace
(
begVar,
endVar - begVar + 1,
buf.str()
);
begVar += buf.str().size();
}
else
{
// not defined - leave original string untouched
begVar = endVar;
}
}
else
{
break;
}
}
else
{
++begVar;
}
}
return s;
}
Foam::string Foam::stringOps::expand
(
const string& original,
)
{
string s(original);
return inplaceExpand(s, allowEmpty);
}
Foam::string& Foam::stringOps::inplaceExpand
(
string& s,
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
)
{
string::size_type begVar = 0;
// Expand $VARS
// Repeat until nothing more is found
while
(
(begVar = s.find('$', begVar)) != string::npos
&& begVar < s.size()-1
)
{
if (begVar == 0 || s[begVar-1] != '\\')
{
// Find end of first occurrence
string::size_type endVar = begVar;
string::size_type delim = 0;
if (s[begVar+1] == '{')
{
endVar = s.find('}', begVar);
delim = 1;
}
else
{
string::iterator iter = s.begin() + begVar + 1;
while
(
iter != s.end()
&& (isalnum(*iter) || *iter == '_')
)
{
++iter;
++endVar;
}
}
if (endVar != string::npos && endVar != begVar)
{
s.substr
(
begVar + 1 + delim,
endVar - begVar - 2*delim
),
false
);
const string varValue = getEnv(varName);
if (varValue.size())
{
s.std::string::replace
(
begVar,
endVar - begVar + 1,
varValue
);
begVar += varValue.size();
}
{
s.std::string::replace
(
begVar,
endVar - begVar + 1,
""
);
}
else
{
FatalErrorIn
(
"stringOps::inplaceExpand(string&, const bool)"
)
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
<< "Unknown variable name " << varName << '.'
<< exit(FatalError);
}
}
else
{
break;
}
}
else
{
++begVar;
}
}
if (!s.empty())
{
if (s[0] == '~')
{
// Expand initial ~
// ~/ => home directory
// ~OpenFOAM => site/user OpenFOAM configuration directory
// ~user => home directory for specified user
word user;
fileName file;
if ((begVar = s.find('/')) != string::npos)
{
user = s.substr(1, begVar - 1);
file = s.substr(begVar + 1);
}
else
{
user = s.substr(1);
}
// NB: be a bit lazy and expand ~unknownUser as an
// empty string rather than leaving it untouched.
// otherwise add extra test
if (user == "OpenFOAM")
{
s = findEtcFile(file);
}
else
{
s = home(user)/file;
}
}
else if (s[0] == '.')
{
// Expand a lone '.' and an initial './' into cwd
if (s.size() == 1)
{
s = cwd();
}
else if (s[1] == '/')
{
s.std::string::replace(0, 1, cwd());
}
}
}
return s;
}
Foam::string Foam::stringOps::trimLeft(const string& s)
{
if (!s.empty())
{
string::size_type beg = 0;
while (beg < s.size() && isspace(s[beg]))
{
++beg;
}
if (beg)
{
return s.substr(beg);
}
}
return s;
}
Foam::string& Foam::stringOps::inplaceTrimLeft(string& s)
{
if (!s.empty())
{
string::size_type beg = 0;
while (beg < s.size() && isspace(s[beg]))
{
++beg;
}
if (beg)
{
s.erase(0, beg);
}
}
return s;
}
Foam::string Foam::stringOps::trimRight(const string& s)
{
if (!s.empty())
{
string::size_type sz = s.size();
while (sz && isspace(s[sz-1]))
{
--sz;
}
if (sz < s.size())
{
return s.substr(0, sz);
}
}
return s;
}
Foam::string& Foam::stringOps::inplaceTrimRight(string& s)
{
if (!s.empty())
{
string::size_type sz = s.size();
while (sz && isspace(s[sz-1]))
{
--sz;
}
s.resize(sz);
}
return s;
}
Foam::string Foam::stringOps::trim(const string& original)
{
return trimLeft(trimRight(original));
}
Foam::string& Foam::stringOps::inplaceTrim(string& s)
{
inplaceTrimRight(s);
inplaceTrimLeft(s);
return s;
}
// ************************************************************************* //