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