ENH: use hasEnv() instead of env() for naming symmetry with getEnv, setEnv

- less confusing than the env() name, which could look like a
  setter/getter instead of a test
This commit is contained in:
Mark Olesen 2020-05-11 09:48:01 +02:00
parent 584ff5e0d0
commit 6a16db3708
8 changed files with 99 additions and 10 deletions

View File

@ -0,0 +1,3 @@
Test-foamEnv.C
EXE = $(FOAM_USER_APPBIN)/Test-foamEnv

View File

@ -0,0 +1,2 @@
/* EXE_INC = */
/* EXE_LIBS = */

View File

@ -0,0 +1,79 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
Application
Test-etcFiles
Description
Test etcFiles functionality.
Similar to foamEtcFile script, but automatically prunes nonexistent
directories from the list.
\*---------------------------------------------------------------------------*/
#include "argList.H"
#include "OSspecific.H"
using namespace Foam;
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Main program:
int main(int argc, char *argv[])
{
argList::noBanner();
argList::noParallel();
argList::noFunctionObjects();
argList::removeOption("case");
argList::addArgument("env...");
argList::addNote
(
"Simple test/report OpenFOAM environment"
);
argList args(argc, argv, false, true);
for (int argi = 1; argi < args.size(); ++argi)
{
const std::string envName(args[argi]);
if (hasEnv(envName))
{
Info<<"Have env " << envName.c_str() << "=" << getEnv(envName)
<< nl;
}
else
{
Info<<"No env " << envName.c_str()<< nl;
}
}
return 0;
}
// ************************************************************************* //

View File

@ -7,7 +7,7 @@
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2011-2017 OpenFOAM Foundation Copyright (C) 2011-2017 OpenFOAM Foundation
Copyright (C) 2011 Symscape Copyright (C) 2011 Symscape
Copyright (C) 2016-2019 OpenCFD Ltd. Copyright (C) 2016-2020 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -360,7 +360,7 @@ pid_t Foam::pgid()
} }
bool Foam::env(const std::string& envName) bool Foam::hasEnv(const std::string& envName)
{ {
// An empty envName => always false // An empty envName => always false
return !envName.empty() && return !envName.empty() &&

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2011-2017 OpenFOAM Foundation Copyright (C) 2011-2017 OpenFOAM Foundation
Copyright (C) 2016-2019 OpenCFD Ltd. Copyright (C) 2016-2020 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -263,7 +263,7 @@ pid_t Foam::pgid()
} }
bool Foam::env(const std::string& envName) bool Foam::hasEnv(const std::string& envName)
{ {
// An empty envName => always false // An empty envName => always false
return !envName.empty() && ::getenv(envName.c_str()) != nullptr; return !envName.empty() && ::getenv(envName.c_str()) != nullptr;

View File

@ -187,7 +187,7 @@ void Foam::IOerror::exitOrAbort(const int, const bool isAbort)
} }
} }
if (env("FOAM_ABORT")) if (hasEnv("FOAM_ABORT"))
{ {
Perr<< nl << *this << nl Perr<< nl << *this << nl
<< "\nFOAM aborting (FOAM_ABORT set)\n" << endl; << "\nFOAM aborting (FOAM_ABORT set)\n" << endl;
@ -246,7 +246,7 @@ void Foam::IOerror::exitOrAbort(const int, const bool isAbort)
void Foam::IOerror::exit(const int) void Foam::IOerror::exit(const int)
{ {
exitOrAbort(1, env("FOAM_ABORT")); exitOrAbort(1, hasEnv("FOAM_ABORT"));
} }

View File

@ -238,7 +238,7 @@ void Foam::error::exitOrAbort(const int errNo, const bool isAbort)
} }
} }
if (env("FOAM_ABORT")) if (hasEnv("FOAM_ABORT"))
{ {
Perr<< nl << *this << nl Perr<< nl << *this << nl
<< "\nFOAM aborting (FOAM_ABORT set)\n" << endl; << "\nFOAM aborting (FOAM_ABORT set)\n" << endl;
@ -297,7 +297,7 @@ void Foam::error::exitOrAbort(const int errNo, const bool isAbort)
void Foam::error::exit(const int errNo) void Foam::error::exit(const int errNo)
{ {
exitOrAbort(errNo, env("FOAM_ABORT")); exitOrAbort(errNo, hasEnv("FOAM_ABORT"));
} }

View File

@ -66,16 +66,21 @@ pid_t pgid();
//- True if environment variable of given name is defined. //- True if environment variable of given name is defined.
// Using an empty name is a no-op and always returns false. // Using an empty name is a no-op and always returns false.
bool env(const std::string& envName); bool hasEnv(const std::string& envName);
//- Get environment value for given envName. //- Get environment value for given envName.
// Return string() if the environment is undefined or envName is empty. // \return empty string if environment is undefined or envName is empty.
string getEnv(const std::string& envName); string getEnv(const std::string& envName);
//- Set an environment variable, return true on success. //- Set an environment variable, return true on success.
// Using an empty name is a no-op and always returns false. // Using an empty name is a no-op and always returns false.
bool setEnv(const word& name, const std::string& value, const bool overwrite); bool setEnv(const word& name, const std::string& value, const bool overwrite);
//- Deprecated(2020-05) check for existence of environment variable
// \deprecated(2020-05) - use hasEnv() function
FOAM_DEPRECATED_FOR(2020-05, "hasEnv() function")
inline bool env(const std::string& envName) { return Foam::hasEnv(envName); }
//- Return the system's host name, as per hostname(1) //- Return the system's host name, as per hostname(1)
// Optionally with the full name (as per the '-f' option) // Optionally with the full name (as per the '-f' option)
string hostName(const bool full=false); string hostName(const bool full=false);