STYLE: code cleanup in stringOps

This commit is contained in:
Mark Olesen
2017-10-29 13:53:34 +01:00
parent f76431c536
commit 743e75b978

View File

@ -29,11 +29,71 @@ License
#include "etcFiles.H" #include "etcFiles.H"
#include "StringStream.H" #include "StringStream.H"
// * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
namespace Foam
{
// Standard handling of "~/", "./" etc.
static void standardExpansions(Foam::string& s)
{
if (s.empty())
{
return;
}
if (s[0] == '.')
{
// Expand a lone '.' and an initial './' into cwd
if (s.size() == 1)
{
s = cwd();
}
else if (s[1] == '/')
{
s.std::string::replace(0, 1, cwd());
}
}
else if (s[0] == '~')
{
// Expand initial ~
// ~/ => home directory
// ~OpenFOAM => site/user OpenFOAM configuration directory
// ~user => home directory for specified user
string user;
fileName file;
const auto slash = s.find('/');
if (slash == std::string::npos)
{
user = s.substr(1);
}
else
{
user = s.substr(1, slash - 1);
file = s.substr(slash + 1);
}
// NB: be a bit lazy and expand ~unknownUser as an
// empty string rather than leaving it untouched.
// otherwise add extra test
if (user == "OpenFOAM")
{
s = findEtcFile(file);
}
else
{
s = home(user)/file;
}
}
}
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
//! \cond fileScope //! \cond fileScope
// Find the type/position of the ":-" or ":+" alternative values // Find the type/position of the ":-" or ":+" alternative values
// Returns 0, '-', '+' corresponding to not-found or ':-' or ':+'
static inline int findParameterAlternative static inline int findParameterAlternative
( (
const std::string& s, const std::string& s,
@ -70,6 +130,8 @@ static inline int findParameterAlternative
//! \endcond //! \endcond
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
Foam::string Foam::stringOps::expand Foam::string Foam::stringOps::expand
( (
const string& original, const string& original,
@ -257,13 +319,14 @@ Foam::string Foam::stringOps::getVariable
{ {
string value; string value;
const entry* ePtr = dict.lookupScopedEntryPtr const entry* eptr = dict.lookupScopedEntryPtr
( (
name, name,
true, true,
false false
); );
if (ePtr)
if (eptr)
{ {
OStringStream buf; OStringStream buf;
// Force floating point numbers to be printed with at least // Force floating point numbers to be printed with at least
@ -271,11 +334,8 @@ Foam::string Foam::stringOps::getVariable
buf << fixed; buf << fixed;
buf.precision(IOstream::defaultPrecision()); buf.precision(IOstream::defaultPrecision());
// fail for non-primitiveEntry // Fails for non-primitiveEntry
dynamicCast<const primitiveEntry> dynamicCast<const primitiveEntry>(*eptr).write(buf, true);
(
*ePtr
).write(buf, true);
value = buf.str(); value = buf.str();
} }
@ -308,8 +368,11 @@ Foam::string Foam::stringOps::getVariable
} }
} }
} }
}
if (!allowEmpty && value.empty()) if (!allowEmpty && value.empty())
{
if (allowEnvVars)
{ {
FatalIOErrorInFunction FatalIOErrorInFunction
( (
@ -317,15 +380,14 @@ Foam::string Foam::stringOps::getVariable
) << "Cannot find dictionary or environment variable " ) << "Cannot find dictionary or environment variable "
<< name << exit(FatalIOError); << name << exit(FatalIOError);
} }
} else
{
if (!allowEmpty && value.empty()) FatalIOErrorInFunction
{ (
FatalIOErrorInFunction dict
( ) << "Cannot find dictionary variable "
dict << name << exit(FatalIOError);
) << "Cannot find dictionary variable " }
<< name << exit(FatalIOError);
} }
return value; return value;
@ -360,8 +422,9 @@ Foam::string Foam::stringOps::expand
{ {
newString.append(string(s[index])); newString.append(string(s[index]));
} }
index++; ++index;
} }
return newString; return newString;
} }
@ -390,7 +453,7 @@ Foam::string& Foam::stringOps::inplaceExpand
if (s[begVar+1] == '{') if (s[begVar+1] == '{')
{ {
// Recursive variable expansion mode // Recursive variable expansion mode
label stringStart = begVar; auto stringStart = begVar;
begVar += 2; begVar += 2;
string varValue string varValue
( (
@ -471,53 +534,8 @@ Foam::string& Foam::stringOps::inplaceExpand
} }
} }
if (!s.empty()) // Standard handling of "~/", "./" etc.
{ standardExpansions(s);
if (s[0] == '~')
{
// Expand initial ~
// ~/ => home directory
// ~OpenFOAM => site/user OpenFOAM configuration directory
// ~user => home directory for specified user
string user;
fileName file;
if ((begVar = s.find('/')) != string::npos)
{
user = s.substr(1, begVar - 1);
file = s.substr(begVar + 1);
}
else
{
user = s.substr(1);
}
// NB: be a bit lazy and expand ~unknownUser as an
// empty string rather than leaving it untouched.
// otherwise add extra test
if (user == "OpenFOAM")
{
s = findEtcFile(file);
}
else
{
s = home(user)/file;
}
}
else if (s[0] == '.')
{
// Expand a lone '.' and an initial './' into cwd
if (s.size() == 1)
{
s = cwd();
}
else if (s[1] == '/')
{
s.std::string::replace(0, 1, cwd());
}
}
}
return s; return s;
} }
@ -596,32 +614,33 @@ Foam::string& Foam::stringOps::inplaceExpand
); );
// lookup in the dictionary // Lookup in the dictionary without wildcards.
const entry* ePtr = dict.lookupScopedEntryPtr // See note in primitiveEntry
const entry* eptr = dict.lookupScopedEntryPtr
( (
varName, varName,
true, true,
false // wildcards disabled. See primitiveEntry false
); );
// if defined - copy its entries // if defined - copy its entries
if (ePtr) if (eptr)
{ {
OStringStream buf; OStringStream buf;
// Force floating point numbers to be printed with at least // Force floating point numbers to be printed with at least
// some decimal digits. // some decimal digits.
buf << fixed; buf << fixed;
buf.precision(IOstream::defaultPrecision()); buf.precision(IOstream::defaultPrecision());
if (ePtr->isDict()) if (eptr->isDict())
{ {
ePtr->dict().write(buf, false); eptr->dict().write(buf, false);
} }
else else
{ {
// fail for other types // Fail for non-primitiveEntry
dynamicCast<const primitiveEntry> dynamicCast<const primitiveEntry>
( (
*ePtr *eptr
).write(buf, true); ).write(buf, true);
} }
@ -816,53 +835,8 @@ Foam::string& Foam::stringOps::inplaceExpand
} }
} }
if (!s.empty()) // Standard handling of "~/", "./" etc.
{ standardExpansions(s);
if (s[0] == '~')
{
// Expand initial ~
// ~/ => home directory
// ~OpenFOAM => site/user OpenFOAM configuration directory
// ~user => home directory for specified user
string user;
fileName file;
if ((begVar = s.find('/')) != string::npos)
{
user = s.substr(1, begVar - 1);
file = s.substr(begVar + 1);
}
else
{
user = s.substr(1);
}
// NB: be a bit lazy and expand ~unknownUser as an
// empty string rather than leaving it untouched.
// otherwise add extra test
if (user == "OpenFOAM")
{
s = findEtcFile(file);
}
else
{
s = home(user)/file;
}
}
else if (s[0] == '.')
{
// Expand a lone '.' and an initial './' into cwd
if (s.size() == 1)
{
s = cwd();
}
else if (s[1] == '/')
{
s.std::string::replace(0, 1, cwd());
}
}
}
return s; return s;
} }
@ -886,11 +860,9 @@ bool Foam::stringOps::inplaceReplaceVar(string& s, const word& varName)
{ {
return false; return false;
} }
else
{ s.replace(i, content.size(), string("${" + varName + "}"));
s.replace(i, content.size(), string("${" + varName + "}")); return true;
return true;
}
} }