fileName.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-2017 OpenFOAM Foundation
9 Copyright (C) 2016-2022 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
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
27Class
28 Foam::fileName
29
30Description
31 A class for handling file names.
32
33 A fileName is a string of characters without whitespace or quotes.
34 A fileName can be
35 - constructed from a char*, a string or a word
36 - concatenated by adding a '/' separator
37 - decomposed into the path, name or component list
38 - interrogated for type and access mode
39
40 The string::expand() method expands environment variables, etc,
41
42SourceFiles
43 fileName.C
44 fileNameIO.C
45
46\*---------------------------------------------------------------------------*/
47
48#ifndef Foam_fileName_H
49#define Foam_fileName_H
50
51#include "word.H"
52
53// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
54
55namespace Foam
56{
57
58// Forward Declarations
59class fileName;
60class token;
61
62template<class T> class List;
63template<class T> class UList;
64typedef List<word> wordList;
65
66//- Hashing for fileName
67template<> struct Hash<fileName> : string::hasher {};
68
69
70/*---------------------------------------------------------------------------*\
71 Class fileName Declaration
72\*---------------------------------------------------------------------------*/
74class fileName
75:
76 public string
77{
78public:
79
80 //- Enumerations to handle directory entry types.
81 enum Type
82 {
84 FILE = 1,
86 SYMLINK = 4,
88 };
89
90
91 // Static Data Members
92
93 //- The typeName
94 static const char* const typeName;
95
96 //- Debugging
97 static int debug;
98
99 //- Allow space character in fileName. To be used with caution.
100 static int allowSpaceInFileName;
101
102 //- An empty fileName
103 static const fileName null;
104
105
106 // Constructors
107
108 //- Default construct
109 fileName() = default;
110
111 //- Copy construct
112 fileName(const fileName&) = default;
113
114 //- Move construct
115 fileName(fileName&&) = default;
116
117 //- Copy construct from word
118 inline fileName(const word& s);
119
120 //- Move construct from word
121 inline fileName(word&& s);
122
123 //- Copy construct from string
124 inline fileName(const string& s, bool doStrip=true);
125
126 //- Move construct from string
127 inline fileName(string&& s, bool doStrip=true);
128
129 //- Copy construct from std::string
130 inline fileName(const std::string& s, bool doStrip=true);
131
132 //- Move construct from std::string
133 inline fileName(std::string&& s, bool doStrip=true);
134
135 //- Copy construct from character array
136 inline fileName(const char* s, bool doStrip=true);
137
138 //- Construct by concatenating elements of wordList separated by '/'
139 explicit fileName(const UList<word>& list);
140
141 //- Construct by concatenating words separated by '/'
142 explicit fileName(std::initializer_list<word> list);
143
144 //- Construct from Istream
145 explicit fileName(Istream& is);
146
147
148 // Member Functions
149
150 //- Inherit all regular string assign() methods
151 using string::assign;
152
153 //- Assign from word or string token.
154 // \return false if the token was the incorrect type
155 bool assign(const token& tok);
156
157 //- Is this character valid for a fileName?
158 inline static bool valid(char c);
159
160 //- Construct fileName without invalid characters, possibly applying
161 //- other transformations such as changing the path separator,
162 //- removing duplicate or trailing slashes, etc.
163 static fileName validate(const std::string&, const bool doClean=true);
164
165 //- Join two strings with a path separator ('/' by default).
166 // No separator is added if either argument is an empty string or
167 // if the arguments already had the path separator at the junction.
168 // Invalid characters are \em not stripped (ie, retained).
169 static fileName concat
170 (
171 const std::string& s1,
172 const std::string& s2,
173 const char delim = '/'
174 );
175
176 //- This is a specialized (possibly slower) version of compare()
177 //- that ignores duplicate or trailing slashes.
178 static bool equals(const std::string& s1, const std::string& s2);
179
180 //- Strip invalid characters
181 inline void stripInvalid();
182
183 //- Cleanup filename string, possibly applies other transformations
184 //- such as changing the path separator etc.
185 //
186 // Changes back-slash to forward-slash path separator,
187 // while preserving windows UNC:
188 // \verbatim
189 // \\server\abc\def --> \\server/abc/def
190 // \endverbatim
191 //
192 // Removes trailing slash:
193 // \verbatim
194 // / --> /
195 // /abc/ --> /abc
196 // \endverbatim
197 //
198 // Removes repeated slashes, but preserves UNC:
199 // \verbatim
200 // /abc////def --> /abc/def
201 // \\server\abc////def --> \\server/abc/def
202 // \endverbatim
203 //
204 // Removes \c "/./" (current directory), except for leading one:
205 // \verbatim
206 // /abc/def/./ghi/. --> /abc/def/ghi
207 // abc/def/./ --> abc/def
208 // ./abc/ --> ./abc
209 // \endverbatim
210 //
211 // Removes \c "/../" (parent directory), except for leading one:
212 // \verbatim
213 // /abc/def/../ghi/jkl/nmo/.. --> /abc/ghi/jkl
214 // abc/../def/ghi/../jkl --> abc/../def/jkl
215 // \endverbatim
216 // .
217 //
218 // \return True if the content changed
219 static bool clean(std::string& str);
220
221 //- Cleanup filename (inplace)
222 // \return True if the content changed
223 bool clean();
224
225
226 // Interrogation
227
228 //- Return the directory entry type:
229 //- UNDEFINED, FILE, DIRECTORY (or SYMLINK).
230 //
231 // \param followLink when false it will return SYMLINK for a symlink
232 // rather than following it.
233 // \param checkGzip add an additional test for a gzip FILE
234 Type type(bool followLink=true, bool checkGzip=false) const;
235
236 //- Return true if filename starts with a '/' or '\\'
237 //- or (windows-only) with a filesystem-root
238 inline static bool isAbsolute(const std::string& str);
239
240 //- Return true if filename is absolute,
241 //- which means it starts with a '/' or '\\'
242 //- or (windows-only) with a filesystem-root
243 inline bool isAbsolute() const;
244
245 //- Convert from relative to absolute
247
248 //- Return true if string ends with "~", ".bak", ".old", ".save"
249 static bool isBackup(const std::string& str);
250
251 //- Return true if file name ends with "~", ".bak", ".old", ".save"
252 inline bool isBackup() const;
253
254
255 // Decomposition
256
257 //- Return basename (part beyond last /), including its extension
258 // The result normally corresponds to a Foam::word
259 //
260 // Behaviour compared to /usr/bin/basename:
261 // \verbatim
262 // input name() basename
263 // ----- ------ --------
264 // "" "" ""
265 // "abc" "abc" "abc"
266 // "/" "" "/"
267 // "/abc" "abc" "abc"
268 // "abc/def" "def" "def"
269 // "/abc/def" "def" "def"
270 // "/abc/def/" "" "def"
271 // "/abc/../def" "def" "def"
272 // \endverbatim
273 inline static std::string name(const std::string& str);
274
275 //- Return basename (part beyond last /), including its extension
276 inline word name() const;
277
278 //- Return basename, without extension
279 // The result normally corresponds to a Foam::word
280 static std::string nameLessExt(const std::string& str);
281
282 //- Return basename, without extension
283 inline word nameLessExt() const;
284
285 //- Deprecated(2017-03) return basename, optionally without extension
286 // \deprecated(2017-03) - use name() or nameLessExt() methods
287 // which describe their behaviour explicitly
288 word name(const bool noExt) const
289 {
290 return noExt ? this->nameLessExt() : this->name();
291 }
292
293 //- Return directory path name (part before last /)
294 // The result normally corresponds to a Foam::fileName
295 //
296 // Behaviour compared to /usr/bin/dirname:
297 // \verbatim
298 // input path() dirname
299 // ----- ------ -------
300 // "" "." "."
301 // "abc" "." "."
302 // "/" "/" "/"
303 // "/abc" "/" "/"
304 // "abc/def" "abc" "abc"
305 // "/abc/def" "/abc" "/abc"
306 // "/abc/def/" "/abc/def" "/abc"
307 // "/abc/../def" "/abc/.." "/abc/.."
308 // \endverbatim
309 inline static std::string path(const std::string& str);
310
311 //- Return directory path name (part before last /)
312 inline fileName path() const;
313
314 //- Return true if it contains a '/' character
315 inline bool hasPath() const;
316
317 //- Remove leading path, returning true if string changed.
318 inline bool removePath();
319
320 //- Return a relative name by stripping off the parent directory
321 //- where possible.
322 //
323 // \param parent the parent directory
324 // \param caseTag replace the parent with <case> for later
325 // use with expand(), or prefix <case> if the file name was
326 // not an absolute location
328 (
329 const fileName& parent,
330 const bool caseTag = false
331 ) const;
332
333 //- Return file name without extension (part before last .)
334 inline fileName lessExt() const;
335
336 //- Return file name extension (part after last .)
337 inline word ext() const;
338
339 //- Append a '.' and the ending, and return the object.
340 // The '.' and ending will not be added when the ending is empty,
341 // or when the file name is empty or ended with a '/'.
342 inline fileName& ext(const word& ending);
343
344 //- Various checks for extensions
345 using string::hasExt;
346
347 //- Remove extension, returning true if string changed.
348 using string::removeExt;
349
350
351 //- Return path components as wordList
352 //
353 // Behaviour:
354 // \verbatim
355 // input components()
356 // ----- ------------
357 // "" ()
358 // "." (".")
359 // "abc" ("abc")
360 // "/abc" ("abc")
361 // "abc/def" ("abc", "def")
362 // "/abc/def" ("abc", "def")
363 // "/abc/def/" ("abc", "def")
364 // \endverbatim
365 wordList components(const char delim = '/') const;
366
367 //- Return a single component of the path or empty if out of range
368 // The location \c npos returns the last component
369 word component(const size_type cmpt, const char delim = '/') const;
370
371
372 // Member Operators
373
374 // Assignment
375
376 //- Copy assignment, no character validation required
377 // Self-assignment is a no-op.
378 inline fileName& operator=(const fileName& str);
379
380 //- Move assignment, no character validation required
381 // Self-assignment is a no-op.
382 inline fileName& operator=(fileName&& str);
383
384 //- Copy assignment, no character validation required
385 inline fileName& operator=(const word& str);
386
387 //- Move assignment, no character validation required
388 inline fileName& operator=(word&& str);
389
390 //- Copy assignment, stripping invalid characters
391 inline fileName& operator=(const string& str);
392
393 //- Move assignment, stripping invalid characters
394 inline fileName& operator=(string&& str);
395
396 //- Copy assignment, stripping invalid characters
397 inline fileName& operator=(const std::string& str);
398
399 //- Move assignment, stripping invalid characters
400 inline fileName& operator=(std::string&& str);
401
402 //- Copy, stripping invalid characters
403 inline fileName& operator=(const char* str);
404
405
406 // Other Operators
407
408 //- Append a path element with '/' separator.
409 // No '/' separator is added if this or the argument are empty.
410 fileName& operator/=(const string& other);
411};
412
413
414// IOstream Operators
415
416//- Read operator
417Istream& operator>>(Istream& is, fileName& val);
418
419//- Write operator
420Ostream& operator<<(Ostream& os, const fileName& val);
421
422
423// Global Operators
424
425//- Assemble words and fileNames as pathnames by adding a '/' separator.
426// No '/' separator is added if either argument is an empty string.
427fileName operator/(const string& s1, const string& s2);
428
429
430//- Recursively search the given directory for the file
431// returning the path relative to the directory or
432// fileName::null if not found
433fileName search(const word& file, const fileName& directory);
434
435
436// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
437
438} // End namespace Foam
439
440// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
441
442#include "fileNameI.H"
443
444// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
445
446#endif
447
448// ************************************************************************* //
graph_traits< Graph >::vertices_size_type size_type
Definition: SloanRenumber.C:75
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:64
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: List.H:77
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: UList.H:94
void assign(const faPatch &fap)
Clear and populate with values from finiteArea patch.
Definition: faPatchData.C:92
A class for handling file names.
Definition: fileName.H:76
wordList components(const char delim='/') const
Return path components as wordList.
Definition: fileName.C:514
fileName relative(const fileName &parent, const bool caseTag=false) const
Definition: fileName.C:425
bool isAbsolute() const
Definition: fileNameI.H:158
static fileName validate(const std::string &, const bool doClean=true)
Definition: fileName.C:206
bool assign(const token &tok)
Assign from word or string token.
Definition: fileNameIO.C:42
bool clean()
Cleanup filename (inplace)
Definition: fileName.C:390
Type
Enumerations to handle directory entry types.
Definition: fileName.H:81
@ SYMLINK
A symbolic link.
Definition: fileName.H:85
@ UNDEFINED
Undefined type.
Definition: fileName.H:82
@ FILE
A regular file.
Definition: fileName.H:83
@ DIRECTORY
A directory.
Definition: fileName.H:84
@ LINK
Same as symlink.
Definition: fileName.H:86
Type type(bool followLink=true, bool checkGzip=false) const
Definition: fileName.C:360
fileName()=default
Default construct.
fileName & operator/=(const string &other)
Append a path element with '/' separator.
Definition: fileName.C:559
word name() const
Return basename (part beyond last /), including its extension.
Definition: fileNameI.H:212
fileName(fileName &&)=default
Move construct.
static fileName concat(const std::string &s1, const std::string &s2, const char delim='/')
Join two strings with a path separator ('/' by default).
Definition: fileName.C:218
bool removePath()
Remove leading path, returning true if string changed.
Definition: fileNameI.H:243
bool hasPath() const
Return true if it contains a '/' character.
Definition: fileNameI.H:170
fileName & operator=(const fileName &str)
Copy assignment, no character validation required.
Definition: fileNameI.H:258
fileName lessExt() const
Return file name without extension (part before last .)
Definition: fileNameI.H:230
static bool equals(const std::string &s1, const std::string &s2)
Definition: fileName.C:245
word component(const size_type cmpt, const char delim='/') const
Return a single component of the path or empty if out of range.
Definition: fileName.C:534
static const fileName null
An empty fileName.
Definition: fileName.H:102
static bool valid(char c)
Is this character valid for a fileName?
Definition: fileNameI.H:102
word ext() const
Return file name extension (part after last .)
Definition: fileNameI.H:218
fileName & toAbsolute()
Convert from relative to absolute.
Definition: fileName.C:377
fileName path() const
Return directory path name (part before last /)
Definition: fileNameI.H:193
static int debug
Debugging.
Definition: fileName.H:96
fileName(const fileName &)=default
Copy construct.
void stripInvalid()
Strip invalid characters.
Definition: fileNameI.H:113
bool isBackup() const
Return true if file name ends with "~", ".bak", ".old", ".save".
Definition: fileNameI.H:164
static int allowSpaceInFileName
Allow space character in fileName. To be used with caution.
Definition: fileName.H:99
word nameLessExt() const
Return basename, without extension.
Definition: fileNameI.H:224
static const char *const typeName
The typeName.
Definition: fileName.H:93
A class for handling character strings derived from std::string.
Definition: string.H:79
bool removeExt()
Remove extension, returning true if string changed.
Definition: stringI.H:96
bool hasExt() const
Return true if it has an extension or simply ends with a '.'.
Definition: stringI.H:56
A token holds an item read from Istream.
Definition: token.H:69
A class for handling words, derived from Foam::string.
Definition: word.H:68
OBJstream os(runTime.globalPath()/outputName)
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))
Namespace for OpenFOAM.
List< word > wordList
A List of words.
Definition: fileName.H:63
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
dimensionedScalar operator/(const scalar s1, const dimensionedScalar &ds2)
Istream & operator>>(Istream &, directionInfo &)
fileName search(const word &file, const fileName &directory)
Recursively search the given directory for the file.
Definition: fileName.C:624
Hash function class. The default definition is for primitives. Non-primitives used to hash entries on...
Definition: Hash.H:54
Hashing functor for string and derived string classes.
Definition: string.H:148