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-2023 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 
36 #ifndef stringOps_H
37 #define stringOps_H
38 
39 #include "string.H"
40 #include "dictionary.H"
41 #include "HashTable.H"
42 
43 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
44 
45 namespace Foam
46 {
47 
48 /*---------------------------------------------------------------------------*\
49  Namespace stringOps Declaration
50 \*---------------------------------------------------------------------------*/
51 
52 namespace stringOps
53 {
54  //- Expand all occurrences of environment variables and paths
55  // Expansion includes:
56  // -# environment variables
57  // - "$VAR", "${VAR}"
58  // -# current directory
59  // - leading "./" : the current directory
60  // -# tilde expansion
61  // - leading "~/" : home directory
62  // - leading "~user" : home directory for specified user
63  // - leading "~OpenFOAM" : site/user OpenFOAM configuration directory
64  //
65  // Supports default values as per the Bourne/Korn shell.
66  // \code
67  // "${parameter:-defValue}"
68  // \endcode
69  // If parameter is unset or null, the \c defValue is substituted.
70  // Otherwise, the value of parameter is substituted.
71  //
72  // Supports alternative values as per the Bourne/Korn shell.
73  // \code
74  // "${parameter:+altValue}"
75  // \endcode
76  // If parameter is unset or null, nothing is substituted.
77  // Otherwise the \c altValue is substituted.
78  //
79  // Any unknown entries are removed silently, if allowEmpty is true.
80  //
81  // Malformed entries (eg, brace mismatch, sigil followed by bad character)
82  // are left as is.
83  //
84  // \sa
85  // Foam::findEtcFile
86  string expandEnvVar
87  (
88  const string&,
89  const bool allowEmpty = false
90  );
91 
92  //- Expand all occurrences of environment variables and paths
93  // See expandEnvVar
94  string& inplaceExpandEnvVar
95  (
96  string&,
97  const bool allowEmpty = false
98  );
99 
100  //- Inplace expand occurrences of variables according to the dictionary
101  // Expansion includes:
102  // -# variables
103  // - "$VAR", "${VAR}"
104  //
105  // Any unknown entries are left as-is
106  //
107  // \note the leading sigil can be changed to avoid conflicts with other
108  // string expansions
110  (
111  string&,
112  const dictionary& dict,
113  const word& dictVar = "dict",
114  const char sigil = '$'
115  );
116 
117  //- Inplace expand occurrences of variables according to the mapping
118  // Expansion includes:
119  // -# variables
120  // - "$VAR", "${VAR}"
121  //
122  // Any unknown entries are removed silently.
123  //
124  // \note the leading sigil can be changed to avoid conflicts with other
125  // string expansions
127  (
128  string&,
130  const char sigil = '$'
131  );
132 
133  //- Inplace expand occurrences of variables according to the dictionary
134  // and optionally environment variables
135  // Expansion includes:
136  // -# variables
137  // - "$VAR", "${VAR}"
138  // -# current directory
139  // - leading "./" : the current directory
140  // -# tilde expansion
141  // - leading "~/" : home directory
142  // - leading "~user" : home directory for specified user
143  // - leading "~OpenFOAM" : site/user OpenFOAM configuration directory
144  //
145  // with the "${}" syntax doing a recursive substitution.
146  //
147  // Any unknown entries are left as-is
148  //
149  // \note the leading sigil can be changed to avoid conflicts with other
150  // string expansions
151  string& inplaceExpandEntry
152  (
153  string& s,
154  const dictionary& dict,
155  const bool allowEnvVars,
156  const bool allowEmpty,
157  const char sigil = '$'
158  );
159 
160 
161  //- Return string trimmed of leading whitespace
162  string trimLeft(const string&);
163 
164  //- Trim leading whitespace inplace
165  string& inplaceTrimLeft(string&);
166 
167  //- Return string trimmed of trailing whitespace
168  string trimRight(const string&);
169 
170  //- Trim trailing whitespace inplace
171  string& inplaceTrimRight(string&);
172 
173  //- Return string trimmed of leading and trailing whitespace
174  string trim(const string&);
175 
176  //- Trim leading and trailing whitespace inplace
177  string& inplaceTrim(string&);
178 
179 
180  //- Break a string up into indented lines
182  (
183  const string& str,
184  const string::size_type nLength = 80,
185  const string::size_type nIndent = 0
186  );
187 
188 
189 } // End namespace stringOps
190 
191 
192 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
193 
194 } // End namespace Foam
195 
196 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
197 
198 #endif
199 
200 // ************************************************************************* //
graph_traits< Graph >::vertices_size_type size_type
Definition: SloanRenumber.C:73
An STL-conforming hash table.
Definition: HashTable.H:127
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:160
A class for handling words, derived from string.
Definition: word.H:62
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.name(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
string & inplaceExpandCodeString(string &, const dictionary &dict, const word &dictVar="dict", const char sigil='$')
Inplace expand occurrences of variables according to the dictionary.
Definition: stringOps.C:410
string & inplaceTrimRight(string &)
Trim trailing whitespace inplace.
Definition: stringOps.C:923
string trim(const string &)
Return string trimmed of leading and trailing whitespace.
Definition: stringOps.C:940
string & inplaceExpandCodeTemplate(string &, const HashTable< string, word, string::hash > &mapping, const char sigil='$')
Inplace expand occurrences of variables according to the mapping.
Definition: stringOps.C:647
string & inplaceExpandEnvVar(string &, const bool allowEmpty=false)
Expand all occurrences of environment variables and paths.
Definition: stringOps.C:252
string expandEnvVar(const string &, const bool allowEmpty=false)
Expand all occurrences of environment variables and paths.
Definition: stringOps.C:241
string & inplaceExpandEntry(string &s, const dictionary &dict, const bool allowEnvVars, const bool allowEmpty, const char sigil='$')
Inplace expand occurrences of variables according to the dictionary.
Definition: stringOps.C:753
string trimRight(const string &)
Return string trimmed of trailing whitespace.
Definition: stringOps.C:903
string breakIntoIndentedLines(const string &str, const string::size_type nLength=80, const string::size_type nIndent=0)
Break a string up into indented lines.
Definition: stringOps.C:956
string trimLeft(const string &)
Return string trimmed of leading whitespace.
Definition: stringOps.C:863
string & inplaceTrim(string &)
Trim leading and trailing whitespace inplace.
Definition: stringOps.C:946
string & inplaceTrimLeft(string &)
Trim leading whitespace inplace.
Definition: stringOps.C:883
Namespace for OpenFOAM.
dictionary dict