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