# PhasicFlow: src/phasicFlow/streams/Stream/Ostream.cpp Source File
PhasicFlow is an open-source parallel DEM (discrete element method) package for simulating granular flow. It is developed in C++ and can be exectued on both GPU (like CUDA) and CPU.

Ostream.cpp
Go to the documentation of this file.
/*------------------------------- phasicFlow ---------------------------------
O C enter of
O O E ngineering and
O O M ultiscale modeling of
OOOOOOO F luid flow
------------------------------------------------------------------------------
Copyright (C): www.cemf.ir
email: hamid.r.norouzi AT gmail.com
------------------------------------------------------------------------------
Licence:
This file is part of phasicFlow code. It is a free software for simulating
granular and multiphase flows. You can redistribute it and/or modify it under
the terms of GNU General Public License v3 or any other later versions.
phasicFlow is distributed to help others in their research in the field of
granular and multiphase flows, but WITHOUT ANY WARRANTY; without even the
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
-----------------------------------------------------------------------------*/
#include "error.hpp"
#include "token.hpp"
#include "Ostream.hpp"
pFlow::Ostream::Ostream
(
std::ostream& os,
const word& streamName,
writeFormat wF
)
:
iOstream(wF),
name_(streamName),
os_(os)
{
if (os_.good())
{
setOpened();
setGood();
os_.precision(precision_);
}
else
{
setState(os_.rdstate());
}
}
bool pFlow::Ostream::write(const token& tok)
{
// Direct token handling only for some types
switch (tok.type())
{
case token::tokenType::FLAG :
{
// silently consume the flag
return true;
}
case token::tokenType::VARIABLE :
{
writeQuoted(tok.wordToken(), false);
return true;
}
default:
break;
}
return false;
}
pFlow::iOstream& pFlow::Ostream::write(const char c)
{
os_ << c;
if (c == token::NL)
{
++lineNumber_;
}
setState(os_.rdstate());
return *this;
}
pFlow::iOstream& pFlow::Ostream::write(const char* str)
{
lineNumber_ += countChar(str, token::NL);
os_ << str;
setState(os_.rdstate());
return *this;
}
pFlow::iOstream& pFlow::Ostream::write(const word& str)
{
os_ << str;
setState(os_.rdstate());
return *this;
}
pFlow::iOstream& pFlow::Ostream::writeQuoted
(
const word& str,
const bool quoted
)
{
if (!quoted)
{
// Output unquoted, only advance line number on newline
lineNumber_ += countChar(str, token::NL);
os_ << str;
setState(os_.rdstate());
return *this;
}
// Output with surrounding quotes and backslash escaping
os_ << token::BEGIN_STRING;
unsigned backslash = 0;
for (auto iter = str.cbegin(); iter != str.cend(); ++iter)
{
const char c = *iter;
if (c == '\\')
{
++backslash;
continue; // only output after escaped character is known
}
else if (c == token::NL)
{
++lineNumber_;
++backslash; // backslash escape for newline
}
else if (c == token::END_STRING)
{
++backslash; // backslash escape for quote
}
// output all pending backslashes
while (backslash)
{
os_ << '\\';
--backslash;
}
os_ << c;
}
// silently drop any trailing backslashes
// they would otherwise appear like an escaped end-quote
os_ << token::END_STRING;
setState(os_.rdstate());
return *this;
}
pFlow::iOstream& pFlow::Ostream::write(const int64 val)
{
os_ << val;
setState(os_.rdstate());
return *this;
}
pFlow::iOstream& pFlow::Ostream::write(const int32 val)
{
os_ << val;
setState(os_.rdstate());
return *this;
}
pFlow::iOstream& pFlow::Ostream::write(const int8 val)
{
os_ << val;
setState(os_.rdstate());
return *this;
}
pFlow::iOstream& pFlow::Ostream::write(const uint64 val)
{
os_ << val;
setState(os_.rdstate());
return *this;
}
pFlow::iOstream& pFlow::Ostream::write(const uint32 val)
{
os_ << val;
setState(os_.rdstate());
return *this;
}
pFlow::iOstream& pFlow::Ostream::write(const uint8 val)
{
os_ << val;
setState(os_.rdstate());
return *this;
}
pFlow::iOstream& pFlow::Ostream::write(const float val)
{
os_ << val;
setState(os_.rdstate());
return *this;
}
pFlow::iOstream& pFlow::Ostream::write(const double val)
{
os_ << val;
setState(os_.rdstate());
return *this;
}
pFlow::iOstream& pFlow::Ostream::write(const size_t val)
{
os_ << val;
setState(os_.rdstate());
return *this;
}
pFlow::iOstream& pFlow::Ostream::write
(
const char* binaryData,
std::streamsize count
)
{
if ( !isBinary() )
{
fatalErrorInFunction<<"stream format is not binray. Stream name is "<<
name()<<'\n';
fatalExit;
}
os_ << token::BEGIN_LIST;
os_.write(binaryData, count);
os_ << token::END_LIST;
return *this;
}
pFlow::iOstream& pFlow::Ostream::writeBinaryBlockFlag()
{
write( static_cast<char>(255));
write( static_cast<char>(255));
write( static_cast<char>(255));
write( static_cast<char>(0));
return *this;
}
void pFlow::Ostream::seek(size_t pos)
{
os_.seekp(pos, std::ios_base::cur);
}
void pFlow::Ostream::indent()
{
for (unsigned short i = 0; i < indentLevel_*indentSize_; ++i)
{
os_ << ' ';
}
}
void pFlow::Ostream::startOfBinaryStreaming()
{
this->endl();
this->flush();
}
void pFlow::Ostream::endOfBinaryStreaming()
{
os_.seekp(0, std::ios_base::end);
this->endl();
this->flush();
}
void pFlow::Ostream::flush()
{
os_.flush();
}
void pFlow::Ostream::endl()
{
write('\n');
os_.flush();
}
std::ios_base::fmtflags pFlow::Ostream::flags() const
{
return os_.flags();
}
std::ios_base::fmtflags pFlow::Ostream::flags(const ios_base::fmtflags f)
{
return os_.flags(f);
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
char pFlow::Ostream::fill() const
{
return os_.fill();
}
char pFlow::Ostream::fill(const char fillch)
{
return os_.fill(fillch);
}
int pFlow::Ostream::width() const
{
return os_.width();
}
int pFlow::Ostream::width(const int w)
{
return os_.width(w);
}
int pFlow::Ostream::precision() const
{
return os_.precision();
}
int pFlow::Ostream::precision(const int p)
{
return os_.precision(p);
}
// ************************************************************************* //
pFlow::Ostream::writeQuoted
iOstream & writeQuoted(const word &str, const bool quoted=true) override
Write std::string surrounded by quotes.
Definition: Ostream.cpp:108
Ostream.hpp
pFlow::Ostream::flags
virtual ios_base::fmtflags flags() const
Return flags of output stream.
Definition: Ostream.cpp:298
pFlow::Ostream::flush
void flush() override
Flush stream.
Definition: Ostream.cpp:287
pFlow::token::type
tokenType type() const
Return the token type.
Definition: tokenI.hpp:302
fatalExit
#define fatalExit
Fatal exit.
Definition: error.hpp:98
pFlow::token
Token class based on OpenFOAM stream, with some modifications/simplifications to be tailored to our n...
Definition: token.hpp:44
pFlow::uint32
unsigned int uint32
Definition: builtinTypes.hpp:56
token.hpp
pFlow::word
std::string word
Definition: builtinTypes.hpp:64
pFlow::Ostream::indent
void indent() override
Add indentation characters.
Definition: Ostream.cpp:266
pFlow::token::NL
@ NL
Tab [isspace].
Definition: token.hpp:88
pFlow::Ostream::width
int width() const override
Get width of output field.
Definition: Ostream.cpp:324
pFlow::int64
long long int int64
Definition: builtinTypes.hpp:52
pFlow::Ostream::endOfBinaryStreaming
void endOfBinaryStreaming() override
Reach end of file add a new line and flush stream.
Definition: Ostream.cpp:280
pFlow::Ostream::fill
char fill() const override
Get the current padding character.
Definition: Ostream.cpp:312
pFlow::flush
iOstream & flush(iOstream &os)
Flush stream.
Definition: iOstream.hpp:333
pFlow::endl
iOstream & endl(iOstream &os)
Add newline and flush stream.
Definition: iOstream.hpp:341
pFlow::Ostream::precision
int precision() const override
Get precision of output field.
Definition: Ostream.cpp:336
pFlow::Ostream::endl
void endl() override
Add newline and flush stream.
Definition: Ostream.cpp:292
pFlow::Ostream::startOfBinaryStreaming
void startOfBinaryStreaming() override
Add a new line and flush stream.
Definition: Ostream.cpp:274
fatalErrorInFunction
#define fatalErrorInFunction
Report a fatal error and function name and exit the application.
Definition: error.hpp:77
pFlow::int32
int int32
Definition: builtinTypes.hpp:50
pFlow::Ostream::writeBinaryBlockFlag
iOstream & writeBinaryBlockFlag() override
Write the flag to indicate the start of a binary block.
Definition: Ostream.cpp:252
pFlow::token::BEGIN_STRING
@ BEGIN_STRING
Divide [isseparator].
Definition: token.hpp:106
pFlow::token::END_LIST
@ END_LIST
Begin list [isseparator].
Definition: token.hpp:92
pFlow::Ostream::seek
void seek(size_t pos) override
Definition: Ostream.cpp:261
pFlow::count
auto count(const Vector< T, Allocator > &vec, const T &val)
Definition: VectorAlgorithm.hpp:26
pFlow::Ostream::Ostream
Ostream(std::ostream &os, const word &streamName, writeFormat wf=ASCII)
Construct from components.
Definition: Ostream.cpp:27
pFlow::token::BEGIN_LIST
@ BEGIN_LIST
End entry [isseparator].
Definition: token.hpp:91
pFlow::uint64
unsigned long long int uint64
Definition: builtinTypes.hpp:58
pFlow::int8
signed char int8
Definition: builtinTypes.hpp:48
pFlow::IOstream::writeFormat
writeFormat
Definition: IOstream.hpp:59
pFlow::token::END_STRING
@ END_STRING
Begin string with double quote.
Definition: token.hpp:107
pFlow::Ostream::write
bool write(const token &tok) override
Write token to stream or otherwise handle it.
Definition: Ostream.cpp:51
pFlow::uint8
unsigned char uint8
Definition: builtinTypes.hpp:54
pFlow::iOstream
Interface class for any output stream.
Definition: iOstream.hpp:59
pFlow::countChar
int32 countChar(const word &s, const char c)
Count numer of chars c in a word.
Definition: bTypesFunctions.cpp:28
pFlow::token::wordToken
const word & wordToken() const
Return const reference to the word contents.
Definition: tokenI.hpp:618
error.hpp