From c46aaab2db4d3256df4be3f28e3a70e2bd40ff8c Mon Sep 17 00:00:00 2001 From: Hamidreza Norouzi Date: Tue, 11 Apr 2023 22:19:36 -0700 Subject: [PATCH] data read and write in binary --- src/phasicFlow/containers/Field/Field.cpp | 52 ++- src/phasicFlow/containers/Field/Field.hpp | 10 +- .../repository/IOobject/IOfileHeader.cpp | 48 ++- .../repository/IOobject/IOfileHeader.hpp | 7 + .../repository/IOobject/IOobject.cpp | 23 +- src/phasicFlow/repository/Time/Time.cpp | 28 +- src/phasicFlow/repository/Time/Time.hpp | 12 + .../repository/repository/repository.hpp | 8 + src/phasicFlow/streams/Fstream/fileStream.cpp | 25 +- src/phasicFlow/streams/Fstream/fileStream.hpp | 4 +- src/phasicFlow/streams/Fstream/iFstream.cpp | 14 +- src/phasicFlow/streams/Fstream/iFstream.hpp | 2 +- src/phasicFlow/streams/Fstream/oFstream.cpp | 16 +- src/phasicFlow/streams/Fstream/oFstream.hpp | 2 +- src/phasicFlow/streams/Stream/Istream.cpp | 25 +- src/phasicFlow/streams/Stream/Istream.hpp | 5 +- src/phasicFlow/streams/Stream/Ostream.cpp | 28 +- src/phasicFlow/streams/Stream/Ostream.hpp | 53 +-- src/phasicFlow/streams/TStream/iTstream.cpp | 10 +- src/phasicFlow/streams/TStream/iTstream.hpp | 2 + src/phasicFlow/streams/TStream/oTstream.cpp | 9 + src/phasicFlow/streams/TStream/oTstream.hpp | 5 + src/phasicFlow/streams/iStream/IOstream.hpp | 360 ++++++++++-------- src/phasicFlow/streams/iStream/iIstream.cpp | 14 +- src/phasicFlow/streams/iStream/iIstream.hpp | 15 + src/phasicFlow/streams/iStream/iOstream.hpp | 334 ++++++++-------- .../pointStructure/pointStructure.cpp | 2 +- .../trisurfaceStructure/multiTriSurface.cpp | 10 +- .../trisurfaceStructure/triSurface.cpp | 14 +- utilities/pFlowToVTK/pointFieldToVTK.hpp | 52 ++- .../particlesPhasicFlow.cpp | 126 +++++- 31 files changed, 873 insertions(+), 442 deletions(-) diff --git a/src/phasicFlow/containers/Field/Field.cpp b/src/phasicFlow/containers/Field/Field.cpp index 2acd3fb0..a2b5ec9b 100644 --- a/src/phasicFlow/containers/Field/Field.cpp +++ b/src/phasicFlow/containers/Field/Field.cpp @@ -83,17 +83,24 @@ bool pFlow::Field::readNonUniform } this->clear(); - VectorType::read(is); - - is.readEndStatement("readField"); - - if( this->size() != flen ) + if(is.isBinary() && !std::is_same_v) { - ioErrorInFile( is.name(), is.lineNumber() ) << - " expected " << flen << " elements, but supplied "<< - this->size() << " elements in file "<< is.name() <resize(flen); + is.read(reinterpret_cast(this->data()), this->size()*sizeof(T)); + is.readEndStatement("readField"); } + else + { + VectorType::read(is); + is.readEndStatement("readField"); + if( this->size() != flen ) + { + ioErrorInFile( is.name(), is.lineNumber() ) << + " expected " << flen << " elements, but supplied "<< + this->size() << " elements in file "<< is.name() <::readField ( iIstream& is, const size_t len, + bool resume, bool readLength ) { - if( !is.findToken(fieldKey_) ) + + bool tokenFound; + if( resume ) + tokenFound = is.findTokenResume(fieldKey_); + else + tokenFound = is.findToken(fieldKey_); + + if( !tokenFound ) { ioErrorInFile( is.name(), is.lineNumber() ) << " error in searching for filedkey " << fieldKey_<::readField template class VectorField, class T, class PropType> bool pFlow::Field::readField ( - iIstream& is + iIstream& is, + bool resume ) { - return readField(is, 0, true); + return readField(is, 0, resume ,true); } @@ -164,7 +180,15 @@ bool pFlow::Field::writeField(iOstream& os)const { os.writeWordKeyword(fieldKey_) << nonUniform__<size()<) + { + os.write(reinterpret_cast(this->data()), this->size()*sizeof(T)); + } + else + { + VectorType::write(os); + } os.endEntry(); return true; -} \ No newline at end of file +} + diff --git a/src/phasicFlow/containers/Field/Field.hpp b/src/phasicFlow/containers/Field/Field.hpp index a0f7020e..1a0f2448 100644 --- a/src/phasicFlow/containers/Field/Field.hpp +++ b/src/phasicFlow/containers/Field/Field.hpp @@ -214,18 +214,18 @@ public: } //// - IO operations - bool readField(iIstream& is, const size_t len, bool readLength = true); + bool readField(iIstream& is, const size_t len, bool resume, bool readLength = true); - bool readField(iIstream& is ); + bool readField(iIstream& is, bool resume ); bool writeField(iOstream& os)const; - bool read(iIstream& is) + bool read(iIstream& is, bool resume = false) { - return readField(is); + return readField(is, resume); } bool write(iOstream& os)const @@ -240,7 +240,7 @@ public: template class VectorField, class T, class PropType> inline iIstream& operator >> (iIstream & is, Field & ifld ) { - if( !ifld.readField(is) ) + if( !ifld.readField(is, false) ) { ioErrorInFile (is.name(), is.lineNumber()); fatalExit; diff --git a/src/phasicFlow/repository/IOobject/IOfileHeader.cpp b/src/phasicFlow/repository/IOobject/IOfileHeader.cpp index a30daa8e..2aae5642 100644 --- a/src/phasicFlow/repository/IOobject/IOfileHeader.cpp +++ b/src/phasicFlow/repository/IOobject/IOfileHeader.cpp @@ -24,22 +24,24 @@ Licence: pFlow::uniquePtr pFlow::IOfileHeader::inStream()const { if( fileExist() ) - return makeUnique(path()); + return makeUnique(path(), inFileBinary()); else return nullptr; } pFlow::uniquePtr pFlow::IOfileHeader::outStream()const { - auto osPtr = makeUnique(path()); - if(osPtr && owner_) - { + + auto osPtr = makeUnique(path(), outFileBinary()); + + if(osPtr && owner_) + { auto outPrecision = owner_->outFilePrecision(); osPtr->precision(outPrecision); - } + } - return osPtr; + return osPtr; } pFlow::IOfileHeader::IOfileHeader @@ -68,6 +70,19 @@ pFlow::fileSystem pFlow::IOfileHeader::path() const return f; } +bool pFlow::IOfileHeader::outFileBinary()const +{ + if(owner_) + return owner_->outFileBinary(); + else + return false; +} + +bool pFlow::IOfileHeader::inFileBinary()const +{ + return toUpper(fileFormat_) == "BINARY"; +} + bool pFlow::IOfileHeader::headerOk(bool silent) { if(!fileExist()) @@ -130,6 +145,15 @@ bool pFlow::IOfileHeader::writeHeader(iOstream& os, const word& typeName) const os.writeWordEntry("objectName", name() ); os.fatalCheck("writing objectName"); + word fileFormat; + if(outFileBinary()) + fileFormat = "Binary"; + else + fileFormat = "ASCII"; + + os.writeWordEntry("fileFormat", fileFormat); + os.fatalCheck("writing fileFormat"); + writeSeparator(os); return true; } @@ -164,6 +188,18 @@ bool pFlow::IOfileHeader::readHeader(iIstream& is, bool silent) return false; } + + if( !is.findTokenAndNextSilent("fileFormat", fileFormat_) ) + { + if(!silent) + { + warningInFunction << + "cannot find/error in reading fileFormat in file "<< + is.name()<("writeFormat", "ASCII")); + + if(wF == "ASCII") + outFormatBinary_ = false; + else if(wF == "BINARY") + outFormatBinary_ = true; + else + { + fatalErrorInFunction<< + "Invalid writeFormat in file "<< dict.name()<outFileBinary(); + else + return false; + } virtual bool write(bool verbose = false) const; }; diff --git a/src/phasicFlow/streams/Fstream/fileStream.cpp b/src/phasicFlow/streams/Fstream/fileStream.cpp index 3a39783a..8edf2d3a 100755 --- a/src/phasicFlow/streams/Fstream/fileStream.cpp +++ b/src/phasicFlow/streams/Fstream/fileStream.cpp @@ -24,7 +24,7 @@ Licence: #include "fileStream.hpp" #include "error.hpp" - +#include "streams.hpp" void pFlow::fileStream::openInFile ( @@ -39,7 +39,12 @@ void pFlow::fileStream::openInFile fatalExit; } - inStream_ = makeUnique( path.wordPath(), std::ios_base::in); + if(binary_) + inStream_ = makeUnique( + path.wordPath(), std::ios_base::in | std::ios_base::binary); + else + inStream_ = makeUnique( + path.wordPath(), std::ios_base::in); if( !inStream_->is_open()) { @@ -66,7 +71,15 @@ void pFlow::fileStream::openOutFile dir.createDirs(); } - outStream_ = makeUnique< std::ofstream>(path.wordPath(), std::ios_base::out); + if(binary_) + { + outStream_ = makeUnique< std::ofstream>( + path.wordPath(), std::ios_base::out| std::ios::binary); + } + else + outStream_ = makeUnique< std::ofstream>( + path.wordPath(), std::ios_base::out); + if(!outStream_->is_open()) { @@ -92,11 +105,13 @@ void pFlow::fileStream::close() pFlow::fileStream::fileStream ( const fileSystem& path, - bool outStream + bool outStream, + bool binary ) : inStream_(nullptr), - outStream_(nullptr) + outStream_(nullptr), + binary_(binary) { if(outStream) diff --git a/src/phasicFlow/streams/Fstream/fileStream.hpp b/src/phasicFlow/streams/Fstream/fileStream.hpp index 560da9ee..8804200c 100755 --- a/src/phasicFlow/streams/Fstream/fileStream.hpp +++ b/src/phasicFlow/streams/Fstream/fileStream.hpp @@ -43,6 +43,8 @@ protected: // - out file stream uniquePtr outStream_; + bool binary_ = false; + // - open input file void openInFile(const fileSystem& path); @@ -56,7 +58,7 @@ public: // - Constructors - fileStream( const fileSystem& path, bool outStream = false); + fileStream( const fileSystem& path, bool outStream = false, bool binary = false); fileStream(const fileStream&)= delete; diff --git a/src/phasicFlow/streams/Fstream/iFstream.cpp b/src/phasicFlow/streams/Fstream/iFstream.cpp index 09008735..2eb0a4fe 100755 --- a/src/phasicFlow/streams/Fstream/iFstream.cpp +++ b/src/phasicFlow/streams/Fstream/iFstream.cpp @@ -24,9 +24,17 @@ Licence: #include "iFstream.hpp" -pFlow::iFstream::iFstream (const fileSystem& path) +pFlow::iFstream::iFstream +( + const fileSystem& path, + bool binary) : - fileStream(path), - Istream( fileStream::inStream(), path.wordPath()) + fileStream(path, false, binary), + Istream + ( + fileStream::inStream(), + path.wordPath(), + (binary)? BINARY : ASCII + ) { } \ No newline at end of file diff --git a/src/phasicFlow/streams/Fstream/iFstream.hpp b/src/phasicFlow/streams/Fstream/iFstream.hpp index 253a4a36..a37baf81 100755 --- a/src/phasicFlow/streams/Fstream/iFstream.hpp +++ b/src/phasicFlow/streams/Fstream/iFstream.hpp @@ -40,7 +40,7 @@ class iFstream public: // - Constructor - iFstream (const fileSystem& path); + iFstream (const fileSystem& path, bool binary = false); // no copy constructor iFstream( const iFstream& src) = delete; diff --git a/src/phasicFlow/streams/Fstream/oFstream.cpp b/src/phasicFlow/streams/Fstream/oFstream.cpp index 3d1cbe9a..5e975843 100755 --- a/src/phasicFlow/streams/Fstream/oFstream.cpp +++ b/src/phasicFlow/streams/Fstream/oFstream.cpp @@ -24,11 +24,13 @@ Licence: #include "oFstream.hpp" -pFlow::oFstream::oFstream (const fileSystem& path) +pFlow::oFstream::oFstream (const fileSystem& path, bool binary) : - fileStream(path, true), - Ostream( fileStream::outStream(), path.wordPath()) -{ - - -} \ No newline at end of file + fileStream(path, true, binary), + Ostream + ( + fileStream::outStream(), + path.wordPath(), + (binary)? BINARY : ASCII + ) +{} \ No newline at end of file diff --git a/src/phasicFlow/streams/Fstream/oFstream.hpp b/src/phasicFlow/streams/Fstream/oFstream.hpp index 2a25e5de..0fa6d86d 100755 --- a/src/phasicFlow/streams/Fstream/oFstream.hpp +++ b/src/phasicFlow/streams/Fstream/oFstream.hpp @@ -41,7 +41,7 @@ class oFstream public: // Constructor - oFstream (const fileSystem& path); + oFstream (const fileSystem& path, bool binary = false); // no copy constructor oFstream( const oFstream& src) = delete; diff --git a/src/phasicFlow/streams/Stream/Istream.cpp b/src/phasicFlow/streams/Stream/Istream.cpp index 748ae06f..06e3a4ed 100755 --- a/src/phasicFlow/streams/Stream/Istream.cpp +++ b/src/phasicFlow/streams/Stream/Istream.cpp @@ -307,10 +307,11 @@ pFlow::Istream& pFlow::Istream::readVariable(word& str) pFlow::Istream::Istream ( std::istream& is, - const word& streamName + const word& streamName, + writeFormat wf ) : - iIstream(), + iIstream(wf), name_(streamName), is_(is) { @@ -846,6 +847,26 @@ pFlow::iIstream& pFlow::Istream::read(double& val) return *this; } +pFlow::iIstream& pFlow::Istream::read +( + char* buffer, + std::streamsize count +) +{ + if ( !isBinary() ) + { + fatalErrorInFunction<<"stream format is not binray. Stream name is "<< + name()<<'\n'; + fatalExit; + } + + readBegin("binaryBlock"); + is_.read(buffer, count); + readEnd("binaryBlock"); + + setState(is_.rdstate()); + return *this; +} void pFlow::Istream::rewind() { diff --git a/src/phasicFlow/streams/Stream/Istream.hpp b/src/phasicFlow/streams/Stream/Istream.hpp index a65d080b..ae2bcab8 100755 --- a/src/phasicFlow/streams/Stream/Istream.hpp +++ b/src/phasicFlow/streams/Stream/Istream.hpp @@ -65,8 +65,7 @@ public: //- Construct wrapper around std::istream, set stream status - Istream( std::istream& is, const word& streamName); - + Istream( std::istream& is, const word& streamName, writeFormat wf = ASCII); //- Destructor virtual ~Istream() = default; @@ -150,6 +149,8 @@ public: //- Read a double virtual iIstream& read(double& val) override; + iIstream& read(char* buffer, std::streamsize count) override; + //- Rewind the stream so that it may be read again virtual void rewind(); diff --git a/src/phasicFlow/streams/Stream/Ostream.cpp b/src/phasicFlow/streams/Stream/Ostream.cpp index 75e4f477..51f2f28d 100755 --- a/src/phasicFlow/streams/Stream/Ostream.cpp +++ b/src/phasicFlow/streams/Stream/Ostream.cpp @@ -28,10 +28,11 @@ Licence: pFlow::Ostream::Ostream ( std::ostream& os, - const word& streamName + const word& streamName, + writeFormat wF ) : - iOstream(), + iOstream(wF), name_(streamName), os_(os) { @@ -230,6 +231,29 @@ pFlow::iOstream& pFlow::Ostream::write(const double val) 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; + + setState(os_.rdstate()); + + return *this; +} + + void pFlow::Ostream::indent() diff --git a/src/phasicFlow/streams/Stream/Ostream.hpp b/src/phasicFlow/streams/Stream/Ostream.hpp index e34c521b..727b65e4 100755 --- a/src/phasicFlow/streams/Stream/Ostream.hpp +++ b/src/phasicFlow/streams/Stream/Ostream.hpp @@ -46,7 +46,7 @@ public: // Constructors - Ostream ( std::ostream& os, const word& streamName); + Ostream ( std::ostream& os, const word& streamName, writeFormat wf = ASCII); //- no copy construct Ostream(const Ostream&) = delete; @@ -75,79 +75,80 @@ public: //- 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; + bool write(const token& tok)override; //- Write character - virtual iOstream& write(const char c)override; + iOstream& write(const char c)override; //- Write character string - virtual iOstream& write(const char* str)override; + iOstream& write(const char* str)override; //- Write word - virtual iOstream& write(const word& str)override; + iOstream& write(const word& str)override; //- Write std::string surrounded by quotes. // Optional write without quotes. - virtual iOstream& writeQuoted ( const word& str, const bool quoted=true )override; + iOstream& writeQuoted ( const word& str, const bool quoted=true ) override; //- Write int64 - virtual iOstream& write(const int64 val) override; + iOstream& write(const int64 val) override; //- Write int32 - virtual iOstream& write(const int32 val) override; - + iOstream& write(const int32 val) override; //- Write label - virtual iOstream& write(const label val) override; + iOstream& write(const label val) override; //- Write uint32 - virtual iOstream& write(const uint32 val) override; + iOstream& write(const uint32 val) override; //- Write uint16 - virtual iOstream& write(const uint16 val) override; + iOstream& write(const uint16 val) override; //- Write float - virtual iOstream& write(const float val) override; + iOstream& write(const float val) override; //- Write double - virtual iOstream& write(const double val) override; + iOstream& write(const double val) override; + + /// Write a block of binray data + iOstream& write(const char* binaryData, std::streamsize count) override; //- Add indentation characters - virtual void indent(); - + void indent() override; //- Set stream flags - virtual ios_base::fmtflags flags(const ios_base::fmtflags f); + ios_base::fmtflags flags(const ios_base::fmtflags f) override; //- Flush stream - virtual void flush(); + void flush() override; //- Add newline and flush stream - virtual void endl(); + void endl() override; //- Get the current padding character - virtual char fill() const; + char fill() const override; //- Set padding character for formatted field up to field width // \return previous padding character - virtual char fill(const char fillch); + char fill(const char fillch) override; //- Get width of output field - virtual int width() const; + int width() const override; //- Set width of output field // \return previous width - virtual int width(const int w); + int width(const int w) override; //- Get precision of output field - virtual int precision() const; + int precision() const override; //- Set precision of output field // return old precision - virtual int precision(const int p); + int precision(const int p) override; //- Access to underlying std::ostream - virtual std::ostream& stdStream() + virtual std::ostream& stdStream() { return os_; } diff --git a/src/phasicFlow/streams/TStream/iTstream.cpp b/src/phasicFlow/streams/TStream/iTstream.cpp index 28e7cf6c..d5f6ab2a 100755 --- a/src/phasicFlow/streams/TStream/iTstream.cpp +++ b/src/phasicFlow/streams/TStream/iTstream.cpp @@ -302,7 +302,15 @@ pFlow::iIstream& pFlow::iTstream::read return *this; } - +pFlow::iIstream& pFlow::iTstream::read +( + char* buffer, + std::streamsize count +) +{ + notImplementedFunction; + return *this; +} void pFlow::iTstream::rewind() { diff --git a/src/phasicFlow/streams/TStream/iTstream.hpp b/src/phasicFlow/streams/TStream/iTstream.hpp index 8b2fecd8..eb77f94e 100755 --- a/src/phasicFlow/streams/TStream/iTstream.hpp +++ b/src/phasicFlow/streams/TStream/iTstream.hpp @@ -123,6 +123,8 @@ public: //- Read a doubleScalar virtual iIstream& read(double&) override; + iIstream& read(char* buffer, std::streamsize count) override; + // - Rewind the stream so that it may be read again virtual void rewind(); diff --git a/src/phasicFlow/streams/TStream/oTstream.cpp b/src/phasicFlow/streams/TStream/oTstream.cpp index a3a67911..5f158545 100755 --- a/src/phasicFlow/streams/TStream/oTstream.cpp +++ b/src/phasicFlow/streams/TStream/oTstream.cpp @@ -148,6 +148,15 @@ pFlow::iOstream& pFlow::oTstream::write(const double val) return *this; } +pFlow::iOstream& pFlow::oTstream::write +( + const char* binaryData, + std::streamsize count +) +{ + notImplementedFunction; + return *this; +} void pFlow::oTstream::append(const token& tok) diff --git a/src/phasicFlow/streams/TStream/oTstream.hpp b/src/phasicFlow/streams/TStream/oTstream.hpp index 12d60322..92b37f60 100755 --- a/src/phasicFlow/streams/TStream/oTstream.hpp +++ b/src/phasicFlow/streams/TStream/oTstream.hpp @@ -102,6 +102,11 @@ public: //- Write double virtual iOstream& write(const double val) override; + /// Write a block of binray data + iOstream& write( + const char* binaryData, + std::streamsize count) override; + // - append token to the stream virtual void append(const token& tok); diff --git a/src/phasicFlow/streams/iStream/IOstream.hpp b/src/phasicFlow/streams/iStream/IOstream.hpp index e44b394c..046aa1f0 100644 --- a/src/phasicFlow/streams/iStream/IOstream.hpp +++ b/src/phasicFlow/streams/iStream/IOstream.hpp @@ -50,226 +50,260 @@ public: enum streamAccess : char { - CLOSED = 0, //!< stream is not open - OPENED //!< stream is open + CLOSED = 0, /// stream is not open + OPENED /// stream is open }; + enum writeFormat: char + { + ASCII = 0, + BINARY + }; - //- Default precision + /// Default precision, only works for ASCII static unsigned int precision_; protected: - //- Name for any generic stream - normally treat as readonly + /// Name for any generic stream - normally treat as readonly static word staticName_; + /// Is stream open or closed streamAccess openClosed_; + /// write format + writeFormat writeFormat_ = ASCII; + + /// state ios_base::iostate ioState_; - //- The file line + /// The file line int32 lineNumber_; - // Protected Member Functions + //- Protected Member Functions - //- Set stream opened - void setOpened() - { - openClosed_ = OPENED; - } + /// Set stream opened + void setOpened() + { + openClosed_ = OPENED; + } - //- Set stream closed - void setClosed() - { - openClosed_ = CLOSED; - } + /// Set stream closed + void setClosed() + { + openClosed_ = CLOSED; + } - //- Set stream state - void setState(ios_base::iostate state) - { - ioState_ = state; - } + /// Set stream state + void setState(ios_base::iostate state) + { + ioState_ = state; + } - //- Set stream to be good - void setGood() - { - ioState_ = ios_base::iostate(0); - } + void setWriteFormat(writeFormat wF) + { + writeFormat_ = wF; + } + /// Set stream to be good + void setGood() + { + ioState_ = ios_base::iostate(0); + } public: - // Constructors - explicit IOstream(): - openClosed_(CLOSED), - ioState_(ios_base::iostate(0)), - lineNumber_(0) - { - setBad(); - } + //- Constructors - IOstream(const IOstream&) = default; + /// Default + explicit IOstream(): + openClosed_(CLOSED), + writeFormat_(ASCII), + ioState_(ios_base::iostate(0)), + lineNumber_(0) + { + setBad(); + } - //- Destructor - virtual ~IOstream() = default; - - - //// Member Functions + /// Construct and set write format + explicit IOstream(writeFormat wF): + openClosed_(CLOSED), + writeFormat_(wF), + ioState_(ios_base::iostate(0)), + lineNumber_(0) + { + setBad(); + } - //- Return the name of the stream - virtual const word& name() const; + /// Copy + IOstream(const IOstream&) = default; - //- Return non-const access to the name of the stream - virtual word& name(); + /// Destructor + virtual ~IOstream() = default; + + + //- Member Functions - //- Check IOstream status for given operation. - // Print IOstream state or generate a FatalIOError - // when an error has occurred. - // The base implementation is a fatalCheck - virtual bool check(const char* operation) const; + /// Return the name of the stream + virtual const word& name() const; - //- Check IOstream status for given operation. - // Generate a FatalIOError when an error has occurred. - bool fatalCheck(const char* operation) const; + /// Return non-const access to the name of the stream + virtual word& name(); - //- Return true if stream has been opened - bool opened() const - { - return openClosed_ == OPENED; - } + /// Check IOstream status for given operation. + /// Print IOstream state or generate a FatalIOError + /// when an error has occurred. + /// The base implementation is a fatalCheck + virtual bool check(const char* operation) const; - //- Return true if stream is closed - bool closed() const - { - return openClosed_ == CLOSED; - } + /// Check IOstream status for given operation. + /// Generate a FatalIOError when an error has occurred. + bool fatalCheck(const char* operation) const; - //- Return true if next operation might succeed - bool good() const - { - return ioState_ == 0; - } + /// Return true if stream has been opened + bool opened() const + { + return openClosed_ == OPENED; + } - //- Return true if end of input seen - bool eof() const - { - return ioState_ & ios_base::eofbit; - } + /// Return true if stream is closed + bool closed() const + { + return openClosed_ == CLOSED; + } - //- Return true if next operation will fail - bool fail() const - { - return ioState_ & (ios_base::badbit | ios_base::failbit); - } + /// Return true if stream format is binray + bool isBinary()const + { + return writeFormat_ == BINARY; + } - //- Return true if stream is corrupted - bool bad() const - { - return ioState_ & ios_base::badbit; - } + /// Return true if next operation might succeed + bool good() const + { + return ioState_ == 0; + } - //- Return true if the stream has not failed - explicit operator bool() const - { - return !fail(); - } + /// Return true if end of input seen + bool eof() const + { + return ioState_ & ios_base::eofbit; + } - //- Return true if the stream has failed - bool operator!() const - { - return fail(); - } + /// Return true if next operation will fail + bool fail() const + { + return ioState_ & (ios_base::badbit | ios_base::failbit); + } - - //- Const access to the current stream line number - int32 lineNumber() const - { - return lineNumber_; - } + /// Return true if stream is corrupted + bool bad() const + { + return ioState_ & ios_base::badbit; + } - //- Non-const access to the current stream line number - int32& lineNumber() - { - return lineNumber_; - } + /// Return true if the stream has not failed + explicit operator bool() const + { + return !fail(); + } - //- Set the stream line number - // \return the previous value - int32 lineNumber(const int32 num) - { - const int32 old(lineNumber_); - lineNumber_ = num; - return old; - } + /// Return true if the stream has failed + bool operator!() const + { + return fail(); + } - //- Return flags of stream - virtual ios_base::fmtflags flags() const = 0; + + /// Const access to the current stream line number + int32 lineNumber() const + { + return lineNumber_; + } - //- Return the default precision - static unsigned int defaultPrecision() - { - return precision_; - } + /// Non-const access to the current stream line number + int32& lineNumber() + { + return lineNumber_; + } - //- Reset the default precision - // \return the previous value - static unsigned int defaultPrecision(unsigned int prec) - { - unsigned int old(precision_); - precision_ = prec; - return old; - } + /// Set the stream line number + /// return the previous value + int32 lineNumber(const int32 num) + { + const int32 old(lineNumber_); + lineNumber_ = num; + return old; + } - //- Set stream to have reached eof - void setEof() - { - ioState_ |= ios_base::eofbit; - } + /// Return flags of stream + virtual ios_base::fmtflags flags() const = 0; - //- Set stream to have failed - void setFail() - { - ioState_ |= ios_base::failbit; - } + /// Return the default precision + static unsigned int defaultPrecision() + { + return precision_; + } - //- Set stream to be bad - void setBad() - { - ioState_ |= ios_base::badbit; - } + /// Reset the default precision + /// return the previous value + static unsigned int defaultPrecision(unsigned int prec) + { + unsigned int old(precision_); + precision_ = prec; + return old; + } - //- Set flags of stream - virtual ios_base::fmtflags flags(const ios_base::fmtflags f) = 0; + /// Set stream to have reached eof + void setEof() + { + ioState_ |= ios_base::eofbit; + } - //- Set flags of stream - ios_base::fmtflags setf(const ios_base::fmtflags f) - { - return flags(flags() | f); - } + /// Set stream to have failed + void setFail() + { + ioState_ |= ios_base::failbit; + } - //- Set flags of given field of stream - ios_base::fmtflags setf - ( - const ios_base::fmtflags f, - const ios_base::fmtflags mask - ) - { - return flags((flags() & ~mask) | (f & mask)); - } + /// Set stream to be bad + void setBad() + { + ioState_ |= ios_base::badbit; + } - //- Unset flags of stream - void unsetf(const ios_base::fmtflags f) - { - flags(flags() & ~f); - } + /// Set flags of stream + virtual ios_base::fmtflags flags(const ios_base::fmtflags f) = 0; + + /// Set flags of stream + ios_base::fmtflags setf(const ios_base::fmtflags f) + { + return flags(flags() | f); + } + + /// Set flags of given field of stream + ios_base::fmtflags setf + ( + const ios_base::fmtflags f, + const ios_base::fmtflags mask + ) + { + return flags((flags() & ~mask) | (f & mask)); + } + + /// Unset flags of stream + void unsetf(const ios_base::fmtflags f) + { + flags(flags() & ~f); + } }; // end of IOstream -//- An IOstream manipulator +/// An IOstream manipulator typedef IOstream& (*IOstreamManip)(IOstream&); inline IOstream& dec(IOstream& io) diff --git a/src/phasicFlow/streams/iStream/iIstream.cpp b/src/phasicFlow/streams/iStream/iIstream.cpp index 7877bd58..96849244 100755 --- a/src/phasicFlow/streams/iStream/iIstream.cpp +++ b/src/phasicFlow/streams/iStream/iIstream.cpp @@ -60,6 +60,11 @@ bool pFlow::iIstream::peekBack(token& tok) bool pFlow::iIstream::findToken( const word & w ) { rewind(); + return findTokenResume(w); +} + +bool pFlow::iIstream::findTokenResume(const word& w) +{ token next; bool isFirstToken = true; @@ -74,8 +79,6 @@ bool pFlow::iIstream::findToken( const word & w ) return false; } - - if( next.isWord() && isFirstToken) { if(next.wordToken() == w ) return true; @@ -93,6 +96,11 @@ bool pFlow::iIstream::findToken( const word & w ) bool pFlow::iIstream::findTokenSilent( const word & w, int32 limitLine ) { rewind(); + return findTokenResumeSilent(w,limitLine); +} + +bool pFlow::iIstream::findTokenResumeSilent( const word & w, int32 limitLine ) +{ token next; bool isFirstToken = true; @@ -115,7 +123,7 @@ bool pFlow::iIstream::findTokenSilent( const word & w, int32 limitLine ) isFirstToken = false; } - return false; + return false; } bool pFlow::iIstream::findTokenAndNext diff --git a/src/phasicFlow/streams/iStream/iIstream.hpp b/src/phasicFlow/streams/iStream/iIstream.hpp index a3618127..cf32961b 100755 --- a/src/phasicFlow/streams/iStream/iIstream.hpp +++ b/src/phasicFlow/streams/iStream/iIstream.hpp @@ -53,6 +53,11 @@ public: putBack_(false) {} + iIstream(writeFormat wf): + IOstream(wf), + putBack_(false) + {} + // - Copy construct iIstream(const iIstream&) = default; @@ -122,6 +127,8 @@ public: //- Read a doubleScalar virtual iIstream& read(double&) = 0; + virtual iIstream& read(char* buffer, std::streamsize count) =0; + //- Rewind the stream so that it may be read again virtual void rewind() = 0; @@ -132,10 +139,18 @@ public: // - search for all tokesn and find the first word token tbat matchs w virtual bool findToken( const word & w ); + /// search for all tokesn after the current file position + /// and find the first word token tbat matchs w + virtual bool findTokenResume(const word& w); + // - search for all tokesn and find the first word token that matchs virtual bool findTokenSilent( const word & w, int32 limitLine = 100 ); + /// search for all tokesn after the current file position + /// and find the first word token tbat matchs w + virtual bool findTokenResumeSilent( const word & w, int32 limitLine = 100 ); + // - search for all tokens and find the first word token and also next word token // chekck if it is eneded with end statement ; virtual bool findTokenAndNext( const word& w, word& nextW, bool checkEndStatement = true); diff --git a/src/phasicFlow/streams/iStream/iOstream.hpp b/src/phasicFlow/streams/iStream/iOstream.hpp index 59f00768..6c083a9b 100644 --- a/src/phasicFlow/streams/iStream/iOstream.hpp +++ b/src/phasicFlow/streams/iStream/iOstream.hpp @@ -58,219 +58,227 @@ protected: // Protected Data - //- Indentation of the entry from the start of the keyword + /// Indentation of the entry from the start of the keyword static constexpr const unsigned short entryIndentation_ = 16; - //- Number of spaces per indent level + /// Number of spaces per indent level unsigned short indentSize_ = 4; - //- Current indent level + /// Current indent level unsigned short indentLevel_ = 0; public: - // Constructor - explicit iOstream() - {} + // Constructors - //- Copy construct - iOstream(const iOstream&) = default; + /// Default + explicit iOstream() + {} - //- Destructor - virtual ~iOstream() = default; + /// Construct from writeFormat + explicit iOstream(writeFormat wF): + IOstream(wF) + {} + + /// Copy construct + iOstream(const iOstream&) = default; + + /// Destructor + virtual ~iOstream() = default; - // 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) = 0; + /// 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) = 0; - //- Write character - virtual iOstream& write(const char c) = 0; + /// Write character + virtual iOstream& write(const char c) = 0; - //- Write character string - virtual iOstream& write(const char* str) = 0; + /// Write character string + virtual iOstream& write(const char* str) = 0; - //- Write word - virtual iOstream& write(const word& str) = 0; + /// Write word + virtual iOstream& write(const word& str) = 0; + /// Write std::string surrounded by quotes. + /// Optional write without quotes. + virtual iOstream& writeQuoted + ( + const word& str, + const bool quoted=true + ) = 0; + + /// Write int64 + virtual iOstream& write(const int64 val) = 0; - //- Write std::string surrounded by quotes. - // Optional write without quotes. - virtual iOstream& writeQuoted - ( - const word& str, - const bool quoted=true - ) = 0; + /// Write int32 + virtual iOstream& write(const int32 val) = 0; + /// Write label + virtual iOstream& write(const label val) = 0; + + /// Write uint32 + virtual iOstream& write(const uint32 val) = 0; + + /// Write uint16 + virtual iOstream& write(const uint16 val) = 0; + + /// Write float + virtual iOstream& write(const float val) = 0; + + /// Write double + virtual iOstream& write(const double val) = 0; + + /// Write a block of binray data + virtual iOstream& write(const char* binaryData, std::streamsize count) = 0; + - //- Write int64 - virtual iOstream& write(const int64 val) = 0; + // - Indent - //- Write int32 - virtual iOstream& write(const int32 val) = 0; + /// Add indentation characters + virtual void indent() = 0; - //- Write label - virtual iOstream& write(const label val) = 0; + /// Return indent level + unsigned short indentSize() const + { + return indentSize_; + } - //- Write uint32 - virtual iOstream& write(const uint32 val) = 0; + /// Access to indent size + unsigned short& indentSize() + { + return indentSize_; + } - //- Write uint16 - virtual iOstream& write(const uint16 val) = 0; + /// Return indent level + unsigned short indentLevel() const + { + return indentLevel_; + } - //- Write float - virtual iOstream& write(const float val) = 0; + /// Access to indent level + unsigned short& indentLevel() + { + return indentLevel_; + } - //- Write double - virtual iOstream& write(const double val) = 0; - + /// Increment the indent level + void incrIndent() + { + ++indentLevel_; + } + /// Decrement the indent level + void decrIndent(); + + //- Punctuations - //- Add indentation characters - virtual void indent() = 0; + /// Write begin block group with a name + /// Increments indentation, adds newline. + virtual iOstream& beginBlock(const word& kw); - //- Return indent level - unsigned short indentSize() const - { - return indentSize_; - } + /// Write begin block group without a name + /// Increments indentation, adds newline. + virtual iOstream& beginBlock(); - //- Access to indent size - unsigned short& indentSize() - { - return indentSize_; - } + /// Write end block group + /// Decrements indentation, adds newline. + virtual iOstream& endBlock(); - //- Return indent level - unsigned short indentLevel() const - { - return indentLevel_; - } + /// Write begin list "(" + virtual iOstream& beginList(); - //- Access to indent level - unsigned short& indentLevel() - { - return indentLevel_; - } + /// Write begin list with keyword "kw (" + virtual iOstream& beginList(const word& kw); - //- Increment the indent level - void incrIndent() - { - ++indentLevel_; - } + /// Write end list ")" + virtual iOstream& endList(); - //- Decrement the indent level - void decrIndent(); + /// Write begin list "[" + virtual iOstream& beginSquare(); - + /// Write begin list with keyword "kw [" + virtual iOstream& beginSquare(const word& kw); - - //- Write begin block group with a name - // Increments indentation, adds newline. - virtual iOstream& beginBlock(const word& kw); + /// Write end list "]" + virtual iOstream& endSquare(); - //- Write begin block group without a name - // Increments indentation, adds newline. - virtual iOstream& beginBlock(); + /// Write end entry (';') followed by newline. + virtual iOstream& endEntry(); - //- Write end block group - // Decrements indentation, adds newline. - virtual iOstream& endBlock(); + /// Write a newLine to stream + virtual iOstream& newLine(); - //- Write begin list "(" - virtual iOstream& beginList(); + /// Write space to stream + virtual iOstream& space(int32 n=1); - //- Write begin list with keyword "kw (" - virtual iOstream& beginList(const word& kw); + /// Write the keyword followed by an appropriate indentation + virtual iOstream& writeWordKeyword(const word& kw); - //- Write end list ")" - virtual iOstream& endList(); - - //- Write begin list "[" - virtual iOstream& beginSquare(); - - //- Write begin list with keyword "kw [" - virtual iOstream& beginSquare(const word& kw); - - //- Write end list "]" - virtual iOstream& endSquare(); - - //- Write end entry (';') followed by newline. - virtual iOstream& endEntry(); - - //- Write a newLine to stream - virtual iOstream& newLine(); - - //- Write space to stream - virtual iOstream& space(int32 n=1); - - - //- Write the keyword followed by an appropriate indentation - virtual iOstream& writeWordKeyword(const word& kw); - - //- Write a keyword/value entry. - template - iOstream& writeWordEntry(const word& key, const T& value) - { - writeWordKeyword(key) << value; - return endEntry(); - } - - //// Stream state functions - - //- Flush stream - virtual void flush() = 0; - - //- Add newline and flush stream - virtual void endl() = 0; - - //- Get padding character - virtual char fill() const = 0; - - //- Set padding character for formatted field up to field width - virtual char fill(const char fillch) = 0; - - //- Get width of output field - virtual int width() const = 0; - - //- Set width of output field (and return old width) - virtual int width(const int w) = 0; - - //- Get precision of output field - virtual int precision() const = 0; - - //- Set precision of output field (and return old precision) - virtual int precision(const int p) = 0; + /// Write a keyword/value entry. + template + iOstream& writeWordEntry(const word& key, const T& value) + { + writeWordKeyword(key) << value; + return endEntry(); + } - // Member Operators + //- Stream state functions - //- Return a non-const reference to const iOstream - // Needed for write functions where the stream argument is temporary: - // e.g. thing thisThing(OFstream("thingFileName")()); - iOstream& operator()() const - { - return const_cast(*this); - } + /// Flush stream + virtual void flush() = 0; + + /// Add newline and flush stream + virtual void endl() = 0; + + /// Get padding character + virtual char fill() const = 0; + + /// Set padding character for formatted field up to field width + virtual char fill(const char fillch) = 0; + + /// Get width of output field + virtual int width() const = 0; + + /// Set width of output field (and return old width) + virtual int width(const int w) = 0; + + /// Get precision of output field + virtual int precision() const = 0; + + /// Set precision of output field (and return old precision) + virtual int precision(const int p) = 0; + + + //- Member Operators + + /// Return a non-const reference to const iOstream + /// Needed for write functions where the stream argument is temporary: + /// e.g. thing thisThing(OFstream("thingFileName")()); + iOstream& operator()() const + { + return const_cast(*this); + } }; -//- An iOstream manipulator +/// An iOstream manipulator typedef iOstream& (*iOstreamManip)(iOstream&); -//- operator<< handling for manipulators without arguments +/// operator<< handling for manipulators without arguments inline iOstream& operator<<(iOstream& os, iOstreamManip f) { return f(os); } -//- operator<< handling for manipulators without arguments +/// operator<< handling for manipulators without arguments inline iOstream& operator<<(iOstream& os, IOstreamManip f) { f(os); @@ -278,21 +286,21 @@ inline iOstream& operator<<(iOstream& os, IOstreamManip f) } -//- Indent stream +/// Indent stream inline iOstream& indent(iOstream& os) { os.indent(); return os; } -//- Increment the indent level +/// Increment the indent level inline iOstream& incrIndent(iOstream& os) { os.incrIndent(); return os; } -//- Decrement the indent level +/// Decrement the indent level inline iOstream& decrIndent(iOstream& os) { os.decrIndent(); @@ -300,7 +308,7 @@ inline iOstream& decrIndent(iOstream& os) } -//- Flush stream +/// Flush stream inline iOstream& flush(iOstream& os) { os.flush(); @@ -308,7 +316,7 @@ inline iOstream& flush(iOstream& os) } -//- Add newline and flush stream +/// Add newline and flush stream inline iOstream& endl(iOstream& os) { os.endl(); @@ -316,7 +324,7 @@ inline iOstream& endl(iOstream& os) } -//- Write begin block group without a name +/// Write begin block group without a name // Increments indentation, adds newline. inline iOstream& beginBlock(iOstream& os) { @@ -325,7 +333,7 @@ inline iOstream& beginBlock(iOstream& os) } -//- Write end block group +/// Write end block group // Decrements indentation, adds newline. inline iOstream& endBlock(iOstream& os) { @@ -334,7 +342,7 @@ inline iOstream& endBlock(iOstream& os) } -//- Write end entry (';') followed by newline. +/// Write end entry (';') followed by newline. inline iOstream& endEntry(iOstream& os) { os.endEntry(); diff --git a/src/phasicFlow/structuredData/pointStructure/pointStructure.cpp b/src/phasicFlow/structuredData/pointStructure/pointStructure.cpp index cacb6280..0b21c528 100644 --- a/src/phasicFlow/structuredData/pointStructure/pointStructure.cpp +++ b/src/phasicFlow/structuredData/pointStructure/pointStructure.cpp @@ -400,7 +400,7 @@ bool pFlow::pointStructure::readPointStructure return false; } - if(! pointFlag_.read(is)) + if(! pointFlag_.read(is, true)) { ioErrorInFile(is.name(), is.lineNumber())<< "Error in reading pointFlag in pointStructure \n"; diff --git a/src/phasicFlow/structuredData/trisurfaceStructure/multiTriSurface.cpp b/src/phasicFlow/structuredData/trisurfaceStructure/multiTriSurface.cpp index 4e3af48b..fe37308c 100644 --- a/src/phasicFlow/structuredData/trisurfaceStructure/multiTriSurface.cpp +++ b/src/phasicFlow/structuredData/trisurfaceStructure/multiTriSurface.cpp @@ -214,14 +214,12 @@ bool pFlow::multiTriSurface::readMultiTriSurface { if( !readTriSurface(is) )return false; - is >> lastPointIndex_; - if(!is.check(FUNCTION_NAME) ) return false; + // from current position + if(!lastPointIndex_.read(is, true)) return false; - is >> lastVertexIndex_; - if(!is.check(FUNCTION_NAME) ) return false; + if(!lastVertexIndex_.read(is, true) ) return false; - is >> surfaceNames_; - if( !is.check(FUNCTION_NAME)) return false; + if( !surfaceNames_.read(is, true)) return false; calculateVars(); diff --git a/src/phasicFlow/structuredData/trisurfaceStructure/triSurface.cpp b/src/phasicFlow/structuredData/trisurfaceStructure/triSurface.cpp index eb292aa8..ff23104b 100644 --- a/src/phasicFlow/structuredData/trisurfaceStructure/triSurface.cpp +++ b/src/phasicFlow/structuredData/trisurfaceStructure/triSurface.cpp @@ -193,14 +193,16 @@ bool pFlow::triSurface::readTriSurface ) { + std::cout<<"triSurface file is binary "<< is.isBinary()<> points_; - is.fatalCheck(FUNCTION_NAME); - - is >> vertices_; - is.fatalCheck(FUNCTION_NAME); - + // from start of file + if(!points_.read(is)) return false; + + // from the current position + if(!vertices_.read(is, true)) return false; + if( !check() ) { ioErrorInFile( is.name(), is.lineNumber() ) << diff --git a/utilities/pFlowToVTK/pointFieldToVTK.hpp b/utilities/pFlowToVTK/pointFieldToVTK.hpp index ca71b028..fd7a77ae 100755 --- a/utilities/pFlowToVTK/pointFieldToVTK.hpp +++ b/utilities/pFlowToVTK/pointFieldToVTK.hpp @@ -31,12 +31,12 @@ Licence: namespace pFlow::PFtoVTK { -template -bool addInt64PointField( +template +bool addIntPointField( iOstream& os, word fieldName, int32 numActivePoints, - int64* field, + IntType* field, IncludeMaskType includeMask ); template @@ -76,6 +76,41 @@ bool checkFieldType(word objectType) } +bool convertInt32PointField +( + iOstream& os, + const IOfileHeader& header, + const pointStructure& pStruct +) +{ + word objectType = header.objectType(); + + if(!checkFieldType(objectType)) + { + return false; + } + + auto objField = IOobject::make + ( + header, + pStruct, + static_cast(0) + ); + + auto& Field = objField().getObject(); + + auto* data = Field.hostVectorAll().data(); + + REPORT(2)<<"writing "<< greenColor <(objectType) || checkFieldType(objectType) || - checkFieldType(objectType) || checkFieldType(objectType) || checkFieldType(objectType) || checkFieldType