From 0927fd47fa900ede47416e27233d19fbff6a10d4 Mon Sep 17 00:00:00 2001 From: Will Bainbridge Date: Wed, 21 Jun 2023 12:14:29 +0100 Subject: [PATCH] stringOps: Rationalisation of expansions Specific names have been given for expand functions. Unused functions have been removed, and functions only used locally have been removed from the namespace. Documentation has been corrected. Default and alternative value handling has been removed from code template expansion. --- .../test/dictionary/Test-dictionary.C | 2 +- .../codedFixedValueFvPatchFieldTemplate.C | 10 +- .../codedFixedValuePointPatchFieldTemplate.C | 10 +- .../dynamicCode/codedFunction1Template.C | 6 +- .../dynamicCode/codedFunction2Template.C | 6 +- .../dynamicCode/codedFunctionObjectTemplate.C | 10 +- .../dynamicCode/codedFvModelTemplate.C | 10 +- .../codedMixedFvPatchFieldTemplate.C | 10 +- src/OpenFOAM/db/dictionary/dictionaryIO.C | 4 +- src/OpenFOAM/db/dictionary/entry/entryIO.C | 4 +- .../functionEntries/ifeqEntry/ifeqEntry.C | 4 +- .../includeEntry/includeEntry.C | 6 +- .../includeEtcEntry/includeEtcEntry.C | 4 +- .../primitiveEntry/primitiveEntry.C | 4 +- .../compileTemplate/compileTemplate.C | 10 +- .../dynamicLibrary/dynamicCode/dynamicCode.C | 10 +- .../dynamicCode/dynamicCodeContext.C | 30 +- .../codedFixedValuePointPatchField.C | 9 +- .../Function1/Coded/CodedFunction1.C | 10 +- .../Function2/Coded/CodedFunction2.C | 10 +- .../primitives/strings/string/string.C | 2 +- .../primitives/strings/stringOps/stringOps.C | 1066 ++++++++--------- .../primitives/strings/stringOps/stringOps.H | 210 +--- .../codedFixedValueFvPatchField.C | 8 +- .../codedMixed/codedMixedFvPatchField.C | 8 +- .../codedFunctionObject/codedFunctionObject.C | 8 +- 26 files changed, 620 insertions(+), 851 deletions(-) diff --git a/applications/test/dictionary/Test-dictionary.C b/applications/test/dictionary/Test-dictionary.C index da970045f4..6b46a61461 100644 --- a/applications/test/dictionary/Test-dictionary.C +++ b/applications/test/dictionary/Test-dictionary.C @@ -51,7 +51,7 @@ int main(int argc, char *argv[]) dict.add("aaOpenFOAMcc", 1); string s("DDD${aa${WM_PROJECT}cc}EEE"); - stringOps::inplaceExpand(s, dict, true, false); + stringOps::inplaceExpandEntry(s, dict, true, false); Info<< "variable expansion:" << s << endl; } diff --git a/etc/codeTemplates/dynamicCode/codedFixedValueFvPatchFieldTemplate.C b/etc/codeTemplates/dynamicCode/codedFixedValueFvPatchFieldTemplate.C index 5989b0c556..0746739419 100644 --- a/etc/codeTemplates/dynamicCode/codedFixedValueFvPatchFieldTemplate.C +++ b/etc/codeTemplates/dynamicCode/codedFixedValueFvPatchFieldTemplate.C @@ -93,7 +93,7 @@ ${typeName}FixedValueFvPatch${FieldType} : fixedValueFvPatchField<${TemplateType}>(p, iF, dict) { - if (${verbose:-false}) + if (${verbose}) { Info<<"construct ${typeName} sha1: ${SHA1sum}" " from patch/dictionary\n"; @@ -112,7 +112,7 @@ ${typeName}FixedValueFvPatch${FieldType} : fixedValueFvPatchField<${TemplateType}>(ptf, p, iF, mapper) { - if (${verbose:-false}) + if (${verbose}) { Info<<"construct ${typeName} sha1: ${SHA1sum}" " from patch/DimensionedField/mapper\n"; @@ -129,7 +129,7 @@ ${typeName}FixedValueFvPatch${FieldType} : fixedValueFvPatchField<${TemplateType}>(ptf, iF) { - if (${verbose:-false}) + if (${verbose}) { Info<<"construct ${typeName} sha1: ${SHA1sum} " "as copy/DimensionedField\n"; @@ -142,7 +142,7 @@ ${typeName}FixedValueFvPatch${FieldType} ${typeName}FixedValueFvPatch${FieldType}:: ~${typeName}FixedValueFvPatch${FieldType}() { - if (${verbose:-false}) + if (${verbose}) { Info<<"destroy ${typeName} sha1: ${SHA1sum}\n"; } @@ -158,7 +158,7 @@ void ${typeName}FixedValueFvPatch${FieldType}::updateCoeffs() return; } - if (${verbose:-false}) + if (${verbose}) { Info<<"updateCoeffs ${typeName} sha1: ${SHA1sum}\n"; } diff --git a/etc/codeTemplates/dynamicCode/codedFixedValuePointPatchFieldTemplate.C b/etc/codeTemplates/dynamicCode/codedFixedValuePointPatchFieldTemplate.C index fe0955df07..ca5cd6028d 100644 --- a/etc/codeTemplates/dynamicCode/codedFixedValuePointPatchFieldTemplate.C +++ b/etc/codeTemplates/dynamicCode/codedFixedValuePointPatchFieldTemplate.C @@ -92,7 +92,7 @@ ${typeName}FixedValuePointPatch${FieldType} : fixedValuePointPatchField<${TemplateType}>(p, iF, dict) { - if (${verbose:-false}) + if (${verbose}) { Info<<"construct ${typeName} sha1: ${SHA1sum}" " from patch/dictionary\n"; @@ -111,7 +111,7 @@ ${typeName}FixedValuePointPatch${FieldType} : fixedValuePointPatchField<${TemplateType}>(ptf, p, iF, mapper) { - if (${verbose:-false}) + if (${verbose}) { Info<<"construct ${typeName} sha1: ${SHA1sum}" " from patch/DimensionedField/mapper\n"; @@ -128,7 +128,7 @@ ${typeName}FixedValuePointPatch${FieldType} : fixedValuePointPatchField<${TemplateType}>(ptf, iF) { - if (${verbose:-false}) + if (${verbose}) { Info<<"construct ${typeName} sha1: ${SHA1sum} " "as copy/DimensionedField\n"; @@ -141,7 +141,7 @@ ${typeName}FixedValuePointPatch${FieldType} ${typeName}FixedValuePointPatch${FieldType}:: ~${typeName}FixedValuePointPatch${FieldType}() { - if (${verbose:-false}) + if (${verbose}) { Info<<"destroy ${typeName} sha1: ${SHA1sum}\n"; } @@ -157,7 +157,7 @@ void ${typeName}FixedValuePointPatch${FieldType}::updateCoeffs() return; } - if (${verbose:-false}) + if (${verbose}) { Info<<"updateCoeffs ${typeName} sha1: ${SHA1sum}\n"; } diff --git a/etc/codeTemplates/dynamicCode/codedFunction1Template.C b/etc/codeTemplates/dynamicCode/codedFunction1Template.C index 278054cea7..18453aa734 100644 --- a/etc/codeTemplates/dynamicCode/codedFunction1Template.C +++ b/etc/codeTemplates/dynamicCode/codedFunction1Template.C @@ -76,7 +76,7 @@ ${typeName}Function1${TemplateType} entryName ) { - if (${verbose:-false}) + if (${verbose}) { Info<< "Construct ${typeName} sha1: ${SHA1sum} from dictionary\n"; } @@ -94,7 +94,7 @@ ${typeName}Function1${TemplateType} f1 ) { - if (${verbose:-false}) + if (${verbose}) { Info<< "Construct ${typeName} sha1: ${SHA1sum} as copy\n"; } @@ -106,7 +106,7 @@ ${typeName}Function1${TemplateType} Foam::Function1s::${typeName}Function1${TemplateType}:: ~${typeName}Function1${TemplateType}() { - if (${verbose:-false}) + if (${verbose}) { Info<< "Destroy ${typeName} sha1: ${SHA1sum}\n"; } diff --git a/etc/codeTemplates/dynamicCode/codedFunction2Template.C b/etc/codeTemplates/dynamicCode/codedFunction2Template.C index 4f83b63179..49388693b1 100644 --- a/etc/codeTemplates/dynamicCode/codedFunction2Template.C +++ b/etc/codeTemplates/dynamicCode/codedFunction2Template.C @@ -76,7 +76,7 @@ ${typeName}Function2${TemplateType} entryName ) { - if (${verbose:-false}) + if (${verbose}) { Info<< "Construct ${typeName} sha1: ${SHA1sum} from dictionary\n"; } @@ -94,7 +94,7 @@ ${typeName}Function2${TemplateType} f1 ) { - if (${verbose:-false}) + if (${verbose}) { Info<< "Construct ${typeName} sha1: ${SHA1sum} as copy\n"; } @@ -106,7 +106,7 @@ ${typeName}Function2${TemplateType} Foam::Function2s::${typeName}Function2${TemplateType}:: ~${typeName}Function2${TemplateType}() { - if (${verbose:-false}) + if (${verbose}) { Info<< "Destroy ${typeName} sha1: ${SHA1sum}\n"; } diff --git a/etc/codeTemplates/dynamicCode/codedFunctionObjectTemplate.C b/etc/codeTemplates/dynamicCode/codedFunctionObjectTemplate.C index 881d5acd10..333013208d 100644 --- a/etc/codeTemplates/dynamicCode/codedFunctionObjectTemplate.C +++ b/etc/codeTemplates/dynamicCode/codedFunctionObjectTemplate.C @@ -108,7 +108,7 @@ ${typeName}FunctionObject::~${typeName}FunctionObject() bool ${typeName}FunctionObject::read(const dictionary& dict) { - if (${verbose:-false}) + if (${verbose}) { Info<<"read ${typeName} sha1: ${SHA1sum}\n"; } @@ -123,7 +123,7 @@ bool ${typeName}FunctionObject::read(const dictionary& dict) Foam::wordList ${typeName}FunctionObject::fields() const { - if (${verbose:-false}) + if (${verbose}) { Info<<"fields ${typeName} sha1: ${SHA1sum}\n"; } @@ -139,7 +139,7 @@ Foam::wordList ${typeName}FunctionObject::fields() const bool ${typeName}FunctionObject::execute() { - if (${verbose:-false}) + if (${verbose}) { Info<<"execute ${typeName} sha1: ${SHA1sum}\n"; } @@ -154,7 +154,7 @@ bool ${typeName}FunctionObject::execute() bool ${typeName}FunctionObject::write() { - if (${verbose:-false}) + if (${verbose}) { Info<<"write ${typeName} sha1: ${SHA1sum}\n"; } @@ -169,7 +169,7 @@ bool ${typeName}FunctionObject::write() bool ${typeName}FunctionObject::end() { - if (${verbose:-false}) + if (${verbose}) { Info<<"end ${typeName} sha1: ${SHA1sum}\n"; } diff --git a/etc/codeTemplates/dynamicCode/codedFvModelTemplate.C b/etc/codeTemplates/dynamicCode/codedFvModelTemplate.C index 50b31b858a..0f6137a355 100644 --- a/etc/codeTemplates/dynamicCode/codedFvModelTemplate.C +++ b/etc/codeTemplates/dynamicCode/codedFvModelTemplate.C @@ -103,7 +103,7 @@ ${typeName}FvModel${SourceType} fvModel(name, modelType, mesh, dict), set_(mesh, coeffs()) { - if (${verbose:-false}) + if (${verbose}) { Info<<"construct ${typeName} sha1: ${SHA1sum}" " from components\n"; @@ -116,7 +116,7 @@ ${typeName}FvModel${SourceType} ${typeName}FvModel${SourceType}:: ~${typeName}FvModel${SourceType}() { - if (${verbose:-false}) + if (${verbose}) { Info<<"destroy ${typeName} sha1: ${SHA1sum}\n"; } @@ -131,7 +131,7 @@ void ${typeName}FvModel${SourceType}::addSup const word& fieldName ) const { - if (${verbose:-false}) + if (${verbose}) { Info<<"${typeName}FvModel${SourceType}::addSup()\n"; } @@ -149,7 +149,7 @@ void ${typeName}FvModel${SourceType}::addSup const word& fieldName ) const { - if (${verbose:-false}) + if (${verbose}) { Info<<"${typeName}FvModel${SourceType}::addSup()\n"; } @@ -168,7 +168,7 @@ void ${typeName}FvModel${SourceType}::addSup const word& fieldName ) const { - if (${verbose:-false}) + if (${verbose}) { Info<<"${typeName}FvModel${SourceType}::addSup()\n"; } diff --git a/etc/codeTemplates/dynamicCode/codedMixedFvPatchFieldTemplate.C b/etc/codeTemplates/dynamicCode/codedMixedFvPatchFieldTemplate.C index e1759d7192..8abe8ffa40 100644 --- a/etc/codeTemplates/dynamicCode/codedMixedFvPatchFieldTemplate.C +++ b/etc/codeTemplates/dynamicCode/codedMixedFvPatchFieldTemplate.C @@ -93,7 +93,7 @@ ${typeName}MixedValueFvPatch${FieldType} : mixedFvPatchField<${TemplateType}>(p, iF, dict) { - if (${verbose:-false}) + if (${verbose}) { Info<<"construct ${typeName} sha1: ${SHA1sum}" " from patch/dictionary\n"; @@ -112,7 +112,7 @@ ${typeName}MixedValueFvPatch${FieldType} : mixedFvPatchField<${TemplateType}>(ptf, p, iF, mapper) { - if (${verbose:-false}) + if (${verbose}) { Info<<"construct ${typeName} sha1: ${SHA1sum}" " from patch/DimensionedField/mapper\n"; @@ -129,7 +129,7 @@ ${typeName}MixedValueFvPatch${FieldType} : mixedFvPatchField<${TemplateType}>(ptf, iF) { - if (${verbose:-false}) + if (${verbose}) { Info<<"construct ${typeName} sha1: ${SHA1sum} " "as copy/DimensionedField\n"; @@ -142,7 +142,7 @@ ${typeName}MixedValueFvPatch${FieldType} ${typeName}MixedValueFvPatch${FieldType}:: ~${typeName}MixedValueFvPatch${FieldType}() { - if (${verbose:-false}) + if (${verbose}) { Info<<"destroy ${typeName} sha1: ${SHA1sum}\n"; } @@ -158,7 +158,7 @@ void ${typeName}MixedValueFvPatch${FieldType}::updateCoeffs() return; } - if (${verbose:-false}) + if (${verbose}) { Info<<"updateCoeffs ${typeName} sha1: ${SHA1sum}\n"; } diff --git a/src/OpenFOAM/db/dictionary/dictionaryIO.C b/src/OpenFOAM/db/dictionary/dictionaryIO.C index f2533c2b57..d139b1d302 100644 --- a/src/OpenFOAM/db/dictionary/dictionaryIO.C +++ b/src/OpenFOAM/db/dictionary/dictionaryIO.C @@ -359,7 +359,7 @@ Foam::fileName Foam::findConfigFile { const fileName dictFile ( - stringOps::expand("$FOAM_CASE")/"system"/region/configName + stringOps::expandEnvVar("$FOAM_CASE")/"system"/region/configName ); if (isFile(dictFile)) @@ -374,7 +374,7 @@ Foam::fileName Foam::findConfigFile { const fileName dictFile ( - stringOps::expand("$FOAM_CASE")/"system"/configName + stringOps::expandEnvVar("$FOAM_CASE")/"system"/configName ); if (isFile(dictFile)) diff --git a/src/OpenFOAM/db/dictionary/entry/entryIO.C b/src/OpenFOAM/db/dictionary/entry/entryIO.C index ba6e676f93..d1f55c8770 100644 --- a/src/OpenFOAM/db/dictionary/entry/entryIO.C +++ b/src/OpenFOAM/db/dictionary/entry/entryIO.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | Website: https://openfoam.org - \\ / A nd | Copyright (C) 2011-2020 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2011-2023 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -192,7 +192,7 @@ bool Foam::entry::New(dictionary& parentDict, Istream& is) // Substitute dictionary and environment variables. Do not allow // empty substitutions. - stringOps::inplaceExpand(s, parentDict, true, false); + stringOps::inplaceExpandEntry(s, parentDict, true, false); keyword.std::string::replace(1, keyword.size() - 1, s); } diff --git a/src/OpenFOAM/db/dictionary/functionEntries/ifeqEntry/ifeqEntry.C b/src/OpenFOAM/db/dictionary/functionEntries/ifeqEntry/ifeqEntry.C index db88a2b3de..7153f3e4b1 100644 --- a/src/OpenFOAM/db/dictionary/functionEntries/ifeqEntry/ifeqEntry.C +++ b/src/OpenFOAM/db/dictionary/functionEntries/ifeqEntry/ifeqEntry.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | Website: https://openfoam.org - \\ / A nd | Copyright (C) 2018-2019 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2018-2023 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -102,7 +102,7 @@ Foam::token Foam::functionEntries::ifeqEntry::expand { // String expansion. Allow unset variables string expanded(var); - stringOps::inplaceExpand(expanded, dict, true, true); + stringOps::inplaceExpandEntry(expanded, dict, true, true); // Re-form as a string token so we can compare to string return token(expanded, t.lineNumber()); diff --git a/src/OpenFOAM/db/dictionary/functionEntries/includeEntry/includeEntry.C b/src/OpenFOAM/db/dictionary/functionEntries/includeEntry/includeEntry.C index 62982f711e..bd0e4e323f 100644 --- a/src/OpenFOAM/db/dictionary/functionEntries/includeEntry/includeEntry.C +++ b/src/OpenFOAM/db/dictionary/functionEntries/includeEntry/includeEntry.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | Website: https://openfoam.org - \\ / A nd | Copyright (C) 2011-2020 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2011-2023 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -78,7 +78,7 @@ Foam::fileName Foam::functionEntries::includeEntry::includeFileName fileName fName(is); // Substitute dictionary and environment variables. Allow empty // substitutions. - stringOps::inplaceExpand(fName, dict, true, true); + stringOps::inplaceExpandEntry(fName, dict, true, true); if (fName.empty() || fName.isAbsolute()) { @@ -102,7 +102,7 @@ Foam::fileName Foam::functionEntries::includeEntry::includeFileName fileName fName(f); // Substitute dictionary and environment variables. Allow empty // substitutions. - stringOps::inplaceExpand(fName, dict, true, true); + stringOps::inplaceExpandEntry(fName, dict, true, true); if (fName.empty() || fName.isAbsolute()) { diff --git a/src/OpenFOAM/db/dictionary/functionEntries/includeEtcEntry/includeEtcEntry.C b/src/OpenFOAM/db/dictionary/functionEntries/includeEtcEntry/includeEtcEntry.C index 4ce1029b4f..d3597ef753 100644 --- a/src/OpenFOAM/db/dictionary/functionEntries/includeEtcEntry/includeEtcEntry.C +++ b/src/OpenFOAM/db/dictionary/functionEntries/includeEtcEntry/includeEtcEntry.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | Website: https://openfoam.org - \\ / A nd | Copyright (C) 2015-2019 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2015-2023 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -78,7 +78,7 @@ Foam::fileName Foam::functionEntries::includeEtcEntry::includeEtcFileName // Substitute dictionary and environment variables. // Allow empty substitutions. - stringOps::inplaceExpand(fName, dict, true, true); + stringOps::inplaceExpandEntry(fName, dict, true, true); if (fName.empty() || fName.isAbsolute()) { diff --git a/src/OpenFOAM/db/dictionary/primitiveEntry/primitiveEntry.C b/src/OpenFOAM/db/dictionary/primitiveEntry/primitiveEntry.C index b06dbe4105..324e772dae 100644 --- a/src/OpenFOAM/db/dictionary/primitiveEntry/primitiveEntry.C +++ b/src/OpenFOAM/db/dictionary/primitiveEntry/primitiveEntry.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | Website: https://openfoam.org - \\ / A nd | Copyright (C) 2011-2019 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2011-2023 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -52,7 +52,7 @@ bool Foam::primitiveEntry::expandVariable // Substitute dictionary and environment variables. Do not allow // empty substitutions. - stringOps::inplaceExpand(s, dict, true, false); + stringOps::inplaceExpandEntry(s, dict, true, false); variable newW(w); newW.std::string::replace(1, newW.size() - 1, s); diff --git a/src/OpenFOAM/db/dynamicLibrary/compileTemplate/compileTemplate.C b/src/OpenFOAM/db/dynamicLibrary/compileTemplate/compileTemplate.C index 93cabb71ae..cc9832189c 100644 --- a/src/OpenFOAM/db/dynamicLibrary/compileTemplate/compileTemplate.C +++ b/src/OpenFOAM/db/dynamicLibrary/compileTemplate/compileTemplate.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | Website: https://openfoam.org - \\ / A nd | Copyright (C) 2021 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2021-2023 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -128,12 +128,12 @@ void Foam::compileTemplate::prepare // Define Make/options dynCode.setMakeOptions(context.options() + "\n\n" + context.libs()); - // Debugging: make verbose + // Make verbose if debugging + dynCode.setFilterVariable("verbose", Foam::name(bool(debug))); + if (debug) { - dynCode.setFilterVariable("verbose", "true"); - Info<<"compile " << codeName() << " sha1: " - << context.sha1() << endl; + Info<<"compile " << codeName() << " sha1: " << context.sha1() << endl; } } diff --git a/src/OpenFOAM/db/dynamicLibrary/dynamicCode/dynamicCode.C b/src/OpenFOAM/db/dynamicLibrary/dynamicCode/dynamicCode.C index 742b8a0498..5e3fb9b348 100644 --- a/src/OpenFOAM/db/dynamicLibrary/dynamicCode/dynamicCode.C +++ b/src/OpenFOAM/db/dynamicLibrary/dynamicCode/dynamicCode.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | Website: https://openfoam.org - \\ / A nd | Copyright (C) 2011-2021 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2011-2023 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -132,7 +132,7 @@ void Foam::dynamicCode::copyAndFilter // Expand according to mapping. // Expanding according to env variables might cause too many // surprises - stringOps::inplaceExpand(line, mapping); + stringOps::inplaceExpandCodeTemplate(line, mapping); os.writeQuoted(line, false) << nl; } while (is.good()); @@ -322,8 +322,8 @@ bool Foam::dynamicCode::writeDigest(const std::string& sha1) const Foam::dynamicCode::dynamicCode(const word& codeName, const word& codeDirName) : - codeRoot_(stringOps::expand("$FOAM_CASE")/topDirName), - libSubDir_(stringOps::expand("platforms/$WM_OPTIONS/lib")), + codeRoot_(stringOps::expandEnvVar("$FOAM_CASE")/topDirName), + libSubDir_(stringOps::expandEnvVar("platforms/$WM_OPTIONS/lib")), codeName_(codeName), codeDirName_(codeDirName) { @@ -491,7 +491,7 @@ bool Foam::dynamicCode::copyOrCreateFiles(const bool verbose) const { const fileName dstFile ( - outputDir/stringOps::expand(createFiles_[fileI].first()) + outputDir/stringOps::expandEnvVar(createFiles_[fileI].first()) ); mkDir(dstFile.path()); diff --git a/src/OpenFOAM/db/dynamicLibrary/dynamicCode/dynamicCodeContext.C b/src/OpenFOAM/db/dynamicLibrary/dynamicCode/dynamicCodeContext.C index ebe94087f3..f5ed95b388 100644 --- a/src/OpenFOAM/db/dynamicLibrary/dynamicCode/dynamicCodeContext.C +++ b/src/OpenFOAM/db/dynamicLibrary/dynamicCode/dynamicCodeContext.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | Website: https://openfoam.org - \\ / A nd | Copyright (C) 2011-2019 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2011-2023 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -63,15 +63,9 @@ Foam::dynamicCodeContext::dynamicCodeContext codePtrs[i] = dict.lookupEntryPtr(key, false, false); if (codePtrs[i]) { - code_.insert - ( - key, - stringOps::expand - ( - stringOps::trim(verbatimString(codePtrs[i]->stream())), - dict - ) - ); + string s(stringOps::trim(verbatimString(codePtrs[i]->stream()))); + stringOps::inplaceExpandCodeString(s, dict); + code_.insert(key, s); } else { @@ -83,24 +77,16 @@ Foam::dynamicCodeContext::dynamicCodeContext const entry* optionsPtr = dict.lookupEntryPtr("codeOptions", false, false); if (optionsPtr) { - options_ = - stringOps::expand - ( - stringOps::trim(verbatimString(optionsPtr->stream())), - dict - ); + options_ = stringOps::trim(verbatimString(optionsPtr->stream())); + stringOps::inplaceExpandCodeString(options_, dict); } // Libs const entry* libsPtr = dict.lookupEntryPtr("codeLibs", false, false); if (libsPtr) { - libs_ = - stringOps::expand - ( - stringOps::trim(verbatimString(libsPtr->stream())), - dict - ); + libs_ = stringOps::trim(verbatimString(libsPtr->stream())); + stringOps::inplaceExpandCodeString(libs_, dict); } // Calculate SHA1 digest from all entries diff --git a/src/OpenFOAM/fields/pointPatchFields/derived/codedFixedValue/codedFixedValuePointPatchField.C b/src/OpenFOAM/fields/pointPatchFields/derived/codedFixedValue/codedFixedValuePointPatchField.C index 2a5b9786d1..e2ce06b437 100644 --- a/src/OpenFOAM/fields/pointPatchFields/derived/codedFixedValue/codedFixedValuePointPatchField.C +++ b/src/OpenFOAM/fields/pointPatchFields/derived/codedFixedValue/codedFixedValuePointPatchField.C @@ -71,13 +71,12 @@ void Foam::codedFixedValuePointPatchField::prepare // Copy filtered H template dynCode.addCopyFile(codeTemplateH("codedFixedValuePointPatchField")); - // Debugging: make BC verbose + // Make verbose if debugging + dynCode.setFilterVariable("verbose", Foam::name(bool(debug))); + if (debug) { - // Debugging: make BC verbose - dynCode.setFilterVariable("verbose", "true"); - Info<<"compile " << codeName() << " sha1: " - << context.sha1() << endl; + Info<<"compile " << codeName() << " sha1: " << context.sha1() << endl; } // Define Make/options diff --git a/src/OpenFOAM/primitives/functions/Function1/Coded/CodedFunction1.C b/src/OpenFOAM/primitives/functions/Function1/Coded/CodedFunction1.C index 945e8ead06..5f7e67e5b3 100644 --- a/src/OpenFOAM/primitives/functions/Function1/Coded/CodedFunction1.C +++ b/src/OpenFOAM/primitives/functions/Function1/Coded/CodedFunction1.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | Website: https://openfoam.org - \\ / A nd | Copyright (C) 2020-2021 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2020-2023 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -58,12 +58,12 @@ void Foam::Function1s::Coded::prepare // Copy filtered H template dynCode.addCopyFile(codeTemplateH("codedFunction1")); - // Debugging: make verbose + // Make verbose if debugging + dynCode.setFilterVariable("verbose", Foam::name(bool(debug))); + if (debug) { - dynCode.setFilterVariable("verbose", "true"); - Info<<"compile " << codeName() << " sha1: " - << context.sha1() << endl; + Info<<"compile " << codeName() << " sha1: " << context.sha1() << endl; } // Define Make/options diff --git a/src/OpenFOAM/primitives/functions/Function2/Coded/CodedFunction2.C b/src/OpenFOAM/primitives/functions/Function2/Coded/CodedFunction2.C index bbe951f492..bb89aae046 100644 --- a/src/OpenFOAM/primitives/functions/Function2/Coded/CodedFunction2.C +++ b/src/OpenFOAM/primitives/functions/Function2/Coded/CodedFunction2.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | Website: https://openfoam.org - \\ / A nd | Copyright (C) 2020-2021 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2020-2023 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -58,12 +58,12 @@ void Foam::Function2s::Coded::prepare // Copy filtered H template dynCode.addCopyFile(codeTemplateH("codedFunction2")); - // Debugging: make verbose + // Make verbose if debugging + dynCode.setFilterVariable("verbose", Foam::name(bool(debug))); + if (debug) { - dynCode.setFilterVariable("verbose", "true"); - Info<<"compile " << codeName() << " sha1: " - << context.sha1() << endl; + Info<<"compile " << codeName() << " sha1: " << context.sha1() << endl; } // Define Make/options diff --git a/src/OpenFOAM/primitives/strings/string/string.C b/src/OpenFOAM/primitives/strings/string/string.C index 8fa9a0e8d1..b2f338c83c 100644 --- a/src/OpenFOAM/primitives/strings/string/string.C +++ b/src/OpenFOAM/primitives/strings/string/string.C @@ -124,7 +124,7 @@ Foam::string Foam::string::replaceAll Foam::string& Foam::string::expand(const bool allowEmpty) { - stringOps::inplaceExpand(*this, allowEmpty); + stringOps::inplaceExpandEnvVar(*this, allowEmpty); return *this; } diff --git a/src/OpenFOAM/primitives/strings/stringOps/stringOps.C b/src/OpenFOAM/primitives/strings/stringOps/stringOps.C index cd49532668..e2e9b0aebc 100644 --- a/src/OpenFOAM/primitives/strings/stringOps/stringOps.C +++ b/src/OpenFOAM/primitives/strings/stringOps/stringOps.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | Website: https://openfoam.org - \\ / A nd | Copyright (C) 2011-2022 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2011-2023 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -29,12 +29,17 @@ License // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // +namespace Foam +{ + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + // Find the type/position of the ":-" or ":+" alternative values static inline int findParameterAlternative ( - const std::string& s, - std::string::size_type& pos, - std::string::size_type endPos + const string& s, + string::size_type& pos, + string::size_type endPos ) { while (pos != std::string::npos) @@ -65,187 +70,8 @@ static inline int findParameterAlternative } -Foam::string Foam::stringOps::expand -( - const string& original, - const HashTable& mapping, - const char sigil -) -{ - string s(original); - return inplaceExpand(s, mapping); -} - - -Foam::string& Foam::stringOps::inplaceExpand -( - string& s, - const HashTable& mapping, - const char sigil -) -{ - string::size_type begVar = 0; - - // Expand $VAR or ${VAR} - // Repeat until nothing more is found - while - ( - (begVar = s.find(sigil, begVar)) != string::npos - && begVar < s.size()-1 - ) - { - if (begVar == 0 || s[begVar-1] != '\\') - { - // Find end of first occurrence - string::size_type endVar = begVar; - string::size_type delim = 0; - - // The type/position of the ":-" or ":+" alternative values - int altType = 0; - string::size_type altPos = string::npos; - - if (s[begVar+1] == '{') - { - endVar = s.find('}', begVar); - delim = 1; - - // Check for ${parameter:-word} or ${parameter:+word} - if (endVar != string::npos) - { - altPos = begVar; - altType = findParameterAlternative(s, altPos, endVar); - } - } - else - { - string::iterator iter = s.begin() + begVar + 1; - - // Accept all dictionary and environment variable characters - while - ( - iter != s.end() - && - ( - isalnum(*iter) - || *iter == '/' // For dictionary slash syntax - || *iter == '!' // For dictionary slash syntax - || *iter == '.' // For dictionary dot syntax - || *iter == ':' // For dictionary dot syntax - || *iter == '_' - ) - ) - { - ++iter; - ++endVar; - } - } - - if (endVar == string::npos) - { - // Likely parsed '${...' without closing '}' - abort - break; - } - else if (endVar == begVar) - { - // Parsed '${}' or $badChar - skip over - begVar = endVar + 1; - } - else - { - const word varName - ( - s.substr - ( - begVar + 1 + delim, - ( - (altPos == string::npos ? endVar : altPos) - - begVar - 2*delim - ) - ), - false - ); - - std::string altValue; - if (altPos != string::npos) - { - // Had ":-" or ":+" alternative value - altValue = s.substr - ( - altPos + 2, - endVar - altPos - 2*delim - ); - } - - - HashTable::const_iterator fnd = - mapping.find(varName); - - if (fnd != HashTable::end()) - { - if (altPos != string::npos && altType == '+') - { - // Was found, use ":+" alternative - s.std::string::replace - ( - begVar, - endVar - begVar + 1, - altValue - ); - begVar += altValue.size(); - } - else - { - // Was found, use value - s.std::string::replace - ( - begVar, - endVar - begVar + 1, - *fnd - ); - begVar += (*fnd).size(); - } - } - else if (altPos != string::npos && altType == '-') - { - // Was not found, use ":-" alternative - s.std::string::replace - ( - begVar, - endVar - begVar + 1, - altValue - ); - begVar += altValue.size(); - } - else - { - // Substitute with nothing, also for ":+" alternative - s.std::string::erase(begVar, endVar - begVar + 1); - } - } - } - else - { - ++begVar; - } - } - - return s; -} - - -Foam::string Foam::stringOps::expand -( - const string& original, - const dictionary& dict, - const char sigil -) -{ - string s(original); - return inplaceExpand(s, dict, sigil); -} - - -Foam::string Foam::stringOps::getVariable +//- Get dictionary or (optionally) environment variable +string getVariable ( const word& name, const dictionary& dict, @@ -314,7 +140,9 @@ Foam::string Foam::stringOps::getVariable } -Foam::string Foam::stringOps::expand +//- Recursively expands dictionary or environment variable starting at index +// in string. Updates index. +string expand ( const string& s, string::size_type& index, @@ -348,7 +176,474 @@ Foam::string Foam::stringOps::expand } -Foam::string& Foam::stringOps::inplaceExpand +//- Expand path parts of a string +Foam::string& inplaceExpandPath(string& s) +{ + if (!s.empty()) + { + if (s[0] == '~') + { + // Expand initial ~ + // ~/ => home directory + // ~OpenFOAM => site/user OpenFOAM configuration directory + // ~user => home directory for specified user + + string user; + fileName file; + + const string::size_type pos = s.find('/'); + if (pos != string::npos) + { + user = s.substr(1, pos - 1); + file = s.substr(pos + 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; +} + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +} // End namespace Foam + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +Foam::string Foam::stringOps::expandEnvVar +( + const string& original, + const bool allowEmpty +) +{ + string s(original); + return inplaceExpandEnvVar(s, allowEmpty); +} + + +Foam::string& Foam::stringOps::inplaceExpandEnvVar +( + string& s, + const bool allowEmpty +) +{ + string::size_type begVar = 0; + + // Expand $VARS + // Repeat until nothing more is found + while + ( + (begVar = s.find('$', begVar)) != string::npos + && begVar < s.size()-1 + ) + { + if (begVar == 0 || s[begVar-1] != '\\') + { + // Find end of first occurrence + string::size_type endVar = begVar; + string::size_type delim = 0; + + // The type/position of the ":-" or ":+" alternative values + int altType = 0; + string::size_type altPos = string::npos; + + if (s[begVar+1] == '{') + { + endVar = s.find('}', begVar); + delim = 1; + + // Check for ${parameter:-word} or ${parameter:+word} + if (endVar != string::npos) + { + altPos = begVar; + altType = findParameterAlternative(s, altPos, endVar); + } + } + else + { + string::iterator iter = s.begin() + begVar + 1; + + while + ( + iter != s.end() + && (isalnum(*iter) || *iter == '_') + ) + { + ++iter; + ++endVar; + } + } + + if (endVar == string::npos) + { + // Likely parsed '${...' without closing '}' - abort + break; + } + else if (endVar == begVar) + { + // Parsed '${}' or $badChar - skip over + begVar = endVar + 1; + } + else + { + const word varName + ( + s.substr + ( + begVar + 1 + delim, + ( + (altPos == string::npos ? endVar : altPos) + - begVar - 2*delim + ) + ), + false + ); + + std::string altValue; + if (altPos != string::npos) + { + // Had ":-" or ":+" alternative value + altValue = s.substr + ( + altPos + 2, + endVar - altPos - 2*delim + ); + } + + const string varValue = getEnv(varName); + if (varValue.size()) + { + if (altPos != string::npos && altType == '+') + { + // Was found, use ":+" alternative + s.std::string::replace + ( + begVar, + endVar - begVar + 1, + altValue + ); + begVar += altValue.size(); + } + else + { + // Was found, use value + s.std::string::replace + ( + begVar, + endVar - begVar + 1, + varValue + ); + begVar += varValue.size(); + } + } + else if (altPos != string::npos) + { + // Use ":-" or ":+" alternative values + if (altType == '-') + { + // Was not found, use ":-" alternative + s.std::string::replace + ( + begVar, + endVar - begVar + 1, + altValue + ); + begVar += altValue.size(); + } + else + { + // Was not found, ":+" alternative implies + // substitute with nothing + s.std::string::erase(begVar, endVar - begVar + 1); + } + } + else if (allowEmpty) + { + s.std::string::erase(begVar, endVar - begVar + 1); + } + else + { + FatalErrorInFunction + << "Unknown variable name '" << varName << "'" + << exit(FatalError); + } + } + } + else + { + ++begVar; + } + } + + return inplaceExpandPath(s); +} + + +Foam::string& Foam::stringOps::inplaceExpandCodeString +( + string& s, + const dictionary& dict, + const char sigil +) +{ + string::size_type begVar = 0; + + // Expand $VAR or ${VAR} + // Repeat until nothing more is found + while + ( + (begVar = s.find(sigil, begVar)) != string::npos + && begVar < s.size()-1 + ) + { + if (begVar == 0 || s[begVar-1] != '\\') + { + // Find end of first occurrence + string::size_type endVar = begVar; + string::size_type delim = 0; + + if (s[begVar+1] == '{') + { + endVar = s.find('}', begVar); + delim = 1; + } + else + { + string::iterator iter = s.begin() + begVar + 1; + + // Accept all dictionary and environment variable characters + while + ( + iter != s.end() + && + ( + isalnum(*iter) + || *iter == '/' // For dictionary slash syntax + || *iter == '!' // For dictionary slash syntax + || *iter == '.' // For dictionary dot syntax + || *iter == ':' // For dictionary dot syntax + || *iter == '_' + ) + ) + { + ++iter; + ++endVar; + } + } + + if (endVar == string::npos) + { + // Likely parsed '${...' without closing '}' - abort + break; + } + else if (endVar == begVar) + { + // Parsed '${}' or $badChar - skip over + begVar = endVar + 1; + } + else + { + const word varName + ( + s.substr + ( + begVar + 1 + delim, + endVar - begVar - 2*delim + ), + false + ); + + // Lookup in the dictionary + const entry* ePtr = dict.lookupScopedEntryPtr + ( + varName, + true, + false // Wildcards disabled. See primitiveEntry + ); + + // Substitute if found + if (ePtr) + { + // Write to string buffer. Force floating point numbers to + // be printed with at least some decimal digits. + OStringStream buf; + buf << scientific; + buf.precision(IOstream::defaultPrecision()); + + if (ePtr->isDict()) + { + ePtr->dict().write(buf, false); + } + else if (isA(*ePtr)) + { + dynamicCast(*ePtr) + .write(buf, true); + } + else + { + // Can't do any other types. Fail. + dynamicCast(*ePtr); + } + + s.std::string::replace + ( + begVar, + endVar - begVar + 1, + buf.str() + ); + begVar += buf.str().size(); + } + else + { + // Not found. Leave original string untouched. + begVar = endVar + 1; + } + } + } + else + { + ++begVar; + } + } + + return s; +} + + +Foam::string& Foam::stringOps::inplaceExpandCodeTemplate +( + string& s, + const HashTable& mapping, + const char sigil +) +{ + string::size_type begVar = 0; + + // Expand $VAR or ${VAR} + // Repeat until nothing more is found + while + ( + (begVar = s.find(sigil, begVar)) != string::npos + && begVar < s.size()-1 + ) + { + if (begVar == 0 || s[begVar-1] != '\\') + { + // Find end of first occurrence + string::size_type endVar = begVar; + string::size_type delim = 0; + + if (s[begVar+1] == '{') + { + endVar = s.find('}', begVar); + delim = 1; + } + else + { + string::iterator iter = s.begin() + begVar + 1; + + // Accept all dictionary and environment variable characters + while + ( + iter != s.end() + && + ( + isalnum(*iter) + || *iter == '/' // For dictionary slash syntax + || *iter == '!' // For dictionary slash syntax + || *iter == '.' // For dictionary dot syntax + || *iter == ':' // For dictionary dot syntax + || *iter == '_' + ) + ) + { + ++iter; + ++endVar; + } + } + + if (endVar == string::npos) + { + // Likely parsed '${...' without closing '}' - abort + break; + } + else if (endVar == begVar) + { + // Parsed '${}' or $badChar - skip over + begVar = endVar + 1; + } + else + { + const word varName + ( + s.substr + ( + begVar + 1 + delim, + endVar - begVar - 2*delim + ), + false + ); + + // Lookup in the table + HashTable::const_iterator fnd = + mapping.find(varName); + + // Substitute if found + if (fnd != HashTable::end()) + { + s.std::string::replace + ( + begVar, + endVar - begVar + 1, + *fnd + ); + begVar += (*fnd).size(); + } + else + { + // Not found. Remove. + s.std::string::erase(begVar, endVar - begVar + 1); + } + } + } + else + { + ++begVar; + } + } + + return s; +} + + +Foam::string& Foam::stringOps::inplaceExpandEntry ( string& s, const dictionary& dict, @@ -455,402 +750,7 @@ Foam::string& Foam::stringOps::inplaceExpand } } - if (!s.empty()) - { - 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; -} - - -Foam::string& Foam::stringOps::inplaceExpand -( - string& s, - const dictionary& dict, - const char sigil -) -{ - string::size_type begVar = 0; - - // Expand $VAR or ${VAR} - // Repeat until nothing more is found - while - ( - (begVar = s.find(sigil, begVar)) != string::npos - && begVar < s.size()-1 - ) - { - if (begVar == 0 || s[begVar-1] != '\\') - { - // Find end of first occurrence - string::size_type endVar = begVar; - string::size_type delim = 0; - - if (s[begVar+1] == '{') - { - endVar = s.find('}', begVar); - delim = 1; - } - else - { - string::iterator iter = s.begin() + begVar + 1; - - // Accept all dictionary and environment variable characters - while - ( - iter != s.end() - && - ( - isalnum(*iter) - || *iter == '/' // For dictionary slash syntax - || *iter == '!' // For dictionary slash syntax - || *iter == '.' // For dictionary dot syntax - || *iter == ':' // For dictionary dot syntax - || *iter == '_' - ) - ) - { - ++iter; - ++endVar; - } - } - - if (endVar == string::npos) - { - // Likely parsed '${...' without closing '}' - abort - break; - } - else if (endVar == begVar) - { - // Parsed '${}' or $badChar - skip over - begVar = endVar + 1; - } - else - { - const word varName - ( - s.substr - ( - begVar + 1 + delim, - endVar - begVar - 2*delim - ), - false - ); - - - // Lookup in the dictionary - const entry* ePtr = dict.lookupScopedEntryPtr - ( - varName, - true, - false // Wildcards disabled. See primitiveEntry - ); - - // If defined - copy its entries - if (ePtr) - { - OStringStream buf; - // Force floating point numbers to be printed with at least - // some decimal digits. - buf << scientific; - buf.precision(IOstream::defaultPrecision()); - if (ePtr->isDict()) - { - ePtr->dict().write(buf, false); - } - else - { - // Fail for other types - dynamicCast - ( - *ePtr - ).write(buf, true); - } - - s.std::string::replace - ( - begVar, - endVar - begVar + 1, - buf.str() - ); - begVar += buf.str().size(); - } - else - { - // Not defined - leave original string untouched - begVar = endVar + 1; - } - } - } - else - { - ++begVar; - } - } - - return s; -} - - -Foam::string Foam::stringOps::expand -( - const string& original, - const bool allowEmpty -) -{ - string s(original); - return inplaceExpand(s, allowEmpty); -} - - -Foam::string& Foam::stringOps::inplaceExpand -( - string& s, - const bool allowEmpty -) -{ - string::size_type begVar = 0; - - // Expand $VARS - // Repeat until nothing more is found - while - ( - (begVar = s.find('$', begVar)) != string::npos - && begVar < s.size()-1 - ) - { - if (begVar == 0 || s[begVar-1] != '\\') - { - // Find end of first occurrence - string::size_type endVar = begVar; - string::size_type delim = 0; - - // The type/position of the ":-" or ":+" alternative values - int altType = 0; - string::size_type altPos = string::npos; - - if (s[begVar+1] == '{') - { - endVar = s.find('}', begVar); - delim = 1; - - // Check for ${parameter:-word} or ${parameter:+word} - if (endVar != string::npos) - { - altPos = begVar; - altType = findParameterAlternative(s, altPos, endVar); - } - } - else - { - string::iterator iter = s.begin() + begVar + 1; - - while - ( - iter != s.end() - && (isalnum(*iter) || *iter == '_') - ) - { - ++iter; - ++endVar; - } - } - - - if (endVar == string::npos) - { - // Likely parsed '${...' without closing '}' - abort - break; - } - else if (endVar == begVar) - { - // Parsed '${}' or $badChar - skip over - begVar = endVar + 1; - } - else - { - const word varName - ( - s.substr - ( - begVar + 1 + delim, - ( - (altPos == string::npos ? endVar : altPos) - - begVar - 2*delim - ) - ), - false - ); - - std::string altValue; - if (altPos != string::npos) - { - // Had ":-" or ":+" alternative value - altValue = s.substr - ( - altPos + 2, - endVar - altPos - 2*delim - ); - } - - const string varValue = getEnv(varName); - if (varValue.size()) - { - if (altPos != string::npos && altType == '+') - { - // Was found, use ":+" alternative - s.std::string::replace - ( - begVar, - endVar - begVar + 1, - altValue - ); - begVar += altValue.size(); - } - else - { - // Was found, use value - s.std::string::replace - ( - begVar, - endVar - begVar + 1, - varValue - ); - begVar += varValue.size(); - } - } - else if (altPos != string::npos) - { - // Use ":-" or ":+" alternative values - if (altType == '-') - { - // Was not found, use ":-" alternative - s.std::string::replace - ( - begVar, - endVar - begVar + 1, - altValue - ); - begVar += altValue.size(); - } - else - { - // Was not found, ":+" alternative implies - // substitute with nothing - s.std::string::erase(begVar, endVar - begVar + 1); - } - } - else if (allowEmpty) - { - s.std::string::erase(begVar, endVar - begVar + 1); - } - else - { - FatalErrorInFunction - << "Unknown variable name '" << varName << "'" - << exit(FatalError); - } - } - } - else - { - ++begVar; - } - } - - if (!s.empty()) - { - 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 inplaceExpandPath(s); } diff --git a/src/OpenFOAM/primitives/strings/stringOps/stringOps.H b/src/OpenFOAM/primitives/strings/stringOps/stringOps.H index 78422637b2..f6ab369fe1 100644 --- a/src/OpenFOAM/primitives/strings/stringOps/stringOps.H +++ b/src/OpenFOAM/primitives/strings/stringOps/stringOps.H @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | Website: https://openfoam.org - \\ / A nd | Copyright (C) 2011-2022 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2011-2023 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -32,6 +32,7 @@ SourceFiles stringOps.C \*---------------------------------------------------------------------------*/ + #ifndef stringOps_H #define stringOps_H @@ -50,10 +51,16 @@ namespace Foam namespace stringOps { - //- Expand occurrences of variables according to the mapping + //- Expand all occurrences of environment variables and paths // Expansion includes: - // -# variables + // -# environment variables // - "$VAR", "${VAR}" + // -# current directory + // - leading "./" : the current directory + // -# tilde expansion + // - leading "~/" : home directory + // - leading "~user" : home directory for specified user + // - leading "~OpenFOAM" : site/user OpenFOAM configuration directory // // Supports default values as per the Bourne/Korn shell. // \code @@ -69,105 +76,78 @@ namespace stringOps // If parameter is unset or null, nothing is substituted. // Otherwise the \c altValue is substituted. // - // Any unknown entries are removed silently. + // Any unknown entries are removed silently, if allowEmpty is true. // // Malformed entries (eg, brace mismatch, sigil followed by bad character) // are left as is. // - // \note the leading sigil can be changed to avoid conflicts with other - // string expansions - string expand + // \sa + // Foam::findEtcFile + string expandEnvVar ( const string&, - const HashTable& mapping, - const char sigil = '$' + const bool allowEmpty = false ); + //- Expand all occurrences of environment variables and paths + // See expandEnvVar + string& inplaceExpandEnvVar + ( + string&, + const bool allowEmpty = false + ); + + //- Inplace expand occurrences of variables according to the dictionary + // Expansion includes: + // -# variables + // - "$VAR", "${VAR}" + // + // Any unknown entries are left as-is + // + // \note the leading sigil can be changed to avoid conflicts with other + // string expansions + string& inplaceExpandCodeString + ( + string&, + const dictionary& dict, + const char sigil = '$' + ); //- Inplace expand occurrences of variables according to the mapping // Expansion includes: // -# variables // - "$VAR", "${VAR}" // - // Supports default values as per the Bourne/Korn shell. - // \code - // "${parameter:-defValue}" - // \endcode - // 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. - // Otherwise the \c altValue is substituted. - // // Any unknown entries are removed silently. // - // Malformed entries (eg, brace mismatch, sigil followed by bad character) - // are left as is. - // // \note the leading sigil can be changed to avoid conflicts with other // string expansions - string& inplaceExpand + string& inplaceExpandCodeTemplate ( string&, const HashTable& mapping, const char sigil = '$' ); - //- Expand occurrences of variables according to the dictionary - // Expansion includes: - // -# variables - // - "$VAR", "${VAR}" - // - // Any unknown entries are left as-is - // - // \note the leading sigil can be changed to avoid conflicts with other - // string expansions - string expand - ( - const string&, - const dictionary& dict, - const char sigil = '$' - ); - - - //- Get dictionary or (optionally) environment variable - string getVariable - ( - const word& name, - const dictionary& dict, - const bool allowEnvVars, - const bool allowEmpty - ); - - - //- Recursively expands (dictionary or environment) variable - // starting at index in string. Updates index. - string expand - ( - const string& s, - string::size_type& index, - const dictionary& dict, - const bool allowEnvVars, - const bool allowEmpty - ); - - //- Inplace expand occurrences of variables according to the dictionary // and optionally environment variables // Expansion includes: // -# variables // - "$VAR", "${VAR}" + // -# current directory + // - leading "./" : the current directory + // -# tilde expansion + // - leading "~/" : home directory + // - leading "~user" : home directory for specified user + // - leading "~OpenFOAM" : site/user OpenFOAM configuration directory // // with the "${}" syntax doing a recursive substitution. + // // Any unknown entries are left as-is // // \note the leading sigil can be changed to avoid conflicts with other // string expansions - string& inplaceExpand + string& inplaceExpandEntry ( string& s, const dictionary& dict, @@ -177,102 +157,6 @@ namespace stringOps ); - //- Inplace expand occurrences of variables according to the dictionary - // Expansion includes: - // -# variables - // - "$VAR", "${VAR}" - // - // Any unknown entries are left as-is - // - // \note the leading sigil can be changed to avoid conflicts with other - // string expansions - string& inplaceExpand - ( - string&, - const dictionary& dict, - const char sigil = '$' - ); - - - //- Expand initial tildes and all occurrences of environment variables - // Expansion includes: - // -# environment variables - // - "$VAR", "${VAR}" - // -# current directory - // - leading "./" : the current directory - // -# tilde expansion - // - leading "~/" : home directory - // - leading "~user" : home directory for specified user - // - leading "~OpenFOAM" : site/user OpenFOAM configuration directory - // - // Supports default values as per the Bourne/Korn shell. - // \code - // "${parameter:-defValue}" - // \endcode - // 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. - // 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) - // are left as is. - // - // \sa - // Foam::findEtcFile - string expand - ( - const string&, - const bool allowEmpty = false - ); - - - //- Expand initial tildes and all occurrences of environment variables - // Expansion includes: - // -# environment variables - // - "$VAR", "${VAR}" - // -# current directory - // - leading "./" : the current directory - // -# tilde expansion - // - leading "~/" : home directory - // - leading "~user" : home directory for specified user - // - leading "~OpenFOAM" : site/user OpenFOAM configuration directory - // - // Supports default values as per the Bourne/Korn shell. - // \code - // "${parameter:-defValue}" - // \endcode - // 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. - // 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) - // are left as is. - // - // Any unknown entries are removed silently if allowEmpty is true. - // \sa - // Foam::findEtcFile - string& inplaceExpand - ( - string&, - const bool allowEmpty = false - ); - - //- Return string trimmed of leading whitespace string trimLeft(const string&); diff --git a/src/finiteVolume/fields/fvPatchFields/derived/codedFixedValue/codedFixedValueFvPatchField.C b/src/finiteVolume/fields/fvPatchFields/derived/codedFixedValue/codedFixedValueFvPatchField.C index 82f891234b..09f24bdad1 100644 --- a/src/finiteVolume/fields/fvPatchFields/derived/codedFixedValue/codedFixedValueFvPatchField.C +++ b/src/finiteVolume/fields/fvPatchFields/derived/codedFixedValue/codedFixedValueFvPatchField.C @@ -70,12 +70,12 @@ void Foam::codedFixedValueFvPatchField::prepare // Copy filtered H template dynCode.addCopyFile(codeTemplateH("codedFixedValueFvPatchField")); - // Debugging: make BC verbose + // Make verbose if debugging + dynCode.setFilterVariable("verbose", Foam::name(bool(debug))); + if (debug) { - dynCode.setFilterVariable("verbose", "true"); - Info<<"compile " << codeName() << " sha1: " - << context.sha1() << endl; + Info<<"compile " << codeName() << " sha1: " << context.sha1() << endl; } // Define Make/options diff --git a/src/finiteVolume/fields/fvPatchFields/derived/codedMixed/codedMixedFvPatchField.C b/src/finiteVolume/fields/fvPatchFields/derived/codedMixed/codedMixedFvPatchField.C index 57dcbf1143..029917e906 100644 --- a/src/finiteVolume/fields/fvPatchFields/derived/codedMixed/codedMixedFvPatchField.C +++ b/src/finiteVolume/fields/fvPatchFields/derived/codedMixed/codedMixedFvPatchField.C @@ -70,12 +70,12 @@ void Foam::codedMixedFvPatchField::prepare // Copy filtered H template dynCode.addCopyFile(codeTemplateH("codedMixedFvPatchField")); - // Debugging: make BC verbose + // Make verbose if debugging + dynCode.setFilterVariable("verbose", Foam::name(bool(debug))); + if (debug) { - dynCode.setFilterVariable("verbose", "true"); - Info<<"compile " << codeName() << " sha1: " - << context.sha1() << endl; + Info<<"compile " << codeName() << " sha1: " << context.sha1() << endl; } // Define Make/options diff --git a/src/functionObjects/utilities/codedFunctionObject/codedFunctionObject.C b/src/functionObjects/utilities/codedFunctionObject/codedFunctionObject.C index 76e16e4d5a..a0f8f5d22b 100644 --- a/src/functionObjects/utilities/codedFunctionObject/codedFunctionObject.C +++ b/src/functionObjects/utilities/codedFunctionObject/codedFunctionObject.C @@ -80,12 +80,12 @@ void Foam::codedFunctionObject::prepare // Copy filtered H template dynCode.addCopyFile(codeTemplateH("codedFunctionObject")); - // Debugging: make verbose + // Make verbose if debugging + dynCode.setFilterVariable("verbose", Foam::name(bool(debug))); + if (debug) { - dynCode.setFilterVariable("verbose", "true"); - Info<<"compile " << codeName() << " sha1: " - << context.sha1() << endl; + Info<<"compile " << codeName() << " sha1: " << context.sha1() << endl; } // Define Make/options