diff --git a/applications/test/charList/Test-charList.C b/applications/test/charList/Test-charList.C index 270e255ae8..34882fc125 100644 --- a/applications/test/charList/Test-charList.C +++ b/applications/test/charList/Test-charList.C @@ -58,8 +58,8 @@ int main(int argc, char *argv[]) #include "setRootCase.H" - Info<< "Known compound tokens: " - << token::compound::IstreamConstructorTablePtr_->sortedToc() << nl; + // Info<< "Known compound tokens: " + // << token::compound::emptyConstructorTablePtr_->sortedToc() << nl; OStringStream ostr; diff --git a/applications/test/compoundToken1/Make/files b/applications/test/compoundToken1/Make/files new file mode 100644 index 0000000000..b0f090a63a --- /dev/null +++ b/applications/test/compoundToken1/Make/files @@ -0,0 +1,3 @@ +Test-compoundToken1.C + +EXE = $(FOAM_USER_APPBIN)/Test-compoundToken1 diff --git a/applications/test/compoundToken1/Make/options b/applications/test/compoundToken1/Make/options new file mode 100644 index 0000000000..1f502ad153 --- /dev/null +++ b/applications/test/compoundToken1/Make/options @@ -0,0 +1 @@ +/* EXE_INC = */ diff --git a/applications/test/compoundToken1/Test-compoundToken1.C b/applications/test/compoundToken1/Test-compoundToken1.C new file mode 100644 index 0000000000..4acbd5e747 --- /dev/null +++ b/applications/test/compoundToken1/Test-compoundToken1.C @@ -0,0 +1,297 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | www.openfoam.com + \\/ M anipulation | +------------------------------------------------------------------------------- + Copyright (C) 2023 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 . + +Description + Test token construct assign etc. +\*---------------------------------------------------------------------------*/ + +#include "argList.H" +#include "IOobject.H" +#include "IOstreams.H" +#include "IFstream.H" +#include "StringStream.H" +#include "cpuTime.H" +#include "labelList.H" +#include "DynamicList.H" + +namespace Foam +{ + +template +OS& printTypeCode(OS& os, char typeCode) +{ + os << int(static_cast(typeCode)); + return os; +} + + +/*---------------------------------------------------------------------------*\ + Class IFstream Declaration +\*---------------------------------------------------------------------------*/ + +bool test_pending = false; + +class IFstreamDelayed +: + public IFstream +{ + virtual bool readCompoundToken(token& tok, const word& type) + { + auto& is = *this; + + bool delay = true; + + // Low-level: get next valid character (after comments) + // and branch based on it being a '{' or not + + char c = 0; + if (is.read(c)) + { + // Delay further reading? + delay = (c == token::BEGIN_BLOCK); + is.putback(c); + + if (c) + { + cerr<< "nextChar:" << c << " : delay read: " << delay << nl; + } + } + + // Caller already checked token::compound::isCompound(...) + // but use readCompoundToken anyhow for convenience + + if (tok.readCompoundToken(type, is, !delay)) + { + cerr<< "readCompound(" << type << ")\n"; + cerr<< "typeCode: "; + printTypeCode(cerr, tok.compoundToken().typeCode()) << nl; + + if (test_pending && delay) + { + InfoErr<< "pending read " + << tok.compoundToken().type() << endl; + + tok.refCompoundToken().pending(true); + } + + return true; + } + + return false; + } + + +public: + + // Constructors + using IFstream::IFstream; + + //- Destructor + ~IFstreamDelayed() = default; +}; + + +} // End namespace Foam + + +using namespace Foam; + +void populateCompound(token::compound& ct, const dictionary& dict) +{ + Info<< "populateCompound: " << nl; + + // This is where runTime dispatch, eg based on transport type + // could be used... + + switch (ct.typeCode()) + { + #undef fillComponents + #define fillComponents(Type, Variable, Value) \ + { \ + ct.pending(false); \ + ct.resize(10); \ + UList Variable \ + ( \ + reinterpret_cast(ct.data_bytes()), \ + label(ct.size_bytes() / sizeof(Type)) \ + ); \ + Variable = Value; \ + } + + case token::tokenType::PUNCTUATION : + { + fillComponents(char, cmpts, '@'); + } + break; + + case token::tokenType::BOOL : + { + fillComponents(bool, cmpts, false); + } + break; + + case token::tokenType::LABEL : + { + fillComponents(label, cmpts, 123); + } + break; + + case token::tokenType::FLOAT : + { + fillComponents(float, cmpts, 2.7); + } + break; + + case token::tokenType::DOUBLE : + { + fillComponents(double, cmpts, 3.1415); + } + break; + + default: + break; + + #undef fillComponents + } + + if (!ct.pending()) + { + Info<< "assigned values:" << endl; + } +} + + +void rewriteCompounds(ITstream& is) +{ + Info<< "rewrite: " << flatOutput(is) << endl; + + for (label toki = 0; toki < is.size(); ++toki) + { + if (is[toki].isCompound() && is[toki].compoundToken().pending()) + { + Info<< "replace : " << is[toki].info() << endl; + + if (is.peekToken(toki+1).isPunctuation(token::BEGIN_BLOCK)) + { + labelRange slice + ( + is.find(token::BEGIN_BLOCK, token::END_BLOCK, toki+1) + ); + + if (slice.good() && (slice.start() == toki+1)) + { + Info<< "Compound at:" << toki + << " dict:" << slice << endl; + + ITstream substream(is.extract(slice)); + + dictionary dict(substream); + + populateCompound(is[toki].refCompoundToken(), dict); + } + } + } + } +} + + +void rewriteDict(dictionary& dict) +{ + for (entry& e : dict) + { + if (e.isDict()) + { + rewriteDict(e.dict()); + } + else if (e.isStream()) + { + rewriteCompounds(e.stream()); + } + } +} + + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // +// Main program: + +int main(int argc, char *argv[]) +{ + argList::noBanner(); + argList::noParallel(); + argList::addBoolOption("std", "standard reading (no delayed compounds)"); + argList::addBoolOption("pending", "read with pending"); + + argList args(argc, argv, false, true); + + Info<< "typeCodes:" << nl; + Info<< " bool="; + printTypeCode(Info, token::tokenType::BOOL) << nl; + Info<< " label="; + printTypeCode(Info, token::tokenType::LABEL) << nl; + Info<< " float="; + printTypeCode(Info, token::tokenType::FLOAT) << nl; + Info<< " double="; + printTypeCode(Info, token::tokenType::DOUBLE) << nl; + Info<< nl; + + if (args.found("pending")) + { + test_pending = true; + } + + if (args.found("std")) + { + for (label argi = 1; argi < args.size(); ++argi) + { + Info<< "Read: " << args[argi] << endl; + IFstream is(args[argi]); + + dictionary dict(is); + + Info<< "read: " << dict << nl; + } + } + else + { + for (label argi = 1; argi < args.size(); ++argi) + { + Info<< "Read delay: " << args[argi] << endl; + + IFstreamDelayed is(args[argi]); + + dictionary dict(is); + Info<< "read: " << dict << nl; + + rewriteDict(dict); + + Info<< "modified: " << dict << nl; + } + } + + return 0; +} + + +// ************************************************************************* // diff --git a/applications/test/compoundToken1/testDict1 b/applications/test/compoundToken1/testDict1 new file mode 100644 index 0000000000..b41a1c0f81 --- /dev/null +++ b/applications/test/compoundToken1/testDict1 @@ -0,0 +1,24 @@ +/*--------------------------------*- C++ -*----------------------------------*\ +| ========= | | +| \\ / F ield | OpenFOAM: The Open Source CFD Toolbox | +| \\ / O peration | Version: v2312 | +| \\ / A nd | Website: www.openfoam.com | +| \\/ M anipulation | | +\*---------------------------------------------------------------------------*/ +FoamFile +{ + version 2.0; + format ascii; + class dictionary; + object dictionary; +} +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +internalField uniform 1; + +temperature List 10(270 271 272 273 274 275 276 277 278 279); + +pressure 1e5; + + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // diff --git a/applications/test/compoundToken1/testDict2 b/applications/test/compoundToken1/testDict2 new file mode 100644 index 0000000000..36b1b81094 --- /dev/null +++ b/applications/test/compoundToken1/testDict2 @@ -0,0 +1,65 @@ +/*--------------------------------*- C++ -*----------------------------------*\ +| ========= | | +| \\ / F ield | OpenFOAM: The Open Source CFD Toolbox | +| \\ / O peration | Version: v2312 | +| \\ / A nd | Website: www.openfoam.com | +| \\/ M anipulation | | +\*---------------------------------------------------------------------------*/ +FoamFile +{ + version 2.0; + format ascii; + class dictionary; + object dictionary; +} +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +internalField uniform 1; + +// Regular syntax +valuesT List (123 456 890); + +// Test some non-standard syntax +temperature List +{ + transport adios; + length 10; + values (270 271 272 273 274 275 276 277 278 279); +}; + +// Test some non-standard syntax +velocity List +{ + transport adios; + length 10; + values (270 271 272 273 274 275 276 277 278 279); +}; + +// Test some non-standard syntax +isGood List +{ + transport adios; + length 10; + values (true false true); +}; + +// Test some non-standard syntax +master List