From 6a16db3708a90ebb042843098a58bd5e4b47d1bf Mon Sep 17 00:00:00 2001 From: Mark Olesen <Mark.Olesen@esi-group.com> Date: Mon, 11 May 2020 09:48:01 +0200 Subject: [PATCH] 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 --- applications/test/foamEnv/Make/files | 3 + applications/test/foamEnv/Make/options | 2 + applications/test/foamEnv/Test-foamEnv.C | 79 ++++++++++++++++++++++++ src/OSspecific/MSwindows/MSwindows.C | 4 +- src/OSspecific/POSIX/POSIX.C | 4 +- src/OpenFOAM/db/error/IOerror.C | 4 +- src/OpenFOAM/db/error/error.C | 4 +- src/OpenFOAM/include/OSspecific.H | 9 ++- 8 files changed, 99 insertions(+), 10 deletions(-) create mode 100644 applications/test/foamEnv/Make/files create mode 100644 applications/test/foamEnv/Make/options create mode 100644 applications/test/foamEnv/Test-foamEnv.C diff --git a/applications/test/foamEnv/Make/files b/applications/test/foamEnv/Make/files new file mode 100644 index 00000000000..b67805d076b --- /dev/null +++ b/applications/test/foamEnv/Make/files @@ -0,0 +1,3 @@ +Test-foamEnv.C + +EXE = $(FOAM_USER_APPBIN)/Test-foamEnv diff --git a/applications/test/foamEnv/Make/options b/applications/test/foamEnv/Make/options new file mode 100644 index 00000000000..18e6fe47afa --- /dev/null +++ b/applications/test/foamEnv/Make/options @@ -0,0 +1,2 @@ +/* EXE_INC = */ +/* EXE_LIBS = */ diff --git a/applications/test/foamEnv/Test-foamEnv.C b/applications/test/foamEnv/Test-foamEnv.C new file mode 100644 index 00000000000..0555c8be11f --- /dev/null +++ b/applications/test/foamEnv/Test-foamEnv.C @@ -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; +} + + +// ************************************************************************* // diff --git a/src/OSspecific/MSwindows/MSwindows.C b/src/OSspecific/MSwindows/MSwindows.C index 6afa4e03f2b..46f2ae99bbb 100644 --- a/src/OSspecific/MSwindows/MSwindows.C +++ b/src/OSspecific/MSwindows/MSwindows.C @@ -7,7 +7,7 @@ ------------------------------------------------------------------------------- Copyright (C) 2011-2017 OpenFOAM Foundation Copyright (C) 2011 Symscape - Copyright (C) 2016-2019 OpenCFD Ltd. + Copyright (C) 2016-2020 OpenCFD Ltd. ------------------------------------------------------------------------------- License 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 return !envName.empty() && diff --git a/src/OSspecific/POSIX/POSIX.C b/src/OSspecific/POSIX/POSIX.C index e32a27c1fa1..2ce70e37182 100644 --- a/src/OSspecific/POSIX/POSIX.C +++ b/src/OSspecific/POSIX/POSIX.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2017 OpenFOAM Foundation - Copyright (C) 2016-2019 OpenCFD Ltd. + Copyright (C) 2016-2020 OpenCFD Ltd. ------------------------------------------------------------------------------- License 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 return !envName.empty() && ::getenv(envName.c_str()) != nullptr; diff --git a/src/OpenFOAM/db/error/IOerror.C b/src/OpenFOAM/db/error/IOerror.C index c0cacb3185e..9ec6377dff1 100644 --- a/src/OpenFOAM/db/error/IOerror.C +++ b/src/OpenFOAM/db/error/IOerror.C @@ -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 << "\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) { - exitOrAbort(1, env("FOAM_ABORT")); + exitOrAbort(1, hasEnv("FOAM_ABORT")); } diff --git a/src/OpenFOAM/db/error/error.C b/src/OpenFOAM/db/error/error.C index dff61cfac9a..897b80a4cad 100644 --- a/src/OpenFOAM/db/error/error.C +++ b/src/OpenFOAM/db/error/error.C @@ -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 << "\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) { - exitOrAbort(errNo, env("FOAM_ABORT")); + exitOrAbort(errNo, hasEnv("FOAM_ABORT")); } diff --git a/src/OpenFOAM/include/OSspecific.H b/src/OpenFOAM/include/OSspecific.H index 0c4fc94c550..6c2a96f3993 100644 --- a/src/OpenFOAM/include/OSspecific.H +++ b/src/OpenFOAM/include/OSspecific.H @@ -66,16 +66,21 @@ pid_t pgid(); //- True if environment variable of given name is defined. // 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. -// 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); //- Set an environment variable, return true on success. // Using an empty name is a no-op and always returns false. 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) // Optionally with the full name (as per the '-f' option) string hostName(const bool full=false); -- GitLab