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-2018 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 /*---------------------------------------------------------------------------*\
67  Class fileName Declaration
68 \*---------------------------------------------------------------------------*/
69 
70 class fileName
71 :
72  public string
73 {
74  // Private Member Functions
75 
76  //- Strip invalid characters
77  inline void stripInvalid();
78 
79 
80 public:
81 
82  //- Enumerations to handle file types and modes.
83  enum Type
84  {
88  LINK
89  };
90 
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  //- Construct as copy
107  inline fileName(const fileName&);
108 
109  //- Construct as copy of word
110  inline fileName(const word&);
111 
112  //- Construct as copy of string
113  inline fileName(const string&);
114 
115  //- Construct as copy of std::string
116  inline fileName(const std::string&);
117 
118  //- Construct as copy of character array
119  inline fileName(const char*);
120 
121  //- Construct by concatenating elements of wordList separated by '/'
122  explicit fileName(const wordList&);
123 
124  //- Construct from Istream
125  fileName(Istream&);
126 
127 
128  // Member functions
129 
130  //- Is this character valid for a fileName?
131  inline static bool valid(char);
132 
133  //- Cleanup file name
134  //
135  // * Removes repeated slashes
136  // /abc////def --> /abc/def
137  //
138  // * Removes '/./'
139  // /abc/def/./ghi/. --> /abc/def/./ghi
140  // abc/def/./ --> abc/def
141  //
142  // * Removes '/../'
143  // /abc/def/../ghi/jkl/nmo/.. --> /abc/ghi/jkl
144  // abc/../def/ghi/../jkl --> abc/../def/jkl
145  //
146  // * Removes trailing '/'
147  //
148  bool clean();
149 
150  //- Cleanup file name
151  // eg, remove repeated slashes, etc.
152  fileName clean() const;
153 
154 
155  // Interrogation
156 
157  //- Return the file type: FILE, DIRECTORY, UNDEFINED or
158  // LINK (only if followLink=false)
159  Type type(const bool followLink = true) const;
160 
161  //- Return true if file name is absolute
162  bool isAbsolute() const;
163 
164  //- Convert from relative to absolute
165  fileName& toAbsolute();
166 
167 
168  // Decomposition
169 
170  //- Return file name (part beyond last /)
171  //
172  // Behaviour compared to /usr/bin/basename:
173  // Input name() basename
174  // ----- ------ --------
175  // "foo" "foo" "foo"
176  // "/foo" "foo" "foo"
177  // "foo/bar" "bar" "bar"
178  // "/foo/bar" "bar" "bar"
179  // "/foo/bar/" "" "bar"
180  //
181  word name() const;
182 
183  //- Return file name (part beyond last /), substitute for FOAM_CASE
184  string caseName() const;
185 
186  //- Return file name, optionally without extension
187  word name(const bool noExt) const;
188 
189  //- Return directory path name (part before last /)
190  //
191  // Behaviour compared to /usr/bin/dirname:
192  // input path() dirname
193  // ----- ------ -------
194  // "foo" "." "."
195  // "/foo" "/" "foo"
196  // "foo/bar" "foo" "foo"
197  // "/foo/bar" "/foo" "/foo"
198  // "/foo/bar/" "/foo/bar/" "/foo"
199  //
200  fileName path() const;
201 
202  //- Return file name without extension (part before last .)
203  fileName lessExt() const;
204 
205  //- Return file name extension (part after last .)
206  word ext() const;
207 
208  //- Return path components as wordList
209  //
210  // Behaviour:
211  // Input components()
212  // ----- ------
213  // "foo" 1("foo")
214  // "/foo" 1("foo")
215  // "foo/bar" 2("foo", "bar")
216  // "/foo/bar" 2("foo", "bar")
217  // "/foo/bar/" 2("foo", "bar")
218  wordList components(const char delimiter='/') const;
219 
220  //- Return a single component of the path
221  word component(const size_type, const char delimiter='/') const;
222 
223 
224  // Member operators
225 
226  // Assignment
227 
228  void operator=(const fileName&);
229  void operator=(const word&);
230  void operator=(const string&);
231  void operator=(const std::string&);
232  void operator=(const char*);
233 
234 
235  // IOstream operators
236 
237  friend Istream& operator>>(Istream&, fileName&);
238  friend Ostream& operator<<(Ostream&, const fileName&);
239 };
240 
241 
242 //- Assemble words and fileNames as pathnames by adding a '/' separator
243 fileName operator/(const string&, const string&);
244 
245 
246 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
247 
248 } // End namespace Foam
249 
250 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
251 
252 #include "fileNameI.H"
253 
254 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
255 
256 #endif
257 
258 // ************************************************************************* //
static bool valid(char)
Is this character valid for a fileName?
Definition: fileNameI.H:94
static int debug
Definition: fileName.H:94
A class for handling file names.
Definition: fileName.H:69
fileName()
Construct null.
Definition: fileNameI.H:52
friend Istream & operator>>(Istream &, fileName &)
bool clean()
Cleanup file name.
Definition: fileName.C:77
string caseName() const
Return file name (part beyond last /), substitute for FOAM_CASE.
Definition: fileName.C:194
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:60
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
static const fileName null
An empty fileName.
Definition: fileName.H:97
dimensionedScalar operator/(const scalar s1, const dimensionedScalar &ds2)
static const char *const typeName
Definition: fileName.H:93
word ext() const
Return file name extension (part after last .)
Definition: fileName.C:283
friend Ostream & operator<<(Ostream &, const fileName &)
Type type(const bool followLink=true) const
Return the file type: FILE, DIRECTORY, UNDEFINED or.
Definition: fileName.C:51
wordList components(const char delimiter='/') const
Return path components as wordList.
Definition: fileName.C:298
bool isAbsolute() const
Return true if file name is absolute.
Definition: fileName.C:57
A class for handling words, derived from string.
Definition: word.H:59
Istream & operator>>(Istream &, directionInfo &)
word name() const
Return file name (part beyond last /)
Definition: fileName.C:179
graph_traits< Graph >::vertices_size_type size_type
Definition: SloanRenumber.C:73
word component(const size_type, const char delimiter='/') const
Return a single component of the path.
Definition: fileName.C:326
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:53
fileName & toAbsolute()
Convert from relative to absolute.
Definition: fileName.C:63
List< word > wordList
A List of words.
Definition: fileName.H:54
Ostream & operator<<(Ostream &, const ensightPart &)
fileName lessExt() const
Return file name without extension (part before last .)
Definition: fileName.C:268
fileName path() const
Return directory path name (part before last /)
Definition: fileName.C:249
A class for handling character strings derived from std::string.
Definition: string.H:74
void operator=(const fileName &)
Definition: fileName.C:337
Type
Enumerations to handle file types and modes.
Definition: fileName.H:82
Namespace for OpenFOAM.