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 | Copyright (C) 2011-2016 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 or UNDEFINED
158  Type type() const;
159 
160  //- Return true if file name is absolute
161  bool isAbsolute() const;
162 
163  //- Convert from relative to absolute
164  fileName& toAbsolute();
165 
166 
167  // Decomposition
168 
169  //- Return file name (part beyond last /)
170  //
171  // Behaviour compared to /usr/bin/basename:
172  // Input name() basename
173  // ----- ------ --------
174  // "foo" "foo" "foo"
175  // "/foo" "foo" "foo"
176  // "foo/bar" "bar" "bar"
177  // "/foo/bar" "bar" "bar"
178  // "/foo/bar/" "" "bar"
179  //
180  word name() const;
181 
182  //- Return file name (part beyond last /), subsitute for FOAM_CASE
183  string caseName() const;
184 
185  //- Return file name, optionally without extension
186  word name(const bool noExt) const;
187 
188  //- Return directory path name (part before last /)
189  //
190  // Behaviour compared to /usr/bin/dirname:
191  // input path() dirname
192  // ----- ------ -------
193  // "foo" "." "."
194  // "/foo" "/" "foo"
195  // "foo/bar" "foo" "foo"
196  // "/foo/bar" "/foo" "/foo"
197  // "/foo/bar/" "/foo/bar/" "/foo"
198  //
199  fileName path() const;
200 
201  //- Return file name without extension (part before last .)
202  fileName lessExt() const;
203 
204  //- Return file name extension (part after last .)
205  word ext() const;
206 
207  //- Return path components as wordList
208  //
209  // Behaviour:
210  // Input components()
211  // ----- ------
212  // "foo" 1("foo")
213  // "/foo" 1("foo")
214  // "foo/bar" 2("foo", "bar")
215  // "/foo/bar" 2("foo", "bar")
216  // "/foo/bar/" 2("foo", "bar")
217  wordList components(const char delimiter='/') const;
218 
219  //- Return a single component of the path
220  word component(const size_type, const char delimiter='/') const;
221 
222 
223  // Member operators
224 
225  // Assignment
226 
227  void operator=(const fileName&);
228  void operator=(const word&);
229  void operator=(const string&);
230  void operator=(const std::string&);
231  void operator=(const char*);
232 
233 
234  // IOstream operators
235 
236  friend Istream& operator>>(Istream&, fileName&);
237  friend Ostream& operator<<(Ostream&, const fileName&);
238 };
239 
240 
241 //- Assemble words and fileNames as pathnames by adding a '/' separator
242 fileName operator/(const string&, const string&);
243 
244 
245 //- Recursively search the given directory for the file
246 // returning the path relative to the directory or
247 // fileName::null if not found
248 fileName search(const word& file, const fileName& directory);
249 
250 
251 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
252 
253 } // End namespace Foam
254 
255 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
256 
257 #include "fileNameI.H"
258 
259 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
260 
261 #endif
262 
263 // ************************************************************************* //
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
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
Type type() const
Return the file type: FILE, DIRECTORY or UNDEFINED.
Definition: fileName.C:51
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
string caseName() const
Return file name (part beyond last /), subsitute for FOAM_CASE.
Definition: fileName.C:194
friend Ostream & operator<<(Ostream &, const fileName &)
word name() const
Return file name (part beyond last /)
Definition: fileName.C:179
A class for handling words, derived from string.
Definition: word.H:59
Istream & operator>>(Istream &, directionInfo &)
wordList components(const char delimiter='/') const
Return path components as wordList.
Definition: fileName.C:298
word component(const size_type, const char delimiter='/') const
Return a single component of the path.
Definition: fileName.C:326
graph_traits< Graph >::vertices_size_type size_type
Definition: SloanRenumber.C:73
bool isAbsolute() const
Return true if file name is absolute.
Definition: fileName.C:57
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
fileName lessExt() const
Return file name without extension (part before last .)
Definition: fileName.C:268
List< word > wordList
A List of words.
Definition: fileName.H:54
Ostream & operator<<(Ostream &, const ensightPart &)
word ext() const
Return file name extension (part after last .)
Definition: fileName.C:283
fileName search(const word &file, const fileName &directory)
Recursively search the given directory for the file.
Definition: fileName.C:401
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.