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-2021 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 
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  {
95  stripInvalid();
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 
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  }
129 
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 
157 
158 inline bool Foam::fileName::isAbsolute() const
159 {
160  return isAbsolute(*this);
161 }
162 
163 
164 inline bool Foam::fileName::isBackup() const
165 {
166  return isBackup(*this);
167 }
168 
169 
170 inline bool Foam::fileName::hasPath() const
171 {
172  return string::hasPath();
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 == npos)
181  {
182  return ".";
183  }
184  else if (i)
185  {
186  return str.substr(0, i);
187  }
188 
189  return "/";
190 }
191 
192 
194 {
195  return 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 (npos == i)
204  {
205  return str;
206  }
207 
208  return str.substr(i+1);
209 }
210 
211 
213 {
214  return fileName::name(*this);
215 }
216 
217 
219 {
220  return string::ext();
221 }
222 
223 
225 {
226  return nameLessExt(*this);
227 }
228 
229 
231 {
232  const auto i = find_ext();
233 
234  if (i == npos)
235  {
236  return *this;
237  }
238 
239  return substr(0, i);
240 }
241 
242 
244 {
245  return string::removePath();
246 }
247 
248 
250 {
251  string::ext(ending);
252  return *this;
253 }
254 
255 
256 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
257 
259 {
260  // Self-assignment is a no-op
261  if (this != &str)
262  {
263  assign(str);
264  }
265  return *this;
266 }
267 
268 
270 {
271  // Self-assignment is a no-op
272  if (this != &str)
273  {
274  assign(std::move(str));
275  }
276  return *this;
277 }
278 
279 
281 {
282  assign(str);
283  return *this;
284 }
285 
286 
288 {
289  assign(std::move(str));
290  return *this;
291 }
292 
293 
294 inline Foam::fileName& Foam::fileName::operator=(const string& str)
295 {
296  assign(str);
297  stripInvalid();
298  return *this;
299 }
300 
301 
303 {
304  assign(std::move(str));
305  stripInvalid();
306  return *this;
307 }
308 
309 
310 inline Foam::fileName& Foam::fileName::operator=(const std::string& str)
311 {
312  assign(str);
313  stripInvalid();
314  return *this;
315 }
316 
317 
318 inline Foam::fileName& Foam::fileName::operator=(std::string&& str)
319 {
320  assign(std::move(str));
321  stripInvalid();
322  return *this;
323 }
324 
325 
327 {
328  assign(str);
329  stripInvalid();
330  return *this;
331 }
332 
333 
334 // ************************************************************************* //
Foam::expressions::patchExpr::debug
int debug
Static debugging option.
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::fileName
A class for handling file names.
Definition: fileName.H:73
s
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))
Definition: gmvOutputSpray.H:25
Foam::isspace
bool isspace(char c) noexcept
Test for whitespace (C-locale)
Definition: char.H:65
Foam::fileName::path
fileName path() const
Return directory path name (part before last /)
Definition: fileNameI.H:193
Foam::fileName::isAbsolute
bool isAbsolute() const
Definition: fileNameI.H:158
Foam::fileName::nameLessExt
word nameLessExt() const
Return basename, without extension.
Definition: fileNameI.H:224
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
Foam::fileName::lessExt
fileName lessExt() const
Return file name without extension (part before last .)
Definition: fileNameI.H:230
Foam::string
A class for handling character strings derived from std::string.
Definition: string.H:76
Foam::fileName::valid
static bool valid(char c)
Is this character valid for a fileName?
Definition: fileNameI.H:102
Foam::fileName::isBackup
bool isBackup() const
Return true if file name ends with "~", ".bak", ".old", ".save".
Definition: fileNameI.H:164
Foam::fileName::name
word name() const
Return basename (part beyond last /), including its extension.
Definition: fileNameI.H:212
Foam::fileName::hasPath
bool hasPath() const
Return true if it contains a '/' character.
Definition: fileNameI.H:170
Foam::string::ext
word ext() const
Return file name extension (part after last .)
Definition: string.C:45
Foam::string::hasPath
bool hasPath() const
Return true if it contains a '/' character.
Definition: stringI.H:50
Foam::fileName::fileName
fileName()=default
Default construct.
Foam::fileName::ext
word ext() const
Return file name extension (part after last .)
Definition: fileNameI.H:218
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::string::removePath
bool removePath()
Remove leading path, returning true if string changed.
Definition: stringI.H:82
Foam::fileName::stripInvalid
void stripInvalid()
Strip invalid characters.
Definition: fileNameI.H:113
Foam::fileName::operator=
fileName & operator=(const fileName &str)
Copy assignment, no character validation required.
Definition: fileNameI.H:258
path
fileName path(UMean.rootPath()/UMean.caseName()/"graphs"/UMean.instance())
Foam::constant::universal::c
const dimensionedScalar c
Speed of light in a vacuum.
Foam::FieldOps::assign
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:35
Foam::fileName::removePath
bool removePath()
Remove leading path, returning true if string changed.
Definition: fileNameI.H:243