ENH: add basic support for file extensions to word

- when a plain word is used as a directory-local name for file.
  We don't have a full blown fileName, but still want to check/remove
  extensions etc.
This commit is contained in:
Mark Olesen
2017-05-26 10:48:01 +02:00
parent ccc1ce4a25
commit 0564efb9e1
10 changed files with 265 additions and 101 deletions

View File

@ -25,6 +25,8 @@ License
#include "string.H"
#include "stringOps.H"
#include "word.H"
#include "wordRe.H"
/* * * * * * * * * * * * * * * Static Member Data * * * * * * * * * * * * * */
@ -33,13 +35,75 @@ int Foam::string::debug(Foam::debug::debugSwitch(string::typeName, 0));
const Foam::string Foam::string::null;
// * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
Foam::word Foam::string::ext() const
{
const size_type i = find_ext();
if (i == npos)
{
return word::null;
}
else
{
return substr(i+1, npos);
}
}
bool Foam::string::ext(const Foam::word& ending)
{
if (!ending.empty() && !empty() && operator[](size()-1) != '/')
{
append(1u, '.');
append(ending);
return true;
}
return false;
}
bool Foam::string::hasExt(const word& ending) const
{
size_type i = find_ext();
if (i == npos)
{
return false;
}
++i; // Compare *after* the dot
return
(
// Lengths must match
((size() - i) == ending.size())
&& !compare(i, npos, ending)
);
}
bool Foam::string::hasExt(const wordRe& ending) const
{
const size_type i = find_ext();
if (i == npos)
{
return false;
}
const std::string end = substr(i+1, npos); // Compare *after* the dot
return ending.match(end);
}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
Foam::string::size_type Foam::string::count(const char c) const
{
size_type cCount = 0;
for (const_iterator iter = begin(); iter != end(); ++iter)
for (const_iterator iter = cbegin(); iter != cend(); ++iter)
{
if (*iter == c)
{

View File

@ -58,6 +58,8 @@ namespace Foam
{
// Forward declaration of classes
class word;
class wordRe;
class Istream;
class Ostream;
@ -76,6 +78,37 @@ class string
:
public std::string
{
protected:
// Protected Member Functions
//- Find position of a file extension dot, return npos on failure.
// A wrapped version of find_last_of("./") with additional logic.
inline size_type find_ext() const;
//- Return file name extension (part after last .)
word ext() const;
//- Append a '.' and the ending.
// The '.' and ending will not be added when the ending is empty,
// or when the object was or ended with a '/'.
//
// \return True if append occurred.
bool 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;
//- Return true if the extension matches the given ending.
bool hasExt(const wordRe& ending) const;
//- Remove extension, returning true if string changed.
inline bool removeExt();
public:
// Static data members

View File

@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2015 OpenFOAM Foundation
\\/ M anipulation |
\\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -25,6 +25,45 @@ License
#include <iostream>
// * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
inline std::string::size_type Foam::string::find_ext() const
{
const size_type i = find_last_of("./");
if (i == npos || i == 0 || operator[](i) == '/')
{
return npos;
}
else
{
return i;
}
}
inline bool Foam::string::hasExt() const
{
return (find_ext() != npos);
}
inline bool Foam::string::removeExt()
{
const size_type i = find_ext();
if (i == npos)
{
return false;
}
else
{
this->resize(i);
return true;
}
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
inline Foam::string::string()