From 3524a6f4df9f6876026bc1e580891fd3d5c795e6 Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Fri, 10 Nov 2023 09:52:40 +0100 Subject: [PATCH] FIX: getLine() line counting (adjustment to 459aaad0f9a7) ENH: return ITstream::emptyStream() in a 'bad' state - to indicate that it is invalid for reading --- applications/test/IFstream/Make/files | 2 +- .../IFstream/{Test-IFstream.C => Test-IFstream.cxx} | 10 +++++----- applications/test/ITstream/Make/files | 2 +- .../ITstream/{Test-ITstream.C => Test-ITstream.cxx} | 11 +++++++++++ applications/test/OFstream/Make/files | 2 +- .../OFstream/{Test-OFstream.C => Test-OFstream.cxx} | 0 src/OpenFOAM/db/IOstreams/Sstreams/ISstreamI.H | 6 +++--- src/OpenFOAM/db/IOstreams/Tstreams/ITstream.C | 9 ++++++--- src/OpenFOAM/db/IOstreams/Tstreams/ITstream.H | 12 +++++++++--- 9 files changed, 37 insertions(+), 17 deletions(-) rename applications/test/IFstream/{Test-IFstream.C => Test-IFstream.cxx} (94%) rename applications/test/ITstream/{Test-ITstream.C => Test-ITstream.cxx} (97%) rename applications/test/OFstream/{Test-OFstream.C => Test-OFstream.cxx} (100%) diff --git a/applications/test/IFstream/Make/files b/applications/test/IFstream/Make/files index 71a272eb18..d7c1d70990 100644 --- a/applications/test/IFstream/Make/files +++ b/applications/test/IFstream/Make/files @@ -1,3 +1,3 @@ -Test-IFstream.C +Test-IFstream.cxx EXE = $(FOAM_USER_APPBIN)/Test-IFstream diff --git a/applications/test/IFstream/Test-IFstream.C b/applications/test/IFstream/Test-IFstream.cxx similarity index 94% rename from applications/test/IFstream/Test-IFstream.C rename to applications/test/IFstream/Test-IFstream.cxx index c3a193fd21..404264cfca 100644 --- a/applications/test/IFstream/Test-IFstream.C +++ b/applications/test/IFstream/Test-IFstream.cxx @@ -66,7 +66,8 @@ int main(int argc, char *argv[]) // Test that buffer resizing works as expected - Info<< "buf: " << buf.size() << '/' << buf.capacity() << nl; + Info<< "buf: " << buf.size() << '/' << buf.capacity() + << nl << nl; bool skipDoc = true; @@ -89,14 +90,13 @@ int main(int argc, char *argv[]) << nl; auto n = is.getLine(nullptr, '}'); - Info<< is.lineNumber() << ": [" << label(n) << "]" << nl; + Info<< is.lineNumber() << ": [" << label(n) << ']' << nl; } } else { - auto n = is.getLine(nullptr); - - Info<< is.lineNumber() << ": [" << label(n) << "]" << nl; + auto n = is.getLine(nullptr); // default: '\n' + Info<< is.lineNumber() << ": [" << label(n) << ']' << nl; } } } diff --git a/applications/test/ITstream/Make/files b/applications/test/ITstream/Make/files index 6b70c4ea83..a29bcbcf52 100644 --- a/applications/test/ITstream/Make/files +++ b/applications/test/ITstream/Make/files @@ -1,3 +1,3 @@ -Test-ITstream.C +Test-ITstream.cxx EXE = $(FOAM_USER_APPBIN)/Test-ITstream diff --git a/applications/test/ITstream/Test-ITstream.C b/applications/test/ITstream/Test-ITstream.cxx similarity index 97% rename from applications/test/ITstream/Test-ITstream.C rename to applications/test/ITstream/Test-ITstream.cxx index d038d1bb47..4c343e2ec1 100644 --- a/applications/test/ITstream/Test-ITstream.C +++ b/applications/test/ITstream/Test-ITstream.cxx @@ -24,6 +24,8 @@ License along with OpenFOAM. If not, see . Description + Basic test for ITstream, which is the input token stream used by + primitiveEntry (dictionary). \*---------------------------------------------------------------------------*/ @@ -473,6 +475,15 @@ int main(int argc, char *argv[]) reverse(listInput); doTest("List", listInput, true); + + + { + auto& empty = ITstream::emptyStream(); + Info<< "The empty stream:" << nl + << " name: " << empty.name() + << " good:" << empty.good() + << " content:" << empty << nl; + } } if (args.found("rewrite")) diff --git a/applications/test/OFstream/Make/files b/applications/test/OFstream/Make/files index 7c5032c730..f9dee28f3b 100644 --- a/applications/test/OFstream/Make/files +++ b/applications/test/OFstream/Make/files @@ -1,3 +1,3 @@ -Test-OFstream.C +Test-OFstream.cxx EXE = $(FOAM_USER_APPBIN)/Test-OFstream diff --git a/applications/test/OFstream/Test-OFstream.C b/applications/test/OFstream/Test-OFstream.cxx similarity index 100% rename from applications/test/OFstream/Test-OFstream.C rename to applications/test/OFstream/Test-OFstream.cxx diff --git a/src/OpenFOAM/db/IOstreams/Sstreams/ISstreamI.H b/src/OpenFOAM/db/IOstreams/Sstreams/ISstreamI.H index 628e404eed..d96588a5be 100644 --- a/src/OpenFOAM/db/IOstreams/Sstreams/ISstreamI.H +++ b/src/OpenFOAM/db/IOstreams/Sstreams/ISstreamI.H @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2014 OpenFOAM Foundation - Copyright (C) 2017-2020 OpenCFD Ltd. + Copyright (C) 2017-2023 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -76,10 +76,10 @@ inline int Foam::ISstream::peek() inline Foam::ISstream& Foam::ISstream::getLine(std::string& str, char delim) { std::getline(is_, str, delim); - std::streamsize count = is_.gcount(); syncState(); - if (delim == '\n' && count > 0) + // Unlike with ignore(), cannot use gcount() to test success + if (delim == '\n') { ++lineNumber_; } diff --git a/src/OpenFOAM/db/IOstreams/Tstreams/ITstream.C b/src/OpenFOAM/db/IOstreams/Tstreams/ITstream.C index f90e4b1577..5fa8c588bb 100644 --- a/src/OpenFOAM/db/IOstreams/Tstreams/ITstream.C +++ b/src/OpenFOAM/db/IOstreams/Tstreams/ITstream.C @@ -84,6 +84,9 @@ Foam::ITstream& Foam::ITstream::emptyStream() emptyStreamPtr_.reset(new ITstream(Foam::zero{}, "empty-stream")); } + // Set stream as bad to indicate that this is an invald stream + emptyStreamPtr_->setBad(); + return *emptyStreamPtr_; } @@ -296,14 +299,14 @@ void Foam::ITstream::print(Ostream& os) const { os << "ITstream : " << name_.c_str() << ", line "; - const tokenList& toks = *this; - - if (toks.empty()) + if (tokenList::empty()) { os << lineNumber(); } else { + const tokenList& toks = *this; + os << toks.front().lineNumber(); if (toks.front().lineNumber() < toks.back().lineNumber()) diff --git a/src/OpenFOAM/db/IOstreams/Tstreams/ITstream.H b/src/OpenFOAM/db/IOstreams/Tstreams/ITstream.H index a0a93ee27f..e2f59353f3 100644 --- a/src/OpenFOAM/db/IOstreams/Tstreams/ITstream.H +++ b/src/OpenFOAM/db/IOstreams/Tstreams/ITstream.H @@ -190,9 +190,15 @@ public: // Static Functions - //- Return readable reference to an empty ITstream. - //- For functions needing to return an ITstream reference - //- that don't have anything valid. + //- Return reference to an empty ITstream, for functions needing to + //- return an ITstream reference but which don't have anything valid + //- of their own. The stream shall be considered \em read-only. + // + // The returned stream has no tokens and has a 'bad' state, + // to indicate that it is invalid for reading. + // + // \caution the caller can still modify the its contents, + // but they should not do that! static ITstream& emptyStream(); //- Create token list by parsing the input character sequence