From 06f22a9bf3eda6decf4d85a5b1cce50ddfe74138 Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Mon, 6 Jun 2016 11:51:05 +0100 Subject: [PATCH 1/8] ENH: cleanup Ostream to ease usage (issue #254) - Include newline in beginBlock/endBlock, since this corresponds to the standard usage. The beginBlock now takes keyType instead of word. - Provide Ostream::writeEntry method to reduce clutter and simplify writing of entries. Before ====== os << indent << "name" << nl << indent << token::BEGIN_BLOCK << incrIndent << nl; os.writeKeyword("key1") << val1 << token::END_STATEMENT << nl; os.writeKeyword("key2") << val2 << token::END_STATEMENT << nl; os << decrIndent << indent << token::END_BLOCK << nl; After ===== os.beginBlock("name"); os.writeEntry("key1", val1); os.writeEntry("key2", val2); os.endBlock(); - For completeness, support inline use of various Ostream methods. For example, os << beginBlock; os.writeEntry("key1", val1); os.writeEntry("key2", val2); os << endBlock; - For those who wish to write in long form, can also use endEntry inline: os.beginBlock("name"); os.writeKeyword("key1") << val2 << endEntry; os.writeKeyword("key2") << val2 << endEntry; os.endBlock(); The endEntry encapsulates a semi-colon, newline combination. --- src/OpenFOAM/db/IOstreams/IOstreams/Ostream.C | 20 ++++--- src/OpenFOAM/db/IOstreams/IOstreams/Ostream.H | 58 ++++++++++++++++--- src/OpenFOAM/db/dictionary/dictionaryIO.C | 4 +- .../GeometricField/GeometricBoundaryField.C | 8 +-- src/OpenFOAM/global/profiling/profiling.C | 51 ++++++---------- .../global/profiling/profilingSysInfo.C | 21 ++----- .../global/profiling/profilingSysInfo.H | 14 ----- .../polyBoundaryMesh/polyBoundaryMesh.C | 4 +- .../meshes/primitiveShapes/plane/plane.C | 11 ++-- .../primitives/functions/Function1/CSV/CSV.C | 37 ++++-------- .../functions/Function1/Sine/Sine.C | 8 +-- .../functions/Function1/Square/Square.C | 10 ++-- .../functions/Function1/TableFile/TableFile.C | 9 +-- 13 files changed, 124 insertions(+), 131 deletions(-) diff --git a/src/OpenFOAM/db/IOstreams/IOstreams/Ostream.C b/src/OpenFOAM/db/IOstreams/IOstreams/Ostream.C index 4131da7af4..754c5fb185 100644 --- a/src/OpenFOAM/db/IOstreams/IOstreams/Ostream.C +++ b/src/OpenFOAM/db/IOstreams/IOstreams/Ostream.C @@ -79,11 +79,9 @@ Foam::Ostream& Foam::Ostream::writeKeyword(const keyType& kw) } -Foam::Ostream& Foam::Ostream::beginBlock(const word& keyword) +Foam::Ostream& Foam::Ostream::beginBlock(const keyType& keyword) { - indent(); - write(keyword); - endl(); + indent(); write(keyword); write('\n'); beginBlock(); return *this; @@ -92,8 +90,7 @@ Foam::Ostream& Foam::Ostream::beginBlock(const word& keyword) Foam::Ostream& Foam::Ostream::beginBlock() { - indent(); - write(char(token::BEGIN_BLOCK)); + indent(); write(char(token::BEGIN_BLOCK)); write('\n'); incrIndent(); return *this; @@ -103,8 +100,15 @@ Foam::Ostream& Foam::Ostream::beginBlock() Foam::Ostream& Foam::Ostream::endBlock() { decrIndent(); - indent(); - write(char(token::END_BLOCK)); + indent(); write(char(token::END_BLOCK)); write('\n'); + + return *this; +} + + +Foam::Ostream& Foam::Ostream::endEntry() +{ + write(char(token::END_STATEMENT)); write('\n'); return *this; } diff --git a/src/OpenFOAM/db/IOstreams/IOstreams/Ostream.H b/src/OpenFOAM/db/IOstreams/IOstreams/Ostream.H index 0bd02a5b55..1634689c64 100644 --- a/src/OpenFOAM/db/IOstreams/IOstreams/Ostream.H +++ b/src/OpenFOAM/db/IOstreams/IOstreams/Ostream.H @@ -154,33 +154,47 @@ public: return indentLevel_; } - //- Incrememt the indent level + //- Increment the indent level void incrIndent() { ++indentLevel_; } - //- Decrememt the indent level + //- Decrement the indent level void decrIndent(); //- Write the keyword followed by an appropriate indentation virtual Ostream& writeKeyword(const keyType&); //- Write begin block group with the given name - // Uses the appropriate indentation, - // does not include a trailing newline. - virtual Ostream& beginBlock(const word&); + // Increments indentation, adds newline. + virtual Ostream& beginBlock(const keyType&); //- Write begin block group without a name - // Uses the appropriate indentation, - // does not include a trailing newline. + // Increments indentation, adds newline. virtual Ostream& beginBlock(); //- Write end block group - // Uses the appropriate indentation, - // does not include a trailing newline. + // Decrements indentation, adds newline. virtual Ostream& endBlock(); + //- Write end entry (';') followed by newline. + virtual Ostream& endEntry(); + + //- Write a keyword/value entry. + // The following two are functionally equivalent: + // \code + // os.writeEntry(key, value); + // + // os.writeKeyword(key) << value << endEntry; + // \endcode + template + Ostream& writeEntry(const keyType& key, const T& value) + { + writeKeyword(key) << value; + return endEntry(); + } + // Stream state functions @@ -273,6 +287,32 @@ inline Ostream& endl(Ostream& os) } +//- Write begin block group without a name +// Increments indentation, adds newline. +inline Ostream& beginBlock(Ostream& os) +{ + os.beginBlock(); + return os; +} + + +//- Write end block group +// Decrements indentation, adds newline. +inline Ostream& endBlock(Ostream& os) +{ + os.endBlock(); + return os; +} + + +//- Write end entry (';') followed by newline. +inline Ostream& endEntry(Ostream& os) +{ + os.endEntry(); + return os; +} + + // Useful aliases for tab and newline characters static const char tab = '\t'; static const char nl = '\n'; diff --git a/src/OpenFOAM/db/dictionary/dictionaryIO.C b/src/OpenFOAM/db/dictionary/dictionaryIO.C index d2880798fc..b9db794b80 100644 --- a/src/OpenFOAM/db/dictionary/dictionaryIO.C +++ b/src/OpenFOAM/db/dictionary/dictionaryIO.C @@ -175,7 +175,7 @@ void Foam::dictionary::write(Ostream& os, bool subDict) const if (subDict) { os << nl; - os.beginBlock() << nl; + os.beginBlock(); } forAllConstIter(IDLList, *this, iter) @@ -203,7 +203,7 @@ void Foam::dictionary::write(Ostream& os, bool subDict) const if (subDict) { - os.endBlock() << endl; + os.endBlock() << flush; } } diff --git a/src/OpenFOAM/fields/GeometricFields/GeometricField/GeometricBoundaryField.C b/src/OpenFOAM/fields/GeometricFields/GeometricField/GeometricBoundaryField.C index fe43d9225d..7e273aa9fd 100644 --- a/src/OpenFOAM/fields/GeometricFields/GeometricField/GeometricBoundaryField.C +++ b/src/OpenFOAM/fields/GeometricFields/GeometricField/GeometricBoundaryField.C @@ -569,16 +569,16 @@ template class PatchField, class GeoMesh> void Foam::GeometricField::Boundary:: writeEntry(const word& keyword, Ostream& os) const { - os.beginBlock(keyword) << nl; + os.beginBlock(keyword); forAll(*this, patchi) { - os.beginBlock(this->operator[](patchi).patch().name()) << nl; + os.beginBlock(this->operator[](patchi).patch().name()); os << this->operator[](patchi); - os.endBlock() << endl; + os.endBlock(); } - os.endBlock() << endl; + os.endBlock() << flush; // Check state of IOstream os.check diff --git a/src/OpenFOAM/global/profiling/profiling.C b/src/OpenFOAM/global/profiling/profiling.C index 6471022850..10c61852ad 100644 --- a/src/OpenFOAM/global/profiling/profiling.C +++ b/src/OpenFOAM/global/profiling/profiling.C @@ -41,17 +41,6 @@ Foam::label Foam::profiling::Information::nextId_(0); // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * // -// file-scope function -template -inline static void writeEntry -( - Foam::Ostream& os, const Foam::word& key, const T& value -) -{ - os.writeKeyword(key) << value << Foam::token::END_STATEMENT << '\n'; -} - - Foam::label Foam::profiling::Information::getNextId() { return nextId_++; @@ -369,7 +358,7 @@ void Foam::profiling::Information::update(const scalar& elapsed) bool Foam::profiling::writeData(Ostream& os) const { - os.beginBlock("profiling") << nl; // FUTURE: without nl + os.beginBlock("profiling"); // Add extra new line between entries label nTrigger = 0; @@ -420,22 +409,22 @@ bool Foam::profiling::writeData(Ostream& os) const } } - os.endBlock() << nl; // FUTURE: without nl + os.endBlock(); if (sysInfo_) { os << nl; - os.beginBlock("sysInfo") << nl; // FUTURE: without nl + os.beginBlock("sysInfo"); sysInfo_->write(os); - os.endBlock() << nl; // FUTURE: without nl + os.endBlock(); } if (cpuInfo_) { os << nl; - os.beginBlock("cpuInfo") << nl; // FUTURE: without nl + os.beginBlock("cpuInfo"); cpuInfo_->write(os); - os.endBlock() << nl; // FUTURE: without nl + os.endBlock(); } if (memInfo_) @@ -443,10 +432,10 @@ bool Foam::profiling::writeData(Ostream& os) const memInfo_->update(); os << nl; - os.beginBlock("memInfo") << nl; // FUTURE: without nl + os.beginBlock("memInfo"); memInfo_->write(os); - writeEntry(os, "units", "kB"); - os.endBlock() << nl; // FUTURE: without nl + os.writeEntry("units", "kB"); + os.endBlock(); } return os; @@ -536,26 +525,24 @@ Foam::Ostream& Foam::profiling::Information::write { // write in dictionary format - os.beginBlock("trigger" + Foam::name(id_)) << nl; // FUTURE: without nl + os.beginBlock(word("trigger" + Foam::name(id_))); - // FUTURE: os.writeEntry(key, value); - - writeEntry(os, "id", id_); + os.writeEntry("id", id_); if (id_ != parent().id()) { - writeEntry(os, "parentId", parent().id()); + os.writeEntry("parentId", parent().id()); } - writeEntry(os, "description", description()); - writeEntry(os, "calls", calls() + (offset ? 1 : 0)); - writeEntry(os, "totalTime", totalTime() + elapsedTime); - writeEntry(os, "childTime", childTime() + childTimes); + os.writeEntry("description", description()); + os.writeEntry("calls", calls() + (offset ? 1 : 0)); + os.writeEntry("totalTime", totalTime() + elapsedTime); + os.writeEntry("childTime", childTime() + childTimes); if (maxMem_) { - writeEntry(os, "maxMem", maxMem_); + os.writeEntry("maxMem", maxMem_); } - writeEntry(os, "onStack", Switch(onStack())); + os.writeEntry("onStack", Switch(onStack())); - os.endBlock() << nl; // FUTURE: without nl + os.endBlock(); return os; } diff --git a/src/OpenFOAM/global/profiling/profilingSysInfo.C b/src/OpenFOAM/global/profiling/profilingSysInfo.C index 9b6ff2f34b..51b0601736 100644 --- a/src/OpenFOAM/global/profiling/profilingSysInfo.C +++ b/src/OpenFOAM/global/profiling/profilingSysInfo.C @@ -29,17 +29,6 @@ License // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * // -// file-scope function -template -inline static void writeEntry -( - Foam::Ostream& os, const Foam::word& key, const T& value -) -{ - os.writeKeyword(key) << value << Foam::token::END_STATEMENT << '\n'; -} - - // file-scope function inline static void printEnv ( @@ -49,7 +38,7 @@ inline static void printEnv const std::string value = getEnv(envName); if (!value.empty()) { - writeEntry(os, key, value); + os.writeEntry(key, value); } } @@ -74,12 +63,12 @@ Foam::Ostream& Foam::profiling::sysInfo::write Ostream& os ) const { - writeEntry(os, "host", hostName(false)); // short name - writeEntry(os, "date", clock::dateTime()); + os.writeEntry("host", hostName(false)); // short name + os.writeEntry("date", clock::dateTime()); // compile-time information - writeEntry(os, "version", std::string(FOAMversion)); - writeEntry(os, "build", std::string(FOAMbuild)); + os.writeEntry("version", std::string(FOAMversion)); + os.writeEntry("build", std::string(FOAMbuild)); printEnv(os, "arch", "WM_ARCH"); printEnv(os, "compilerType", "WM_COMPILER_TYPE"); diff --git a/src/OpenFOAM/global/profiling/profilingSysInfo.H b/src/OpenFOAM/global/profiling/profilingSysInfo.H index 26201fac36..b2e84f888f 100644 --- a/src/OpenFOAM/global/profiling/profilingSysInfo.H +++ b/src/OpenFOAM/global/profiling/profilingSysInfo.H @@ -51,12 +51,6 @@ class Ostream; class profiling::sysInfo { - // Private Static Data Members - - - // Private Data Members - - // Private Member Functions //- Disallow default bitwise copy construct @@ -72,9 +66,6 @@ protected: friend class profiling; - - // Member Functions - public: @@ -90,11 +81,6 @@ public: // Member Functions - // Access - - - // Edit - //- Update it with a new timing information void update(); diff --git a/src/OpenFOAM/meshes/polyMesh/polyBoundaryMesh/polyBoundaryMesh.C b/src/OpenFOAM/meshes/polyMesh/polyBoundaryMesh/polyBoundaryMesh.C index aa619792c3..edb37c727b 100644 --- a/src/OpenFOAM/meshes/polyMesh/polyBoundaryMesh/polyBoundaryMesh.C +++ b/src/OpenFOAM/meshes/polyMesh/polyBoundaryMesh/polyBoundaryMesh.C @@ -1120,9 +1120,9 @@ bool Foam::polyBoundaryMesh::writeData(Ostream& os) const forAll(patches, patchi) { - os.beginBlock(patches[patchi].name()) << nl; + os.beginBlock(patches[patchi].name()); os << patches[patchi]; - os.endBlock() << endl; + os.endBlock(); } os << decrIndent << token::END_LIST; diff --git a/src/OpenFOAM/meshes/primitiveShapes/plane/plane.C b/src/OpenFOAM/meshes/primitiveShapes/plane/plane.C index 06a08a3598..99539ee2c6 100644 --- a/src/OpenFOAM/meshes/primitiveShapes/plane/plane.C +++ b/src/OpenFOAM/meshes/primitiveShapes/plane/plane.C @@ -449,15 +449,14 @@ Foam::point Foam::plane::mirror(const point& p) const void Foam::plane::writeDict(Ostream& os) const { - os.writeKeyword("planeType") << "pointAndNormal" - << token::END_STATEMENT << nl; + os.writeEntry("planeType", "pointAndNormal"); - os.beginBlock("pointAndNormalDict") << nl; + os.beginBlock("pointAndNormalDict"); - os.writeKeyword("point") << point_ << token::END_STATEMENT << nl; - os.writeKeyword("normal") << normal_ << token::END_STATEMENT << nl; + os.writeEntry("point", point_); + os.writeEntry("normal", normal_); - os.endBlock() << endl; + os.endBlock() << flush; } diff --git a/src/OpenFOAM/primitives/functions/Function1/CSV/CSV.C b/src/OpenFOAM/primitives/functions/Function1/CSV/CSV.C index a929f868ee..9d2d858174 100644 --- a/src/OpenFOAM/primitives/functions/Function1/CSV/CSV.C +++ b/src/OpenFOAM/primitives/functions/Function1/CSV/CSV.C @@ -263,41 +263,28 @@ template void Foam::Function1Types::CSV::writeData(Ostream& os) const { Function1::writeData(os); - os << token::END_STATEMENT << nl; + os.endEntry(); - os.beginBlock(word(this->name() + "Coeffs")) << nl; + os.beginBlock(word(this->name() + "Coeffs")); // Note: for TableBase write the dictionary entries it needs but not // the values themselves TableBase::writeEntries(os); - os.writeKeyword("nHeaderLine") << nHeaderLine_ - << token::END_STATEMENT << nl; - os.writeKeyword("refColumn") << refColumn_ - << token::END_STATEMENT << nl; + os.writeEntry("nHeaderLine", nHeaderLine_); + os.writeEntry("refColumn", refColumn_); // Force writing labelList in ascii - os.writeKeyword("componentColumns"); - if (os.format() == IOstream::BINARY) - { - os.format(IOstream::ASCII); - os << componentColumns_; - os.format(IOstream::BINARY); - } - else - { - os << componentColumns_; - } - os << token::END_STATEMENT << nl; + const enum IOstream::streamFormat fmt = os.format(); + os.format(IOstream::ASCII); + os.writeEntry("componentColumns", componentColumns_); + os.format(fmt); - os.writeKeyword("separator") << string(separator_) - << token::END_STATEMENT << nl; - os.writeKeyword("mergeSeparators") << mergeSeparators_ - << token::END_STATEMENT << nl; - os.writeKeyword("fileName") << fName_ - << token::END_STATEMENT << nl; + os.writeEntry("separator", string(separator_)); + os.writeEntry("mergeSeparators", mergeSeparators_); + os.writeEntry("fileName", fName_); - os.endBlock() << endl; + os.endBlock() << flush; } diff --git a/src/OpenFOAM/primitives/functions/Function1/Sine/Sine.C b/src/OpenFOAM/primitives/functions/Function1/Sine/Sine.C index 85a7aefe04..a11acd4df8 100644 --- a/src/OpenFOAM/primitives/functions/Function1/Sine/Sine.C +++ b/src/OpenFOAM/primitives/functions/Function1/Sine/Sine.C @@ -89,17 +89,17 @@ template void Foam::Function1Types::Sine::writeData(Ostream& os) const { Function1::writeData(os); - os << token::END_STATEMENT << nl; + os.endEntry(); - os.beginBlock(word(this->name() + "Coeffs")) << nl; + os.beginBlock(word(this->name() + "Coeffs")); - os.writeKeyword("t0") << t0_ << token::END_STATEMENT << nl; + os.writeEntry("t0", t0_); amplitude_->writeData(os); frequency_->writeData(os); scale_->writeData(os); level_->writeData(os); - os.endBlock() << endl; + os.endBlock() << flush; } diff --git a/src/OpenFOAM/primitives/functions/Function1/Square/Square.C b/src/OpenFOAM/primitives/functions/Function1/Square/Square.C index 9c55e8800e..0e258bc30e 100644 --- a/src/OpenFOAM/primitives/functions/Function1/Square/Square.C +++ b/src/OpenFOAM/primitives/functions/Function1/Square/Square.C @@ -102,18 +102,18 @@ template void Foam::Function1Types::Square::writeData(Ostream& os) const { Function1::writeData(os); - os << token::END_STATEMENT << nl; + os.endEntry(); - os.beginBlock(word(this->name() + "Coeffs")) << nl; + os.beginBlock(word(this->name() + "Coeffs")); - os.writeKeyword("t0") << t0_ << token::END_STATEMENT << nl; - os.writeKeyword("markSpace") << markSpace_ << token::END_STATEMENT << nl; + os.writeEntry("t0", t0_); + os.writeEntry("markSpace", markSpace_); amplitude_->writeData(os); frequency_->writeData(os); scale_->writeData(os); level_->writeData(os); - os.endBlock() << endl; + os.endBlock() << flush; } diff --git a/src/OpenFOAM/primitives/functions/Function1/TableFile/TableFile.C b/src/OpenFOAM/primitives/functions/Function1/TableFile/TableFile.C index 414e616077..3682836999 100644 --- a/src/OpenFOAM/primitives/functions/Function1/TableFile/TableFile.C +++ b/src/OpenFOAM/primitives/functions/Function1/TableFile/TableFile.C @@ -78,16 +78,17 @@ template void Foam::Function1Types::TableFile::writeData(Ostream& os) const { Function1::writeData(os); - os << token::END_STATEMENT << nl; + os.endEntry(); - os.beginBlock(word(this->name() + "Coeffs")) << nl; + os.beginBlock(word(this->name() + "Coeffs")); // Note: for TableBase write the dictionary entries it needs but not // the values themselves TableBase::writeEntries(os); - os.writeKeyword("fileName")<< fName_ << token::END_STATEMENT << nl; - os.endBlock() << endl; + os.writeEntry("fileName", fName_); + + os.endBlock() << flush; } From 6d5db96ad9c7b513b401dadcaf4f06d6d51cb012 Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Thu, 9 Jun 2016 18:33:56 +0100 Subject: [PATCH 2/8] ENH: subdict output with leading name (issue #255) - Introduce dictionary::writeEntries for better code-reuse. Before ====== os << nl << indent << "name"; dict.write(os); After ===== dict.write(os, "name"); --- .../test/dictionary/Test-dictionary.C | 11 ++++--- src/OpenFOAM/db/dictionary/dictionary.H | 10 +++++- .../dictionaryEntry/dictionaryEntryIO.C | 7 ++--- src/OpenFOAM/db/dictionary/dictionaryIO.C | 31 ++++++++++++++----- 4 files changed, 40 insertions(+), 19 deletions(-) diff --git a/applications/test/dictionary/Test-dictionary.C b/applications/test/dictionary/Test-dictionary.C index 287c63db21..dc55b9902f 100644 --- a/applications/test/dictionary/Test-dictionary.C +++ b/applications/test/dictionary/Test-dictionary.C @@ -48,14 +48,13 @@ int main(int argc, char *argv[]) { dictionary dict; - dict.add("aaOPENMPIcc", 1); + dict.add(word("aa" + getEnv("WM_MPLIB") + "cc"), 16); string s("DDD${aa${WM_MPLIB}cc}EEE"); stringOps::inplaceExpand(s, dict, true, false); Info<< "variable expansion:" << s << endl; } - Info<< nl << "FOAM_CASE=" << getEnv("FOAM_CASE") << nl << "FOAM_CASENAME=" << getEnv("FOAM_CASENAME") << nl @@ -65,7 +64,9 @@ int main(int argc, char *argv[]) { { dictionary dict1(IFstream("testDict")()); - Info<< "dict1: " << dict1 << nl + dict1.writeEntry("dict1", Info); + + Info<< nl << "toc: " << dict1.toc() << nl << "keys: " << dict1.keys() << nl << "patterns: " << dict1.keys(true) << endl; @@ -89,14 +90,14 @@ int main(int argc, char *argv[]) << "no = " << dict4.name() << " " << dict4.toc() << endl; } - IOobject::writeDivider(Info); { dictionary dict(IFstream("testDictRegex")()); dict.add(keyType("fooba[rz]", true), "anything"); - Info<< "dict:" << dict << nl + dict.writeEntry("testDictRegex", Info); + Info<< nl << "toc: " << dict.toc() << nl << "keys: " << dict.keys() << nl << "patterns: " << dict.keys(true) << endl; diff --git a/src/OpenFOAM/db/dictionary/dictionary.H b/src/OpenFOAM/db/dictionary/dictionary.H index 8c4d544f4d..4a7c728437 100644 --- a/src/OpenFOAM/db/dictionary/dictionary.H +++ b/src/OpenFOAM/db/dictionary/dictionary.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 | + \\/ M anipulation | Copyright (C) 2016 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -503,6 +503,14 @@ public: // Write + //- Write sub-dictionary with the keyword as its header + void writeEntry(const keyType& keyword, Ostream&) const; + + //- Write dictionary entries. + // Optionally with extra new line between entries for + // "top-level" dictionaries + void writeEntries(Ostream&, const bool extraNewLine=false) const; + //- Write dictionary, normally with sub-dictionary formatting void write(Ostream&, const bool subDict=true) const; diff --git a/src/OpenFOAM/db/dictionary/dictionaryEntry/dictionaryEntryIO.C b/src/OpenFOAM/db/dictionary/dictionaryEntry/dictionaryEntryIO.C index 677d5beaac..0b2c5315ec 100644 --- a/src/OpenFOAM/db/dictionary/dictionaryEntry/dictionaryEntryIO.C +++ b/src/OpenFOAM/db/dictionary/dictionaryEntry/dictionaryEntryIO.C @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011-2015 OpenFOAM Foundation - \\/ M anipulation | + \\/ M anipulation | Copyright (C) 2016 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -68,10 +68,7 @@ Foam::dictionaryEntry::dictionaryEntry void Foam::dictionaryEntry::write(Ostream& os) const { - // write keyword with indent but without trailing spaces - os.indent(); - os.write(keyword()); - dictionary::write(os); + dictionary::writeEntry(keyword(), os); } diff --git a/src/OpenFOAM/db/dictionary/dictionaryIO.C b/src/OpenFOAM/db/dictionary/dictionaryIO.C index b9db794b80..081068488f 100644 --- a/src/OpenFOAM/db/dictionary/dictionaryIO.C +++ b/src/OpenFOAM/db/dictionary/dictionaryIO.C @@ -170,14 +170,16 @@ Foam::Istream& Foam::operator>>(Istream& is, dictionary& dict) // * * * * * * * * * * * * * * Ostream Operator * * * * * * * * * * * * * * // -void Foam::dictionary::write(Ostream& os, bool subDict) const +void Foam::dictionary::writeEntry(const keyType& kw, Ostream& os) const { - if (subDict) - { - os << nl; - os.beginBlock(); - } + os.beginBlock(kw); + writeEntries(os); + os.endBlock() << flush; +} + +void Foam::dictionary::writeEntries(Ostream& os, const bool extraNewLine) const +{ forAllConstIter(IDLList, *this, iter) { const entry& e = *iter; @@ -185,8 +187,9 @@ void Foam::dictionary::write(Ostream& os, bool subDict) const // Write entry os << e; - // Add extra new line between entries for "top-level" dictionaries - if (!subDict && parent() == dictionary::null && e != *last()) + // Add extra new line between entries for "top-level" dictionaries, + // but not after the last entry (looks ugly). + if (extraNewLine && parent() == dictionary::null && e != *last()) { os << nl; } @@ -200,6 +203,18 @@ void Foam::dictionary::write(Ostream& os, bool subDict) const << endl; } } +} + + +void Foam::dictionary::write(Ostream& os, const bool subDict) const +{ + if (subDict) + { + os << nl; + os.beginBlock(); + } + + writeEntries(os, !subDict); if (subDict) { From cae7ce37f522b4a5aa9b8f5b217f0f6038ea697c Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Fri, 1 Jul 2016 08:23:13 +0200 Subject: [PATCH 3/8] ENH: provide formatting version of Foam::name() (issue #253) - there are some cases in which the C-style sprintf is much more convenient, albeit problematic for buffer overwrites. Provide a formatting version of Foam::name() for language primitives that is buffer-safe. Returns a Foam::word, so that further output will be unquoted, but without any checking that the characters are indeed entirely valid word characters. Example use, i = 1234; s = Foam::name("%08d", i); produces '00001234' Alternative using string streams: std::ostringstream buf; buf.fill('0'); buf << setw(8) << i; s = buf.str(); Note that the format specification can also be slightly more complex: Foam::name("output%08d.vtk", i); Foam::name("timing=%.2fs", time); It remains the caller's responsibility to ensure that the format mask is valid. --- applications/test/string/Test-string.C | 42 ++++++++++- src/OpenFOAM/primitives/Scalar/Scalar.C | 16 ++++- src/OpenFOAM/primitives/Scalar/Scalar.H | 12 +++- src/OpenFOAM/primitives/ints/int32/int32.H | 19 ++++- src/OpenFOAM/primitives/ints/int32/int32IO.C | 15 ++-- src/OpenFOAM/primitives/ints/int64/int64.H | 19 ++++- src/OpenFOAM/primitives/ints/int64/int64IO.C | 13 ++-- src/OpenFOAM/primitives/ints/uint32/uint32.H | 21 +++++- .../primitives/ints/uint32/uint32IO.C | 13 ++-- src/OpenFOAM/primitives/ints/uint64/uint64.H | 21 +++++- .../primitives/ints/uint64/uint64IO.C | 13 ++-- .../primitives/strings/stringOps/stringOps.H | 25 ++++++- .../strings/stringOps/stringOpsTemplates.C | 69 +++++++++++++++++++ 13 files changed, 266 insertions(+), 32 deletions(-) create mode 100644 src/OpenFOAM/primitives/strings/stringOps/stringOpsTemplates.C diff --git a/applications/test/string/Test-string.C b/applications/test/string/Test-string.C index f39be8c206..a8276c2926 100644 --- a/applications/test/string/Test-string.C +++ b/applications/test/string/Test-string.C @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011-2013 OpenFOAM Foundation - \\/ M anipulation | + \\/ M anipulation | Copyright (C) 2016 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -31,6 +31,10 @@ Description #include "dictionary.H" #include "IOstreams.H" +#include "int.H" +#include "uint.H" +#include "scalar.H" + using namespace Foam; // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // @@ -118,6 +122,8 @@ int main(int argc, char *argv[]) Info<< "after replace: " << test2 << endl; } + cout<< "\nEnter some string to test:\n"; + string s; Sin.getLine(s); @@ -126,7 +132,39 @@ int main(int argc, char *argv[]) cout<< "output string with " << s2.length() << " characters\n"; cout<< "ostream<< >" << s2 << "<\n"; Info<< "Ostream<< >" << s2 << "<\n"; - Info<< "hash:" << hex << string::hash()(s2) << endl; + Info<< "hash:" << hex << string::hash()(s2) << dec << endl; + + cout<< "\ntest Foam::name()\n"; + + Info<< "hash: = " << Foam::name("0x%012X", string::hash()(s2)) << endl; + + // test formatting on int + { + label val = 25; + Info<<"val: " << val << "\n"; + + Info<< "int " << val << " as word >" + << Foam::name(val) << "< or " + << Foam::name("formatted >%08d<", val) << "\n"; + } + + // test formatting on scalar + { + scalar val = 3.1415926535897931; + Info<< "scalar " << val << " as word >" + << Foam::name(val) << "< or " + << Foam::name("formatted >%.9f<", val) << "\n"; + } + + // test formatting on uint + { + uint64_t val = 25000000ul; + Info<<"val: " << val << "\n"; + + Info<< "uint64 " << val << " as word >" + << Foam::name(val) << "< or " + << Foam::name("formatted >%08d<", val) << "\n"; + } Info<< "\nEnd\n" << endl; return 0; diff --git a/src/OpenFOAM/primitives/Scalar/Scalar.C b/src/OpenFOAM/primitives/Scalar/Scalar.C index 0a3e09b0b1..a801378b21 100644 --- a/src/OpenFOAM/primitives/Scalar/Scalar.C +++ b/src/OpenFOAM/primitives/Scalar/Scalar.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) 2016 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -23,6 +23,8 @@ License \*---------------------------------------------------------------------------*/ +#include "stringOps.H" + // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // namespace Foam @@ -62,6 +64,18 @@ word name(const Scalar val) } +word name(const char* fmt, const Scalar val) +{ + return stringOps::name(fmt, val); +} + + +word name(const std::string& fmt, const Scalar val) +{ + return stringOps::name(fmt, val); +} + + // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * // Scalar readScalar(Istream& is) diff --git a/src/OpenFOAM/primitives/Scalar/Scalar.H b/src/OpenFOAM/primitives/Scalar/Scalar.H index 4d8f29ff54..6b2213fa6a 100644 --- a/src/OpenFOAM/primitives/Scalar/Scalar.H +++ b/src/OpenFOAM/primitives/Scalar/Scalar.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 | + \\/ M anipulation | Copyright (C) 2016 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -109,6 +109,16 @@ public: word name(const Scalar); +//- Return a word representation of a Scalar, using printf-style formatter. +// The representation is not checked for valid word characters. +word name(const char* fmt, const Scalar); + + +//- Return a word representation of a Scalar, using printf-style formatter. +// The representation is not checked for valid word characters. +word name(const std::string& fmt, const Scalar); + + // Standard C++ transcendental functions transFunc(sqrt) diff --git a/src/OpenFOAM/primitives/ints/int32/int32.H b/src/OpenFOAM/primitives/ints/int32/int32.H index de8baa2109..96c748b052 100644 --- a/src/OpenFOAM/primitives/ints/int32/int32.H +++ b/src/OpenFOAM/primitives/ints/int32/int32.H @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2014-2016 OpenFOAM Foundation - \\/ M anipulation | + \\/ M anipulation | Copyright (C) 2016 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -56,7 +56,22 @@ class Ostream; // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // //- Return a word representation of an int32 -word name(const int32_t); +inline word name(const int32_t val) +{ + // no stripping required + return word(std::to_string(val), false); +} + + +//- Return a word representation of an int32, using printf-style formatter. +// The representation is not checked for valid word characters. +word name(const char* fmt, const int32_t); + + +//- Return a word representation of an int32, using printf-style formatter. +// The representation is not checked for valid word characters. +word name(const std::string&, const int32_t); + // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * // diff --git a/src/OpenFOAM/primitives/ints/int32/int32IO.C b/src/OpenFOAM/primitives/ints/int32/int32IO.C index a8c972377d..c5023315f0 100644 --- a/src/OpenFOAM/primitives/ints/int32/int32IO.C +++ b/src/OpenFOAM/primitives/ints/int32/int32IO.C @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2014-2016 OpenFOAM Foundation - \\/ M anipulation | + \\/ M anipulation | Copyright (C) 2016 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -24,6 +24,7 @@ License \*---------------------------------------------------------------------------*/ #include "int32.H" +#include "stringOps.H" #include "IOstreams.H" #include @@ -32,11 +33,15 @@ License // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // -Foam::word Foam::name(const int32_t val) +Foam::word Foam::name(const char* fmt, const int32_t val) { - std::ostringstream buf; - buf << val; - return buf.str(); + return stringOps::name(fmt, val); +} + + +Foam::word Foam::name(const std::string& fmt, const int32_t val) +{ + return stringOps::name(fmt, val); } diff --git a/src/OpenFOAM/primitives/ints/int64/int64.H b/src/OpenFOAM/primitives/ints/int64/int64.H index 2bb5a05804..bcbcea38f7 100644 --- a/src/OpenFOAM/primitives/ints/int64/int64.H +++ b/src/OpenFOAM/primitives/ints/int64/int64.H @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2014-2016 OpenFOAM Foundation - \\/ M anipulation | + \\/ M anipulation | Copyright (C) 2016 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -60,7 +60,22 @@ class Ostream; // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // //- Return a word representation of an int64 -word name(const int64_t); +inline word name(const int64_t val) +{ + // no stripping required + return word(std::to_string(val), false); +} + + +//- Return a word representation of an int64, using printf-style formatter. +// The representation is not checked for valid word characters. +word name(const char* fmt, const int64_t); + + +//- Return a word representation of an int64, using printf-style formatter. +// The representation is not checked for valid word characters. +word name(const std::string& fmt, const int64_t); + // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * // diff --git a/src/OpenFOAM/primitives/ints/int64/int64IO.C b/src/OpenFOAM/primitives/ints/int64/int64IO.C index 89f14163a0..0523bb94cb 100644 --- a/src/OpenFOAM/primitives/ints/int64/int64IO.C +++ b/src/OpenFOAM/primitives/ints/int64/int64IO.C @@ -24,6 +24,7 @@ License \*---------------------------------------------------------------------------*/ #include "int64.H" +#include "stringOps.H" #include "IOstreams.H" #include @@ -32,11 +33,15 @@ License // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // -Foam::word Foam::name(const int64_t val) +Foam::word Foam::name(const char* fmt, const int64_t val) { - std::ostringstream buf; - buf << val; - return buf.str(); + return stringOps::name(fmt, val); +} + + +Foam::word Foam::name(const std::string& fmt, const int64_t val) +{ + return stringOps::name(fmt, val); } diff --git a/src/OpenFOAM/primitives/ints/uint32/uint32.H b/src/OpenFOAM/primitives/ints/uint32/uint32.H index 34803c218f..469189e976 100644 --- a/src/OpenFOAM/primitives/ints/uint32/uint32.H +++ b/src/OpenFOAM/primitives/ints/uint32/uint32.H @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2014-2016 OpenFOAM Foundation - \\/ M anipulation | + \\/ M anipulation | Copyright (C) 2016 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -55,8 +55,23 @@ class Ostream; // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // -//- Return a word representation of an uint32 -word name(const uint32_t); +//- Return a word representation of a uint32 +inline word name(const uint32_t val) +{ + // no stripping required + return word(std::to_string(val), false); +} + + +//- Return a word representation of a uint32, using printf-style formatter. +// The representation is not checked for valid word characters. +word name(const char* fmt, const uint32_t); + + +//- Return a word representation of a uint32, using printf-style formatter. +// The representation is not checked for valid word characters. +word name(const std::string& fmt, const uint32_t); + // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * // diff --git a/src/OpenFOAM/primitives/ints/uint32/uint32IO.C b/src/OpenFOAM/primitives/ints/uint32/uint32IO.C index 2746938d63..f88f5b1ad8 100644 --- a/src/OpenFOAM/primitives/ints/uint32/uint32IO.C +++ b/src/OpenFOAM/primitives/ints/uint32/uint32IO.C @@ -24,17 +24,22 @@ License \*---------------------------------------------------------------------------*/ #include "uint32.H" +#include "stringOps.H" #include "IOstreams.H" #include // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // -Foam::word Foam::name(const uint32_t val) +Foam::word Foam::name(const char* fmt, const uint32_t val) { - std::ostringstream buf; - buf << val; - return buf.str(); + return stringOps::name(fmt, val); +} + + +Foam::word Foam::name(const std::string& fmt, const uint32_t val) +{ + return stringOps::name(fmt, val); } diff --git a/src/OpenFOAM/primitives/ints/uint64/uint64.H b/src/OpenFOAM/primitives/ints/uint64/uint64.H index e1a742b6c2..2cf102e057 100644 --- a/src/OpenFOAM/primitives/ints/uint64/uint64.H +++ b/src/OpenFOAM/primitives/ints/uint64/uint64.H @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2014-2016 OpenFOAM Foundation - \\/ M anipulation | + \\/ M anipulation | Copyright (C) 2016 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -59,8 +59,23 @@ class Ostream; // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // -//- Return a word representation of an uint64 -word name(const uint64_t); +//- Return a word representation of a uint64 +inline word name(const uint64_t val) +{ + // no stripping required + return word(std::to_string(val), false); +} + + +//- Return a word representation of a uint64_t, using printf-style formatter. +// The representation is not checked for valid word characters. +word name(const char* fmt, const uint64_t); + + +//- Return a word representation of a uint64_t, using printf-style formatter. +// The representation is not checked for valid word characters. +word name(const std::string& fmt, const uint64_t); + // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * // diff --git a/src/OpenFOAM/primitives/ints/uint64/uint64IO.C b/src/OpenFOAM/primitives/ints/uint64/uint64IO.C index be10f83b01..3e110d933a 100644 --- a/src/OpenFOAM/primitives/ints/uint64/uint64IO.C +++ b/src/OpenFOAM/primitives/ints/uint64/uint64IO.C @@ -24,17 +24,22 @@ License \*---------------------------------------------------------------------------*/ #include "uint64.H" +#include "stringOps.H" #include "IOstreams.H" #include // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // -Foam::word Foam::name(const uint64_t val) +Foam::word Foam::name(const char* fmt, const uint64_t val) { - std::ostringstream buf; - buf << val; - return buf.str(); + return stringOps::name(fmt, val); +} + + +Foam::word Foam::name(const std::string& fmt, const uint64_t val) +{ + return stringOps::name(fmt, val); } diff --git a/src/OpenFOAM/primitives/strings/stringOps/stringOps.H b/src/OpenFOAM/primitives/strings/stringOps/stringOps.H index 3a72467bc9..fb408455f6 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 | + \\/ M anipulation | Copyright (C) 2016 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -36,6 +36,7 @@ SourceFiles #define stringOps_H #include "string.H" +#include "word.H" #include "dictionary.H" #include "HashTable.H" @@ -292,6 +293,21 @@ namespace stringOps string& inplaceTrim(string&); + //- Return a word representation of the primitive, + // using printf-style formatter. + // The representation is not checked for valid word characters - + // it is assumed that the caller knows what they are doing + template + Foam::word name(const char* fmt, const PrimitiveType& val); + + //- Return a word representation of the primitive, + // using printf-style formatter. + // The representation is not checked for valid word characters - + // it is assumed that the caller knows what they are doing + template + Foam::word name(const std::string& fmt, const PrimitiveType& val); + + } // End namespace stringOps @@ -299,6 +315,13 @@ namespace stringOps } // End namespace Foam + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#ifdef NoRepository + #include "stringOpsTemplates.C" +#endif + // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // #endif diff --git a/src/OpenFOAM/primitives/strings/stringOps/stringOpsTemplates.C b/src/OpenFOAM/primitives/strings/stringOps/stringOpsTemplates.C new file mode 100644 index 0000000000..a3874aa88e --- /dev/null +++ b/src/OpenFOAM/primitives/strings/stringOps/stringOpsTemplates.C @@ -0,0 +1,69 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2016 OpenCFD Ltd. + \\/ M anipulation | +------------------------------------------------------------------------------- +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 . + +\*---------------------------------------------------------------------------*/ + +#include + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +// NOTE: with C++11 could consider variadic templates for a more general +// sprintf implementation + +template +Foam::word Foam::stringOps::name +( + const char* fmt, + const PrimitiveType& val +) +{ + // same concept as GNU/BSD asprintf() + // use snprintf with zero to determine the number of characters required + + int n = ::snprintf(0, 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::null; +} + + +template +Foam::word Foam::stringOps::name +( + const std::string& fmt, + const PrimitiveType& val +) +{ + return stringOps::name(fmt.c_str(), val); +} + + +// ************************************************************************* // From ce121c92dcd8dfdd08ac42e83d713d8e270c958b Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Fri, 1 Jul 2016 09:09:50 +0200 Subject: [PATCH 4/8] STYLE: minor simplification of check for uniform contents --- .../Lists/CompactListList/CompactListListIO.C | 6 ++--- .../containers/Lists/FixedList/FixedListIO.C | 17 ++++++++------ src/OpenFOAM/containers/Lists/List/ListIO.C | 4 ++++ .../containers/Lists/PackedList/PackedList.C | 11 +++++----- .../Lists/UIndirectList/UIndirectListIO.C | 15 ++++++------- src/OpenFOAM/containers/Lists/UList/UListIO.C | 21 +++++++++++------- src/OpenFOAM/fields/Fields/Field/Field.C | 8 +++---- src/OpenFOAM/matrices/Matrix/MatrixIO.C | 22 +++++++++---------- 8 files changed, 56 insertions(+), 48 deletions(-) diff --git a/src/OpenFOAM/containers/Lists/CompactListList/CompactListListIO.C b/src/OpenFOAM/containers/Lists/CompactListList/CompactListListIO.C index 727e8803ac..5a001a1932 100644 --- a/src/OpenFOAM/containers/Lists/CompactListList/CompactListListIO.C +++ b/src/OpenFOAM/containers/Lists/CompactListList/CompactListListIO.C @@ -42,13 +42,13 @@ Foam::Istream& Foam::operator>>(Istream& is, CompactListList& lst) { is >> lst.offsets_ >> lst.m_; // Note: empty list gets output as two empty lists - if (lst.offsets_.size() == 0) + if (lst.offsets_.size()) { - lst.size_ = 0; + lst.size_ = lst.offsets_.size()-1; } else { - lst.size_ = lst.offsets_.size()-1; + lst.size_ = 0; } return is; } diff --git a/src/OpenFOAM/containers/Lists/FixedList/FixedListIO.C b/src/OpenFOAM/containers/Lists/FixedList/FixedListIO.C index 9119234ef6..31cf08ada6 100644 --- a/src/OpenFOAM/containers/Lists/FixedList/FixedListIO.C +++ b/src/OpenFOAM/containers/Lists/FixedList/FixedListIO.C @@ -118,6 +118,8 @@ Foam::Istream& Foam::operator>>(Foam::Istream& is, FixedList& L) } else { + // contents are binary and contiguous + is.read(reinterpret_cast(L.data()), Size*sizeof(T)); is.fatalCheck @@ -167,12 +169,10 @@ Foam::Ostream& Foam::operator<<(Ostream& os, const FixedList& L) // Write list contents depending on data format if (os.format() == IOstream::ASCII || !contiguous()) { - bool uniform = false; - - if (Size > 1 && contiguous()) + // Can the contents be considered 'uniform' (ie, identical)? + bool uniform = (Size > 1 && contiguous()); + if (uniform) { - uniform = true; - forAll(L, i) { if (L[i] != L[0]) @@ -194,7 +194,7 @@ Foam::Ostream& Foam::operator<<(Ostream& os, const FixedList& L) // Write end delimiter os << token::END_BLOCK; } - else if (Size <= 1 ||(Size < 11 && contiguous())) + else if (Size <= 1 || (Size < 11 && contiguous())) { // Write start delimiter os << token::BEGIN_LIST; @@ -202,7 +202,7 @@ Foam::Ostream& Foam::operator<<(Ostream& os, const FixedList& L) // Write contents forAll(L, i) { - if (i > 0) os << token::SPACE; + if (i) os << token::SPACE; os << L[i]; } @@ -226,6 +226,9 @@ Foam::Ostream& Foam::operator<<(Ostream& os, const FixedList& L) } else { + // Contents are binary and contiguous + + // write(...) includes surrounding start/end delimiters os.write(reinterpret_cast(L.cdata()), Size*sizeof(T)); } diff --git a/src/OpenFOAM/containers/Lists/List/ListIO.C b/src/OpenFOAM/containers/Lists/List/ListIO.C index d43fda22d7..bbc55ec10d 100644 --- a/src/OpenFOAM/containers/Lists/List/ListIO.C +++ b/src/OpenFOAM/containers/Lists/List/ListIO.C @@ -92,6 +92,8 @@ Foam::Istream& Foam::operator>>(Istream& is, List& L) } else { + // uniform content (delimiter == token::BEGIN_BLOCK) + T element; is >> element; @@ -113,6 +115,8 @@ Foam::Istream& Foam::operator>>(Istream& is, List& L) } else { + // contents are binary and contiguous + if (s) { is.read(reinterpret_cast(L.data()), s*sizeof(T)); diff --git a/src/OpenFOAM/containers/Lists/PackedList/PackedList.C b/src/OpenFOAM/containers/Lists/PackedList/PackedList.C index 5e0487ebcd..6addd5d23e 100644 --- a/src/OpenFOAM/containers/Lists/PackedList/PackedList.C +++ b/src/OpenFOAM/containers/Lists/PackedList/PackedList.C @@ -407,12 +407,10 @@ Foam::Ostream& Foam::PackedList::write // Write list contents depending on data format if (os.format() == IOstream::ASCII) { - bool uniform = false; - - if (sz > 1 && !indexedOutput) + // Can the contents be considered 'uniform' (ie, identical)? + bool uniform = (sz > 1 && !indexedOutput); + if (uniform) { - uniform = true; - forAll(lst, i) { if (lst[i] != lst[0]) @@ -475,9 +473,12 @@ Foam::Ostream& Foam::PackedList::write } else { + // Contents are binary and contiguous + os << nl << sz << nl; if (sz) { + // write(...) includes surrounding start/end delimiters os.write ( reinterpret_cast(lst.storage().cdata()), diff --git a/src/OpenFOAM/containers/Lists/UIndirectList/UIndirectListIO.C b/src/OpenFOAM/containers/Lists/UIndirectList/UIndirectListIO.C index 18f1f5fe94..3b52f8484c 100644 --- a/src/OpenFOAM/containers/Lists/UIndirectList/UIndirectListIO.C +++ b/src/OpenFOAM/containers/Lists/UIndirectList/UIndirectListIO.C @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011-2014 OpenFOAM Foundation - \\/ M anipulation | + \\/ M anipulation | Copyright (C) 2016 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -40,12 +40,10 @@ Foam::Ostream& Foam::operator<< // Write list contents depending on data format if (os.format() == IOstream::ASCII || !contiguous()) { - bool uniform = false; - - if (L.size() > 1 && contiguous()) + // Can the contents be considered 'uniform' (ie, identical)? + bool uniform = (L.size() > 1 && contiguous()); + if (uniform) { - uniform = true; - forAll(L, i) { if (L[i] != L[0]) @@ -99,14 +97,15 @@ Foam::Ostream& Foam::operator<< } else { - // this is annoying, and wasteful, but there's currently no alternative - + // Contents are binary and contiguous os << nl << L.size() << nl; if (L.size()) { + // This is annoying, and wasteful, but currently no alternative List lst = L(); + // write(...) includes surrounding start/end delimiters os.write ( reinterpret_cast(lst.cdata()), diff --git a/src/OpenFOAM/containers/Lists/UList/UListIO.C b/src/OpenFOAM/containers/Lists/UList/UListIO.C index e31b5dd2bc..cba400379f 100644 --- a/src/OpenFOAM/containers/Lists/UList/UListIO.C +++ b/src/OpenFOAM/containers/Lists/UList/UListIO.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) 2016 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -65,12 +65,10 @@ Foam::Ostream& Foam::operator<<(Foam::Ostream& os, const Foam::UList& L) // Write list contents depending on data format if (os.format() == IOstream::ASCII || !contiguous()) { - bool uniform = false; - - if (L.size() > 1 && contiguous()) + // Can the contents be considered 'uniform' (ie, identical)? + bool uniform = (L.size() > 1 && contiguous()); + if (uniform) { - uniform = true; - forAll(L, i) { if (L[i] != L[0]) @@ -100,7 +98,7 @@ Foam::Ostream& Foam::operator<<(Foam::Ostream& os, const Foam::UList& L) // Write contents forAll(L, i) { - if (i > 0) os << token::SPACE; + if (i) os << token::SPACE; os << L[i]; } @@ -124,10 +122,13 @@ Foam::Ostream& Foam::operator<<(Foam::Ostream& os, const Foam::UList& L) } else { + // Contents are binary and contiguous os << nl << L.size() << nl; + if (L.size()) { - os.write(reinterpret_cast(L.v_), L.byteSize()); + // write(...) includes surrounding start/end delimiters + os.write(reinterpret_cast(L.cdata()), L.byteSize()); } } @@ -208,6 +209,8 @@ Foam::Istream& Foam::operator>>(Istream& is, UList& L) } else { + // uniform content (delimiter == token::BEGIN_BLOCK) + T element; is >> element; @@ -229,6 +232,8 @@ Foam::Istream& Foam::operator>>(Istream& is, UList& L) } else { + // contents are binary and contiguous + if (s) { is.read(reinterpret_cast(L.data()), s*sizeof(T)); diff --git a/src/OpenFOAM/fields/Fields/Field/Field.C b/src/OpenFOAM/fields/Fields/Field/Field.C index 1c021bcca2..997135bc34 100644 --- a/src/OpenFOAM/fields/Fields/Field/Field.C +++ b/src/OpenFOAM/fields/Fields/Field/Field.C @@ -727,12 +727,10 @@ void Foam::Field::writeEntry(const word& keyword, Ostream& os) const { os.writeKeyword(keyword); - bool uniform = false; - - if (this->size() && contiguous()) + // Can the contents be considered 'uniform' (ie, identical)? + bool uniform = (this->size() && contiguous()); + if (uniform) { - uniform = true; - forAll(*this, i) { if (this->operator[](i) != this->operator[](0)) diff --git a/src/OpenFOAM/matrices/Matrix/MatrixIO.C b/src/OpenFOAM/matrices/Matrix/MatrixIO.C index 154a066914..b965bb37d7 100644 --- a/src/OpenFOAM/matrices/Matrix/MatrixIO.C +++ b/src/OpenFOAM/matrices/Matrix/MatrixIO.C @@ -160,15 +160,13 @@ Foam::Ostream& Foam::operator<<(Ostream& os, const Matrix& M) { if (mn) { - bool uniform = false; - const Type* v = M.v_; - if (mn > 1 && contiguous()) + // can the contents be considered 'uniform' (ie, identical) + bool uniform = (mn > 1 && contiguous()); + if (uniform) { - uniform = true; - - for (label i=0; i& M) if (uniform) { - // Write size of list and start contents delimiter + // Write start delimiter os << token::BEGIN_BLOCK; - // Write list contents + // Write contents os << v[0]; - // Write end of contents delimiter + // Write end delimiter os << token::END_BLOCK; } else if (mn < 10 && contiguous()) { - // Write size of list and start contents delimiter + // Write start contents delimiter os << token::BEGIN_LIST; label k = 0; @@ -204,7 +202,7 @@ Foam::Ostream& Foam::operator<<(Ostream& os, const Matrix& M) // Write row for (label j=0; j< M.n(); j++) { - if (j > 0) os << token::SPACE; + if (j) os << token::SPACE; os << v[k++]; } @@ -216,7 +214,7 @@ Foam::Ostream& Foam::operator<<(Ostream& os, const Matrix& M) } else { - // Write size of list and start contents delimiter + // Write start contents delimiter os << nl << token::BEGIN_LIST; label k = 0; From 9965c5006c794f262a531de0c8d9dc2a6222adbe Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Fri, 1 Jul 2016 09:55:51 +0200 Subject: [PATCH 5/8] STYLE: minor improvement when writing list entry --- .../containers/Lists/FixedList/FixedListIO.C | 8 +++----- src/OpenFOAM/containers/Lists/UList/UList.H | 2 +- src/OpenFOAM/containers/Lists/UList/UListIO.C | 15 ++++++--------- 3 files changed, 10 insertions(+), 15 deletions(-) diff --git a/src/OpenFOAM/containers/Lists/FixedList/FixedListIO.C b/src/OpenFOAM/containers/Lists/FixedList/FixedListIO.C index 31cf08ada6..e6ba7a4678 100644 --- a/src/OpenFOAM/containers/Lists/FixedList/FixedListIO.C +++ b/src/OpenFOAM/containers/Lists/FixedList/FixedListIO.C @@ -138,12 +138,10 @@ Foam::Istream& Foam::operator>>(Foam::Istream& is, FixedList& L) template void Foam::FixedList::writeEntry(Ostream& os) const { - if - ( - token::compound::isCompound("List<" + word(pTraits::typeName) + '>') - ) + const word tag = "List<" + word(pTraits::typeName) + '>'; + if (token::compound::isCompound(tag)) { - os << word("List<" + word(pTraits::typeName) + '>') << " "; + os << tag << " "; } os << *this; diff --git a/src/OpenFOAM/containers/Lists/UList/UList.H b/src/OpenFOAM/containers/Lists/UList/UList.H index ee574c33ec..719df10a0f 100644 --- a/src/OpenFOAM/containers/Lists/UList/UList.H +++ b/src/OpenFOAM/containers/Lists/UList/UList.H @@ -219,7 +219,7 @@ public: //- Copy elements of the given UList void deepCopy(const UList&); - //- Write the UList as a dictionary entry + //- Write the UList with its compound type void writeEntry(Ostream&) const; //- Write the UList as a dictionary entry with keyword diff --git a/src/OpenFOAM/containers/Lists/UList/UListIO.C b/src/OpenFOAM/containers/Lists/UList/UListIO.C index cba400379f..3d78024079 100644 --- a/src/OpenFOAM/containers/Lists/UList/UListIO.C +++ b/src/OpenFOAM/containers/Lists/UList/UListIO.C @@ -34,16 +34,13 @@ License template void Foam::UList::writeEntry(Ostream& os) const { - if - ( - size() - && token::compound::isCompound - ( - "List<" + word(pTraits::typeName) + '>' - ) - ) + if (size()) { - os << word("List<" + word(pTraits::typeName) + '>') << " "; + const word tag = "List<" + word(pTraits::typeName) + '>'; + if (token::compound::isCompound(tag)) + { + os << tag << ' '; + } } os << *this; From 56787c8074c25230363411d4e884d8784a248444 Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Mon, 3 Oct 2016 17:20:14 +0200 Subject: [PATCH 6/8] STYLE: extraneous UINT64_MIN macros (issue #257) --- src/OpenFOAM/primitives/ints/int64/int64.H | 3 --- src/OpenFOAM/primitives/ints/uint64/uint64.H | 3 --- 2 files changed, 6 deletions(-) diff --git a/src/OpenFOAM/primitives/ints/int64/int64.H b/src/OpenFOAM/primitives/ints/int64/int64.H index bcbcea38f7..58b0adc5b7 100644 --- a/src/OpenFOAM/primitives/ints/int64/int64.H +++ b/src/OpenFOAM/primitives/ints/int64/int64.H @@ -45,9 +45,6 @@ SourceFiles #include "pTraits.H" #include "direction.H" -#ifndef UINT64_MIN -#define UINT64_MIN 0 -#endif // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // diff --git a/src/OpenFOAM/primitives/ints/uint64/uint64.H b/src/OpenFOAM/primitives/ints/uint64/uint64.H index 2cf102e057..f55c73d531 100644 --- a/src/OpenFOAM/primitives/ints/uint64/uint64.H +++ b/src/OpenFOAM/primitives/ints/uint64/uint64.H @@ -45,9 +45,6 @@ SourceFiles #include "pTraits.H" #include "direction.H" -#ifndef UINT64_MIN -#define UINT64_MIN 0 -#endif // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // From 8dc3bbe6fd5bf7ba27d6818dd7713543602a7273 Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Tue, 4 Oct 2016 08:40:55 +0200 Subject: [PATCH 7/8] ENH: remove unneeded lines in List binary output (issue #256) Writing an empty list in binary results in unnecessary newlines that make the output 'noisier'. Old output ~~~~~~~~~~ ASCII someEmptyList___0(); Binary someEmptyList___ 0 ; Updated ~~~~~~~ ASCII someEmptyList___0(); Binary someEmptyList___0; --- src/OpenFOAM/containers/Lists/UList/UListIO.C | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/OpenFOAM/containers/Lists/UList/UListIO.C b/src/OpenFOAM/containers/Lists/UList/UListIO.C index 3d78024079..a4648137cf 100644 --- a/src/OpenFOAM/containers/Lists/UList/UListIO.C +++ b/src/OpenFOAM/containers/Lists/UList/UListIO.C @@ -41,9 +41,18 @@ void Foam::UList::writeEntry(Ostream& os) const { os << tag << ' '; } + os << *this; + } + else if (os.format() == IOstream::ASCII) + { + // Zero-sized ASCII - Write size and delimiters + os << 0 << token::BEGIN_LIST << token::END_LIST; + } + else + { + // Zero-sized binary - Write size only + os << 0; } - - os << *this; } From 3e0e4532fd6a423faeb53907231b3a64c2f995b2 Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Tue, 4 Oct 2016 09:16:08 +0200 Subject: [PATCH 8/8] STYLE: minor adjustments to doxygen comments --- .../mesh/conversion/star4ToFoam/star4ToFoam.C | 2 +- .../decomposePar/decomposePar.C | 16 ++++++++-------- .../dataConversion/foamToEnsight/foamToEnsight.C | 14 ++++++-------- .../foamToEnsightParts/foamToEnsightParts.C | 4 ++-- .../dataConversion/foamToVTK/foamToVTK.C | 4 ++-- .../surfaceMeshImport/surfaceMeshImport.C | 14 +++++++------- 6 files changed, 26 insertions(+), 28 deletions(-) diff --git a/applications/utilities/mesh/conversion/star4ToFoam/star4ToFoam.C b/applications/utilities/mesh/conversion/star4ToFoam/star4ToFoam.C index 245f1175af..65cb4d01b7 100644 --- a/applications/utilities/mesh/conversion/star4ToFoam/star4ToFoam.C +++ b/applications/utilities/mesh/conversion/star4ToFoam/star4ToFoam.C @@ -31,7 +31,7 @@ Description Converts a Star-CD (v4) pro-STAR mesh into OpenFOAM format. Usage - \b star4ToFoam [OPTION] ccmMesh + \b star4ToFoam [OPTION] prostarMesh Options: - \par -ascii diff --git a/applications/utilities/parallelProcessing/decomposePar/decomposePar.C b/applications/utilities/parallelProcessing/decomposePar/decomposePar.C index cab70e7076..62723280b3 100644 --- a/applications/utilities/parallelProcessing/decomposePar/decomposePar.C +++ b/applications/utilities/parallelProcessing/decomposePar/decomposePar.C @@ -39,34 +39,34 @@ Usage Write the cell distribution as a labelList, for use with 'manual' decomposition method or as a volScalarField for post-processing. - - \par -region \ \n + - \par -region \ Decompose named region. Does not check for existence of processor*. - - \par -allRegions \n + - \par -allRegions Decompose all regions in regionProperties. Does not check for existence of processor*. - - \par -copyUniform \n + - \par -copyUniform Copy any \a uniform directories too. - \par -constant - - \par -time xxx:yyy \n + - \par -time xxx:yyy Override controlDict settings and decompose selected times. Does not re-decompose the mesh i.e. does not handle moving mesh or changing mesh cases. - - \par -fields \n + - \par -fields Use existing geometry decomposition and convert fields only. - - \par -noSets \n + - \par -noSets Skip decomposing cellSets, faceSets, pointSets. - - \par -force \n + - \par -force Remove any existing \a processor subdirectories before decomposing the geometry. - - \par -ifRequired \n + - \par -ifRequired Only decompose the geometry if the number of domains has changed from a previous decomposition. No \a processor subdirectories will be removed unless the \a -force option is also specified. This option can be used diff --git a/applications/utilities/postProcessing/dataConversion/foamToEnsight/foamToEnsight.C b/applications/utilities/postProcessing/dataConversion/foamToEnsight/foamToEnsight.C index 565c8a8439..654b14b868 100644 --- a/applications/utilities/postProcessing/dataConversion/foamToEnsight/foamToEnsight.C +++ b/applications/utilities/postProcessing/dataConversion/foamToEnsight/foamToEnsight.C @@ -34,7 +34,6 @@ Description Usage \b foamToEnsight [OPTION] - Translates OpenFOAM data to EnSight format Options: - \par -ascii @@ -43,29 +42,28 @@ Usage - \par -noZero Exclude the often incomplete initial conditions. - - \par -noLagrangian \n + - \par -noLagrangian Suppress writing lagrangian positions and fields. - \par -noPatches Suppress writing any patches. - - \par -patches patchList \n + - \par -patches patchList Specify particular patches to write. Specifying an empty list suppresses writing the internalMesh. - - \par -faceZones zoneList \n + - \par -faceZones zoneList Specify faceZones to write, with wildcards - \par -cellZone zoneName Specify single cellZone to write (not lagrangian) - - \par -width \\n + - \par -width \ Width of EnSight data subdir (default: 8) Note - Parallel support for cloud data is not supported - - writes to \a EnSight directory to avoid collisions with - foamToEnsightParts + Writes to \a EnSight directory to avoid collisions with + foamToEnsightParts \*---------------------------------------------------------------------------*/ diff --git a/applications/utilities/postProcessing/dataConversion/foamToEnsightParts/foamToEnsightParts.C b/applications/utilities/postProcessing/dataConversion/foamToEnsightParts/foamToEnsightParts.C index f39f970b55..8c0bfb376a 100644 --- a/applications/utilities/postProcessing/dataConversion/foamToEnsightParts/foamToEnsightParts.C +++ b/applications/utilities/postProcessing/dataConversion/foamToEnsightParts/foamToEnsightParts.C @@ -48,10 +48,10 @@ Usage Ignore the time index contained in the time file and use a simple indexing when creating the \c Ensight/data/######## files. - - \par -noLagrangian \n + - \par -noLagrangian Suppress writing lagrangian positions and fields. - - \par -index \\n + - \par -index \ Ignore the time index contained in the time file and use a simple indexing when creating the \c Ensight/data/######## files. diff --git a/applications/utilities/postProcessing/dataConversion/foamToVTK/foamToVTK.C b/applications/utilities/postProcessing/dataConversion/foamToVTK/foamToVTK.C index 55b6bac97c..4e42b5afbf 100644 --- a/applications/utilities/postProcessing/dataConversion/foamToVTK/foamToVTK.C +++ b/applications/utilities/postProcessing/dataConversion/foamToVTK/foamToVTK.C @@ -72,10 +72,10 @@ Usage - \par -noInternal Do not generate file for mesh, only for patches - - \par -noLagrangian \n + - \par -noLagrangian Suppress writing lagrangian positions and fields. - - \par -noPointValues \n + - \par -noPointValues No pointFields - \par -noFaceZones diff --git a/applications/utilities/surface/surfaceMeshImport/surfaceMeshImport.C b/applications/utilities/surface/surfaceMeshImport/surfaceMeshImport.C index dc0b779fe1..056f513baf 100644 --- a/applications/utilities/surface/surfaceMeshImport/surfaceMeshImport.C +++ b/applications/utilities/surface/surfaceMeshImport/surfaceMeshImport.C @@ -36,25 +36,25 @@ Usage \b surfaceMeshImport inputFile [OPTION] Options: - - \par -clean \n + - \par -clean Perform some surface checking/cleanup on the input surface. - - \par -name \ \n + - \par -name \ Specify an alternative surface name when writing. - - \par -scaleIn \ \n + - \par -scaleIn \ Specify a scaling factor when reading files. - - \par -scaleOut \ \n + - \par -scaleOut \ Specify a scaling factor when writing files. - - \par -dict \ \n + - \par -dict \ Specify an alternative dictionary for constant/coordinateSystems. - - \par -from \ \n + - \par -from \ Specify a coordinate system when reading files. - - \par -to \ \n + - \par -to \ Specify a coordinate system when writing files. Note