fileName.C
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 \*---------------------------------------------------------------------------*/
25 
26 #include "fileName.H"
27 #include "wordList.H"
28 #include "DynamicList.H"
29 #include "OSspecific.H"
30 
31 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
32 
33 const char* const Foam::fileName::typeName = "fileName";
34 int Foam::fileName::debug(debug::debugSwitch(fileName::typeName, 0));
36 
37 
38 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
39 
41 {
42  forAll(lst, elemI)
43  {
44  operator=((*this)/lst[elemI]);
45  }
46 }
47 
48 
49 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
50 
52 (
53  const bool checkVariants,
54  const bool followLink
55 ) const
56 {
57  return ::Foam::type(*this, checkVariants, followLink);
58 }
59 
60 
62 {
63  return !empty() && operator[](0) == '/';
64 }
65 
66 
68 {
69  fileName& f = *this;
70 
71  if (!f.isAbsolute())
72  {
73  f = cwd()/f;
74  f.clean();
75  }
76 
77  return f;
78 }
79 
80 
82 {
83  // The top slash - we are never allowed to go above it
84  string::size_type top = this->find('/');
85 
86  // No slashes - nothing to do
87  if (top == string::npos)
88  {
89  return false;
90  }
91 
92  // Start with the '/' found:
93  char prev = '/';
94  string::size_type nChar = top+1;
95  string::size_type maxLen = this->size();
96 
97  for
98  (
99  string::size_type src = nChar;
100  src < maxLen;
101  )
102  {
103  char c = operator[](src++);
104 
105  if (prev == '/')
106  {
107  // Repeated '/' - skip it
108  if (c == '/')
109  {
110  continue;
111  }
112 
113  // Could be '/./' or '/../'
114  if (c == '.')
115  {
116  // Found trailing '/.' - skip it
117  if (src >= maxLen)
118  {
119  continue;
120  }
121 
122 
123  // Peek at the next character
124  char c1 = operator[](src);
125 
126  // Found '/./' - skip it
127  if (c1 == '/')
128  {
129  src++;
130  continue;
131  }
132 
133  // It is '/..' or '/../'
134  if (c1 == '.' && (src+1 >= maxLen || operator[](src+1) == '/'))
135  {
136  string::size_type parent;
137 
138  // Backtrack to find the parent directory
139  // Minimum of 3 characters: '/x/../'
140  // Strip it, provided it is above the top point
141  if
142  (
143  nChar > 2
144  && (parent = this->rfind('/', nChar-2)) != string::npos
145  && parent >= top
146  )
147  {
148  nChar = parent + 1; // Retain '/' from the parent
149  src += 2;
150  continue;
151  }
152 
153  // Bad resolution, eg 'abc/../../'
154  // Retain the sequence, but move the top to avoid it being
155  // considered a valid parent later
156  top = nChar + 2;
157  }
158  }
159  }
160  operator[](nChar++) = prev = c;
161  }
162 
163  // Remove trailing slash
164  if (nChar > 1 && operator[](nChar-1) == '/')
165  {
166  nChar--;
167  }
168 
169  this->resize(nChar);
170 
171  return (nChar != maxLen);
172 }
173 
174 
176 {
177  fileName fName(*this);
178  fName.clean();
179  return fName;
180 }
181 
182 
184 {
185  size_type i = rfind('/');
186 
187  if (i == npos)
188  {
189  return *this;
190  }
191  else
192  {
193  return substr(i+1, npos);
194  }
195 }
196 
197 
199 {
200  string cName = *this;
201 
202  const string caseStr(getEnv("FOAM_CASE"));
203 
204  const size_type i = find(caseStr);
205 
206  if (i == npos)
207  {
208  return cName;
209  }
210  else
211  {
212  return cName.replace(i, caseStr.size(), string("$FOAM_CASE"));
213  }
214 }
215 
216 
217 Foam::word Foam::fileName::name(const bool noExt) const
218 {
219  if (noExt)
220  {
221  size_type beg = rfind('/');
222  if (beg == npos)
223  {
224  beg = 0;
225  }
226  else
227  {
228  ++beg;
229  }
230 
231  size_type dot = rfind('.');
232  if (dot != npos && dot <= beg)
233  {
234  dot = npos;
235  }
236 
237  if (dot == npos)
238  {
239  return substr(beg, npos);
240  }
241  else
242  {
243  return substr(beg, dot - beg);
244  }
245  }
246  else
247  {
248  return this->name();
249  }
250 }
251 
252 
254 {
255  size_type i = rfind('/');
256 
257  if (i == npos)
258  {
259  return ".";
260  }
261  else if (i)
262  {
263  return substr(0, i);
264  }
265  else
266  {
267  return "/";
268  }
269 }
270 
271 
273 {
274  size_type i = find_last_of("./");
275 
276  if (i == npos || i == 0 || operator[](i) == '/')
277  {
278  return *this;
279  }
280  else
281  {
282  return substr(0, i);
283  }
284 }
285 
286 
288 {
289  size_type i = find_last_of("./");
290 
291  if (i == npos || i == 0 || operator[](i) == '/')
292  {
293  return word::null;
294  }
295  else
296  {
297  return substr(i+1, npos);
298  }
299 }
300 
301 
302 Foam::wordList Foam::fileName::components(const char delimiter) const
303 {
304  DynamicList<word> wrdList(20);
305 
306  size_type beg=0, end=0;
307 
308  while ((end = find(delimiter, beg)) != npos)
309  {
310  // Avoid empty element (caused by doubled slashes)
311  if (beg < end)
312  {
313  wrdList.append(substr(beg, end-beg));
314  }
315  beg = end + 1;
316  }
317 
318  // Avoid empty trailing element
319  if (beg < size())
320  {
321  wrdList.append(substr(beg, npos));
322  }
323 
324  // Transfer to wordList
325  return wordList(move(wrdList));
326 }
327 
328 
330 (
331  const size_type cmpt,
332  const char delimiter
333 ) const
334 {
335  return components(delimiter)[cmpt];
336 }
337 
338 
339 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
340 
342 {
343  string::operator=(str);
344 }
345 
346 
348 {
349  string::operator=(move(str));
350 }
351 
352 
354 {
355  string::operator=(str);
356 }
357 
358 
359 void Foam::fileName::operator=(const string& str)
360 {
361  string::operator=(str);
362  stripInvalid();
363 }
364 
365 
366 void Foam::fileName::operator=(const std::string& str)
367 {
368  string::operator=(str);
369  stripInvalid();
370 }
371 
372 
373 void Foam::fileName::operator=(const char* str)
374 {
375  string::operator=(str);
376  stripInvalid();
377 }
378 
379 
380 // * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * //
381 
382 Foam::fileName Foam::operator/(const string& a, const string& b)
383 {
384  if (a.size()) // First string non-null
385  {
386  if (b.size()) // Second string non-null
387  {
388  return fileName(a + '/' + b);
389  }
390  else // Second string null
391  {
392  return a;
393  }
394  }
395  else // First string null
396  {
397  if (b.size()) // Second string non-null
398  {
399  return b;
400  }
401  else // Second string null
402  {
403  return fileName();
404  }
405  }
406 }
407 
408 
409 // ************************************************************************* //
string getEnv(const word &)
Return environment variable of given name.
Definition: POSIX.C:97
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
static int debug
Definition: fileName.H:94
A class for handling file names.
Definition: fileName.H:79
fileName()
Construct null.
Definition: fileNameI.H:52
bool clean()
Cleanup file name.
Definition: fileName.C:81
string caseName() const
Return file name (part beyond last /), substitute for FOAM_CASE.
Definition: fileName.C:198
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:287
void dot(FieldField< Field1, typename innerProduct< Type1, Type2 >::type > &f, const FieldField< Field1, Type1 > &f1, const FieldField< Field2, Type2 > &f2)
wordList components(const char delimiter='/') const
Return path components as wordList.
Definition: fileName.C:302
fileType type(const bool checkVariants=true, const bool followLink=true) const
Return the file type: file, directory, undefined or.
Definition: fileName.C:52
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
const dimensionedScalar b
Wien displacement law constant: default SI units: [m K].
Definition: createFields.H:27
bool isAbsolute() const
Return true if file name is absolute.
Definition: fileName.C:61
A class for handling words, derived from string.
Definition: word.H:59
int debugSwitch(const char *name, const int defaultValue=0)
Lookup debug switch or add default value.
Definition: debug.C:178
word name() const
Return file name (part beyond last /)
Definition: fileName.C:183
triSurfaceToAgglom resize(surfacesMesh.size())
DynamicList< T, SizeInc, SizeMult, SizeDiv > & append(const T &)
Append an element at the end of the list.
Definition: DynamicListI.H:296
static const word null
An empty word.
Definition: word.H:77
fileType
Enumeration of file types.
Definition: fileName.H:66
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:330
fileName & toAbsolute()
Convert from relative to absolute.
Definition: fileName.C:67
string()
Construct null.
Definition: stringI.H:30
labelList f(nPoints)
List< word > wordList
A List of words.
Definition: fileName.H:54
string & replace(const string &oldStr, const string &newStr, size_type start=0)
Replace first occurrence of sub-string oldStr with newStr.
Definition: string.C:56
const dimensionedScalar c
Speed of light in a vacuum.
fileType type(const fileName &, const bool checkVariants=true, const bool followLink=true)
Return the file type: directory or file.
Definition: POSIX.C:488
void operator=(const string &)
Definition: stringI.H:229
fileName lessExt() const
Return file name without extension (part before last .)
Definition: fileName.C:272
fileName cwd()
Return current working directory path name.
Definition: POSIX.C:241
const dimensionedScalar c1
First radiation constant: default SI units: [W/m^2].
fileName path() const
Return directory path name (part before last /)
Definition: fileName.C:253
A class for handling character strings derived from std::string.
Definition: string.H:74
void operator=(const fileName &)
Definition: fileName.C:341