Loading...
Searching...
No Matches
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-------------------------------------------------------------------------------
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/>.
27\*---------------------------------------------------------------------------*/
28
29#include <iostream> // For std::cerr
30
31// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
33inline Foam::fileName::fileName(const word& s)
34:
35 string(s)
36{}
37
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)
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
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
158inline bool Foam::fileName::isAbsolute() const
159{
160 return fileName::isAbsolute(*this);
161}
162
164inline bool Foam::fileName::isBackup() const
165{
166 return isBackup(*this);
167}
168
170inline bool Foam::fileName::has_path() const
171{
172 return contains('/');
173}
174
175
176inline 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
199inline 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
212inline Foam::word Foam::fileName::name() const
213{
214 return fileName::name(*this);
215}
216
219{
220 return string::ext();
221}
222
224inline 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
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)
265 assign(str);
266 }
267 return *this;
268}
269
270
271inline Foam::fileName& Foam::fileName::operator=(fileName&& str)
272{
273 // Self-assignment is a no-op
274 if (this != &str)
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
296inline Foam::fileName& Foam::fileName::operator=(const string& str)
298 assign(str);
299 stripInvalid();
300 return *this;
301}
302
303
304inline Foam::fileName& Foam::fileName::operator=(string&& str)
306 assign(std::move(str));
307 stripInvalid();
308 return *this;
309}
310
311
312inline Foam::fileName& Foam::fileName::operator=(const std::string& str)
314 assign(str);
315 stripInvalid();
316 return *this;
317}
318
319
320inline Foam::fileName& Foam::fileName::operator=(std::string&& str)
322 assign(std::move(str));
323 stripInvalid();
324 return *this;
325}
326
327
328inline 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:75
bool isAbsolute() const
Return true if filename is absolute, which means it starts with a '/' or '\' or (windows-only) with a...
Definition fileNameI.H:151
bool assign(const token &tok)
Assign from word or string token.
Definition fileNameIO.C:35
fileName & replace_ext(const word &ending)
Remove extension (if any) and append a new one.
Definition fileNameI.H:230
fileName()=default
Default construct.
bool has_path() const
True if it contains a '/' character.
Definition fileNameI.H:163
word name() const
Return basename (part beyond last /), including its extension.
Definition fileNameI.H:205
fileName & operator=(const fileName &str)
Copy assignment, no character validation required.
Definition fileNameI.H:253
static bool isBackup(const std::string &str)
Return true if string ends with "~", ".bak", ".old", ".save".
Definition fileName.C:279
fileName lessExt() const
Return file name without extension (part before last .).
Definition fileNameI.H:238
static bool valid(char c)
Is this character valid for a fileName?
Definition fileNameI.H:95
word ext() const
Return file name extension (part after last .).
Definition fileNameI.H:211
fileName path() const
Return directory path name (part before last /).
Definition fileNameI.H:186
static int debug
Debugging.
Definition fileName.H:101
void stripInvalid()
Strip invalid characters.
Definition fileNameI.H:106
bool isBackup() const
Return true if file name ends with "~", ".bak", ".old", ".save".
Definition fileNameI.H:157
static int allowSpaceInFileName
Allow space character in fileName. To be used with caution.
Definition fileName.H:106
word stem() const
Return basename, without extension.
Definition fileNameI.H:217
A class for handling character strings derived from std::string.
Definition string.H:76
bool remove_ext()
Remove extension, return true if string changed.
Definition stringI.H:93
string()=default
Default construct.
static bool stripInvalid(std::string &str)
Strip invalid characters from the given string.
Definition stringI.H:164
bool contains(char c) const noexcept
True if string contains given character (cf. C++23).
Definition string.H:412
bool removeRepeated(const char character)
Remove repeated characters.
Definition string.C:173
word ext() const
Return file name extension (part after last .).
Definition string.C:38
static std::string::size_type find_ext(const std::string &str)
Find position of a file extension dot, return npos on failure.
Definition stringI.H:24
bool removeEnd(const std::string &text)
Remove the given text from the end of the string.
Definition string.C:222
A class for handling words, derived from Foam::string.
Definition word.H:66
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))
Namespace for handling debugging switches.
Definition debug.C:45
constexpr bool isspace(char c) noexcept
Test for whitespace (C-locale only).
Definition char.H:77