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-2019 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 word_H
44 #define 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& w);
56 Ostream& operator<<(Ostream& os, const word& w);
57 
58 
59 /*---------------------------------------------------------------------------*\
60  Class word Declaration
61 \*---------------------------------------------------------------------------*/
62 
63 class word
64 :
65  public string
66 {
67 public:
68 
69  // Static Data Members
70 
71  //- The typeName
72  static const char* const typeName;
73 
74  //- Debugging
75  static int debug;
76 
77  //- An empty word
78  static const word null;
79 
80 
81  // Constructors
82 
83  //- Construct null
84  word() = default;
85 
86  //- Copy construct
87  word(const word&) = default;
88 
89  //- Move construct
90  word(word&& w) = default;
91 
92  //- Copy construct from Foam::string
93  inline word(const string& s, bool doStrip=true);
94 
95  //- Move construct from Foam::string
96  inline word(string&& s, bool doStrip=true);
97 
98  //- Copy construct from std::string
99  inline word(const std::string& s, bool doStrip=true);
100 
101  //- Move construct from std::string
102  inline word(std::string&& s, bool doStrip=true);
103 
104  //- Copy from character array
105  inline word(const char* s, bool doStrip=true);
106 
107  //- Copy from buffer for a maximum number of characters
108  inline word(const char* s, size_type len, bool doStrip);
109 
110  //- Construct from Istream
111  explicit word(Istream& is);
112 
113 
114  // Member Functions
115 
116  //- Use a printf-style formatter for a primitive.
117  // The representation is not checked for valid characters -
118  // it is assumed that the caller knows what they are doing
119  template<class PrimitiveType>
120  inline static word printf
121  (
122  const char* fmt,
123  const PrimitiveType& val
124  );
125 
126  //- Use a printf-style formatter for a primitive.
127  // The representation is not checked for valid characters -
128  // it is assumed that the caller knows what they are doing
129  template<class PrimitiveType>
130  inline static word printf
131  (
132  const std::string& fmt,
133  const PrimitiveType& val
134  );
135 
136  //- Is this character valid for a word?
137  inline static bool valid(char c);
138 
139  //- Construct validated word (no invalid characters).
140  // Optionally prefix any leading digit with '_' to have words
141  // that work nicely as dictionary keywords.
142  static word validate(const std::string& s, const bool prefix=false);
143 
144  //- Construct validated word (no invalid characters) from a sequence
145  //- of characters in the range [first,last),
146  // Optionally prefix any leading digit with '_'.
147  static word validate
148  (
149  const char* first,
150  const char* last,
151  const bool prefix=false
152  );
153 
154  //- Strip invalid characters from this word
155  // Trips an abort on invalid characters for debug 2 or greater
156  inline void stripInvalid();
157 
158 
159  // File-like Functions
160 
161  //- Return word without extension (part before last .)
162  word lessExt() const;
163 
164  //- Return file name extension (part after last .)
165  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  word& ext(const word& ending);
171 
172  //- Return true if it has an extension or simply ends with a '.'
173  inline bool hasExt() const;
174 
175  //- Return true if the extension is the same as the given ending.
176  bool hasExt(const word& ending) const;
177 
178  //- Return true if the extension matches the given ending.
179  bool hasExt(const wordRe& ending) const;
180 
181  //- Remove extension, returning true if string changed.
182  inline bool removeExt();
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 
214 // IOstream Operators
215 
216 //- Read operator
217 Istream& operator>>(Istream& is, word& val);
218 
219 //- Write operator
220 Ostream& operator<<(Ostream& os, const word& val);
221 
222 
223 // * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * * //
224 
225 //- Join words as camelCase, capitalizing the first letter of b.
226 // No effect if either argument is empty.
227 word operator&(const word& a, const word& b);
228 
229 
230 // * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * * //
231 
232 //- A word representation of a memory address as hexadecimal.
233 // No special handling of nullptr (renders as 0x0)
234 word name(const void* ptr);
235 
236 
237 //- Extract name (as a word) from an object, typically using its name() method.
238 template<class T>
239 struct nameOp
240 {
241  inline word operator()(const T& obj) const
242  {
243  return obj.name();
244  }
245 };
246 
247 
248 //- Extract type (as a word) from an object, typically using its type() method.
249 template<class T>
250 struct typeOp
251 {
252  inline word operator()(const T& obj) const
253  {
254  return obj.type();
255  }
256 };
257 
258 
259 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
260 
261 } // End namespace Foam
262 
263 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
264 
265 #include "wordI.H"
266 
267 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
268 
269 #endif
270 
271 // ************************************************************************* //
Foam::word::lessExt
word lessExt() const
Return word without extension (part before last .)
Definition: word.C:113
Foam::word::operator=
word & operator=(const word &s)
Copy assignment, no character validation required.
Definition: wordI.H:179
Foam::typeOp::operator()
word operator()(const T &obj) const
Definition: word.H:251
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
s
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))
Definition: gmvOutputSpray.H:25
Foam::operator&
tmp< GeometricField< Type, fvPatchField, volMesh > > operator&(const fvMatrix< Type > &, const DimensionedField< Type, volMesh > &)
Foam::word::validate
static word validate(const std::string &s, const bool prefix=false)
Construct validated word (no invalid characters).
Definition: word.C:45
string.H
Foam::operator>>
Istream & operator>>(Istream &, directionInfo &)
Definition: directionInfo.C:230
Foam::string
A class for handling character strings derived from std::string.
Definition: string.H:73
Foam::word::ext
word ext() const
Return file name extension (part after last .)
Definition: word.C:126
Foam::wordRe
A wordRe is a Foam::word, but can contain a regular expression for matching words or strings.
Definition: wordRe.H:72
Foam::operator<<
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
Foam::word::hasExt
bool hasExt() const
Return true if it has an extension or simply ends with a '.'.
Definition: wordI.H:165
Foam::constant::physicoChemical::b
const dimensionedScalar b
Wien displacement law constant: default SI units: [m.K].
Definition: createFields.H:27
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::name
word name(const complex &c)
Return string representation of complex.
Definition: complex.C:76
Foam::T
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Definition: FieldFieldFunctions.C:58
Foam::nameOp
Extract name (as a word) from an object, typically using its name() method.
Definition: word.H:238
Foam::word::removeExt
bool removeExt()
Remove extension, returning true if string changed.
Definition: wordI.H:171
Foam::word::stripInvalid
void stripInvalid()
Strip invalid characters from this word.
Definition: wordI.H:145
size_type
graph_traits< Graph >::vertices_size_type size_type
Definition: SloanRenumber.C:76
Foam::nameOp::operator()
word operator()(const T &obj) const
Definition: word.H:240
Foam::word::valid
static bool valid(char c)
Is this character valid for a word?
Definition: wordI.H:130
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::word::typeName
static const char *const typeName
The typeName.
Definition: word.H:71
wordI.H
Foam::word::debug
static int debug
Debugging.
Definition: word.H:74
Foam::word::word
word()=default
Construct null.
Foam::constant::universal::c
const dimensionedScalar c
Speed of light in a vacuum.
Foam::word::printf
static word printf(const char *fmt, const PrimitiveType &val)
Use a printf-style formatter for a primitive.
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::typeOp
Extract type (as a word) from an object, typically using its type() method.
Definition: word.H:249