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-------------------------------------------------------------------------------
11License
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
45inline Foam::fileName::fileName(const string& s, bool doStrip)
46:
47 string(s)
48{
49 if (doStrip)
50 {
52 }
53}
54
55
56inline Foam::fileName::fileName(string&& s, bool doStrip)
57:
58 string(std::move(s))
59{
60 if (doStrip)
61 {
63 }
64}
65
66
67inline Foam::fileName::fileName(const std::string& s, bool doStrip)
68:
69 string(s)
70{
71 if (doStrip)
72 {
74 }
75}
76
77
78inline Foam::fileName::fileName(std::string&& s, bool doStrip)
79:
80 string(std::move(s))
81{
82 if (doStrip)
83 {
85 }
86}
87
88
89inline 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
102inline 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
136inline 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
158inline bool Foam::fileName::isAbsolute() const
159{
160 return isAbsolute(*this);
161}
162
163
164inline bool Foam::fileName::isBackup() const
165{
166 return isBackup(*this);
167}
168
169
170inline bool Foam::fileName::hasPath() const
171{
172 return string::hasPath();
173}
174
175
176inline 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
199inline 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
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
310inline Foam::fileName& Foam::fileName::operator=(const std::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
327{
328 assign(str);
329 stripInvalid();
330 return *this;
331}
332
333
334// ************************************************************************* //
bool valid() const
True if all internal ids are non-negative.
A class for handling file names.
Definition: fileName.H:76
bool isAbsolute() const
Definition: fileNameI.H:158
fileName()=default
Default construct.
word name() const
Return basename (part beyond last /), including its extension.
Definition: fileNameI.H:212
bool removePath()
Remove leading path, returning true if string changed.
Definition: fileNameI.H:243
bool hasPath() const
Return true if it contains a '/' character.
Definition: fileNameI.H:170
fileName & operator=(const fileName &str)
Copy assignment, no character validation required.
Definition: fileNameI.H:258
fileName lessExt() const
Return file name without extension (part before last .)
Definition: fileNameI.H:230
word ext() const
Return file name extension (part after last .)
Definition: fileNameI.H:218
fileName path() const
Return directory path name (part before last /)
Definition: fileNameI.H:193
void stripInvalid()
Strip invalid characters.
Definition: fileNameI.H:113
bool isBackup() const
Return true if file name ends with "~", ".bak", ".old", ".save".
Definition: fileNameI.H:164
word nameLessExt() const
Return basename, without extension.
Definition: fileNameI.H:224
A class for handling character strings derived from std::string.
Definition: string.H:79
bool removePath()
Remove leading path, returning true if string changed.
Definition: stringI.H:82
bool hasPath() const
Return true if it contains a '/' character.
Definition: stringI.H:50
word ext() const
Return file name extension (part after last .)
Definition: string.C:45
A class for handling words, derived from Foam::string.
Definition: word.H:68
fileName path(UMean.rootPath()/UMean.caseName()/"graphs"/UMean.instance())
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))
bool isspace(char c) noexcept
Test for whitespace (C-locale)
Definition: char.H:65