Commit 6c91048e authored by Mark Olesen's avatar Mark Olesen
Browse files

ENH: fileName hasPath(), removePath() methods

- improved move constructors/assignments for fileName, string, etc
parent 3963cd95
......@@ -621,12 +621,18 @@ int main(int argc, char *argv[])
<< "pathName.name() = >" << pathName.name() << "<\n"
<< "pathName.path() = " << pathName.path() << nl
<< "pathName.ext() = >" << pathName.ext() << "<\n"
<< "pathName.name(true) = >" << pathName.name(true) << "<\n";
<< "pathName.nameLessExt= >" << pathName.nameLessExt() << "<\n";
Info<< "pathName.components() = " << pathName.components() << nl
<< "pathName.component(2) = " << pathName.component(2) << nl
<< endl;
Info<< "hasPath = " << Switch(pathName.hasPath()) << nl;
pathName.removePath();
Info<< "removed path = " << pathName << nl;
Info<< nl << nl;
// try with different combination
// The final one should emit warnings
for (label start = 0; start <= wrdList.size(); ++start)
......
......@@ -36,6 +36,7 @@ Description
#include "uint.H"
#include "scalar.H"
#include "Switch.H"
#include "fileName.H"
#include "stringList.H"
using namespace Foam;
......@@ -64,6 +65,34 @@ int main(int argc, char *argv[])
subDict.add("value2", "test2");
dict.add("FOAM_RUN", subDict);
if (false)
{
typedef std::string inputType;
typedef string outputType;
inputType in1("move-construct-from");
Info<<"move construct from " << in1.length() << nl;
outputType out1(std::move(in1));
Info<<"after move construct "
<< out1.size() << ", " << in1.size() << nl;
in1 = "move-assign-from";
out1 = "some-text-rubbish";
out1.resize(10);
Info<<"move assign from " << in1.length() << nl;
out1 = std::move(in1);
Info<<"after move assign "
<< out1.size() << ", " << in1.size() << nl;
return 0;
}
// basic expansions
{
......
......@@ -24,10 +24,10 @@ License
\*---------------------------------------------------------------------------*/
#include "fileName.H"
#include "wordRe.H"
#include "wordList.H"
#include "DynamicList.H"
#include "OSspecific.H"
#include "wordRe.H"
#include "fileOperation.H"
#include "stringOps.H"
......@@ -142,7 +142,7 @@ bool Foam::fileName::isBackup(const std::string& str)
return false;
}
const std::string ending = str.substr(dot+1, npos);
const std::string ending = str.substr(dot+1);
if (ending.empty())
{
......@@ -159,44 +159,40 @@ bool Foam::fileName::isBackup(const std::string& str)
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::fileName::fileName(const UList<word>& lst)
Foam::fileName::fileName(const UList<word>& list)
{
// Estimate overall size
size_type sz = lst.size(); // Approx number of '/' needed
for (const word& item : lst)
size_type len = 0;
for (const word& item : list)
{
sz += item.size();
len += 1 + item.length(); // Include space for '/' needed
}
reserve(sz);
reserve(len);
sz = 0;
for (const word& item : lst)
for (const word& item : list)
{
if (item.size())
if (item.length())
{
if (sz++) operator+=('/');
if (length()) operator+=('/');
operator+=(item);
}
}
}
Foam::fileName::fileName(std::initializer_list<word> lst)
Foam::fileName::fileName(std::initializer_list<word> list)
{
// Estimate overall size
size_type sz = lst.size(); // Approx number of '/' needed
for (const word& item : lst)
size_type len = 0;
for (const word& item : list)
{
sz += item.size();
len += 1 + item.length(); // Include space for '/' needed
}
reserve(sz);
reserve(len);
sz = 0;
for (const word& item : lst)
for (const word& item : list)
{
if (item.size())
if (item.length())
{
if (sz++) operator+=('/');
if (length()) operator+=('/');
operator+=(item);
}
}
......@@ -238,11 +234,11 @@ bool Foam::fileName::clean(std::string& str)
}
// Number of output characters
std::string::size_type nChar = top+1;
auto nChar = top+1;
const string::size_type maxLen = str.size();
const auto maxLen = str.size();
for (string::size_type src = nChar; src < maxLen; /*nil*/)
for (auto src = nChar; src < maxLen; /*nil*/)
{
const char c = str[src++];
......@@ -329,29 +325,10 @@ Foam::fileName Foam::fileName::clean() const
}
std::string Foam::fileName::name(const std::string& str)
{
const auto beg = str.rfind('/');
if (beg == npos)
{
return str;
}
return str.substr(beg+1);
}
Foam::word Foam::fileName::name() const
{
return fileName::name(*this);
}
std::string Foam::fileName::nameLessExt(const std::string& str)
{
size_type beg = str.rfind('/');
size_type dot = str.rfind('.');
auto beg = str.rfind('/');
auto dot = str.rfind('.');
if (beg == npos)
{
......@@ -376,35 +353,6 @@ std::string Foam::fileName::nameLessExt(const std::string& str)
}
Foam::word Foam::fileName::nameLessExt() const
{
return nameLessExt(*this);
}
std::string Foam::fileName::path(const std::string& str)
{
const auto i = str.rfind('/');
if (i == npos)
{
return ".";
}
else if (i)
{
return str.substr(0, i);
}
return "/";
}
Foam::fileName Foam::fileName::path() const
{
return path(*this);
}
Foam::fileName Foam::fileName::relative(const fileName& parent) const
{
const auto top = parent.size();
......@@ -424,38 +372,6 @@ Foam::fileName Foam::fileName::relative(const fileName& parent) const
}
Foam::fileName Foam::fileName::lessExt() const
{
const auto i = find_ext();
if (i == npos)
{
return *this;
}
return substr(0, i);
}
Foam::word Foam::fileName::ext() const
{
return string::ext();
}
Foam::fileName& Foam::fileName::ext(const word& ending)
{
string::ext(ending);
return *this;
}
bool Foam::fileName::hasExt(const word& ending) const
{
return string::hasExt(ending);
}
bool Foam::fileName::hasExt(const wordRe& ending) const
{
return string::hasExt(ending);
......@@ -500,39 +416,6 @@ Foam::word Foam::fileName::component
// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
void Foam::fileName::operator=(const fileName& str)
{
assign(str);
}
void Foam::fileName::operator=(const word& str)
{
assign(str);
}
void Foam::fileName::operator=(const string& str)
{
assign(str);
stripInvalid();
}
void Foam::fileName::operator=(const std::string& str)
{
assign(str);
stripInvalid();
}
void Foam::fileName::operator=(const char* str)
{
assign(str);
stripInvalid();
}
Foam::fileName& Foam::fileName::operator/=(const string& other)
{
fileName& s = *this;
......
......@@ -102,28 +102,40 @@ public:
// Constructors
//- Construct null
inline fileName();
fileName() = default;
//- Construct as copy
inline fileName(const fileName& fn);
//- Copy construct
fileName(const fileName&) = default;
//- Construct as copy of word
inline fileName(const word& w);
//- Move construct
fileName(fileName&&) = default;
//- Construct as copy of string
inline fileName(const string& s, const bool doStripInvalid=true);
//- Copy construct from word
inline fileName(const word& s);
//- Construct as copy of std::string
inline fileName(const std::string& s, const bool doStripInvalid=true);
//- Move construct from word
inline fileName(word&& s);
//- Construct as copy of character array
inline fileName(const char* s, const bool doStripInvalid=true);
//- Copy construct from string
inline fileName(const string& s, bool doStrip=true);
//- Move construct from string
inline fileName(string&& s, bool doStrip=true);
//- Copy construct from std::string
inline fileName(const std::string& s, bool doStrip=true);
//- Move construct from std::string
inline fileName(std::string&& s, bool doStrip=true);
//- Copy construct from character array
inline fileName(const char* s, bool doStrip=true);
//- Construct by concatenating elements of wordList separated by '/'
explicit fileName(const UList<word>& lst);
explicit fileName(const UList<word>& list);
//- Construct by concatenating words separated by '/'
explicit fileName(std::initializer_list<word> lst);
explicit fileName(std::initializer_list<word> list);
//- Construct from Istream
......@@ -226,17 +238,17 @@ public:
// "/foo/bar" "bar" "bar"
// "/foo/bar/" "" "bar"
// \endverbatim
static std::string name(const std::string& str);
inline static std::string name(const std::string& str);
//- Return basename (part beyond last /), including its extension
word name() const;
inline word name() const;
//- Return basename, without extension
// The result normally corresponds to a Foam::word
static std::string nameLessExt(const std::string& str);
//- Return basename, without extension
word nameLessExt() const;
inline word nameLessExt() const;
//- Return basename, optionally without extension
// \deprecated in favour of name() or nameLessExt() which describe
......@@ -260,30 +272,36 @@ public:
// "/foo/bar" "/foo" "/foo"
// "/foo/bar/" "/foo/bar/" "/foo"
// \endverbatim
static std::string path(const std::string& str);
inline static std::string path(const std::string& str);
//- Return directory path name (part before last /)
fileName path() const;
inline fileName path() const;
//- Return true if it contains a '/' character
inline bool hasPath() const;
//- Remove leading path, returning true if string changed.
inline bool removePath();
//- Return name after stripping off the parent directory
fileName relative(const fileName& parent) const;
//- Return file name without extension (part before last .)
fileName lessExt() const;
inline fileName lessExt() const;
//- Return file name extension (part after last .)
word ext() const;
inline word ext() const;
//- Append a '.' and the ending, and return the object.
// The '.' and ending will not be added when the ending is empty,
// or when the file name is empty or ended with a '/'.
fileName& ext(const word& ending);
inline fileName& ext(const word& ending);
//- Return true if it has an extension or simply ends with a '.'
inline bool hasExt() const;
//- Return true if the extension is the same as the given ending.
bool hasExt(const word& ending) const;
inline bool hasExt(const word& ending) const;
//- Return true if the extension matches the given ending.
bool hasExt(const wordRe& ending) const;
......@@ -318,20 +336,32 @@ public:
// Assignment
//- Copy, no character validation required
void operator=(const fileName& str);
//- Copy assignment, no character validation required
fileName& operator=(const fileName&) = default;
//- Copy, no character validation required
void operator=(const word& str);
//- Move assignment, no character validation required
fileName& operator=(fileName&&) = default;
//- Copy, stripping invalid characters
void operator=(const string& str);
//- Copy assignment, no character validation required
inline fileName& operator=(const word& str);
//- Copy, stripping invalid characters
void operator=(const std::string& str);
//- Move assignment, no character validation required
inline fileName& operator=(word&& str);
//- Copy assignment, stripping invalid characters
inline fileName& operator=(const string& str);
//- Move assignment, stripping invalid characters
inline fileName& operator=(string&& str);
//- Copy assignment, stripping invalid characters
inline fileName& operator=(const std::string& str);
//- Move assignment, stripping invalid characters
inline fileName& operator=(std::string&& str);
//- Copy, stripping invalid characters
void operator=(const char* str);
inline fileName& operator=(const char* str);
// Other operators
......
......@@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
\\/ M anipulation | Copyright (C) 2016-2017 OpenCFD Ltd.
\\/ M anipulation | Copyright (C) 2016-2018 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -52,51 +52,67 @@ inline void Foam::fileName::stripInvalid()
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
inline Foam::fileName::fileName()
inline Foam::fileName::fileName(const word& s)
:
string()
string(s)
{}
inline Foam::fileName::fileName(const fileName& fn)
inline Foam::fileName::fileName(word&& s)
:
string(fn)
string(std::move(s))
{}
inline Foam::fileName::fileName(const word& w)
inline Foam::fileName::fileName(const string& s, bool doStrip)
:
string(w)
{}
string(s)
{
if (doStrip)
{
stripInvalid();
}
}
inline Foam::fileName::fileName(const string& s, const bool doStripInvalid)
inline Foam::fileName::fileName(string&& s, bool doStrip)
:
string(s)
string(std::move(s))
{
if (doStripInvalid)
if (doStrip)
{
stripInvalid();
}
}
inline Foam::fileName::fileName(const std::string& s, const bool doStripInvalid)
inline Foam::fileName::fileName(const std::string& s, bool doStrip)
:
string(s)
{
if (doStripInvalid)
if (doStrip)
{
stripInvalid();
}
}
inline Foam::fileName::fileName(std::string&& s, bool doStrip)
:
string(std::move(s))
{
if (doStrip)
{
stripInvalid();
}
}
inline Foam::fileName::fileName(const char* s, const bool doStripInvalid)
inline Foam::fileName::fileName(const char* s, bool doStrip)
:
string(s)
{
if (doStripInvalid)
if (doStrip)
{
stripInvalid();
}
......@@ -134,16 +150,164 @@ inline bool Foam::fileName::isBackup() const
}
inline bool Foam::fileName::hasPath() const
{
return string::hasPath();
}
inline bool Foam::fileName::hasExt() const
{
return string::hasExt();
}
inline bool Foam::fileName::hasExt(const word& ending) const
{
return string::hasExt(ending);
}
inline std::string Foam::fileName::path(const std::string& str)
{
const auto i = str.rfind('/');
if (i == npos)
{
return ".";
}
else if (i)
{
return str.substr(0, i);
}
return "/";
}
inline Foam::fileName Foam::fileName::path() const
{
return path(*this);
}
inline std::string Foam::fileName::name(const std::string& str)
{
const auto i = str.rfind('/');