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