# PhasicFlow: src/phasicFlow/streams/Stream/Ostream.cpp Source File

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.
-----------------------------------------------------------------------------*/
// based on OpenFOAM stream, with some modifications/simplifications
// to be tailored to our needs
#include "error.hpp"
#include "token.hpp"
#include "Ostream.hpp"
pFlow::Ostream::Ostream
(
std::ostream& os,
const word& streamName
)
:
iOstream(),
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 int16 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 label 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 uint16 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;
}
void pFlow::Ostream::indent()
{
for (unsigned short i = 0; i < indentLevel_*indentSize_; ++i)
{
os_ << ' ';
}
}
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
virtual iOstream & writeQuoted(const word &str, const bool quoted=true) override
Definition: Ostream.cpp:109
pFlow::Ostream::flush
virtual void flush()
Definition: Ostream.cpp:244
Ostream.hpp
pFlow::Ostream::flags
virtual ios_base::fmtflags flags() const
Definition: Ostream.cpp:257
pFlow::token::type
tokenType type() const
Definition: tokenI.hpp:284
pFlow::Ostream::endl
virtual void endl()
Definition: Ostream.cpp:250
pFlow::token
Definition: token.hpp:42
pFlow::uint32
unsigned int uint32
Definition: builtinTypes.hpp:59
token.hpp
pFlow::word
std::string word
Definition: builtinTypes.hpp:63
pFlow::token::NL
@ NL
Newline [isspace].
Definition: token.hpp:86
pFlow::int64
long long int int64
Definition: builtinTypes.hpp:55
pFlow::Ostream::precision
virtual int precision() const
Definition: Ostream.cpp:295
pFlow::uint16
unsigned short int uint16
Definition: builtinTypes.hpp:57
pFlow::Ostream::Ostream
Ostream(std::ostream &os, const word &streamName)
Definition: Ostream.cpp:29
pFlow::int32
int int32
Definition: builtinTypes.hpp:53
pFlow::token::BEGIN_STRING
@ BEGIN_STRING
Begin string with double quote.
Definition: token.hpp:104
pFlow::Ostream::width
virtual int width() const
Definition: Ostream.cpp:283
pFlow::Ostream::fill
virtual char fill() const
Definition: Ostream.cpp:271
pFlow::Ostream::indent
virtual void indent()
Definition: Ostream.cpp:235
pFlow::label
std::size_t label
Definition: builtinTypes.hpp:61
pFlow::token::END_STRING
@ END_STRING
End string with double quote.
Definition: token.hpp:105
pFlow::Ostream::write
virtual bool write(const token &tok) override
Definition: Ostream.cpp:52
pFlow::iOstream
Definition: iOstream.hpp:53
pFlow::countChar
int32 countChar(const word &s, const char c)
Definition: bTypesFunctions.cpp:28
pFlow::token::wordToken
const word & wordToken() const
Definition: tokenI.hpp:600
error.hpp