stringOps.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2018 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Namespace
25  Foam::stringOps
26 
27 Description
28  Collection of static functions to do various simple string-related
29  operations
30 
31 SourceFiles
32  stringOps.C
33 
34 \*---------------------------------------------------------------------------*/
35 #ifndef stringOps_H
36 #define stringOps_H
37 
38 #include "string.H"
39 #include "dictionary.H"
40 #include "HashTable.H"
41 
42 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
43 
44 namespace Foam
45 {
46 
47 /*---------------------------------------------------------------------------*\
48  Namespace stringOps Declaration
49 \*---------------------------------------------------------------------------*/
50 
51 namespace stringOps
52 {
53  //- Expand occurrences of variables according to the mapping
54  // Expansion includes:
55  // -# variables
56  // - "$VAR", "${VAR}"
57  //
58  // Supports default values as per the Bourne/Korn shell.
59  // \code
60  // "${parameter:-defValue}"
61  // \endcode
62  // If parameter is unset or null, the \c defValue is substituted.
63  // Otherwise, the value of parameter is substituted.
64  //
65  // Supports alternative values as per the Bourne/Korn shell.
66  // \code
67  // "${parameter:+altValue}"
68  // \endcode
69  // If parameter is unset or null, nothing is substituted.
70  // Otherwise the \c altValue is substituted.
71  //
72  // Any unknown entries are removed silently.
73  //
74  // Malformed entries (eg, brace mismatch, sigil followed by bad character)
75  // are left as is.
76  //
77  // \note the leading sigil can be changed to avoid conflicts with other
78  // string expansions
79  string expand
80  (
81  const string&,
82  const HashTable<string, word, string::hash>& mapping,
83  const char sigil = '$'
84  );
85 
86 
87  //- Inplace expand occurrences of variables according to the mapping
88  // Expansion includes:
89  // -# variables
90  // - "$VAR", "${VAR}"
91  //
92  // Supports default values as per the Bourne/Korn shell.
93  // \code
94  // "${parameter:-defValue}"
95  // \endcode
96  // If parameter is unset or null, the \c defValue is substituted.
97  // Otherwise, the value of parameter is substituted.
98  //
99  // Supports alternative values as per the Bourne/Korn shell.
100  // \code
101  // "${parameter:+altValue}"
102  // \endcode
103  // If parameter is unset or null, nothing is substituted.
104  // Otherwise the \c altValue is substituted.
105  //
106  // Any unknown entries are removed silently.
107  //
108  // Malformed entries (eg, brace mismatch, sigil followed by bad character)
109  // are left as is.
110  //
111  // \note the leading sigil can be changed to avoid conflicts with other
112  // string expansions
113  string& inplaceExpand
114  (
115  string&,
116  const HashTable<string, word, string::hash>& mapping,
117  const char sigil = '$'
118  );
119 
120  //- Expand occurrences of variables according to the dictionary
121  // Expansion includes:
122  // -# variables
123  // - "$VAR", "${VAR}"
124  //
125  // Any unknown entries are left as-is
126  //
127  // \note the leading sigil can be changed to avoid conflicts with other
128  // string expansions
129  string expand
130  (
131  const string&,
132  const dictionary& dict,
133  const char sigil = '$'
134  );
135 
136 
137  //- Get dictionary or (optionally) environment variable
138  string getVariable
139  (
140  const word& name,
141  const dictionary& dict,
142  const bool allowEnvVars,
143  const bool allowEmpty
144  );
145 
146 
147  //- Recursively expands (dictionary or environment) variable
148  // starting at index in string. Updates index.
149  string expand
150  (
151  const string& s,
152  string::size_type& index,
153  const dictionary& dict,
154  const bool allowEnvVars,
155  const bool allowEmpty
156  );
157 
158 
159  //- Inplace expand occurrences of variables according to the dictionary
160  // and optionally environment variables
161  // Expansion includes:
162  // -# variables
163  // - "$VAR", "${VAR}"
164  //
165  // with the "${}" syntax doing a recursive substitution.
166  // Any unknown entries are left as-is
167  //
168  // \note the leading sigil can be changed to avoid conflicts with other
169  // string expansions
170  string& inplaceExpand
171  (
172  string& s,
173  const dictionary& dict,
174  const bool allowEnvVars,
175  const bool allowEmpty,
176  const char sigil = '$'
177  );
178 
179 
180  //- Inplace expand occurrences of variables according to the dictionary
181  // Expansion includes:
182  // -# variables
183  // - "$VAR", "${VAR}"
184  //
185  // Any unknown entries are left as-is
186  //
187  // \note the leading sigil can be changed to avoid conflicts with other
188  // string expansions
189  string& inplaceExpand
190  (
191  string&,
192  const dictionary& dict,
193  const char sigil = '$'
194  );
195 
196 
197  //- Expand initial tildes and all occurrences of environment variables
198  // Expansion includes:
199  // -# environment variables
200  // - "$VAR", "${VAR}"
201  // -# current directory
202  // - leading "./" : the current directory
203  // -# tilde expansion
204  // - leading "~/" : home directory
205  // - leading "~user" : home directory for specified user
206  // - leading "~OpenFOAM" : site/user OpenFOAM configuration directory
207  //
208  // Supports default values as per the Bourne/Korn shell.
209  // \code
210  // "${parameter:-defValue}"
211  // \endcode
212  // If parameter is unset or null, the \c defValue is substituted.
213  // Otherwise, the value of parameter is substituted.
214  //
215  // Supports alternative values as per the Bourne/Korn shell.
216  // \code
217  // "${parameter:+altValue}"
218  // \endcode
219  // If parameter is unset or null, nothing is substituted.
220  // Otherwise the \c altValue is substituted.
221  //
222  // Any unknown entries are removed silently, if allowEmpty is true.
223  //
224  // Malformed entries (eg, brace mismatch, sigil followed by bad character)
225  // are left as is.
226  //
227  // \sa
228  // Foam::findEtcFile
229  string expand
230  (
231  const string&,
232  const bool allowEmpty = false
233  );
234 
235 
236  //- Expand initial tildes and all occurrences of environment variables
237  // Expansion includes:
238  // -# environment variables
239  // - "$VAR", "${VAR}"
240  // -# current directory
241  // - leading "./" : the current directory
242  // -# tilde expansion
243  // - leading "~/" : home directory
244  // - leading "~user" : home directory for specified user
245  // - leading "~OpenFOAM" : site/user OpenFOAM configuration directory
246  //
247  // Supports default values as per the Bourne/Korn shell.
248  // \code
249  // "${parameter:-defValue}"
250  // \endcode
251  // If parameter is unset or null, the \c defValue is substituted.
252  // Otherwise, the value of parameter is substituted.
253  //
254  // Supports alternative values as per the Bourne/Korn shell.
255  // \code
256  // "${parameter:+altValue}"
257  // \endcode
258  // If parameter is unset or null, nothing is substituted.
259  // Otherwise the \c altValue is substituted.
260  //
261  // Any unknown entries are removed silently, if allowEmpty is true.
262  //
263  // Malformed entries (eg, brace mismatch, sigil followed by bad character)
264  // are left as is.
265  //
266  // Any unknown entries are removed silently if allowEmpty is true.
267  // \sa
268  // Foam::findEtcFile
269  string& inplaceExpand
270  (
271  string&,
272  const bool allowEmpty = false
273  );
274 
275 
276  //- Return string trimmed of leading whitespace
277  string trimLeft(const string&);
278 
279  //- Trim leading whitespace inplace
280  string& inplaceTrimLeft(string&);
281 
282  //- Return string trimmed of trailing whitespace
283  string trimRight(const string&);
284 
285  //- Trim trailing whitespace inplace
286  string& inplaceTrimRight(string&);
287 
288  //- Return string trimmed of leading and trailing whitespace
289  string trim(const string&);
290 
291  //- Trim leading and trailing whitespace inplace
292  string& inplaceTrim(string&);
293 
294 
295 } // End namespace stringOps
296 
297 
298 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
299 
300 } // End namespace Foam
301 
302 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
303 
304 #endif
305 
306 // ************************************************************************* //
string & inplaceTrim(string &)
Trim leading and trailing whitespace inplace.
Definition: stringOps.C:940
dictionary dict
string expand(const string &, const HashTable< string, word, string::hash > &mapping, const char sigil='$')
Expand occurrences of variables according to the mapping.
Definition: stringOps.C:69
string getVariable(const word &name, const dictionary &dict, const bool allowEnvVars, const bool allowEmpty)
Get dictionary or (optionally) environment variable.
Definition: stringOps.C:249
string trim(const string &)
Return string trimmed of leading and trailing whitespace.
Definition: stringOps.C:934
gmvFile<< "tracers "<< particles.size()<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().x()<< " ";}gmvFile<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().y()<< " ";}gmvFile<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().z()<< " ";}gmvFile<< nl;forAll(lagrangianScalarNames, i){ word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject(name, runTime.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
graph_traits< Graph >::vertices_size_type size_type
Definition: SloanRenumber.C:73
string & inplaceTrimRight(string &)
Trim trailing whitespace inplace.
Definition: stringOps.C:917
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
string trimRight(const string &)
Return string trimmed of trailing whitespace.
Definition: stringOps.C:897
string & inplaceExpand(string &, const HashTable< string, word, string::hash > &mapping, const char sigil='$')
Inplace expand occurrences of variables according to the mapping.
Definition: stringOps.C:81
string & inplaceTrimLeft(string &)
Trim leading whitespace inplace.
Definition: stringOps.C:877
string trimLeft(const string &)
Return string trimmed of leading whitespace.
Definition: stringOps.C:857
Namespace for OpenFOAM.