diff --git a/src/OSspecific/POSIX/POSIX.C b/src/OSspecific/POSIX/POSIX.C index 790170941b9dd8029156ba8e47b1908338e7d471..3c8a2b255393f0ee09709771026639a94af9c16e 100644 --- a/src/OSspecific/POSIX/POSIX.C +++ b/src/OSspecific/POSIX/POSIX.C @@ -96,13 +96,13 @@ pid_t Foam::pgid() } -bool Foam::env(const word& envName) +bool Foam::env(const std::string& envName) { return ::getenv(envName.c_str()) != nullptr; } -Foam::string Foam::getEnv(const word& envName) +Foam::string Foam::getEnv(const std::string& envName) { char* env = ::getenv(envName.c_str()); @@ -126,7 +126,7 @@ bool Foam::setEnv const bool overwrite ) { - return setenv(envName.c_str(), value.c_str(), overwrite) == 0; + return ::setenv(envName.c_str(), value.c_str(), overwrite) == 0; } @@ -215,7 +215,7 @@ Foam::fileName Foam::home() } -Foam::fileName Foam::home(const string& userName) +Foam::fileName Foam::home(const std::string& userName) { struct passwd* pw; @@ -252,13 +252,13 @@ Foam::fileName Foam::cwd() List<char> path(pathLengthLimit); // Resize path if getcwd fails with an ERANGE error - while(pathLengthLimit == path.size()) + while (pathLengthLimit == path.size()) { if (::getcwd(path.data(), path.size())) { return path.data(); } - else if(errno == ERANGE) + else if (errno == ERANGE) { // Increment path length upto the pathLengthMax limit if @@ -888,7 +888,7 @@ bool Foam::rm(const fileName& file) } // Try returning plain file name; if not there, try with .gz - if (remove(file.c_str()) == 0) + if (::remove(file.c_str()) == 0) { return true; } @@ -998,7 +998,7 @@ void Foam::fdClose(const int fd) bool Foam::ping ( - const string& destName, + const std::string& destName, const label destPort, const label timeOut ) @@ -1074,9 +1074,9 @@ bool Foam::ping } -bool Foam::ping(const string& hostname, const label timeOut) +bool Foam::ping(const std::string& host, const label timeOut) { - return ping(hostname, 222, timeOut) || ping(hostname, 22, timeOut); + return ping(host, 222, timeOut) || ping(host, 22, timeOut); } diff --git a/src/OpenFOAM/include/OSspecific.H b/src/OpenFOAM/include/OSspecific.H index 4b47e344258060a68671a6b0f347d503de972b1a..3c76a141931f5b3005596cb1aad2b2e553419d46 100644 --- a/src/OpenFOAM/include/OSspecific.H +++ b/src/OpenFOAM/include/OSspecific.H @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation - \\/ M anipulation | Copyright (C) 2016 OpenCFD Ltd. + \\/ M anipulation | Copyright (C) 2016-2017 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -58,11 +58,11 @@ pid_t ppid(); pid_t pgid(); //- Return true if environment variable of given name is defined -bool env(const word&); +bool env(const std::string& envName); //- Return environment variable of given name // Return string() if the environment is undefined -string getEnv(const word&); +string getEnv(const std::string& envName); //- Set an environment variable bool setEnv(const word& name, const std::string& value, const bool overwrite); @@ -84,7 +84,7 @@ bool isAdministrator(); fileName home(); //- Return home directory path name for a particular user -fileName home(const string& userName); +fileName home(const std::string& userName); //- Return current working directory path name fileName cwd(); @@ -176,10 +176,10 @@ unsigned int sleep(const unsigned int); void fdClose(const int); //- Check if machine is up by pinging given port -bool ping(const string&, const label port, const label timeOut); +bool ping(const std::string& destName, const label port, const label timeOut); //- Check if machine is up by pinging port 22 (ssh) and 222 (rsh) -bool ping(const string&, const label timeOut=10); +bool ping(const std::string& host, const label timeOut=10); //- Execute the specified command via the shell. // Uses vfork/execl internally. diff --git a/src/OpenFOAM/primitives/strings/fileName/fileName.C b/src/OpenFOAM/primitives/strings/fileName/fileName.C index 70e925ea010e13c9beed06e77470afcc3c925b0a..d0dfa2c074d0cdfec45b12518ed654e48f1aaab7 100644 --- a/src/OpenFOAM/primitives/strings/fileName/fileName.C +++ b/src/OpenFOAM/primitives/strings/fileName/fileName.C @@ -228,25 +228,6 @@ Foam::word Foam::fileName::name() const } -Foam::string Foam::fileName::caseName() const -{ - string cName = *this; - - const string caseStr(getEnv("FOAM_CASE")); - - const size_type i = find(caseStr); - - if (i == npos) - { - return cName; - } - else - { - return cName.replace(i, caseStr.size(), string("$FOAM_CASE")); - } -} - - Foam::word Foam::fileName::name(const bool noExt) const { if (noExt) diff --git a/src/OpenFOAM/primitives/strings/fileName/fileName.H b/src/OpenFOAM/primitives/strings/fileName/fileName.H index 4195077d819587ccd59d73d475fe4c1b19b905fc..5580ffa3f7d244522a2c222648578013c69b2b86 100644 --- a/src/OpenFOAM/primitives/strings/fileName/fileName.H +++ b/src/OpenFOAM/primitives/strings/fileName/fileName.H @@ -189,9 +189,6 @@ public: // word name() const; - //- Return file name (part beyond last /), subsitute for FOAM_CASE - string caseName() const; - //- Return file name, optionally without extension word name(const bool noExt) const; diff --git a/src/OpenFOAM/primitives/strings/stringOps/stringOps.C b/src/OpenFOAM/primitives/strings/stringOps/stringOps.C index 4dc7c08f0fe5962b9470e3d85daae2930a644b76..9b7f73a4f2d4c0c68ed42b417ddcc41633cf0226 100644 --- a/src/OpenFOAM/primitives/strings/stringOps/stringOps.C +++ b/src/OpenFOAM/primitives/strings/stringOps/stringOps.C @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation - \\/ M anipulation | + \\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -34,7 +34,6 @@ License //! \cond fileScope // Find the type/position of the ":-" or ":+" alternative values -// static inline int findParameterAlternative ( const std::string& s, @@ -132,7 +131,7 @@ Foam::string& Foam::stringOps::inplaceExpand iter != s.end() && ( - isalnum(*iter) + std::isalnum(*iter) || *iter == '.' || *iter == ':' || *iter == '_' @@ -285,6 +284,30 @@ Foam::string Foam::stringOps::getVariable { value = getEnv(name); + if (value.empty() && !name.empty()) + { + // The type/position of the ":-" or ":+" alternative values + string::size_type altPos = 0; + + // check for parameter:-word or parameter:+word + int altType = findParameterAlternative(name, altPos, name.size()-1); + if (altType) + { + value = getEnv + ( + // var-name + word(name.substr(0, altPos), false) + ); + + // ":-" or ":+" alternative value + if (value.empty() ? (altType == '-') : (altType == '+')) + { + // alternative + value = name.substr(altPos + 2); + } + } + } + if (value.empty()) { FatalIOErrorInFunction @@ -391,15 +414,15 @@ Foam::string& Foam::stringOps::inplaceExpand else { string::iterator iter = s.begin() + begVar + 1; + string::size_type endVar = begVar; // more generous in accepting keywords than for env variables - string::size_type endVar = begVar; while ( iter != s.end() && ( - isalnum(*iter) + std::isalnum(*iter) || *iter == '.' || *iter == ':' || *iter == '_' @@ -536,7 +559,7 @@ Foam::string& Foam::stringOps::inplaceExpand iter != s.end() && ( - isalnum(*iter) + std::isalnum(*iter) || *iter == '.' || *iter == ':' || *iter == '_' @@ -681,7 +704,7 @@ Foam::string& Foam::stringOps::inplaceExpand while ( iter != s.end() - && (isalnum(*iter) || *iter == '_') + && (std::isalnum(*iter) || *iter == '_') ) { ++iter; @@ -843,12 +866,38 @@ Foam::string& Foam::stringOps::inplaceExpand } +bool Foam::stringOps::inplaceReplaceVar(string& s, const word& varName) +{ + if (s.empty() || varName.empty()) + { + return false; + } + + const string content(getEnv(varName)); + if (content.empty()) + { + return false; + } + + const std::string::size_type i = s.find(content); + if (i == std::string::npos) + { + return false; + } + else + { + s.replace(i, content.size(), string("${" + varName + "}")); + return true; + } +} + + Foam::string Foam::stringOps::trimLeft(const string& s) { if (!s.empty()) { string::size_type beg = 0; - while (beg < s.size() && isspace(s[beg])) + while (beg < s.size() && std::isspace(s[beg])) { ++beg; } @@ -868,7 +917,7 @@ Foam::string& Foam::stringOps::inplaceTrimLeft(string& s) if (!s.empty()) { string::size_type beg = 0; - while (beg < s.size() && isspace(s[beg])) + while (beg < s.size() && std::isspace(s[beg])) { ++beg; } @@ -888,7 +937,7 @@ Foam::string Foam::stringOps::trimRight(const string& s) if (!s.empty()) { string::size_type sz = s.size(); - while (sz && isspace(s[sz-1])) + while (sz && std::isspace(s[sz-1])) { --sz; } @@ -908,7 +957,7 @@ Foam::string& Foam::stringOps::inplaceTrimRight(string& s) if (!s.empty()) { string::size_type sz = s.size(); - while (sz && isspace(s[sz-1])) + while (sz && std::isspace(s[sz-1])) { --sz; } diff --git a/src/OpenFOAM/primitives/strings/stringOps/stringOps.H b/src/OpenFOAM/primitives/strings/stringOps/stringOps.H index fb408455f610d09d8165e88769fa69f564605197..66e87623e50dba05a396a8faf191143059c8746d 100644 --- a/src/OpenFOAM/primitives/strings/stringOps/stringOps.H +++ b/src/OpenFOAM/primitives/strings/stringOps/stringOps.H @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011-2012 OpenFOAM Foundation - \\/ M anipulation | Copyright (C) 2016 OpenCFD Ltd. + \\/ M anipulation | Copyright (C) 2016-2017 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -56,30 +56,26 @@ namespace stringOps // -# variables // - "$VAR", "${VAR}" // - // Supports default values as per the Bourne/Korn shell. + // Supports default and alternative values as per the POSIX shell. // \code - // "${parameter:-defValue}" + // a) "${parameter:-defValue}" + // b) "${parameter:+altValue}" // \endcode - // If parameter is unset or null, the \c defValue is substituted. + // a) If parameter is unset or null, the \c defValue is substituted. // Otherwise, the value of parameter is substituted. // - // Supports alternative values as per the Bourne/Korn shell. - // \code - // "${parameter:+altValue}" - // \endcode - // If parameter is unset or null, nothing is substituted. + // b) If parameter is unset or null, nothing is substituted. // Otherwise the \c altValue is substituted. // - // Any unknown entries are removed silently. - // - // Malformed entries (eg, brace mismatch, sigil followed by bad character) + // - Any unknown entries are removed silently. + // - Malformed entries (eg, brace mismatch, sigil followed by bad chars) // are left as is. // // \note the leading sigil can be changed to avoid conflicts with other // string expansions string expand ( - const string&, + const string& original, const HashTable<string, word, string::hash>& mapping, const char sigil = '$' ); @@ -90,30 +86,26 @@ namespace stringOps // -# variables // - "$VAR", "${VAR}" // - // Supports default values as per the Bourne/Korn shell. + // Supports default and alternative values as per the POSIX shell. // \code - // "${parameter:-defValue}" + // a) "${parameter:-defValue}" + // b) "${parameter:+altValue}" // \endcode - // If parameter is unset or null, the \c defValue is substituted. + // a) If parameter is unset or null, the \c defValue is substituted. // Otherwise, the value of parameter is substituted. // - // Supports alternative values as per the Bourne/Korn shell. - // \code - // "${parameter:+altValue}" - // \endcode - // If parameter is unset or null, nothing is substituted. + // b) If parameter is unset or null, nothing is substituted. // Otherwise the \c altValue is substituted. // - // Any unknown entries are removed silently. - // - // Malformed entries (eg, brace mismatch, sigil followed by bad character) + // - Any unknown entries are removed silently. + // - Malformed entries (eg, brace mismatch, sigil followed by bad chars) // are left as is. // // \note the leading sigil can be changed to avoid conflicts with other // string expansions string& inplaceExpand ( - string&, + string& s, const HashTable<string, word, string::hash>& mapping, const char sigil = '$' ); @@ -129,13 +121,20 @@ namespace stringOps // string expansions string expand ( - const string&, + const string& original, const dictionary& dict, const char sigil = '$' ); //- Get dictionary or (optionally) environment variable + // + // The environment variable lookup supports default and alternative + // values as per the POSIX shell. + // \code + // ${parameter:-defValue} + // ${parameter:+altValue} + // \endcode string getVariable ( const word& name, @@ -189,7 +188,7 @@ namespace stringOps // string expansions string& inplaceExpand ( - string&, + string& s, const dictionary& dict, const char sigil = '$' ); @@ -206,30 +205,26 @@ namespace stringOps // - leading "~user" : home directory for specified user // - leading "~OpenFOAM" : site/user OpenFOAM configuration directory // - // Supports default values as per the Bourne/Korn shell. + // Supports default and alternative values as per the POSIX shell. // \code - // "${parameter:-defValue}" + // a) "${parameter:-defValue}" + // b) "${parameter:+altValue}" // \endcode - // If parameter is unset or null, the \c defValue is substituted. + // a) If parameter is unset or null, the \c defValue is substituted. // Otherwise, the value of parameter is substituted. // - // Supports alternative values as per the Bourne/Korn shell. - // \code - // "${parameter:+altValue}" - // \endcode - // If parameter is unset or null, nothing is substituted. + // b) If parameter is unset or null, nothing is substituted. // Otherwise the \c altValue is substituted. // - // Any unknown entries are removed silently, if allowEmpty is true. - // - // Malformed entries (eg, brace mismatch, sigil followed by bad character) + // - Any unknown entries are removed silently, if allowEmpty is true. + // - Malformed entries (eg, brace mismatch, sigil followed by bad chars) // are left as is. // // \sa // Foam::findEtcFile string expand ( - const string&, + const string& original, const bool allowEmpty = false ); @@ -245,63 +240,62 @@ namespace stringOps // - leading "~user" : home directory for specified user // - leading "~OpenFOAM" : site/user OpenFOAM configuration directory // - // Supports default values as per the Bourne/Korn shell. + // Supports default and alternative values as per the POSIX shell. // \code - // "${parameter:-defValue}" + // a) "${parameter:-defValue}" + // b) "${parameter:+altValue}" // \endcode - // If parameter is unset or null, the \c defValue is substituted. + // a) If parameter is unset or null, the \c defValue is substituted. // Otherwise, the value of parameter is substituted. // - // Supports alternative values as per the Bourne/Korn shell. - // \code - // "${parameter:+altValue}" - // \endcode - // If parameter is unset or null, nothing is substituted. + // b) If parameter is unset or null, nothing is substituted. // Otherwise the \c altValue is substituted. // - // Any unknown entries are removed silently, if allowEmpty is true. - // - // Malformed entries (eg, brace mismatch, sigil followed by bad character) + // - Any unknown entries are removed silently if allowEmpty is true. + // - Malformed entries (eg, brace mismatch, sigil followed by bad chars) // are left as is. // - // Any unknown entries are removed silently if allowEmpty is true. // \sa // Foam::findEtcFile string& inplaceExpand ( - string&, + string& s, const bool allowEmpty = false ); + //- Replace environment variable contents with its name. + // This is essentially the inverse operation for inplaceExpand. + // Return true if a replacement was successful. + bool inplaceReplaceVar(string& s, const word& varName); + + //- Return string trimmed of leading whitespace - string trimLeft(const string&); + string trimLeft(const string& s); //- Trim leading whitespace inplace - string& inplaceTrimLeft(string&); + string& inplaceTrimLeft(string& s); //- Return string trimmed of trailing whitespace - string trimRight(const string&); + string trimRight(const string& s); //- Trim trailing whitespace inplace - string& inplaceTrimRight(string&); + string& inplaceTrimRight(string& s); //- Return string trimmed of leading and trailing whitespace - string trim(const string&); + string trim(const string& original); //- Trim leading and trailing whitespace inplace - string& inplaceTrim(string&); + string& inplaceTrim(string& s); - //- Return a word representation of the primitive, - // using printf-style formatter. + //- Using printf-formatter for a word representation of the primitive. // The representation is not checked for valid word characters - // it is assumed that the caller knows what they are doing template<class PrimitiveType> Foam::word name(const char* fmt, const PrimitiveType& val); - //- Return a word representation of the primitive, - // using printf-style formatter. + //- Using printf-formatter for a word representation of the primitive. // The representation is not checked for valid word characters - // it is assumed that the caller knows what they are doing template<class PrimitiveType> diff --git a/src/OpenFOAM/primitives/strings/stringOps/stringOpsTemplates.C b/src/OpenFOAM/primitives/strings/stringOps/stringOpsTemplates.C index a3874aa88ee30aac71a845e8b774458036ac6ae3..a8f70303006ca95985ac0b837c9f97a14a3e92f6 100644 --- a/src/OpenFOAM/primitives/strings/stringOps/stringOpsTemplates.C +++ b/src/OpenFOAM/primitives/strings/stringOps/stringOpsTemplates.C @@ -40,15 +40,14 @@ Foam::word Foam::stringOps::name // same concept as GNU/BSD asprintf() // use snprintf with zero to determine the number of characters required - int n = ::snprintf(0, 0, fmt, val); + const int n = ::snprintf(nullptr, 0, fmt, val); if (n > 0) { char buf[n+1]; ::snprintf(buf, n+1, fmt, val); buf[n] = 0; - // no stripping desired - return word(buf, false); + return word(buf, false); // no stripping desired } return word::null;