ENH: improve stream output of std::string etc.

- change write(const string&) to write(const std::string&).
  This allows output of std::string without an intermediate copy.

- additional writeQuoted method to handle range of char data:

     writeQuoted(const char* str, std::streamsize len, bool)

  This helps with supporting string_view and span<char>

- add operator<< for stdFoam::span<char> and std::string_view (c++17)

- avoid duplicate code in OBJstream

STYLE: add override keyword for IO stream methods
This commit is contained in:
Mark Olesen 2023-10-03 14:17:10 +02:00 committed by Andrew Heather
parent d9727fad1c
commit b76595df42
35 changed files with 1036 additions and 788 deletions

View File

@ -57,7 +57,7 @@ class IFstreamDelayed
:
public IFstream
{
virtual bool readCompoundToken(token& tok, const word& type)
virtual bool readCompoundToken(token& tok, const word& type) override
{
auto& is = *this;

View File

@ -0,0 +1,3 @@
Test-string_view1.C
EXE = $(FOAM_USER_APPBIN)/Test-string_view1

View File

@ -0,0 +1,2 @@
/* EXE_INC = */
/* EXE_LIBS = */

View File

@ -0,0 +1,94 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / 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 <http://www.gnu.org/licenses/>.
Description
Test some string_view functionality
\*---------------------------------------------------------------------------*/
#include "string.H"
#include "IOstreams.H"
#include "List.H"
using namespace Foam;
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Main program:
int main(int argc, char *argv[])
{
Info<< "Compiled with C++ " << __cplusplus;
#if __cplusplus >= 201703L
Info<< " - has std::string_view" << nl << nl;
#else
Info<< " - NO std::string_view" << nl << nl;
#endif
// basics
{
for
(
const auto& cstr
:
{
"abcdef"
}
)
{
const auto len = strlen(cstr);
Info<< nl
<< "input: <" << cstr << '>'
<< " type: " << typeid(cstr).name() << " len:" << len << nl;
#if __cplusplus >= 201703L
Info<< " view: " << std::string_view(cstr) << nl;
#endif
Info<< " span: "
<< stdFoam::span<const char>(cstr, len) << nl;
Info<< " span: "
<< stdFoam::span<char>(const_cast<char*>(cstr), len) << nl;
}
}
// This should fail to compile:
#if 0
{
labelList values(identity(4));
Info<< "values: " << values << nl;
Info<< " span: "
<< stdFoam::span<label>(values.data(), values.size()) << nl;
}
#endif
Info<< "\nEnd\n" << endl;
return 0;
}
// ************************************************************************* //

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2021 OpenCFD Ltd.
Copyright (C) 2021-2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM, distributed under GPL-3.0-or-later.
@ -33,11 +33,7 @@ Description
OBJstream os(runTime.globalPath()/outputName);
os.writeQuoted
(
("# " + outputName + "\n"),
false
);
os.writeComment(outputName);
os.write(aMesh.patch().edges(), aMesh.patch().localPoints());
}

View File

@ -105,20 +105,20 @@ public:
// STL stream
//- Access to underlying std::istream
virtual std::istream& stdStream();
virtual std::istream& stdStream() override;
//- Const access to underlying std::istream
virtual const std::istream& stdStream() const;
virtual const std::istream& stdStream() const override;
//- Rewind the stream so that it may be read again.
// Includes special handling for compressed streams.
virtual void rewind();
virtual void rewind() override;
// Print
//- Print stream description
virtual void print(Ostream& os) const;
virtual void print(Ostream& os) const override;
// Member Operators

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2017 OpenFOAM Foundation
Copyright (C) 2017-2022 OpenCFD Ltd.
Copyright (C) 2017-2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -130,11 +130,11 @@ public:
// STL stream
//- Access to underlying std::ostream
virtual std::ostream& stdStream();
//- Const access to underlying std::ostream
virtual const std::ostream& stdStream() const;
virtual const std::ostream& stdStream() const override;
//- Access to underlying std::ostream
virtual std::ostream& stdStream() override;
//- Rewind the stream so that it may be written again.
//- Reopens the file (truncation)
@ -144,7 +144,7 @@ public:
// Print
//- Print stream description
void print(Ostream& os) const;
void print(Ostream& os) const override;
// Additional constructors and methods

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2016-2019 OpenCFD Ltd.
Copyright (C) 2016-2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -48,16 +48,26 @@ void Foam::Ostream::decrIndent()
}
Foam::Ostream& Foam::Ostream::writeQuoted
(
const std::string& str,
const bool quoted
)
{
return writeQuoted(str.data(), str.size(), quoted);
}
Foam::Ostream& Foam::Ostream::write(const keyType& kw)
{
return writeQuoted(kw, kw.isPattern());
return writeQuoted(kw.data(), kw.size(), kw.isPattern());
}
Foam::Ostream& Foam::Ostream::writeKeyword(const keyType& kw)
{
indent();
writeQuoted(kw, kw.isPattern());
writeQuoted(kw.data(), kw.size(), kw.isPattern());
if (indentSize_ <= 1)
{
@ -86,7 +96,7 @@ Foam::Ostream& Foam::Ostream::writeKeyword(const keyType& kw)
Foam::Ostream& Foam::Ostream::beginBlock(const keyType& kw)
{
indent(); writeQuoted(kw, kw.isPattern()); write('\n');
indent(); writeQuoted(kw.data(), kw.size(), kw.isPattern()); write('\n');
beginBlock();
return *this;

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2016-2022 OpenCFD Ltd.
Copyright (C) 2016-2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -41,6 +41,7 @@ SourceFiles
#include "IOstream.H"
#include "keyType.H"
#include "stdFoam.H" // For span etc.
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -118,28 +119,35 @@ public:
//- Write character
virtual Ostream& write(const char c) = 0;
//- Write character/string content, with/without surrounding quotes
virtual Ostream& writeQuoted
(
const char* str,
std::streamsize len,
const bool quoted=true
) = 0;
//- Write string content, with/without surrounding quotes
virtual Ostream& writeQuoted
(
const std::string& str,
const bool quoted=true
);
//- Write character string
virtual Ostream& write(const char* str) = 0;
//- Write word
//- Write word (unquoted)
virtual Ostream& write(const word& str) = 0;
//- Write string content (usually quoted)
virtual Ostream& write(const std::string& str) = 0;
//- Write keyType
// A plain word is written unquoted.
// A regular expression is written as a quoted string.
virtual Ostream& write(const keyType& kw);
//- Write string
virtual Ostream& write(const string& str) = 0;
//- Write std::string surrounded by quotes.
// Optional write without quotes.
virtual Ostream& writeQuoted
(
const std::string& str,
const bool quoted=true
) = 0;
//- Write int32_t
virtual Ostream& write(const int32_t val) = 0;
@ -317,9 +325,38 @@ public:
};
// --------------------------------------------------------------------
// ------ Manipulators (not taking arguments)
// --------------------------------------------------------------------
// * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * //
#if __cplusplus >= 201703L
//- Write operator for std::string_view
inline Ostream& operator<<(Ostream& os, std::string_view s)
{
os.writeQuoted(s.data(), s.size(), true); // quoted
os.check("Foam::operator<<(Ostream&, std::string_view)");
return os;
}
#endif
//- Write operator for character span. Output like string data
inline Ostream& operator<<(Ostream& os, stdFoam::span<char> s)
{
os.writeQuoted(s.data(), s.size(), true); // quoted
os.check("Foam::operator<<(Ostream&, stdFoam::span<char>)");
return os;
}
//- Write operator for const character span. Output like string data
inline Ostream& operator<<(Ostream& os, stdFoam::span<const char> s)
{
os.writeQuoted(s.data(), s.size(), true); // quoted
os.check("Foam::operator<<(Ostream&, stdFoam::span<const char>)");
return os;
}
/*---------------------------------------------------------------------------*\
Manipulators (without arguments)
\*---------------------------------------------------------------------------*/
//- An Ostream manipulator
typedef Ostream& (*OstreamManip)(Ostream&);

View File

@ -149,77 +149,71 @@ public:
// Member Functions
// Inquiry
// Stream State Functions
//- Return flags of output stream
virtual ios_base::fmtflags flags() const
{
return ios_base::fmtflags(0);
}
//- Return stream flags
virtual ios_base::fmtflags flags() const override
{
return ios_base::fmtflags(0);
}
// Read Functions
//- Return next token from stream
Istream& read(token& t);
//- Read a character
Istream& read(char& c);
//- Read a word
Istream& read(word& str);
// Read a string
Istream& read(string& str);
//- Read a label
Istream& read(label& val);
//- Read a float
Istream& read(float& val);
//- Read a double
Istream& read(double& val);
//- Read binary block with 8-byte alignment.
//- Reading into a null pointer behaves like a forward seek of
//- count characters.
Istream& read(char* data, std::streamsize count);
//- Low-level raw binary read.
//- Reading into a null pointer behaves like a forward seek of
//- count characters.
Istream& readRaw(char* data, std::streamsize count);
//- Start of low-level raw binary read
bool beginRawRead();
//- End of low-level raw binary read
bool endRawRead()
{
return true;
}
// Positioning
//- Rewind the receive stream position so that it may be read again
virtual void rewind();
// Edit
//- Set flags of stream
virtual ios_base::fmtflags flags(const ios_base::fmtflags)
//- Set flags of stream flags
virtual ios_base::fmtflags flags(const ios_base::fmtflags) override
{
return ios_base::fmtflags(0);
}
// Read Functions
//- Return next token from stream
virtual Istream& read(token&) override;
//- Read a character
virtual Istream& read(char& c) override;
//- Read a word
virtual Istream& read(word& str) override;
// Read a string
virtual Istream& read(string& str) override;
//- Read a label
virtual Istream& read(label& val) override;
//- Read a float
virtual Istream& read(float& val) override;
//- Read a double
virtual Istream& read(double& val) override;
//- Read binary block with 8-byte alignment.
//- Reading into a null pointer behaves like a forward seek of
//- count characters.
virtual Istream& read(char* data, std::streamsize count) override;
//- Low-level raw binary read.
//- Reading into a null pointer behaves like a forward seek of
//- count characters.
virtual Istream& readRaw(char* data, std::streamsize count) override;
//- Start of low-level raw binary read
virtual bool beginRawRead() override;
//- End of low-level raw binary read
virtual bool endRawRead() override { return true; }
// Positioning
//- Rewind the receive stream position so that it may be read again
virtual void rewind() override;
// Print
//- Print stream description to Ostream
void print(Ostream& os) const;
void print(Ostream& os) const override;
};

View File

@ -137,7 +137,7 @@ inline void Foam::UIPstreamBase::readFromBuffer
inline Foam::Istream& Foam::UIPstreamBase::readString(std::string& str)
{
// Use std::string::assign() to copy content, including '\0'.
// Use std::string::assign() to copy content, including embedded nul chars.
// Stripping (when desired) is the responsibility of the sending side.
size_t len;

View File

@ -83,8 +83,10 @@ class UOPstreamBase
//- Add a single char to the send buffer. No alignment needed
inline void putChar(const char c);
//- Write string length and string content.
// The content includes the trailing nul char.
//- Write string length and content, including embedded nul chars.
inline void putString(const char* str, const size_t len);
//- Write string length and content, including embedded nul chars.
inline void putString(const std::string& str);
@ -142,124 +144,135 @@ public:
// Member Functions
// Inquiry
// Inquiry
//- Return flags of output stream
virtual ios_base::fmtflags flags() const
{
return ios_base::fmtflags(0);
}
//- Return flags of output stream
virtual ios_base::fmtflags flags() const override
{
return ios_base::fmtflags(0);
}
// Write Functions
// Write Functions
//- Write token to stream or otherwise handle it.
// \return false if the token type was not handled by this method
virtual bool write(const token& tok);
//- Inherit write methods from Ostream
using Ostream::writeQuoted;
//- Write single character. Whitespace is suppressed.
virtual Ostream& write(const char c);
//- Write token to stream or otherwise handle it.
// \return false if the token type was not handled by this method
virtual bool write(const token& tok) override;
//- Write the word-characters of a character string.
// Sends as a single char, or as word.
virtual Ostream& write(const char* str);
//- Write single character. Whitespace is suppressed.
virtual Ostream& write(const char c) override;
//- Write word
virtual Ostream& write(const word& str);
//- Write character/string content, with/without surrounding quotes
virtual Ostream& writeQuoted
(
const char* str,
std::streamsize len,
const bool quoted=true
) override;
//- Write string
virtual Ostream& write(const string& str);
//- Write the word-characters of a character string.
// Sends as a single char, or as word.
virtual Ostream& write(const char* str) override;
//- Write std::string surrounded by quotes.
// Optional write without quotes.
virtual Ostream& writeQuoted
(
const std::string& str,
const bool quoted=true
);
//- Write word
virtual Ostream& write(const word& str) override;
//- Write int32_t as a label
virtual Ostream& write(const int32_t val);
//- Write string
virtual Ostream& write(const std::string& str) override;
//- Write int64_t as a label
virtual Ostream& write(const int64_t val);
//- Write int32_t as a label
virtual Ostream& write(const int32_t val) override;
//- Write float
virtual Ostream& write(const float val);
//- Write int64_t as a label
virtual Ostream& write(const int64_t val) override;
//- Write double
virtual Ostream& write(const double val);
//- Write float
virtual Ostream& write(const float val) override;
//- Write binary block with 8-byte alignment.
virtual Ostream& write(const char* data, std::streamsize count);
//- Write double
virtual Ostream& write(const double val) override;
//- Low-level raw binary output.
virtual Ostream& writeRaw(const char* data, std::streamsize count);
//- Write binary block with 8-byte alignment.
virtual Ostream& write
(
const char* data,
std::streamsize count
) override;
//- Begin marker for low-level raw binary output.
// The count indicates the number of bytes for subsequent
// writeRaw calls.
virtual bool beginRawWrite(std::streamsize count);
//- Low-level raw binary output.
virtual Ostream& writeRaw
(
const char* data,
std::streamsize count
) override;
//- End marker for low-level raw binary output.
virtual bool endRawWrite()
{
return true;
}
//- Begin marker for low-level raw binary output.
// The count indicates the number of bytes for subsequent
// writeRaw calls.
virtual bool beginRawWrite(std::streamsize count) override;
//- Add indentation characters
virtual void indent()
{}
//- End marker for low-level raw binary output.
virtual bool endRawWrite() override
{
return true;
}
//- Add indentation characters
virtual void indent() override
{}
// Stream state functions
// Stream state functions
//- Flush stream
virtual void flush()
{}
//- Flush stream
virtual void flush() override
{}
//- Add newline and flush stream
virtual void endl()
{}
//- Add newline and flush stream
virtual void endl() override
{}
//- Get the current padding character
// \return previous padding character
virtual char fill() const
{
return 0;
}
//- Get the current padding character
// \return previous padding character
virtual char fill() const override
{
return 0;
}
//- Set padding character for formatted field up to field width
virtual char fill(const char)
{
return 0;
}
//- Set padding character for formatted field up to field width
virtual char fill(const char) override
{
return 0;
}
//- Get width of output field
virtual int width() const
{
return 0;
}
//- Get width of output field
virtual int width() const override
{
return 0;
}
//- Set width of output field
// \return previous width
virtual int width(const int)
{
return 0;
}
//- Set width of output field
// \return previous width
virtual int width(const int) override
{
return 0;
}
//- Get precision of output field
virtual int precision() const
{
return 0;
}
//- Get precision of output field
virtual int precision() const override
{
return 0;
}
//- Set precision of output field
// \return old precision
virtual int precision(const int)
{
return 0;
}
//- Set precision of output field
// \return old precision
virtual int precision(const int) override
{
return 0;
}
// Positioning
@ -271,7 +284,7 @@ public:
// Edit
//- Set flags of stream
virtual ios_base::fmtflags flags(const ios_base::fmtflags)
virtual ios_base::fmtflags flags(const ios_base::fmtflags) override
{
return ios_base::fmtflags(0);
}
@ -280,7 +293,7 @@ public:
// Print
//- Print stream description to Ostream
void print(Ostream& os) const;
void print(Ostream& os) const override;
};

View File

@ -121,11 +121,20 @@ inline void Foam::UOPstreamBase::putChar(const char c)
}
inline void Foam::UOPstreamBase::putString
(
const char* str,
const size_t len
)
{
writeToBuffer(len);
writeToBuffer(str, len, 1); // no-op when len == 0
}
inline void Foam::UOPstreamBase::putString(const std::string& str)
{
const size_t len = str.size();
writeToBuffer(len);
writeToBuffer(str.data(), len, 1); // no-op when len == 0
putString(str.data(), str.size());
}
@ -253,6 +262,27 @@ Foam::Ostream& Foam::UOPstreamBase::write(const char c)
}
Foam::Ostream& Foam::UOPstreamBase::writeQuoted
(
const char* str,
std::streamsize len,
const bool quoted
)
{
if (quoted)
{
putChar(token::tokenType::STRING);
}
else
{
putChar(token::tokenType::WORD);
}
putString(str, len);
return *this;
}
Foam::Ostream& Foam::UOPstreamBase::write(const char* str)
{
const word nonWhiteChars(string::validate<word>(str));
@ -279,7 +309,7 @@ Foam::Ostream& Foam::UOPstreamBase::write(const word& str)
}
Foam::Ostream& Foam::UOPstreamBase::write(const string& str)
Foam::Ostream& Foam::UOPstreamBase::write(const std::string& str)
{
putChar(token::tokenType::STRING);
putString(str);
@ -288,26 +318,6 @@ Foam::Ostream& Foam::UOPstreamBase::write(const string& str)
}
Foam::Ostream& Foam::UOPstreamBase::writeQuoted
(
const std::string& str,
const bool quoted
)
{
if (quoted)
{
putChar(token::tokenType::STRING);
}
else
{
putChar(token::tokenType::WORD);
}
putString(str);
return *this;
}
Foam::Ostream& Foam::UOPstreamBase::write(const int32_t val)
{
putChar(token::tokenType::LABEL);

View File

@ -126,7 +126,7 @@ public:
//- The name of the input serial stream.
//- (eg, the name of the Fstream file name)
virtual const fileName& name() const { return name_; }
virtual const fileName& name() const override { return name_; }
//- The name of the input serial stream, for modification.
virtual fileName& name() { return name_; }
@ -144,13 +144,13 @@ public:
// Stream State
//- Return flags of output stream
virtual ios_base::fmtflags flags() const
virtual ios_base::fmtflags flags() const override
{
return is_.flags();
}
//- Set stream flags
virtual ios_base::fmtflags flags(const ios_base::fmtflags f)
virtual ios_base::fmtflags flags(const ios_base::fmtflags f) override
{
return is_.flags(f);
}
@ -181,74 +181,81 @@ public:
);
// Read Functions
// Serial-stream functions
//- Raw, low-level get character function.
inline ISstream& get(char& c);
//- Raw, low-level get character function.
inline ISstream& get(char& c);
//- Raw, low-level peek function.
// Does not remove the character from the stream.
// Returns the next character in the stream or EOF if the
// end of file is read.
inline int peek();
//- Raw, low-level peek function.
// Does not remove the character from the stream.
// Returns the next character in the stream or EOF if the
// end of file is read.
inline int peek();
//- Raw, low-level getline (until delimiter) into a string.
inline ISstream& getLine(std::string& str, char delim = '\n');
//- Raw, low-level getline (until delimiter) into a string.
inline ISstream& getLine(std::string& str, char delim = '\n');
//- Low-level discard until delimiter
// \return the number of characters extracted
inline std::streamsize getLine(std::nullptr_t, char delim = '\n');
//- Low-level discard until delimiter
// \return the number of characters extracted
inline std::streamsize getLine(std::nullptr_t, char delim = '\n');
//- Raw, low-level putback character function.
inline ISstream& putback(const char c);
//- Return next token from stream
virtual Istream& read(token& t);
//- Read a character
virtual Istream& read(char& c);
//- Read a word
virtual Istream& read(word& str);
//- Read a string (including enclosing double-quotes).
// Backslashes are retained, except when escaping double-quotes
// and an embedded newline character.
virtual Istream& read(string& str);
//- Read a label
virtual Istream& read(label& val);
//- Read a float
virtual Istream& read(float& val);
//- Read a double
virtual Istream& read(double& val);
//- Read binary block (with any possible block delimiters).
//- Reading into a null pointer behaves like a forward seek of
//- count characters.
virtual Istream& read(char* data, std::streamsize count);
//- Low-level raw binary read (without possible block delimiters).
//- Reading into a null pointer behaves like a forward seek of
//- count characters.
virtual Istream& readRaw(char* data, std::streamsize count);
//- Start of low-level raw binary read
virtual bool beginRawRead();
//- End of low-level raw binary read
virtual bool endRawRead();
//- Rewind the stream so that it may be read again
virtual void rewind();
//- Raw, low-level putback character function.
inline ISstream& putback(const char c);
// Print
// Read Functions
//- Print stream description to Ostream
virtual void print(Ostream& os) const;
//- Return next token from stream
virtual Istream& read(token& t) override;
//- Read a character
virtual Istream& read(char& c) override;
//- Read a word
virtual Istream& read(word& str) override;
//- Read a string (including enclosing double-quotes).
// Backslashes are retained, except when escaping double-quotes
// and an embedded newline character.
virtual Istream& read(string& str) override;
//- Read a label
virtual Istream& read(label& val) override;
//- Read a float
virtual Istream& read(float& val) override;
//- Read a double
virtual Istream& read(double& val) override;
//- Read binary block (with any possible block delimiters).
//- Reading into a null pointer behaves like a forward seek of
//- count characters.
virtual Istream& read(char* data, std::streamsize count) override;
//- Low-level raw binary read (without possible block delimiters).
//- Reading into a null pointer behaves like a forward seek of
//- count characters.
virtual Istream& readRaw
(
char* data,
std::streamsize count
) override;
//- Start of low-level raw binary read
virtual bool beginRawRead() override;
//- End of low-level raw binary read
virtual bool endRawRead() override;
//- Rewind the stream so that it may be read again
virtual void rewind() override;
// Print
//- Print stream description to Ostream
virtual void print(Ostream& os) const override;
};

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2017-2022 OpenCFD Ltd.
Copyright (C) 2017-2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -29,7 +29,7 @@ License
#include "error.H"
#include "token.H"
#include "OSstream.H"
#include "stringOps.H"
#include <algorithm>
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
@ -89,70 +89,67 @@ bool Foam::OSstream::write(const token& tok)
Foam::Ostream& Foam::OSstream::write(const char c)
{
os_ << c;
if (c == token::NL)
{
++lineNumber_;
}
syncState();
return *this;
}
Foam::Ostream& Foam::OSstream::write(const char* str)
{
lineNumber_ += stringOps::count(str, token::NL);
os_ << str;
syncState();
return *this;
}
Foam::Ostream& Foam::OSstream::write(const word& str)
{
os_ << str;
syncState();
// Advance line number on newline
if (c == token::NL) ++lineNumber_;
return *this;
}
Foam::Ostream& Foam::OSstream::writeQuoted
(
const std::string& str,
const char* str,
std::streamsize len,
const bool quoted
)
{
if (!str || len <= 0) return *this;
const char* last = (str + len);
if (!quoted)
{
// Output unquoted, only advance line number on newline
lineNumber_ += stringOps::count(str, token::NL);
os_ << str;
#if __cplusplus >= 201703L
os_ << std::string_view(str, len);
#else
for (const char* iter = str; iter != last; ++iter)
{
os_ << *iter;
}
#endif
syncState();
// Unquoted, only advance line number on newline
lineNumber_ += std::count(str, last, '\n');
return *this;
}
// Output with surrounding quotes and backslash escaping
// - functionality like std::quoted (from <iomanip>), while also
// counting the newlines.
os_ << token::DQUOTE;
unsigned backslash = 0;
for (auto iter = str.cbegin(); iter != str.cend(); ++iter)
for (auto iter = str; iter != last; ++iter)
{
const char c = *iter;
if (c == '\\')
{
++backslash;
continue; // only output after escaped character is known
continue; // Delay output until escaped character is known
}
else if (c == token::NL)
{
++lineNumber_;
++backslash; // backslash escape for newline
++backslash; // Add backslash escape for newline
++lineNumber_; // Advance line number on newline
}
else if (c == token::DQUOTE)
{
++backslash; // backslash escape for quote
++backslash; // Add backslash escape for quote
}
// output all pending backslashes
@ -174,9 +171,34 @@ Foam::Ostream& Foam::OSstream::writeQuoted
}
Foam::Ostream& Foam::OSstream::write(const string& str)
Foam::Ostream& Foam::OSstream::write(const char* str)
{
return writeQuoted(str, true);
if (!str) return *this;
const char* last = (str + strlen(str));
os_ << str;
syncState();
// Advance line number on newline
lineNumber_ += std::count(str, last, '\n');
return *this;
}
Foam::Ostream& Foam::OSstream::write(const word& str)
{
// Unquoted, and no newlines expected.
os_ << str;
syncState();
return *this;
}
Foam::Ostream& Foam::OSstream::write(const std::string& str)
{
return writeQuoted(str.data(), str.size(), true);
}

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2014 OpenFOAM Foundation
Copyright (C) 2017-2022 OpenCFD Ltd.
Copyright (C) 2017-2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -117,7 +117,7 @@ public:
//- Get the name of the output serial stream.
//- (eg, the name of the Fstream file name)
virtual const fileName& name() const { return name_; }
virtual const fileName& name() const override { return name_; }
// STL stream
@ -132,13 +132,13 @@ public:
// Stream State
//- Get stream flags
virtual ios_base::fmtflags flags() const
virtual ios_base::fmtflags flags() const override
{
return os_.flags();
}
//- Set stream flags
virtual ios_base::fmtflags flags(const ios_base::fmtflags f)
virtual ios_base::fmtflags flags(const ios_base::fmtflags f) override
{
return os_.flags(f);
}
@ -152,101 +152,108 @@ public:
// Write Functions
//- Write token to stream or otherwise handle it.
// \return false if the token type was not handled by this method
virtual bool write(const token& tok);
//- Inherit write methods from Ostream
using Ostream::writeQuoted;
//- Write character
virtual Ostream& write(const char c);
//- Write token to stream or otherwise handle it.
// \return false if the token type was not handled by this method
virtual bool write(const token& tok) override;
//- Write character string
virtual Ostream& write(const char* str);
//- Write character
virtual Ostream& write(const char c) override;
//- Write word
virtual Ostream& write(const word& str);
//- Write character/string content, with/without surrounding quotes
virtual Ostream& writeQuoted
(
const char* str,
std::streamsize len,
const bool quoted=true
) override;
//- Write string (quoted)
// In the rare case that the string contains a final trailing
// backslash, it will be dropped to the appearance of an escaped
// double-quote.
virtual Ostream& write(const string& str);
//- Write character string
virtual Ostream& write(const char* str) override;
//- Write std::string surrounded by quotes.
// Optional write without quotes.
virtual Ostream& writeQuoted
(
const std::string& str,
const bool quoted=true
);
//- Write word
virtual Ostream& write(const word& str) override;
//- Write int32_t
virtual Ostream& write(const int32_t val);
//- Write string (quoted)
// In the rare case that the string contains a final trailing
// backslash, it will be dropped to the appearance of an escaped
// double-quote.
virtual Ostream& write(const std::string& str) override;
//- Write int64_t
virtual Ostream& write(const int64_t val);
//- Write int32_t
virtual Ostream& write(const int32_t val) override;
//- Write float
virtual Ostream& write(const float val);
//- Write int64_t
virtual Ostream& write(const int64_t val) override;
//- Write double
virtual Ostream& write(const double val);
//- Write float
virtual Ostream& write(const float val) override;
//- Write binary block
virtual Ostream& write(const char* data, std::streamsize count);
//- Write double
virtual Ostream& write(const double val) override;
//- Low-level raw binary output
virtual Ostream& writeRaw
(
const char* data,
std::streamsize count
);
//- Write binary block
virtual Ostream& write
(
const char* data,
std::streamsize count
) override;
//- Begin marker for low-level raw binary output.
// The count indicates the number of bytes for subsequent
// writeRaw calls.
virtual bool beginRawWrite(std::streamsize count);
//- Low-level raw binary output
virtual Ostream& writeRaw
(
const char* data,
std::streamsize count
) override;
//- End marker for low-level raw binary output.
virtual bool endRawWrite();
//- Begin marker for low-level raw binary output.
// The count indicates the number of bytes for subsequent
// writeRaw calls.
virtual bool beginRawWrite(std::streamsize count) override;
//- Add indentation characters
virtual void indent();
//- End marker for low-level raw binary output.
virtual bool endRawWrite() override;
//- Add indentation characters
virtual void indent() override;
// Stream state functions
// Stream state functions
//- Flush stream
virtual void flush();
//- Flush stream
virtual void flush() override;
//- Add newline and flush stream
virtual void endl();
//- Add newline and flush stream
virtual void endl() override;
//- Get the current padding character
virtual char fill() const;
//- Get the current padding character
virtual char fill() const override;
//- Set padding character for formatted field up to field width
// \return previous padding character
virtual char fill(const char fillch);
//- Set padding character for formatted field up to field width
// \return previous padding character
virtual char fill(const char fillch) override;
//- Get width of output field
virtual int width() const;
//- Get width of output field
virtual int width() const override;
//- Set width of output field
// \return previous width
virtual int width(const int w);
//- Set width of output field
// \return previous width
virtual int width(const int w) override;
//- Get precision of output field
virtual int precision() const;
//- Get precision of output field
virtual int precision() const override;
//- Set precision of output field
// \return old precision
virtual int precision(const int p);
//- Set precision of output field
// \return old precision
virtual int precision(const int p) override;
// Print
// Print
//- Print stream description to Ostream
virtual void print(Ostream& os) const;
//- Print stream description to Ostream
virtual void print(Ostream& os) const override;
};

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2014 OpenFOAM Foundation
Copyright (C) 2020-2022 OpenCFD Ltd.
Copyright (C) 2020-2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -77,11 +77,21 @@ Foam::Ostream& Foam::prefixOSstream::write(const char c)
checkWritePrefix();
OSstream::write(c);
if (c == token::NL)
{
printPrefix_ = true;
}
// Reset prefix state on newline
if (c == token::NL) printPrefix_ = true;
return *this;
}
Foam::Ostream& Foam::prefixOSstream::writeQuoted
(
const char* str,
std::streamsize len,
const bool quoted
)
{
checkWritePrefix();
OSstream::writeQuoted(str, len, quoted);
return *this;
}
@ -92,8 +102,9 @@ Foam::Ostream& Foam::prefixOSstream::write(const char* str)
OSstream::write(str);
const size_t len = strlen(str);
if (len && str[len-1] == token::NL)
if (len > 0 && str[len-1] == token::NL)
{
// Reset prefix state on newline
printPrefix_ = true;
}
@ -103,26 +114,15 @@ Foam::Ostream& Foam::prefixOSstream::write(const char* str)
Foam::Ostream& Foam::prefixOSstream::write(const word& val)
{
// Unquoted, and no newlines expected.
checkWritePrefix();
return OSstream::write(val);
}
Foam::Ostream& Foam::prefixOSstream::write(const string& val)
Foam::Ostream& Foam::prefixOSstream::write(const std::string& str)
{
checkWritePrefix();
return OSstream::write(val);
}
Foam::Ostream& Foam::prefixOSstream::writeQuoted
(
const std::string& val,
const bool quoted
)
{
checkWritePrefix();
return OSstream::writeQuoted(val, quoted);
return writeQuoted(str.data(), str.size(), true);
}

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2014 OpenFOAM Foundation
Copyright (C) 2020-2022 OpenCFD Ltd.
Copyright (C) 2020-2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -65,6 +65,7 @@ class prefixOSstream
// Private Member Functions
//- Emit pending prefix before any output
inline void checkWritePrefix();
@ -83,70 +84,67 @@ public:
// Member Functions
// Enquiry
// Enquiry
//- Return the stream prefix
const string& prefix() const noexcept
{
return prefix_;
}
//- Return the stream prefix
const string& prefix() const noexcept { return prefix_; }
//- Return non-const access to the stream prefix
string& prefix() noexcept
{
return prefix_;
}
//- Return non-const access to the stream prefix
string& prefix() noexcept { return prefix_; }
// Write Functions
// Write Functions
//- Write token to stream or otherwise handle it.
// \return false if the token type was not handled by this method
virtual bool write(const token& tok);
//- Inherit write methods from OSstream
using OSstream::writeQuoted;
//- Write character
virtual Ostream& write(const char c);
//- Write token to stream or otherwise handle it.
// \return false if the token type was not handled by this method
virtual bool write(const token& tok) override;
//- Write character string
virtual Ostream& write(const char* str);
//- Write character
virtual Ostream& write(const char c) override;
//- Write word
virtual Ostream& write(const word& val);
//- Write character/string content, with/without surrounding quotes
virtual Ostream& writeQuoted
(
const char* str,
std::streamsize len,
const bool quoted=true
) override;
//- Write string
virtual Ostream& write(const string& val);
//- Write character string
virtual Ostream& write(const char* str) override;
//- Write std::string surrounded by quotes.
// Optional write without quotes.
virtual Ostream& writeQuoted
(
const std::string& val,
const bool quoted=true
);
//- Write word
virtual Ostream& write(const word& val) override;
//- Write int32_t
virtual Ostream& write(const int32_t val);
//- Write string
virtual Ostream& write(const std::string& val) override;
//- Write int64_t
virtual Ostream& write(const int64_t val);
//- Write int32_t
virtual Ostream& write(const int32_t val) override;
//- Write float
virtual Ostream& write(const float val);
//- Write int64_t
virtual Ostream& write(const int64_t val) override;
//- Write double
virtual Ostream& write(const double val);
//- Write float
virtual Ostream& write(const float val) override;
//- Write binary block
virtual Ostream& write(const char* buf, std::streamsize count);
//- Write double
virtual Ostream& write(const double val) override;
//- Add indentation characters
virtual void indent();
//- Write binary block
virtual Ostream& write(const char* buf, std::streamsize count) override;
//- Add indentation characters
virtual void indent() override;
// Print
//- Print stream description to Ostream
virtual void print(Ostream& os) const;
virtual void print(Ostream& os) const override;
};

View File

@ -166,7 +166,7 @@ public:
}
//- Print stream description to Ostream
virtual void print(Ostream& os) const;
virtual void print(Ostream& os) const override;
// Member Operators
@ -263,7 +263,7 @@ public:
}
//- Print stream description to Ostream
virtual void print(Ostream& os) const;
virtual void print(Ostream& os) const override;
// Older style, without stream option (including 2012 release)

View File

@ -223,7 +223,7 @@ public:
// Characteristics
//- The name of the input token stream.
virtual const fileName& name() const { return name_; }
virtual const fileName& name() const override { return name_; }
//- The name of the input token stream, for modification.
virtual fileName& name() { return name_; }
@ -371,13 +371,13 @@ public:
// Stream State Functions
//- Get stream flags - always 0
virtual ios_base::fmtflags flags() const
virtual ios_base::fmtflags flags() const override
{
return ios_base::fmtflags(0);
}
//- Set flags of stream - ignored
ios_base::fmtflags flags(const ios_base::fmtflags)
ios_base::fmtflags flags(const ios_base::fmtflags) override
{
return ios_base::fmtflags(0);
}
@ -386,47 +386,47 @@ public:
// Read Functions
//- Return next token from stream
virtual Istream& read(token& tok);
virtual Istream& read(token& tok) override;
//- Read a character : triggers not implemented error
virtual Istream& read(char&);
virtual Istream& read(char&) override;
//- Read a word : triggers not implemented error
virtual Istream& read(word&);
virtual Istream& read(word&) override;
//- Read a string (including enclosing double-quotes) :
//- triggers not implemented error
virtual Istream& read(string&);
virtual Istream& read(string&) override;
//- Read a label : triggers not implemented error
virtual Istream& read(label&);
virtual Istream& read(label&) override;
//- Read a float : triggers not implemented error
virtual Istream& read(float&);
virtual Istream& read(float&) override;
//- Read a double : triggers not implemented error
virtual Istream& read(double&);
virtual Istream& read(double&) override;
//- Read binary block : triggers not implemented error
virtual Istream& read(char* data, std::streamsize);
virtual Istream& read(char* data, std::streamsize) override;
//- Low-level raw binary read : triggers not implemented error
virtual Istream& readRaw(char* data, std::streamsize count);
virtual Istream& readRaw(char* data, std::streamsize count) override;
//- Start of low-level raw binary read : no-op
virtual bool beginRawRead() { return false; }
virtual bool beginRawRead() override { return false; }
//- End of low-level raw binary read : no-op
virtual bool endRawRead() { return false; }
virtual bool endRawRead() override { return false; }
//- Rewind the stream so that it may be read again. Same as seek(0)
virtual void rewind();
virtual void rewind() override;
// Output
//- Print stream description to Ostream
void print(Ostream& os) const;
void print(Ostream& os) const override;
//- Concatenate tokens into a space-separated std::string.
//- The resulting string may contain quote characters.

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2019-2022 OpenCFD Ltd.
Copyright (C) 2019-2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -55,9 +55,31 @@ Foam::Ostream& Foam::OTstream::write(const char c)
}
Foam::Ostream& Foam::OTstream::writeQuoted
(
const char* str,
std::streamsize len,
const bool quoted
)
{
if (quoted)
{
// tokenType::STRING
tokens().emplace_back() = string(str, len);
}
else if (len > 0)
{
// tokenType::WORD
tokens().emplace_back() = word(str, len, false); // No stripping
}
return *this;
}
Foam::Ostream& Foam::OTstream::write(const char* str)
{
const word nonWhiteChars(string::validate<word>(str));
word nonWhiteChars(string::validate<word>(str));
if (nonWhiteChars.size() == 1)
{
@ -67,7 +89,7 @@ Foam::Ostream& Foam::OTstream::write(const char* str)
else if (nonWhiteChars.size())
{
// As a word
write(nonWhiteChars);
tokens().emplace_back() = std::move(nonWhiteChars); // Move assign
}
return *this;
@ -76,34 +98,17 @@ Foam::Ostream& Foam::OTstream::write(const char* str)
Foam::Ostream& Foam::OTstream::write(const word& str)
{
tokens().push_back(token(str)); // tokenType::WORD
// tokenType::WORD
tokens().emplace_back() = str; // Copy assign
return *this;
}
Foam::Ostream& Foam::OTstream::write(const string& str)
Foam::Ostream& Foam::OTstream::write(const std::string& str)
{
tokens().push_back(token(str)); // tokenType::STRING
return *this;
}
Foam::Ostream& Foam::OTstream::writeQuoted
(
const std::string& str,
const bool quoted
)
{
if (quoted)
{
tokens().push_back(token(string(str))); // tokenType::STRING
}
else if (!str.empty())
{
tokens().push_back(token(word(str, false))); // tokenType::WORD
}
// tokenType::STRING
tokens().emplace_back() = Foam::string(str); // Move assign
return *this;
}

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2019-2022 OpenCFD Ltd.
Copyright (C) 2019-2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -115,122 +115,133 @@ public:
// Write
//- Inherit write methods from Ostream
using Ostream::writeQuoted;
//- Write token to stream or otherwise handle it.
// \return false if the token type was not handled by this method
virtual bool write(const token& tok);
virtual bool write(const token& tok) override;
//- Write single character. Whitespace is suppressed.
virtual Ostream& write(const char c);
virtual Ostream& write(const char c) override;
//- Write character/string content, with/without surrounding quotes
virtual Ostream& writeQuoted
(
const char* str,
std::streamsize len,
const bool quoted=true
) override;
//- Write the word-characters of a character string.
// Sends as a single char, or as word.
virtual Ostream& write(const char* str);
virtual Ostream& write(const char* str) override;
//- Write word
virtual Ostream& write(const word& str);
virtual Ostream& write(const word& str) override;
//- Write string
virtual Ostream& write(const string& str);
//- Write std::string surrounded by quotes.
// Optional write without quotes.
virtual Ostream& writeQuoted
(
const std::string& str,
const bool quoted=true
);
virtual Ostream& write(const std::string& str) override;
//- Write int32_t as a label
virtual Ostream& write(const int32_t val);
virtual Ostream& write(const int32_t val) override;
//- Write int64_t as a label
virtual Ostream& write(const int64_t val);
virtual Ostream& write(const int64_t val) override;
//- Write float
virtual Ostream& write(const float val);
virtual Ostream& write(const float val) override;
//- Write double
virtual Ostream& write(const double val);
virtual Ostream& write(const double val) override;
//- Write binary block with 8-byte alignment.
virtual Ostream& write(const char* data, std::streamsize count);
virtual Ostream& write
(
const char* data,
std::streamsize count
) override;
//- Low-level raw binary output.
virtual Ostream& writeRaw(const char* data, std::streamsize count);
virtual Ostream& writeRaw
(
const char* data,
std::streamsize count
) override;
//- Begin marker for low-level raw binary output.
// The count indicates the number of bytes for subsequent
// writeRaw calls.
virtual bool beginRawWrite(std::streamsize count);
virtual bool beginRawWrite(std::streamsize count) override;
//- End marker for low-level raw binary output.
virtual bool endRawWrite()
virtual bool endRawWrite() override
{
return true;
}
//- Add indentation characters
virtual void indent()
virtual void indent() override
{}
// Stream State Functions
//- Get flags of output stream
virtual ios_base::fmtflags flags() const
virtual ios_base::fmtflags flags() const override
{
return ios_base::fmtflags(0);
}
//- Set flags of stream - ignored
std::ios_base::fmtflags flags(const ios_base::fmtflags)
std::ios_base::fmtflags flags(const ios_base::fmtflags) override
{
return ios_base::fmtflags(0);
}
//- Flush stream
virtual void flush()
virtual void flush() override
{}
//- Add newline and flush stream
virtual void endl()
virtual void endl() override
{}
//- Get the current padding character
// \return previous padding character
virtual char fill() const
virtual char fill() const override
{
return 0;
}
//- Set padding character for formatted field up to field width
virtual char fill(const char)
virtual char fill(const char) override
{
return 0;
}
//- Get width of output field
virtual int width() const
virtual int width() const override
{
return 0;
}
//- Set width of output field
// \return previous width
virtual int width(const int)
virtual int width(const int) override
{
return 0;
}
//- Get precision of output field
virtual int precision() const
virtual int precision() const override
{
return 0;
}
//- Set precision of output field
// \return old precision
virtual int precision(const int)
virtual int precision(const int) override
{
return 0;
}
@ -253,7 +264,7 @@ public:
}
//- Print stream description to Ostream
void print(Ostream& os) const;
void print(Ostream& os) const override;
// Additional constructors and methods (as per v2012 and earlier)

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2017 OpenFOAM Foundation
Copyright (C) 2019-2022 OpenCFD Ltd.
Copyright (C) 2019-2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -71,98 +71,101 @@ public:
// Member Functions
// Read Functions
// Stream-state
//- Return next token from stream
virtual Istream& read(token&)
{
NotImplemented;
return *this;
}
//- Return flags of stream
virtual ios_base::fmtflags flags() const override
{
return ios_base::fmtflags(0);
}
//- Read a character
virtual Istream& read(char&)
{
NotImplemented;
return *this;
}
//- Set flags of stream
virtual ios_base::fmtflags flags(const ios_base::fmtflags) override
{
return ios_base::fmtflags(0);
}
//- Read a word
virtual Istream& read(word&)
{
NotImplemented;
return *this;
}
// Read a string (including enclosing double-quotes)
virtual Istream& read(string&)
{
NotImplemented;
return *this;
}
// Read Functions
//- Read a label
virtual Istream& read(label&)
{
NotImplemented;
return *this;
}
//- Return next token from stream
virtual Istream& read(token&) override
{
NotImplemented;
return *this;
}
//- Read a float
virtual Istream& read(float&)
{
NotImplemented;
return *this;
}
//- Read a character
virtual Istream& read(char&) override
{
NotImplemented;
return *this;
}
//- Read a double
virtual Istream& read(double&)
{
NotImplemented;
return *this;
}
//- Read a word
virtual Istream& read(word&) override
{
NotImplemented;
return *this;
}
//- Read binary block
virtual Istream& read(char*, std::streamsize)
{
NotImplemented;
return *this;
}
// Read a string (including enclosing double-quotes)
virtual Istream& read(string&) override
{
NotImplemented;
return *this;
}
//- Low-level raw binary read
virtual Istream& readRaw(char*, std::streamsize)
{
NotImplemented;
return *this;
}
//- Read a label
virtual Istream& read(label&) override
{
NotImplemented;
return *this;
}
//- Start of low-level raw binary read
virtual bool beginRawRead()
{
return false;
}
//- Read a float
virtual Istream& read(float&) override
{
NotImplemented;
return *this;
}
//- End of low-level raw binary read
virtual bool endRawRead()
{
return false;
}
//- Read a double
virtual Istream& read(double&) override
{
NotImplemented;
return *this;
}
//- Rewind the stream so that it may be read again
virtual void rewind()
{}
//- Read binary block
virtual Istream& read(char*, std::streamsize) override
{
NotImplemented;
return *this;
}
//- Return flags of stream
virtual ios_base::fmtflags flags() const
{
return ios_base::fmtflags(0);
}
//- Low-level raw binary read
virtual Istream& readRaw(char*, std::streamsize) override
{
NotImplemented;
return *this;
}
//- Set flags of stream
virtual ios_base::fmtflags flags(const ios_base::fmtflags)
{
return ios_base::fmtflags(0);
}
//- Start of low-level raw binary read
virtual bool beginRawRead() override
{
return false;
}
//- End of low-level raw binary read
virtual bool endRawRead() override
{
return false;
}
//- Rewind the stream so that it may be read again
virtual void rewind() override
{}
};

View File

@ -191,9 +191,9 @@ public:
//- Add (unquoted) string contents.
// Ensures that SHA1 of C-string or C++-string content are identical.
virtual Ostream& write(const string& str)
virtual Ostream& write(const std::string& str) override
{
return writeQuoted(str, false); // Unquoted!
return writeQuoted(str, false); // Unquoted!
}

View File

@ -296,13 +296,6 @@ public:
//- Span of the input characters (is modifiable!)
UList<char> list() const { return stream_.list(); }
//- Rewind the stream, clearing any old errors
virtual void rewind()
{
stream_.rewind();
syncState();
}
//- Reset content (copy)
void reset(const char* buffer, size_t nbytes)
{
@ -325,8 +318,15 @@ public:
syncState();
}
//- Rewind the stream, clearing any old errors
virtual void rewind() override
{
stream_.rewind();
syncState();
}
//- Print stream description to Ostream
virtual void print(Ostream& os) const;
virtual void print(Ostream& os) const override;
// Member Operators

View File

@ -319,14 +319,14 @@ public:
}
//- Rewind the stream, clearing any old errors
virtual void rewind()
virtual void rewind() override
{
stream_.rewind();
syncState();
}
//- Print stream description to Ostream
virtual void print(Ostream& os) const;
virtual void print(Ostream& os) const override;
// Member Operators

View File

@ -272,13 +272,6 @@ public:
//- Span of the current output characters (is modifiable!)
UList<char> list() const { return stream_.list(); }
//- Rewind the stream, clearing any old errors
virtual void rewind()
{
stream_.rewind();
syncState();
}
//- Transfer list contents to List buffer
void swap(List<char>& other)
{
@ -294,8 +287,15 @@ public:
syncState();
}
//- Rewind the stream, clearing any old errors
virtual void rewind()
{
stream_.rewind();
syncState();
}
//- Print stream description to Ostream
virtual void print(Ostream& os) const;
virtual void print(Ostream& os) const override;
// Houskeeping

View File

@ -255,7 +255,7 @@ public:
}
//- Print stream description to Ostream
virtual void print(Ostream& os) const;
virtual void print(Ostream& os) const override;
};

View File

@ -343,7 +343,7 @@ public:
}
//- Print stream description to Ostream
virtual void print(Ostream& os) const;
virtual void print(Ostream& os) const override;
};

View File

@ -74,7 +74,7 @@ Foam::Istream& Foam::operator>>(Istream& is, string& val)
Foam::Ostream& Foam::operator<<(Ostream& os, const string& val)
{
os.write(val);
os.write(static_cast<const std::string&>(val));
os.check(FUNCTION_NAME);
return os;
}
@ -82,7 +82,7 @@ Foam::Ostream& Foam::operator<<(Ostream& os, const string& val)
Foam::Ostream& Foam::operator<<(Ostream& os, const std::string& val)
{
os.write(string(val));
os.write(val);
os.check(FUNCTION_NAME);
return os;
}

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2015 OpenFOAM Foundation
Copyright (C) 2016-2022 OpenCFD Ltd.
Copyright (C) 2016-2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -147,13 +147,16 @@ float Foam::ensightFile::undefValue(float value) noexcept
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
Foam::Ostream& Foam::ensightFile::writeString(const char* str)
Foam::Ostream& Foam::ensightFile::writeString(const char* str, size_t len)
{
// Output 80 chars, but allocate for trailing nul character
// to avoid -Wstringop-truncation warnings/errors.
// Output 79 chars (ASCII) or 80 chars (BINARY)
char buf[80];
if (len > 80) len = 80;
char buf[80+1];
strncpy(buf, str, 80); // max 80 chars or padded with nul if smaller
// TBD: truncate at newline? (shouldn't really occur anyhow)
std::copy_n(str, len, buf);
std::fill_n(buf + len, (len - 80), '\0'); // Pad trailing with nul
if (format() == IOstreamOption::BINARY)
{
@ -161,7 +164,7 @@ Foam::Ostream& Foam::ensightFile::writeString(const char* str)
}
else
{
buf[79] = 0; // max 79 in ASCII, ensure it is indeed nul-terminated
buf[79] = 0; // Max 79 in ASCII
stdStream() << buf;
syncState();
}
@ -170,27 +173,33 @@ Foam::Ostream& Foam::ensightFile::writeString(const char* str)
}
Foam::Ostream& Foam::ensightFile::writeString(const char* str)
{
return writeString(str, strlen(str));
}
Foam::Ostream& Foam::ensightFile::writeString(const std::string& str)
{
return writeString(str.c_str());
return writeString(str.data(), str.size());
}
Foam::Ostream& Foam::ensightFile::write(const char* str)
{
return writeString(str);
return writeString(str, strlen(str));
}
Foam::Ostream& Foam::ensightFile::write(const word& str)
{
return writeString(str);
return writeString(str.data(), str.size());
}
Foam::Ostream& Foam::ensightFile::write(const string& str)
Foam::Ostream& Foam::ensightFile::write(const std::string& str)
{
return writeString(str);
return writeString(str.data(), str.size());
}

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2015 OpenFOAM Foundation
Copyright (C) 2016-2022 OpenCFD Ltd.
Copyright (C) 2016-2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -145,13 +145,12 @@ public:
// Output
//- Write element keyword with trailing newline,
//- optionally with undef and the value for undefined
virtual Ostream& writeKeyword(const keyType& key);
//- Write "C Binary" string for binary files (eg, geometry/measured)
Ostream& writeBinaryHeader();
//- Write character/string content as "%79s" or as binary (max 80 chars)
Ostream& writeString(const char* str, size_t len);
//- Write C-string as "%79s" or as binary (max 80 chars)
Ostream& writeString(const char* str);
@ -162,46 +161,50 @@ public:
Ostream& writeUndef();
//- Write element keyword with trailing newline,
//- optionally with undef and the value for undefined
virtual Ostream& writeKeyword(const keyType& key) override;
//- Writing token does not make sense
virtual bool write(const token&)
virtual bool write(const token&) override
{
NotImplemented;
return false;
}
//- Writing single character does not make sense
virtual Ostream& write(const char)
virtual Ostream& write(const char) override
{
NotImplemented;
return *this;
}
//- Binary write
virtual Ostream& write(const char* buf, std::streamsize count);
virtual Ostream& write(const char* buf, std::streamsize count) override;
//- Write C-string, uses writeString()
virtual Ostream& write(const char* str);
virtual Ostream& write(const char* str) override;
//- Write word, uses writeString()
virtual Ostream& write(const word& str);
virtual Ostream& write(const word& str) override;
//- Write string, uses writeString()
virtual Ostream& write(const string& str);
virtual Ostream& write(const std::string& str) override;
//- Write integer as "%10d" or as binary
virtual Ostream& write(const int32_t val);
virtual Ostream& write(const int32_t val) override;
//- Write integer as "%10d" or as binary
virtual Ostream& write(const int64_t val);
virtual Ostream& write(const int64_t val) override;
//- Write integer with specified width or as binary
Ostream& write(const label value, const label fieldWidth);
//- Write floating-point as "%12.5e" or as binary
virtual Ostream& write(const float val);
virtual Ostream& write(const float val) override;
//- Write floating-point as "%12.5e" or as binary (narrowed to float)
virtual Ostream& write(const double val);
virtual Ostream& write(const double val) override;
//- Add carriage return to ascii stream
void newline();

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2016-2022 OpenCFD Ltd.
Copyright (C) 2016-2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -96,22 +96,22 @@ public:
using Istream::read;
//- Binary read
virtual Istream& read(char* buf, std::streamsize count);
virtual Istream& read(char* buf, std::streamsize count) override;
//- Read string as "%80s" or as binary
virtual Istream& read(string& value);
virtual Istream& read(string& value) override;
//- Read integer as "%10d" or as binary
virtual Istream& read(label& value);
virtual Istream& read(label& value) override;
//- Read floating-point as "%12.5e" or as binary
virtual Istream& read(float& value);
virtual Istream& read(float& value) override;
//- Read floating-point as "%12.5e" or as a binary (narrowed) float
virtual Istream& read(double& value);
virtual Istream& read(double& value) override;
//- Read element keyword
virtual Istream& readKeyword(string& key);
//- Read element keyword. Currently the same as read(string)
Istream& readKeyword(string& key);
//- Read "C Binary" for binary files (eg, geometry/measured)
Istream& readBinaryHeader();

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2012-2016 OpenFOAM Foundation
Copyright (C) 2020-2022 OpenCFD Ltd.
Copyright (C) 2020-2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -40,7 +40,7 @@ namespace Foam
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
void Foam::OBJstream::writeAndCheck(const char c)
inline void Foam::OBJstream::vertex_state(const char c)
{
if (c == '\n')
{
@ -54,8 +54,6 @@ void Foam::OBJstream::writeAndCheck(const char c)
++nVertices_;
}
}
OFstream::write(c);
}
@ -77,16 +75,51 @@ Foam::OBJstream::OBJstream
Foam::Ostream& Foam::OBJstream::write(const char c)
{
writeAndCheck(c);
OFstream::write(c);
vertex_state(c);
return *this;
}
Foam::Ostream& Foam::OBJstream::writeQuoted
(
const char* str,
std::streamsize len,
const bool quoted
)
{
OFstream::writeQuoted(str, len, quoted);
// NOTE:
// Since vertex_state() handling only tracks newline followed by
// an initial 'v' (vertex) character, it probably should not be possible
// to triggered from within a quoted string.
if (str && len >= 0)
{
if (quoted) vertex_state(0); // Begin quote: reset state
const char* last = (str + len);
// std::for_each
for (const char* iter = str; iter != last; ++iter)
{
vertex_state(*iter);
}
if (quoted) vertex_state(0); // End quote
}
return *this;
}
Foam::Ostream& Foam::OBJstream::write(const char* str)
{
OFstream::write(str);
for (const char* iter = str; *iter; ++iter)
{
writeAndCheck(*iter);
vertex_state(*iter);
}
return *this;
}
@ -94,68 +127,54 @@ Foam::Ostream& Foam::OBJstream::write(const char* str)
Foam::Ostream& Foam::OBJstream::write(const word& str)
{
return writeQuoted(str, false);
return writeQuoted(str.data(), str.size(), false);
}
Foam::Ostream& Foam::OBJstream::write(const string& str)
Foam::Ostream& Foam::OBJstream::write(const std::string& str)
{
return writeQuoted(str, true);
return writeQuoted(str.data(), str.size(), true);
}
Foam::Ostream& Foam::OBJstream::writeQuoted
(
const std::string& str,
const bool quoted
)
Foam::Ostream& Foam::OBJstream::writeComment(const std::string& str)
{
if (!quoted)
if (!startOfLine_)
{
// Output unquoted, only advance line number on newline
for (auto iter = str.cbegin(); iter != str.cend(); ++iter)
{
writeAndCheck(*iter);
}
OFstream::write('\n');
startOfLine_ = true;
}
if (str.empty())
{
OFstream::write("#\n");
startOfLine_ = true;
return *this;
}
// Output with surrounding quotes and backslash escaping
OFstream::write(static_cast<char>(token::DQUOTE));
const char* iter = str.data();
const char* last = (str.data() + str.size());
unsigned backslash = 0;
for (auto iter = str.cbegin(); iter != str.cend(); ++iter)
// std::for_each
for (; iter != last; ++iter)
{
const char c = *iter;
if (c == '\\')
if (startOfLine_)
{
++backslash;
continue; // only output after escaped character is known
}
else if (c == token::NL)
{
++lineNumber_;
++backslash; // backslash escape for newline
}
else if (c == token::DQUOTE)
{
++backslash; // backslash escape for quote
OFstream::write("# ");
startOfLine_ = false;
}
// output all pending backslashes
while (backslash)
{
OFstream::write('\\');
--backslash;
}
writeAndCheck(c);
startOfLine_ = (c == '\n');
OFstream::write(c);
}
// silently drop any trailing backslashes
// they would otherwise appear like an escaped end-quote
OFstream::write(static_cast<char>(token::DQUOTE));
if (!startOfLine_)
{
OFstream::write('\n');
startOfLine_ = true;
}
return *this;
}

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2012-2015 OpenFOAM Foundation
Copyright (C) 2017-2022 OpenCFD Ltd.
Copyright (C) 2017-2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -70,7 +70,9 @@ class OBJstream
// Private Member Functions
void writeAndCheck(const char c);
//- State engine to count number of vertices.
//- Triggered on newline and 'v' (vertex).
inline void vertex_state(const char c);
public:
@ -105,104 +107,107 @@ public:
// Member Functions
// Access
//- Return the number of vertices written
label nVertices() const noexcept
{
return nVertices_;
}
label nVertices() const noexcept { return nVertices_; }
// Ostream implementation
// Write Functions
//- Inherit write from Ostream
using Ostream::write;
//- Inherit write methods from OFstream
using OFstream::write;
using OFstream::writeQuoted;
//- Write character
virtual Ostream& write(const char c);
//- Write character
virtual Ostream& write(const char c) override;
//- Write character string
virtual Ostream& write(const char* str);
//- Write character/string content, with/without surrounding quotes
virtual Ostream& writeQuoted
(
const char* str,
std::streamsize len,
const bool quoted=true
) override;
//- Write word
virtual Ostream& write(const word& str);
//- Write character string
virtual Ostream& write(const char* str) override;
//- Write string
virtual Ostream& write(const string& str);
//- Write word
virtual Ostream& write(const word& str) override;
//- Write std::string surrounded by quotes.
// Optional write without quotes.
virtual Ostream& writeQuoted
(
const std::string& str,
const bool quoted=true
);
//- Write string
virtual Ostream& write(const std::string& str) override;
// Direct write functionality
// Direct write functionality
//- Write point
Ostream& write(const point& p);
//- Write comment (with '# ' prefix)
Ostream& writeComment(const std::string& str);
//- Write point and vector normal ('vn')
Ostream& write(const point& p, const vector& n);
//- Write point (vertex)
Ostream& write(const point& p);
//- Write multiple points
Ostream& write(const UList<point>& points);
//- Write point and vector normal ('vn')
Ostream& write(const point& p, const vector& n);
//- Write edge as points with line
Ostream& write(const edge& e, const UList<point>& points);
//- Write multiple points
Ostream& write(const UList<point>& points);
//- Write line
Ostream& write(const linePointRef& ln);
//- Write edge as points with line
Ostream& write(const edge& e, const UList<point>& points);
//- Write line with points and vector normals ('vn')
Ostream& write
(
const linePointRef& ln,
const vector& n0,
const vector& n1
);
//- Write line
Ostream& write(const linePointRef& ln);
//- Write line joining two points
Ostream& writeLine(const point& p0, const point& p1);
//- Write line with points and vector normals ('vn')
Ostream& write
(
const linePointRef& ln,
const vector& n0,
const vector& n1
);
//- Write triangle as points with lines/filled-polygon
Ostream& write(const triPointRef& f, const bool lines = true);
//- Write line joining two points
Ostream& writeLine(const point& p0, const point& p1);
//- Write face loop points with lines/filled-polygon
Ostream& writeFace
(
const UList<point>& points,
const bool lines = true
);
//- Write triangle as points with lines/filled-polygon
Ostream& write(const triPointRef& f, const bool lines = true);
//- Write face as points with lines/filled-polygon
Ostream& write
(
const face& f,
const UList<point>& points,
const bool lines = true
);
//- Write face loop points with lines/filled-polygon
Ostream& writeFace
(
const UList<point>& points,
const bool lines = true
);
//- Write patch faces as points with lines/filled-polygon
Ostream& write
(
const UList<face>& faces,
const pointField& points,
const bool lines = true
);
//- Write face as points with lines/filled-polygon
Ostream& write
(
const face& f,
const UList<point>& points,
const bool lines = true
);
//- Write edges as points with lines.
// Optionally eliminate unused points.
Ostream& write
(
const UList<edge>& edges,
const UList<point>& points,
const bool compact = false
);
//- Write patch faces as points with lines/filled-polygon
Ostream& write
(
const UList<face>& faces,
const pointField& points,
const bool lines = true
);
//- Write tree-bounding box with lines/filled-polygons
Ostream& write(const treeBoundBox& bb, const bool lines = true);
//- Write edges as points with lines.
// Optionally eliminate unused points.
Ostream& write
(
const UList<edge>& edges,
const UList<point>& points,
const bool compact = false
);
//- Write tree-bounding box with lines/filled-polygons
Ostream& write(const treeBoundBox& bb, const bool lines = true);
};