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-2019 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
139  (
140  (!str.empty() && str.front() == '/') // ie, str.starts_with('/')
141  #ifdef _WIN32
142  ||
143  (
144  // Eg, d:/path or \\machine/path
145  (str.length() > 2) &&
146  (
147  (str[1] == ':' && str[2] == '/')
148  || (str[0] == '\\' && str[1] == '\\')
149  )
150  )
151  #endif
152  );
153 }
154 
155 
156 inline bool Foam::fileName::isAbsolute() const
157 {
158  return isAbsolute(*this);
159 }
160 
161 
162 inline bool Foam::fileName::isBackup() const
163 {
164  return isBackup(*this);
165 }
166 
167 
168 inline bool Foam::fileName::hasPath() const
169 {
170  return string::hasPath();
171 }
172 
173 
174 inline bool Foam::fileName::hasExt() const
175 {
176  return string::hasExt();
177 }
178 
179 
180 inline bool Foam::fileName::hasExt(const word& ending) const
181 {
182  return string::hasExt(ending);
183 }
184 
185 
186 inline std::string Foam::fileName::path(const std::string& str)
187 {
188  const auto i = str.rfind('/');
189 
190  if (i == npos)
191  {
192  return ".";
193  }
194  else if (i)
195  {
196  return str.substr(0, i);
197  }
198 
199  return "/";
200 }
201 
202 
204 {
205  return path(*this);
206 }
207 
208 
209 inline std::string Foam::fileName::name(const std::string& str)
210 {
211  const auto i = str.rfind('/');
212 
213  if (npos == i)
214  {
215  return str;
216  }
217 
218  return str.substr(i+1);
219 }
220 
221 
223 {
224  return fileName::name(*this);
225 }
226 
227 
229 {
230  return string::ext();
231 }
232 
233 
235 {
236  return nameLessExt(*this);
237 }
238 
239 
241 {
242  const auto i = find_ext();
243 
244  if (i == npos)
245  {
246  return *this;
247  }
248 
249  return substr(0, i);
250 }
251 
252 
254 {
255  return string::removePath();
256 }
257 
258 
260 {
261  return string::removeExt();
262 }
263 
264 
266 {
267  string::ext(ending);
268  return *this;
269 }
270 
271 
272 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
273 
275 {
276  // Self-assignment is a no-op
277  if (this != &str)
278  {
279  assign(str);
280  }
281  return *this;
282 }
283 
284 
286 {
287  // Self-assignment is a no-op
288  if (this != &str)
289  {
290  assign(std::move(str));
291  }
292  return *this;
293 }
294 
295 
297 {
298  assign(str);
299  return *this;
300 }
301 
302 
304 {
305  assign(std::move(str));
306  return *this;
307 }
308 
309 
310 inline Foam::fileName& Foam::fileName::operator=(const string& str)
311 {
312  assign(str);
313  stripInvalid();
314  return *this;
315 }
316 
317 
319 {
320  assign(std::move(str));
321  stripInvalid();
322  return *this;
323 }
324 
325 
326 inline Foam::fileName& Foam::fileName::operator=(const std::string& str)
327 {
328  assign(str);
329  stripInvalid();
330  return *this;
331 }
332 
333 
334 inline Foam::fileName& Foam::fileName::operator=(std::string&& str)
335 {
336  assign(std::move(str));
337  stripInvalid();
338  return *this;
339 }
340 
341 
343 {
344  assign(str);
345  stripInvalid();
346  return *this;
347 }
348 
349 
350 // ************************************************************************* //
Foam::expressions::patchExpr::debug
int debug
Static debugging option.
Foam::string::hasExt
bool hasExt() const
Return true if it has an extension or simply ends with a '.'.
Definition: stringI.H:56
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::fileName
A class for handling file names.
Definition: fileName.H:69
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::string::removeExt
bool removeExt()
Remove extension, returning true if string changed.
Definition: stringI.H:76
Foam::fileName::path
fileName path() const
Return directory path name (part before last /)
Definition: fileNameI.H:203
Foam::fileName::isAbsolute
bool isAbsolute() const
Return true if file name is absolute (starts with a '/')
Definition: fileNameI.H:156
Foam::fileName::nameLessExt
word nameLessExt() const
Return basename, without extension.
Definition: fileNameI.H:234
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:350
Foam::fileName::lessExt
fileName lessExt() const
Return file name without extension (part before last .)
Definition: fileNameI.H:240
Foam::string
A class for handling character strings derived from std::string.
Definition: string.H:73
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:162
Foam::fileName::name
word name() const
Return basename (part beyond last /), including its extension.
Definition: fileNameI.H:222
Foam::fileName::hasPath
bool hasPath() const
Return true if it contains a '/' character.
Definition: fileNameI.H:168
Foam::fileName::hasExt
bool hasExt() const
Return true if it has an extension or simply ends with a '.'.
Definition: fileNameI.H:174
Foam::string::ext
word ext() const
Return file name extension (part after last .)
Definition: string.C:45
Foam::fileName::removeExt
bool removeExt()
Remove extension, returning true if string changed.
Definition: fileNameI.H:259
Foam::string::hasPath
bool hasPath() const
Return true if it contains a '/' character.
Definition: stringI.H:50
Foam::fileName::fileName
fileName()=default
Construct null.
Foam::fileName::ext
word ext() const
Return file name extension (part after last .)
Definition: fileNameI.H:228
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:62
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:274
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::isspace
bool isspace(char c)
Test for horizontal whitespace.
Definition: char.H:63
Foam::fileName::removePath
bool removePath()
Remove leading path, returning true if string changed.
Definition: fileNameI.H:253