word.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2016 OpenFOAM Foundation
9  Copyright (C) 2017-2022 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 Class
28  Foam::word
29 
30 Description
31  A class for handling words, derived from Foam::string.
32 
33  A word is a string of characters without whitespace, quotes, slashes,
34  semicolons or brace brackets. Words are delimited by whitespace.
35 
36 SourceFiles
37  wordI.H
38  word.C
39  wordIO.C
40 
41 \*---------------------------------------------------------------------------*/
42 
43 #ifndef Foam_word_H
44 #define Foam_word_H
45 
46 #include "string.H"
47 
48 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
49 
50 namespace Foam
51 {
52 
53 // Forward Declarations
54 class word;
55 Istream& operator>>(Istream& is, word& val);
56 Ostream& operator<<(Ostream& os, const word& val);
57 
58 //- Hashing for word
59 template<> struct Hash<word> : string::hasher {};
60 
61 
62 /*---------------------------------------------------------------------------*\
63  Class word Declaration
64 \*---------------------------------------------------------------------------*/
65 
66 class word
67 :
68  public string
69 {
70 public:
71 
72  // Static Data Members
73 
74  //- The typeName
75  static const char* const typeName;
76 
77  //- Debugging
78  static int debug;
79 
80  //- An empty word
81  static const word null;
82 
83 
84  // Constructors
85 
86  //- Default construct
87  word() = default;
88 
89  //- Copy construct
90  word(const word&) = default;
91 
92  //- Move construct
93  word(word&& w) = default;
94 
95  //- Copy construct from Foam::string
96  inline word(const string& s, bool doStrip=true);
97 
98  //- Move construct from Foam::string
99  inline word(string&& s, bool doStrip=true);
100 
101  //- Copy construct from std::string
102  inline word(const std::string& s, bool doStrip=true);
103 
104  //- Move construct from std::string
105  inline word(std::string&& s, bool doStrip=true);
106 
107  //- Copy from character array
108  inline word(const char* s, bool doStrip=true);
109 
110  //- Copy from buffer for a maximum number of characters
111  inline word(const char* s, size_type len, bool doStrip);
112 
113  //- Construct from Istream
114  explicit word(Istream& is);
115 
116 
117  // Member Functions
118 
119  //- Use a printf-style formatter for a primitive.
120  // The representation is not checked for valid characters -
121  // it is assumed that the caller knows what they are doing
122  template<class PrimitiveType>
123  inline static word printf
124  (
125  const char* fmt,
126  const PrimitiveType& val
127  );
128 
129  //- Use a printf-style formatter for a primitive.
130  // The representation is not checked for valid characters -
131  // it is assumed that the caller knows what they are doing
132  template<class PrimitiveType>
133  inline static word printf
134  (
135  const std::string& fmt,
136  const PrimitiveType& val
137  );
138 
139  //- Is this character valid for a word?
140  inline static bool valid(char c);
141 
142  //- Construct validated word (no invalid characters).
143  // Optionally prefix any leading digit with '_' to have words
144  // that work nicely as dictionary keywords.
145  static word validate(const std::string& s, const bool prefix=false);
146 
147  //- Construct validated word (no invalid characters) from a sequence
148  //- of characters in the range [first,last),
149  // Optionally prefix any leading digit with '_'.
150  static word validate
151  (
152  const char* first,
153  const char* last,
154  const bool prefix=false
155  );
156 
157  //- Strip invalid characters from this word
158  // Trips an abort on invalid characters for debug 2 or greater
159  inline void stripInvalid();
160 
161 
162  // File-like Functions
163 
164  //- Return file name extension (part after last .)
165  inline word ext() const;
166 
167  //- Append a '.' and the ending, and return the object.
168  // The '.' and ending will not be added when the ending is empty,
169  // or when the file name is empty or ended with a '/'.
170  inline word& ext(const word& ending);
171 
172  //- Remove extension (if any) and append a new one
173  inline word& replace_ext(const word& ending);
174 
175  //- Return word without extension (part before last .)
176  inline word lessExt() const;
177 
178  //- Remove extension, return true if string changed.
179  using string::remove_ext;
180 
181  //- Various checks for extensions
182  using string::has_ext;
183 
184 
185  // Member Operators
186 
187  // Assignment
188 
189  //- Copy assignment, no character validation required.
190  // Self-assignment is a no-op
191  inline word& operator=(const word& s);
192 
193  //- Move assignment, no character validation required
194  // Self-assignment is a no-op
195  inline word& operator=(word&& s);
196 
197  //- Copy assignment from Foam::string, stripping invalid characters
198  inline word& operator=(const string& s);
199 
200  //- Move assignment from Foam::string, stripping invalid characters
201  inline word& operator=(string&& s);
202 
203  //- Copy assignment from std::string, stripping invalid characters
204  inline word& operator=(const std::string& s);
205 
206  //- Move assignment from std::string, stripping invalid characters
207  inline word& operator=(std::string&& s);
208 
209  //- Copy, stripping invalid characters
210  inline word& operator=(const char* s);
211 
212 
213  // Housekeeping
214 
215  //- Same as has_ext()
216  bool hasExt() const { return has_ext(); }
217 
218  //- Same as has_ext()
219  bool hasExt(const std::string& s) const { return has_ext(s); }
220 
221  //- Same as remove_ext()
222  bool removeExt() { return remove_ext(); }
223 };
224 
225 
226 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
227 
228 // IOstream Operators
229 
230 //- Read operator
231 Istream& operator>>(Istream& is, word& val);
232 
233 //- Write operator
234 Ostream& operator<<(Ostream& os, const word& val);
235 
236 
237 // * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * * //
238 
239 //- Join words as camelCase, capitalizing the first letter of b.
240 // No effect if either argument is empty.
241 word operator&(const word& a, const word& b);
242 
243 
244 // * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * * //
245 
246 //- A word representation of a memory address as hexadecimal.
247 // No special handling of nullptr (renders as 0x0)
248 word name(const void* ptr);
249 
250 
251 //- Extract name (as a word) from an object, typically using its name() method.
252 template<class T>
253 struct nameOp
254 {
255  word operator()(const T& obj) const
256  {
257  return obj.name();
258  }
259 
260  //- Less-compare two objects by their name() method - for sorting
261  bool operator()(const T& a, const T& b) const
262  {
263  return (a.name() < b.name());
264  }
265 };
266 
267 
268 //- Extract type (as a word) from an object, typically using its type() method.
269 template<class T>
270 struct typeOp
271 {
272  word operator()(const T& obj) const
273  {
274  return obj.type();
275  }
276 
277  //- Less-compare two objects by their type() method - for sorting
278  bool operator()(const T& a, const T& b) const
279  {
280  return (a.type() < b.type());
281  }
282 };
283 
284 
285 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
286 
287 } // End namespace Foam
288 
289 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
290 
291 #include "wordI.H"
293 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
294 
295 #endif
296 
297 // ************************************************************************* //
static word validate(const std::string &s, const bool prefix=false)
Construct validated word (no invalid characters).
Definition: word.C:39
word()=default
Default construct.
word operator()(const T &obj) const
Definition: word.H:363
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
word ext() const
Return file name extension (part after last .)
Definition: wordI.H:171
bool has_ext() const
Return true if it has an extension or simply ends with a &#39;.&#39;.
Definition: stringI.H:43
word & operator=(const word &s)
Copy assignment, no character validation required.
Definition: wordI.H:207
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
const dimensionedScalar b
Wien displacement law constant: default SI units: [m.K].
Definition: createFields.H:27
A class for handling words, derived from Foam::string.
Definition: word.H:63
Istream & operator>>(Istream &, directionInfo &)
static int debug
Debugging.
Definition: word.H:79
word lessExt() const
Return word without extension (part before last .)
Definition: wordI.H:192
static const word null
An empty word.
Definition: word.H:84
void stripInvalid()
Strip invalid characters from this word.
Definition: wordI.H:137
graph_traits< Graph >::vertices_size_type size_type
Definition: SloanRenumber.C:68
static bool valid(char c)
Is this character valid for a word?
Definition: wordI.H:52
static word printf(const char *fmt, const PrimitiveType &val)
Use a printf-style formatter for a primitive.
word operator()(const T &obj) const
Definition: word.H:342
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
OBJstream os(runTime.globalPath()/outputName)
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Hashing functor for string and derived string classes.
Definition: string.H:171
const word & name() const noexcept
Return const reference to name.
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:77
Hash function class. The default definition is for primitives. Non-primitives used to hash entries on...
Definition: Hash.H:47
tmp< GeometricField< Type, faPatchField, areaMesh > > operator &(const faMatrix< Type > &, const DimensionedField< Type, areaMesh > &)
const dimensionedScalar c
Speed of light in a vacuum.
word & replace_ext(const word &ending)
Remove extension (if any) and append a new one.
Definition: wordI.H:184
bool removeExt()
Same as remove_ext()
Definition: word.H:297
static const char *const typeName
The typeName.
Definition: word.H:74
gmvFile<< "tracers "<< particles.size()<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().x()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().y()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.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))
A class for handling character strings derived from std::string.
Definition: string.H:72
bool hasExt() const
Same as has_ext()
Definition: word.H:287
Namespace for OpenFOAM.
bool remove_ext()
Remove extension, return true if string changed.
Definition: stringI.H:93