diff --git a/src/OpenFOAM/db/IOstreams/Pstreams/UIPstream.C b/src/OpenFOAM/db/IOstreams/Pstreams/UIPstream.C index 5707d2d5c96d8d87bf14f161984d08735fa38714..333aeca259c72b0a1b8edbc98155725df7aab477 100644 --- a/src/OpenFOAM/db/IOstreams/Pstreams/UIPstream.C +++ b/src/OpenFOAM/db/IOstreams/Pstreams/UIPstream.C @@ -56,8 +56,8 @@ inline void Foam::UIPstream::readFromBuffer(T& t) inline void Foam::UIPstream::readFromBuffer ( void* data, - size_t count, - size_t align + const size_t count, + const size_t align ) { if (align > 1) @@ -76,6 +76,22 @@ inline void Foam::UIPstream::readFromBuffer } +inline Foam::Istream& Foam::UIPstream::readStringFromBuffer(std::string& str) +{ + size_t len; + readFromBuffer(len); + // Uses the underlying std::string::operator=() + // - no stripInvalid invoked (the sending side should have done that) + // - relies on trailing '\0' char (so cannot send anything with an embedded + // nul char) + str = &externalBuf_[externalBufPosition_]; + externalBufPosition_ += len + 1; + checkEof(); + + return *this; +} + + // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * // Foam::UIPstream::~UIPstream() @@ -107,7 +123,7 @@ Foam::Istream& Foam::UIPstream::read(token& t) char c; - // return on error + // Return on error if (!read(c)) { t.setBad(); @@ -141,7 +157,7 @@ Foam::Istream& Foam::UIPstream::read(token& t) } // Word - case token::WORD : + case token::tokenType::WORD : { word* pval = new word; if (read(*pval)) @@ -165,30 +181,26 @@ Foam::Istream& Foam::UIPstream::read(token& t) } // String - case token::VERBATIMSTRING : + case token::tokenType::VERBATIMSTRING : { // Recurse to read actual string read(t); - t.type() = token::VERBATIMSTRING; + t.type() = token::tokenType::VERBATIMSTRING; return *this; } - case token::VARIABLE : + case token::tokenType::VARIABLE : { // Recurse to read actual string read(t); - t.type() = token::VARIABLE; + t.type() = token::tokenType::VARIABLE; return *this; } - case token::STRING : + case token::tokenType::STRING : { string* pval = new string; if (read(*pval)) { t = pval; - if (c == token::VERBATIMSTRING) - { - t.type() = token::VERBATIMSTRING; - } } else { @@ -199,7 +211,7 @@ Foam::Istream& Foam::UIPstream::read(token& t) } // Label - case token::LABEL : + case token::tokenType::LABEL : { label val; if (read(val)) @@ -214,7 +226,7 @@ Foam::Istream& Foam::UIPstream::read(token& t) } // floatScalar - case token::FLOAT_SCALAR : + case token::tokenType::FLOAT_SCALAR : { floatScalar val; if (read(val)) @@ -229,7 +241,7 @@ Foam::Istream& Foam::UIPstream::read(token& t) } // doubleScalar - case token::DOUBLE_SCALAR : + case token::tokenType::DOUBLE_SCALAR : { doubleScalar val; if (read(val)) @@ -272,23 +284,13 @@ Foam::Istream& Foam::UIPstream::read(char& c) Foam::Istream& Foam::UIPstream::read(word& str) { - size_t len; - readFromBuffer(len); - str = &externalBuf_[externalBufPosition_]; - externalBufPosition_ += len + 1; - checkEof(); - return *this; + return readStringFromBuffer(str); } Foam::Istream& Foam::UIPstream::read(string& str) { - size_t len; - readFromBuffer(len); - str = &externalBuf_[externalBufPosition_]; - externalBufPosition_ += len + 1; - checkEof(); - return *this; + return readStringFromBuffer(str); } diff --git a/src/OpenFOAM/db/IOstreams/Pstreams/UIPstream.H b/src/OpenFOAM/db/IOstreams/Pstreams/UIPstream.H index b4aefe4446b7978ffa177eedcbea18d38fd0a960..4913693741f8b24e50f858947f6ec90b5943a3bd 100644 --- a/src/OpenFOAM/db/IOstreams/Pstreams/UIPstream.H +++ b/src/OpenFOAM/db/IOstreams/Pstreams/UIPstream.H @@ -80,10 +80,19 @@ class UIPstream //- Read a T from the transfer buffer template<class T> - inline void readFromBuffer(T&); + inline void readFromBuffer(T& t); - //- Read data from the transfer buffer - inline void readFromBuffer(void* data, size_t count, size_t align); + //- Read count bytes of data from the transfer buffer + // using align byte alignment + inline void readFromBuffer + ( + void* data, + const size_t count, + const size_t align + ); + + //- Read string length and its content. + inline Istream& readStringFromBuffer(std::string& str); public: @@ -139,28 +148,28 @@ public: ); //- Return next token from stream - Istream& read(token&); + Istream& read(token& t); //- Read a character - Istream& read(char&); + Istream& read(char& c); //- Read a word - Istream& read(word&); + Istream& read(word& str); - // Read a string (including enclosing double-quotes) - Istream& read(string&); + // Read a string + Istream& read(string& str); //- Read a label - Istream& read(label&); + Istream& read(label& val); //- Read a floatScalar - Istream& read(floatScalar&); + Istream& read(floatScalar& val); //- Read a doubleScalar - Istream& read(doubleScalar&); + Istream& read(doubleScalar& val); - //- Read binary block - Istream& read(char*, std::streamsize); + //- Read binary block with 8-byte alignment. + Istream& read(char* data, const std::streamsize count); //- Rewind and return the stream so that it may be read again Istream& rewind(); diff --git a/src/OpenFOAM/db/IOstreams/Pstreams/UOPstream.C b/src/OpenFOAM/db/IOstreams/Pstreams/UOPstream.C index cf53be0f848d3672486ae47f8f73effae8c57de0..6667389b742231d9e691716d6dc487a3503db62a 100644 --- a/src/OpenFOAM/db/IOstreams/Pstreams/UOPstream.C +++ b/src/OpenFOAM/db/IOstreams/Pstreams/UOPstream.C @@ -51,8 +51,8 @@ inline void Foam::UOPstream::writeToBuffer(const char& c) inline void Foam::UOPstream::writeToBuffer ( const void* data, - size_t count, - size_t align + const size_t count, + const size_t align ) { if (!sendBuf_.capacity()) @@ -77,6 +77,13 @@ inline void Foam::UOPstream::writeToBuffer } +inline void Foam::UOPstream::writeStringToBuffer(const std::string& str) +{ + const size_t len = str.size(); + writeToBuffer(len); + writeToBuffer(str.c_str(), len + 1, 1); +} + // * * * * * * * * * * * * * * * * Constructor * * * * * * * * * * * * * * * // @@ -153,14 +160,14 @@ Foam::UOPstream::~UOPstream() Foam::Ostream& Foam::UOPstream::write(const token& t) { // Raw token output only supported for verbatim strings for now - if (t.type() == token::VERBATIMSTRING) + if (t.type() == token::tokenType::VERBATIMSTRING) { - write(char(token::VERBATIMSTRING)); + writeToBuffer(char(token::tokenType::VERBATIMSTRING)); write(t.stringToken()); } - else if (t.type() == token::VARIABLE) + else if (t.type() == token::tokenType::VARIABLE) { - write(char(token::VARIABLE)); + writeToBuffer(char(token::tokenType::VARIABLE)); write(t.stringToken()); } else @@ -204,11 +211,8 @@ Foam::Ostream& Foam::UOPstream::write(const char* str) Foam::Ostream& Foam::UOPstream::write(const word& str) { - write(char(token::WORD)); - - size_t len = str.size(); - writeToBuffer(len); - writeToBuffer(str.c_str(), len + 1, 1); + writeToBuffer(char(token::tokenType::WORD)); + writeStringToBuffer(str); return *this; } @@ -216,11 +220,8 @@ Foam::Ostream& Foam::UOPstream::write(const word& str) Foam::Ostream& Foam::UOPstream::write(const string& str) { - write(char(token::STRING)); - - size_t len = str.size(); - writeToBuffer(len); - writeToBuffer(str.c_str(), len + 1, 1); + writeToBuffer(char(token::tokenType::STRING)); + writeStringToBuffer(str); return *this; } @@ -234,16 +235,13 @@ Foam::Ostream& Foam::UOPstream::writeQuoted { if (quoted) { - write(char(token::STRING)); + writeToBuffer(char(token::tokenType::STRING)); } else { - write(char(token::WORD)); + writeToBuffer(char(token::tokenType::WORD)); } - - size_t len = str.size(); - writeToBuffer(len); - writeToBuffer(str.c_str(), len + 1, 1); + writeStringToBuffer(str); return *this; } @@ -251,7 +249,7 @@ Foam::Ostream& Foam::UOPstream::writeQuoted Foam::Ostream& Foam::UOPstream::write(const int32_t val) { - write(char(token::LABEL)); + writeToBuffer(char(token::tokenType::LABEL)); writeToBuffer(val); return *this; } @@ -259,7 +257,7 @@ Foam::Ostream& Foam::UOPstream::write(const int32_t val) Foam::Ostream& Foam::UOPstream::write(const int64_t val) { - write(char(token::LABEL)); + writeToBuffer(char(token::tokenType::LABEL)); writeToBuffer(val); return *this; } @@ -267,7 +265,7 @@ Foam::Ostream& Foam::UOPstream::write(const int64_t val) Foam::Ostream& Foam::UOPstream::write(const floatScalar val) { - write(char(token::FLOAT_SCALAR)); + writeToBuffer(char(token::tokenType::FLOAT_SCALAR)); writeToBuffer(val); return *this; } @@ -275,13 +273,17 @@ Foam::Ostream& Foam::UOPstream::write(const floatScalar val) Foam::Ostream& Foam::UOPstream::write(const doubleScalar val) { - write(char(token::DOUBLE_SCALAR)); + writeToBuffer(char(token::tokenType::DOUBLE_SCALAR)); writeToBuffer(val); return *this; } -Foam::Ostream& Foam::UOPstream::write(const char* data, std::streamsize count) +Foam::Ostream& Foam::UOPstream::write +( + const char* data, + const std::streamsize count +) { if (format() != BINARY) { diff --git a/src/OpenFOAM/db/IOstreams/Pstreams/UOPstream.H b/src/OpenFOAM/db/IOstreams/Pstreams/UOPstream.H index 349ac52104aedfaa0775bae53aa7f37463908a1b..6ee40e03781dc5042ca3b36d08bcaa639d425671 100644 --- a/src/OpenFOAM/db/IOstreams/Pstreams/UOPstream.H +++ b/src/OpenFOAM/db/IOstreams/Pstreams/UOPstream.H @@ -74,13 +74,23 @@ class UOPstream //- Write a T to the transfer buffer template<class T> - inline void writeToBuffer(const T&); + inline void writeToBuffer(const T& t); //- Write a char to the transfer buffer - inline void writeToBuffer(const char&); + inline void writeToBuffer(const char& c); - //- Write data to the transfer buffer - inline void writeToBuffer(const void* data, size_t count, size_t align); + //- Write count bytes of data to the transfer buffer + // using align byte alignment + inline void writeToBuffer + ( + const void* data, + const size_t count, + const size_t align + ); + + //- Write string length and content. + // The content includes the trailing nul char. + inline void writeStringToBuffer(const std::string& str); public: @@ -102,7 +112,7 @@ public: ); //- Construct given buffers - UOPstream(const int toProcNo, PstreamBuffers&); + UOPstream(const int toProcNo, PstreamBuffers& buffers); //- Destructor @@ -134,42 +144,43 @@ public: ); //- Write next token to stream - Ostream& write(const token&); + Ostream& write(const token& t); - //- Write character - Ostream& write(const char); + //- Write single character. Whitespace is suppressed. + Ostream& write(const char c); - //- Write character string - Ostream& write(const char*); + //- Write the word-characters of a character string. + // Sends as a single char, or as word. + Ostream& write(const char* str); //- Write word - Ostream& write(const word&); + Ostream& write(const word& str); //- Write string - Ostream& write(const string&); + Ostream& write(const string& str); //- Write std::string surrounded by quotes. // Optional write without quotes. Ostream& writeQuoted ( - const std::string&, + const std::string& str, const bool quoted=true ); - //- Write int32_t - virtual Ostream& write(const int32_t); + //- Write int32_t as a label + virtual Ostream& write(const int32_t val); - //- Write int64_t - Ostream& write(const int64_t); + //- Write int64_t as a label + Ostream& write(const int64_t val); //- Write floatScalar - Ostream& write(const floatScalar); + Ostream& write(const floatScalar val); //- Write doubleScalar - Ostream& write(const doubleScalar); + Ostream& write(const doubleScalar val); - //- Write binary block - Ostream& write(const char*, std::streamsize); + //- Write binary block with 8-byte alignment. + Ostream& write(const char* data, const std::streamsize count); //- Add indentation characters void indent() @@ -223,7 +234,7 @@ public: // Print //- Print description of IOstream to Ostream - void print(Ostream&) const; + void print(Ostream& os) const; }; diff --git a/src/OpenFOAM/db/IOstreams/Pstreams/UPstream.H b/src/OpenFOAM/db/IOstreams/Pstreams/UPstream.H index 7e71fc543c211a7d1db8a2305023233b33cf9eb5..0b936c9f7eca40bf373c6d109bfd135ee8e7a2ff 100644 --- a/src/OpenFOAM/db/IOstreams/Pstreams/UPstream.H +++ b/src/OpenFOAM/db/IOstreams/Pstreams/UPstream.H @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011-2017 OpenFOAM Foundation - \\/ M anipulation | Copyright 2015-2016 OpenCFD Ltd. + \\/ M anipulation | Copyright (C) 2015-2016 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM.