Commit a9741cea authored by Mark Olesen's avatar Mark Olesen
Browse files

ENH: additional text expansion shortcuts (issue #792)

Support the following expansions when they occur at the start of a
string:

    Short-form       Equivalent
    =========       ===========
      <etc>/          ~OpenFOAM/   (as per foamEtcFile)
      <case>/         $FOAM_CASE/
      <constant>/     $FOAM_CASE/constant/
      <system>/       $FOAM_CASE/system/

These can be used in fileName expansions to improve clarity and reduce
some typing

     "<constant>/reactions"   vs  "$FOAM_CASE/constant/reactions"
parent d814bce3
......@@ -30,6 +30,7 @@ Description
#include "stringOps.H"
#include "dictionary.H"
#include "IOstreams.H"
#include "OSspecific.H"
#include "int.H"
#include "uint.H"
......@@ -53,6 +54,8 @@ int main(int argc, char *argv[])
" or with '${__UNKNOWN:+unknown}' empty"
);
setEnv("FOAM_CASE", cwd(), true);
dictionary dict;
dict.add("HOME", "myHome");
......@@ -62,6 +65,38 @@ int main(int argc, char *argv[])
dict.add("FOAM_RUN", subDict);
// basic expansions
{
for
(
const auto& cstr
:
{
"~OpenFOAM/controlDict",
"<etc>/controlDict",
"$FOAM_CASE/test",
"<case>/test",
"$FOAM_CASE/constant/test",
"<case>/constant/test",
"<constant>/test",
"$FOAM_CASE/system/test",
"<case>/system/test",
"<system>/test",
}
)
{
string input(cstr);
string output(stringOps::expand(input));
Info<<"input: " << input << nl
<< "expand: " << output << nl << nl;
}
}
// Test Foam::name with formatting string
{
word formatted = word::printf("formatted=<%X>", 0xdeadbeef);
......
......@@ -34,15 +34,93 @@ License
namespace Foam
{
// Standard handling of "~/", "./" etc.
static void standardExpansions(std::string& s)
// Expand a leading <tag>/
// Convenient for frequently used directories
//
// <etc>/ => user/group/other OpenFOAM directory
// <case>/ => FOAM_CASE directory
// <constant>/ => FOAM_CASE/constant directory
// <system>/ => FOAM_CASE/system directory
static void expandLeadingTag(std::string& s, const char b, const char e)
{
if (s.empty())
if (s[0] != b)
{
return;
}
if (s[0] == '.')
auto delim = s.find(e);
if (delim == std::string::npos || s[++delim] != '/')
{
return; // Ignore if there is no '/' after <tag>
}
const std::string tag(s, 1, delim-2);
fileName file(s.substr(delim + 1));
if (tag == "etc")
{
s = findEtcFile(file);
}
else if (tag == "case")
{
s = fileName(getEnv("FOAM_CASE"))/file;
}
else if (tag == "constant" || tag == "system")
{
s = fileName(Foam::getEnv("FOAM_CASE"))/tag/file;
}
}
// Expand a leading tilde
// ~/ => home directory
// ~OpenFOAM => user/group/other OpenFOAM directory
// ~user => home directory for specified user
static void expandLeadingTilde(std::string& s)
{
if (s[0] != '~')
{
return;
}
std::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;
}
}
// Expand leading contents: "./", "~..", "<tag>/"
static void expandLeading(std::string& s)
{
if (s.empty())
{
return;
}
else if (s[0] == '.')
{
// Expand a lone '.' and an initial './' into cwd
if (s.size() == 1)
......@@ -54,42 +132,17 @@ static void standardExpansions(std::string& s)
s.std::string::replace(0, 1, cwd());
}
}
else if (s[0] == '<')
{
expandLeadingTag(s, '<', '>');
}
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;
}
expandLeadingTilde(s);
}
}
}
} // end of namespace Foam
//! \cond fileScope
......@@ -571,8 +624,7 @@ void Foam::stringOps::inplaceExpand
}
}
// Standard handling of "~/", "./" etc.
standardExpansions(s);
expandLeading(s);
}
......@@ -869,8 +921,7 @@ void Foam::stringOps::inplaceExpand
}
}
// Standard handling of "~/", "./" etc.
standardExpansions(s);
expandLeading(s);
}
......
......@@ -217,10 +217,15 @@ namespace stringOps
// - "$VAR", "${VAR}"
// -# current directory
// - leading "./" : the current directory
// -# leading tag expansion for commonly used directories
// - <etc>/ : user/group/other OpenFOAM directory
// - <case>/ : FOAM_CASE directory
// - <constant>/ : FOAM_CASE/constant directory
// - <system>/ : FOAM_CASE/system directory
// -# tilde expansion
// - leading "~/" : home directory
// - leading "~user" : home directory for specified user
// - leading "~OpenFOAM" : site/user OpenFOAM configuration directory
// - leading "~OpenFOAM" : user/group/other OpenFOAM directory
//
// Supports default and alternative values as per the POSIX shell.
// \code
......@@ -252,10 +257,15 @@ namespace stringOps
// - "$VAR", "${VAR}"
// -# current directory
// - leading "./" : the current directory
// -# leading tag expansion for commonly used directories
// - <etc>/ : user/group/other OpenFOAM directory
// - <case>/ : FOAM_CASE directory
// - <constant>/ : FOAM_CASE/constant directory
// - <system>/ : FOAM_CASE/system directory
// -# tilde expansion
// - leading "~/" : home directory
// - leading "~user" : home directory for specified user
// - leading "~OpenFOAM" : site/user OpenFOAM configuration directory
// - leading "~OpenFOAM" : user/group/other OpenFOAM directory
//
// Supports default and alternative values as per the POSIX shell.
// \code
......
......@@ -118,10 +118,10 @@ sigmaRadialCoeffs
offsetSurfaceCoeffs
{
// Surface that mesh has been meshed to
baseSurface "$FOAM_CASE/constant/triSurface/DTC-scaled-inflated.obj";
baseSurface "<constant>/triSurface/DTC-scaled-inflated.obj";
// Surface to fill in to
offsetSurface "$FOAM_CASE/constant/triSurface/DTC-scaled.obj";
offsetSurface "<constant>/triSurface/DTC-scaled.obj";
}
......
......@@ -27,7 +27,7 @@ thermoType
}
chemistryReader foamChemistryReader;
foamChemistryFile "$FOAM_CASE/constant/reactions";
foamChemistryThermoFile "$FOAM_CASE/constant/thermo";
foamChemistryFile "<constant>/reactions";
foamChemistryThermoFile "<constant>/thermo";
// ************************************************************************* //
......@@ -26,8 +26,8 @@ thermoType
specie specie;
}
CHEMKINFile "$FOAM_CASE/chemkin/chem.inp";
CHEMKINThermoFile "$FOAM_CASE/chemkin/therm.dat";
CHEMKINTransportFile "$FOAM_CASE/chemkin/transportProperties";
CHEMKINFile "<case>/chemkin/chem.inp";
CHEMKINThermoFile "<case>/chemkin/therm.dat";
CHEMKINTransportFile "<case>/chemkin/transportProperties";
// ************************************************************************* //
......@@ -26,8 +26,8 @@ thermoType
specie specie;
}
CHEMKINFile "$FOAM_CASE/chemkin/chem.inp";
CHEMKINThermoFile "$FOAM_CASE/chemkin/therm.dat";
CHEMKINTransportFile "$FOAM_CASE/chemkin/transportProperties";
CHEMKINFile "<case>/chemkin/chem.inp";
CHEMKINThermoFile "<case>/chemkin/therm.dat";
CHEMKINTransportFile "<case>/chemkin/transportProperties";
// ************************************************************************* //
......@@ -26,8 +26,8 @@ thermoType
specie specie;
}
CHEMKINFile "$FOAM_CASE/chemkin/chem.inp";
CHEMKINThermoFile "$FOAM_CASE/chemkin/therm.dat";
CHEMKINTransportFile "$FOAM_CASE/chemkin/transportProperties";
CHEMKINFile "<case>/chemkin/chem.inp";
CHEMKINThermoFile "<case>/chemkin/therm.dat";
CHEMKINTransportFile "<case>/chemkin/transportProperties";
// ************************************************************************* //
......@@ -26,8 +26,8 @@ thermoType
specie specie;
}
CHEMKINFile "$FOAM_CASE/chemkin/chem.inp";
CHEMKINThermoFile "$FOAM_CASE/chemkin/therm.dat";
CHEMKINTransportFile "$FOAM_CASE/chemkin/transportProperties";
CHEMKINFile "<case>/chemkin/chem.inp";
CHEMKINThermoFile "<case>/chemkin/therm.dat";
CHEMKINTransportFile "<case>/chemkin/transportProperties";
// ************************************************************************* //
......@@ -27,7 +27,7 @@ boundaryField
massFlowRate tableFile;
massFlowRateCoeffs
{
file "$FOAM_CASE/constant/massLossRate";
file "<constant>/massLossRate";
}
value uniform (0 0 0);
}
......
......@@ -33,9 +33,9 @@ fuel C7H16;
chemistryReader foamChemistryReader;
foamChemistryFile "$FOAM_CASE/constant/reactions";
foamChemistryFile "<constant>/reactions";
foamChemistryThermoFile "$FOAM_CASE/constant/thermo.compressibleGas";
foamChemistryThermoFile "<constant>/thermo.compressibleGas";
dpdt no;
......
......@@ -28,9 +28,9 @@ thermoType
chemistryReader foamChemistryReader;
foamChemistryFile "$FOAM_CASE/constant/pyrolysisRegion/reactions";
foamChemistryFile "<constant>/pyrolysisRegion/reactions";
foamChemistryThermoFile "$FOAM_CASE/constant/pyrolysisRegion/thermo.solid";
foamChemistryThermoFile "<constant>/pyrolysisRegion/thermo.solid";
gasThermoType
{
......
......@@ -32,9 +32,9 @@ fuel C3H8;
chemistryReader foamChemistryReader;
foamChemistryFile "$FOAM_CASE/constant/reactions";
foamChemistryFile "<constant>/reactions";
foamChemistryThermoFile "$FOAM_CASE/constant/thermo.compressibleGas";
foamChemistryThermoFile "<constant>/thermo.compressibleGas";
liquids
{
......
......@@ -28,9 +28,9 @@ thermoType
chemistryReader foamChemistryReader;
foamChemistryFile "$FOAM_CASE/constant/panelRegion/reactions";
foamChemistryFile "<constant>/panelRegion/reactions";
foamChemistryThermoFile "$FOAM_CASE/constant/panelRegion/thermo.solid";
foamChemistryThermoFile "<constant>/panelRegion/thermo.solid";
gasThermoType
{
......
......@@ -32,9 +32,9 @@ fuel C3H8;
chemistryReader foamChemistryReader;
foamChemistryFile "$FOAM_CASE/constant/reactions";
foamChemistryFile "<constant>/reactions";
foamChemistryThermoFile "$FOAM_CASE/constant/thermo.compressibleGas";
foamChemistryThermoFile "<constant>/thermo.compressibleGas";
// ************************************************************************* //
......@@ -28,9 +28,9 @@ thermoType
chemistryReader foamChemistryReader;
foamChemistryFile "$FOAM_CASE/constant/panelRegion/reactions";
foamChemistryFile "<constant>/panelRegion/reactions";
foamChemistryThermoFile "$FOAM_CASE/constant/panelRegion/thermo.solid";
foamChemistryThermoFile "<constant>/panelRegion/thermo.solid";
gasThermoType
{
......
......@@ -33,8 +33,8 @@ fuel CH4;
chemistryReader foamChemistryReader;
foamChemistryFile "$FOAM_CASE/constant/reactions";
foamChemistryFile "<constant>/reactions";
foamChemistryThermoFile "$FOAM_CASE/constant/thermo.compressibleGas";
foamChemistryThermoFile "<constant>/thermo.compressibleGas";
// ************************************************************************* //
......@@ -32,9 +32,9 @@ fuel CH4;
chemistryReader foamChemistryReader;
foamChemistryFile "$FOAM_CASE/constant/reactions";
foamChemistryFile "<constant>/reactions";
foamChemistryThermoFile "$FOAM_CASE/constant/thermo.compressibleGas";
foamChemistryThermoFile "<constant>/thermo.compressibleGas";
// ************************************************************************* //
......@@ -32,9 +32,9 @@ fuel CH4;
chemistryReader foamChemistryReader;
foamChemistryFile "$FOAM_CASE/constant/reactions";
foamChemistryFile "<constant>/reactions";
foamChemistryThermoFile "$FOAM_CASE/constant/thermo.compressibleGas";
foamChemistryThermoFile "<constant>/thermo.compressibleGas";
// ************************************************************************* //
......@@ -32,9 +32,9 @@ fuel CH4;
chemistryReader foamChemistryReader;
foamChemistryFile "$FOAM_CASE/constant/reactions";
foamChemistryFile "<constant>/reactions";
foamChemistryThermoFile "$FOAM_CASE/constant/thermo.compressibleGas";
foamChemistryThermoFile "<constant>/thermo.compressibleGas";
// ************************************************************************* //
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment