fileName.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2021 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Class
25  Foam::fileName
26 
27 Description
28  A class for handling file names.
29 
30  A fileName is a string of characters without whitespace or quotes.
31  A fileName can be
32  - constructed from a char*, a string or a word
33  - concatenated by adding a '/' separator
34  - decomposed into the path, name or component list
35  - interrogated for type and access mode
36 
37  The string::expand() method expands environment variables, etc,
38 
39 SourceFiles
40  fileName.C
41  fileNameIO.C
42 
43 \*---------------------------------------------------------------------------*/
44 
45 #ifndef fileName_H
46 #define fileName_H
47 
48 #include "word.H"
49 
50 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
51 
52 namespace Foam
53 {
54 
55 template<class T> class List;
56 typedef List<word> wordList;
57 
58 // Forward declaration of friend functions and operators
59 
60 class fileName;
61 
64 
65 
66 //- Enumeration of file types
67 enum class fileType
68 {
69  undefined,
70  file,
71  directory,
72  link
73 };
74 
75 
76 /*---------------------------------------------------------------------------*\
77  Class fileName Declaration
78 \*---------------------------------------------------------------------------*/
79 
80 class fileName
81 :
82  public string
83 {
84  // Private Member Functions
85 
86  //- Strip invalid characters
87  inline void stripInvalid();
88 
89 
90 public:
91 
92  // Static Data Members
93 
94  static const char* const typeName;
95  static int debug;
96 
97  //- An empty fileName
98  static const fileName null;
99 
100 
101  // Constructors
102 
103  //- Construct null
104  inline fileName();
105 
106  //- Copy constructor
107  inline fileName(const fileName&);
108 
109  //- Move constructor
110  inline fileName(fileName&&);
111 
112  //- Copy constructor of word
113  inline fileName(const word&);
114 
115  //- Copy constructor of string
116  inline fileName(const string&);
117 
118  //- Copy constructor of std::string
119  inline fileName(const std::string&);
120 
121  //- Copy constructor of character array
122  inline fileName(const char*);
123 
124  //- Construct by concatenating elements of wordList separated by '/'
125  explicit fileName(const wordList&);
126 
127  //- Construct from Istream
128  fileName(Istream&);
129 
130 
131  // Member Functions
132 
133  //- Is this character valid for a fileName?
134  inline static bool valid(char);
135 
136  //- Cleanup file name
137  //
138  // * Removes repeated slashes
139  // /abc////def --> /abc/def
140  //
141  // * Removes '/./'
142  // /abc/def/./ghi/. --> /abc/def/./ghi
143  // abc/def/./ --> abc/def
144  //
145  // * Removes '/../'
146  // /abc/def/../ghi/jkl/nmo/.. --> /abc/ghi/jkl
147  // abc/../def/ghi/../jkl --> abc/../def/jkl
148  //
149  // * Removes trailing '/'
150  //
151  bool clean();
152 
153  //- Cleanup file name
154  // eg, remove repeated slashes, etc.
155  fileName clean() const;
156 
157 
158  // Interrogation
159 
160  //- Return the file type: file, directory, undefined or
161  // link (only if followLink=false)
162  fileType type
163  (
164  const bool checkVariants = true,
165  const bool followLink = true
166  ) const;
167 
168  //- Return true if file name is a name without a path
169  // i.e. does not contain a '/'
170  bool isName() const;
171 
172  //- Return true if file name has a path
173  // i.e. contains a '/'
174  bool hasPath() const;
175 
176  //- Return true if file name is absolute
177  bool isAbsolute() const;
178 
179  //- Convert from relative to absolute
180  fileName& toAbsolute();
181 
182 
183  // Decomposition
184 
185  //- Return file name (part beyond last /)
186  //
187  // Behaviour compared to /usr/bin/basename:
188  // Input name() basename
189  // ----- ------ --------
190  // "foo" "foo" "foo"
191  // "/foo" "foo" "foo"
192  // "foo/bar" "bar" "bar"
193  // "/foo/bar" "bar" "bar"
194  // "/foo/bar/" "" "bar"
195  //
196  word name() const;
197 
198  //- Return file name (part beyond last /), substitute for FOAM_CASE
199  string caseName() const;
200 
201  //- Return file name, optionally without extension
202  word name(const bool noExt) const;
203 
204  //- Return directory path name (part before last /)
205  //
206  // Behaviour compared to /usr/bin/dirname:
207  // input path() dirname
208  // ----- ------ -------
209  // "foo" "." "."
210  // "/foo" "/" "foo"
211  // "foo/bar" "foo" "foo"
212  // "/foo/bar" "/foo" "/foo"
213  // "/foo/bar/" "/foo/bar/" "/foo"
214  //
215  fileName path() const;
216 
217  //- Return file name without extension (part before last .)
218  fileName lessExt() const;
219 
220  //- Return file name extension (part after last .)
221  word ext() const;
222 
223  //- Return path components as wordList
224  //
225  // Behaviour:
226  // Input components()
227  // ----- ------
228  // "foo" 1("foo")
229  // "/foo" 1("foo")
230  // "foo/bar" 2("foo", "bar")
231  // "/foo/bar" 2("foo", "bar")
232  // "/foo/bar/" 2("foo", "bar")
233  wordList components(const char delimiter='/') const;
234 
235  //- Return a single component of the path
236  word component(const size_type, const char delimiter='/') const;
237 
238 
239  // Member Operators
240 
241  // Assignment
242 
243  void operator=(const fileName&);
244  void operator=(fileName&&);
245  void operator=(const word&);
246  void operator=(const string&);
247  void operator=(const std::string&);
248  void operator=(const char*);
249 
250 
251  // IOstream Operators
252 
253  friend Istream& operator>>(Istream&, fileName&);
254  friend Ostream& operator<<(Ostream&, const fileName&);
255 };
256 
257 
258 //- Assemble words and fileNames as filePaths by adding a '/' separator
259 fileName operator/(const string&, const string&);
260 
261 
262 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
263 
264 } // End namespace Foam
265 
266 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
267 
268 #include "fileNameI.H"
269 
270 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
271 
272 #endif
273 
274 // ************************************************************************* //
A class for handling file names.
Definition: fileName.H:79
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:59
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
tmp< fvMatrix< Type > > operator/(const fvMatrix< Type > &, const volScalarField::Internal &)
A class for handling words, derived from string.
Definition: word.H:59
Istream & operator>>(Istream &, directionInfo &)
fileType
Enumeration of file types.
Definition: fileName.H:66
graph_traits< Graph >::vertices_size_type size_type
Definition: SloanRenumber.C:73
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:54
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
List< word > wordList
A List of words.
Definition: fileName.H:54
Ostream & operator<<(Ostream &, const ensightPart &)
fileType type(const fileName &, const bool checkVariants=true, const bool followLink=true)
Return the file type: directory or file.
Definition: POSIX.C:488
A class for handling character strings derived from std::string.
Definition: string.H:76
void component(FieldField< Field, typename FieldField< Field, Type >::cmptType > &sf, const FieldField< Field, Type > &f, const direction d)
Namespace for OpenFOAM.