Commit 66c836e1 authored by Mark Olesen's avatar Mark Olesen
Browse files

Merge branch 'bug-pstream-tokensending' into 'develop'

Bug pstream token sending

See merge request !140
parents a09125c3 37e86352
......@@ -89,11 +89,7 @@ int main(int argc, char *argv[])
break;
}
if
(
fieldName.type() != token::WORD
&& fieldName.wordToken() != "CELL"
)
if (!fieldName.isWord() || fieldName.wordToken() != "CELL")
{
FatalErrorInFunction
<< "Expected first CELL, found "
......@@ -103,7 +99,7 @@ int main(int argc, char *argv[])
label nCols = 0;
smapFile >> fieldName;
while (fieldName.type() == token::WORD)
while (fieldName.isWord())
{
starFieldNames[nCols++] = fieldName.wordToken();
smapFile >> fieldName;
......
......@@ -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);
}
......
......@@ -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();
......
......@@ -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)
{
......
......@@ -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;
};
......
......@@ -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.
......
......@@ -224,7 +224,7 @@ Foam::Istream& Foam::ISstream::read(token& t)
else
{
t = sPtr;
t.type() = token::VERBATIMSTRING;
t.type() = token::tokenType::VERBATIMSTRING;
}
return *this;
......@@ -266,7 +266,7 @@ Foam::Istream& Foam::ISstream::read(token& t)
else
{
t = sPtr;
t.type() = token::VARIABLE;
t.type() = token::tokenType::VARIABLE;
}
return *this;
}
......
......@@ -31,7 +31,7 @@ License
Foam::Ostream& Foam::OSstream::write(const token& t)
{
if (t.type() == token::VERBATIMSTRING)
if (t.type() == token::tokenType::VERBATIMSTRING)
{
write(char(token::HASH));
write(char(token::BEGIN_BLOCK));
......@@ -39,9 +39,9 @@ Foam::Ostream& Foam::OSstream::write(const token& t)
write(char(token::HASH));
write(char(token::END_BLOCK));
}
else if (t.type() == token::VARIABLE)
else if (t.type() == token::tokenType::VARIABLE)
{
writeQuoted( t.stringToken(), false);
writeQuoted(t.stringToken(), false);
}
return *this;
}
......
......@@ -67,7 +67,7 @@ void Foam::prefixOSstream::print(Ostream& os) const
Foam::Ostream& Foam::prefixOSstream::write(const token& t)
{
if (t.type() == token::VERBATIMSTRING)
if (t.type() == token::tokenType::VERBATIMSTRING)
{
write(char(token::HASH));
write(char(token::BEGIN_BLOCK));
......@@ -75,7 +75,7 @@ Foam::Ostream& Foam::prefixOSstream::write(const token& t)
write(char(token::HASH));
write(char(token::END_BLOCK));
}
else if (t.type() == token::VARIABLE)
else if (t.type() == token::tokenType::VARIABLE)
{
writeQuoted(t.stringToken(), false);
}
......
......@@ -92,7 +92,7 @@ bool Foam::token::compound::isCompound(const word& name)
Foam::token::compound& Foam::token::transferCompoundToken(const Istream& is)
{
if (type_ == COMPOUND)
if (type_ == tokenType::COMPOUND)
{
if (compoundTokenPtr_->empty())
{
......
......@@ -197,7 +197,7 @@ public:
// Write
virtual void write(Ostream&) const = 0;
virtual void write(Ostream& os) const = 0;
// IOstream Operators
......@@ -284,28 +284,47 @@ public:
inline token();
//- Construct as copy
inline token(const token&);
inline token(const token& t);
//- Construct punctuation character token
inline token(punctuationToken, label lineNumber=0);
inline explicit token(punctuationToken p);
//- Construct word token
inline token(const word&, label lineNumber=0);
inline explicit token(const word& w);
//- Construct string token
inline token(const string&, label lineNumber=0);
inline explicit token(const string& str);
//- Construct label token
inline token(const label, label lineNumber=0);
inline explicit token(const label val);
//- Construct floatScalar token
inline token(const floatScalar, label lineNumber=0);
inline explicit token(const floatScalar val);
//- Construct doubleScalar token
inline token(const doubleScalar, label lineNumber=0);
inline explicit token(const doubleScalar val);
//- Construct punctuation character token
inline token(punctuationToken p, const label lineNumber);
//- Construct word token
inline token(const word& w, const label lineNumber);
//- Construct string token
inline token(const string& str, const label lineNumber);
//- Construct label token
inline token(const label val, const label lineNumber);
//- Construct floatScalar token
inline token(const floatScalar val, const label lineNumber);
//- Construct doubleScalar token
inline token(const doubleScalar val, const label lineNumber);
//- Construct from Istream
token(Istream&);
token(Istream& is);
//- Destructor
......@@ -377,43 +396,43 @@ public:
// Assignment
inline void operator=(const token&);