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