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-2019 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 absolute
169  bool isAbsolute() const;
170 
171  //- Convert from relative to absolute
172  fileName& toAbsolute();
173 
174 
175  // Decomposition
176 
177  //- Return file name (part beyond last /)
178  //
179  // Behaviour compared to /usr/bin/basename:
180  // Input name() basename
181  // ----- ------ --------
182  // "foo" "foo" "foo"
183  // "/foo" "foo" "foo"
184  // "foo/bar" "bar" "bar"
185  // "/foo/bar" "bar" "bar"
186  // "/foo/bar/" "" "bar"
187  //
188  word name() const;
189 
190  //- Return file name (part beyond last /), substitute for FOAM_CASE
191  string caseName() const;
192 
193  //- Return file name, optionally without extension
194  word name(const bool noExt) const;
195 
196  //- Return directory path name (part before last /)
197  //
198  // Behaviour compared to /usr/bin/dirname:
199  // input path() dirname
200  // ----- ------ -------
201  // "foo" "." "."
202  // "/foo" "/" "foo"
203  // "foo/bar" "foo" "foo"
204  // "/foo/bar" "/foo" "/foo"
205  // "/foo/bar/" "/foo/bar/" "/foo"
206  //
207  fileName path() const;
208 
209  //- Return file name without extension (part before last .)
210  fileName lessExt() const;
211 
212  //- Return file name extension (part after last .)
213  word ext() const;
214 
215  //- Return path components as wordList
216  //
217  // Behaviour:
218  // Input components()
219  // ----- ------
220  // "foo" 1("foo")
221  // "/foo" 1("foo")
222  // "foo/bar" 2("foo", "bar")
223  // "/foo/bar" 2("foo", "bar")
224  // "/foo/bar/" 2("foo", "bar")
225  wordList components(const char delimiter='/') const;
226 
227  //- Return a single component of the path
228  word component(const size_type, const char delimiter='/') const;
229 
230 
231  // Member Operators
232 
233  // Assignment
234 
235  void operator=(const fileName&);
236  void operator=(fileName&&);
237  void operator=(const word&);
238  void operator=(const string&);
239  void operator=(const std::string&);
240  void operator=(const char*);
241 
242 
243  // IOstream Operators
244 
245  friend Istream& operator>>(Istream&, fileName&);
246  friend Ostream& operator<<(Ostream&, const fileName&);
247 };
248 
249 
250 //- Assemble words and fileNames as pathnames by adding a '/' separator
251 fileName operator/(const string&, const string&);
252 
253 
254 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
255 
256 } // End namespace Foam
257 
258 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
259 
260 #include "fileNameI.H"
261 
262 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
263 
264 #endif
265 
266 // ************************************************************************* //
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
dimensionedScalar operator/(const scalar s1, const dimensionedScalar &ds2)
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:53
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:74
void component(FieldField< Field, typename FieldField< Field, Type >::cmptType > &sf, const FieldField< Field, Type > &f, const direction d)
Namespace for OpenFOAM.
fileName path(UMean.rootPath()/UMean.caseName()/functionObjects::writeFile::outputPrefix/"graphs"/UMean.instance())