fileNameI.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 | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011 OpenFOAM Foundation
9  Copyright (C) 2016-2022 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include <iostream> // For std::cerr
30 
31 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
32 
33 inline Foam::fileName::fileName(const word& s)
34 :
35  string(s)
36 {}
37 
38 
40 :
41  string(std::move(s))
42 {}
43 
44 
45 inline Foam::fileName::fileName(const string& s, bool doStrip)
46 :
47  string(s)
48 {
49  if (doStrip)
50  {
51  stripInvalid();
52  }
53 }
54 
55 
56 inline Foam::fileName::fileName(string&& s, bool doStrip)
57 :
58  string(std::move(s))
59 {
60  if (doStrip)
61  {
62  stripInvalid();
63  }
64 }
65 
66 
67 inline Foam::fileName::fileName(const std::string& s, bool doStrip)
68 :
69  string(s)
70 {
71  if (doStrip)
72  {
73  stripInvalid();
74  }
75 }
76 
77 
78 inline Foam::fileName::fileName(std::string&& s, bool doStrip)
79 :
80  string(std::move(s))
81 {
82  if (doStrip)
83  {
84  stripInvalid();
85  }
86 }
87 
88 
89 inline Foam::fileName::fileName(const char* s, bool doStrip)
90 :
91  string(s)
92 {
93  if (doStrip)
94  {
96  }
97 }
98 
99 
100 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
101 
102 inline bool Foam::fileName::valid(char c)
103 {
104  return
105  (
106  c != '"' // string quote
107  && c != '\'' // string quote
108  && (!isspace(c) || (allowSpaceInFileName && c == ' '))
109  );
110 }
111 
112 
113 inline void Foam::fileName::stripInvalid()
114 {
115  // Only strip when debug is active (potentially costly operation)
116  if (debug && string::stripInvalid<fileName>(*this))
117  {
118  std::cerr
119  << "fileName::stripInvalid() called for invalid fileName "
120  << this->c_str() << std::endl;
121 
122  if (debug > 1)
123  {
124  std::cerr
125  << " For debug level (= " << debug
126  << ") > 1 this is considered fatal" << std::endl;
127  std::exit(1);
128  }
130  removeRepeated('/');
131  removeEnd('/');
132  }
133 }
134 
135 
136 inline bool Foam::fileName::isAbsolute(const std::string& str)
137 {
138  return !str.empty() &&
139  (
140  // Starts with '/', but also accept '\\' since it will be
141  // converted to a generic '/' or it is part of a (windows)
142  // UNC '\\server-name\path'
143  // - accept even on non-windows systems
144 
145  (str[0] == '/' || str[0] == '\\')
146 
147 #ifdef _WIN32
148  // Filesytem root - eg, d:/path or d:\path
149  || (
150  (str.length() > 2 && str[1] == ':')
151  && (str[2] == '/' || str[2] == '\\')
152  )
153 #endif
154  );
155 }
156 
158 inline bool Foam::fileName::isAbsolute() const
159 {
160  return fileName::isAbsolute(*this);
161 }
162 
164 inline bool Foam::fileName::isBackup() const
165 {
166  return isBackup(*this);
167 }
168 
170 inline bool Foam::fileName::has_path() const
171 {
172  return contains('/');
173 }
174 
175 
176 inline std::string Foam::fileName::path(const std::string& str)
177 {
178  const auto i = str.rfind('/');
179 
180  if (i == std::string::npos)
181  {
182  return ".";
183  }
184  else if (i)
185  {
186  return str.substr(0, i);
187  }
188 
189  return "/";
190 }
191 
194 {
195  return fileName::path(*this);
196 }
197 
198 
199 inline std::string Foam::fileName::name(const std::string& str)
200 {
201  const auto i = str.rfind('/');
202 
203  if (i == std::string::npos)
204  {
205  return str;
206  }
207 
208  return str.substr(i+1);
209 }
210 
212 inline Foam::word Foam::fileName::name() const
213 {
214  return fileName::name(*this);
215 }
216 
219 {
220  return string::ext();
221 }
222 
224 inline Foam::word Foam::fileName::stem() const
225 {
226  return fileName::stem(*this);
227 }
228 
229 
231 {
232  string::ext(ending);
233  return *this;
234 }
235 
236 
237 inline Foam::fileName& Foam::fileName::replace_ext(const word& ending)
238 {
240  string::ext(ending);
241  return *this;
242 }
243 
244 
246 {
247  const auto i = find_ext();
248 
249  if (i == std::string::npos)
250  {
251  return *this;
252  }
254  return substr(0, i);
255 }
256 
257 
258 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
259 
261 {
262  // Self-assignment is a no-op
263  if (this != &str)
264  {
265  assign(str);
266  }
267  return *this;
268 }
269 
270 
271 inline Foam::fileName& Foam::fileName::operator=(fileName&& str)
272 {
273  // Self-assignment is a no-op
274  if (this != &str)
275  {
276  assign(std::move(str));
277  }
278  return *this;
279 }
280 
281 
283 {
284  assign(str);
285  return *this;
286 }
287 
288 
290 {
291  assign(std::move(str));
292  return *this;
293 }
294 
295 
296 inline Foam::fileName& Foam::fileName::operator=(const string& str)
297 {
298  assign(str);
299  stripInvalid();
300  return *this;
301 }
302 
303 
304 inline Foam::fileName& Foam::fileName::operator=(string&& str)
305 {
306  assign(std::move(str));
307  stripInvalid();
308  return *this;
309 }
310 
311 
312 inline Foam::fileName& Foam::fileName::operator=(const std::string& str)
313 {
314  assign(str);
315  stripInvalid();
316  return *this;
317 }
318 
319 
320 inline Foam::fileName& Foam::fileName::operator=(std::string&& str)
321 {
322  assign(std::move(str));
323  stripInvalid();
324  return *this;
325 }
326 
327 
328 inline Foam::fileName& Foam::fileName::operator=(const char* str)
329 {
330  assign(str);
331  stripInvalid();
332  return *this;
333 }
334 
335 
336 // ************************************************************************* //
A class for handling file names.
Definition: fileName.H:72
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
word stem() const
Return basename, without extension.
Definition: fileNameI.H:217
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
fileName & replace_ext(const word &ending)
Remove extension (if any) and append a new one.
Definition: fileNameI.H:230
word ext() const
Return file name extension (part after last .)
Definition: fileNameI.H:211
bool has_path() const
True if it contains a &#39;/&#39; character.
Definition: fileNameI.H:163
fileName()=default
Default construct.
void stripInvalid()
Strip invalid characters.
Definition: fileNameI.H:106
bool isAbsolute() const
Return true if filename is absolute, which means it starts with a &#39;/&#39; or &#39;\&#39; or (windows-only) with a...
Definition: fileNameI.H:151
A class for handling words, derived from Foam::string.
Definition: word.H:63
word ext() const
Return file name extension (part after last .)
Definition: string.C:38
word name() const
Return basename (part beyond last /), including its extension.
Definition: fileNameI.H:205
void assign(Field< Tout > &result, const Field< T1 > &a, const UnaryOp &op)
Populate a field as the result of a unary operation on an input.
Definition: FieldOps.C:28
int debug
Static debugging option.
static bool valid(char c)
Is this character valid for a fileName?
Definition: fileNameI.H:95
bool isBackup() const
Return true if file name ends with "~", ".bak", ".old", ".save".
Definition: fileNameI.H:157
const dimensionedScalar c
Speed of light in a vacuum.
bool isspace(char c) noexcept
Test for whitespace (C-locale)
Definition: char.H:69
fileName lessExt() const
Return file name without extension (part before last .)
Definition: fileNameI.H:238
fileName path() const
Return directory path name (part before last /)
Definition: fileNameI.H:186
gmvFile<< "tracers "<< particles.size()<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().x()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().y()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().z()<< " ";}gmvFile<< nl;forAll(lagrangianScalarNames, i){ word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject(name, runTime.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
A class for handling character strings derived from std::string.
Definition: string.H:72
fileName & operator=(const fileName &str)
Copy assignment, no character validation required.
Definition: fileNameI.H:253
bool remove_ext()
Remove extension, return true if string changed.
Definition: stringI.H:93