# PhasicFlow: src/phasicFlow/repository/IOobject/IOfileHeader.cpp Source File

IOfileHeader.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 "IOfileHeader.hpp"
#include "repository.hpp"
pFlow::uniquePtr<pFlow::iFstream> pFlow::IOfileHeader::inStream()const
{
if( fileExist() )
return makeUnique<iFstream>(path());
else
return nullptr;
}
pFlow::uniquePtr<pFlow::oFstream> pFlow::IOfileHeader::outStream()const
{
auto osPtr = makeUnique<oFstream>(path());
if(osPtr && owner_)
{
auto outPrecision = owner_->outFilePrecision();
osPtr->precision(outPrecision);
}
return osPtr;
}
pFlow::IOfileHeader::IOfileHeader
(
const objectFile& objf,
const repository* owner
)
:
objectFile(objf),
owner_(owner)
{}
pFlow::fileSystem pFlow::IOfileHeader::path() const
{
fileSystem f;
if( owner_ )
{
f = owner_->path()/localPath();
}else
{
f = localPath();
}
f += name_;
return f;
}
bool pFlow::IOfileHeader::headerOk(bool silent)
{
if(!fileExist())
{
if(!silent)
warningInFunction <<
" the file "<< path() << " does not exist. \n";
return false;
}
if( implyRead() )
{
if( auto ptrIS = inStream(); ptrIS )
{
return readHeader( ptrIS(), silent );
}
else
{
if(!silent)
warningInFunction<<
"could not open file " << path() <<endl;
return false;
}
}
return true;
}
bool pFlow::IOfileHeader::implyRead() const
{
if (isReadAlways()) return true;
return readIfPresent();
}
bool pFlow::IOfileHeader::implyWrite() const
{
return isWriteAlways();
}
bool pFlow::IOfileHeader::fileExist() const
{
return path().exist();
}
bool pFlow::IOfileHeader::readIfPresent()const
{
return fileExist() && isReadIfPresent();
}
bool pFlow::IOfileHeader::writeHeader(iOstream& os, const word& typeName) const
{
writeBanner(os);
os.writeWordEntry("objectType", typeName );
os.fatalCheck("writing objectType");
os.writeWordEntry("objectName", name() );
os.fatalCheck("writing objectName");
writeSeparator(os);
return true;
}
bool pFlow::IOfileHeader::writeHeader(iOstream& os) const
{
return writeHeader(os, objectType_);
}
bool pFlow::IOfileHeader::readHeader(iIstream& is, bool silent)
{
if( !is.findTokenAndNextSilent("objectName", objectName_) )
{
if(!silent)
{
warningInFunction<<
"cannot find/error in reading objectName in file " <<
is.name()<<endl;
}
return false;
}
if( !is.findTokenAndNextSilent("objectType", objectType_) )
{
if(!silent)
{
warningInFunction <<
"cannot find/error in reading objectType in file "<<
is.name()<<endl;
}
return false;
}
return true;
}
bool pFlow::IOfileHeader::writeBanner(iOstream& os)const
{
os<<
"/* -------------------------------*- C++ -*---------------------------------- *\\ \n"<<
"| phasicFlow File | \n"<<
"| copyright: www.cemf.ir | \n"<<
"\\* ------------------------------------------------------------------------- */ \n \n";
return true;
}
bool pFlow::IOfileHeader::writeSeparator(iOstream& os)const
{
os<< "\n" <<
"// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // \n \n";
return true;
}
pFlow::iIstream::findTokenAndNextSilent
virtual bool findTokenAndNextSilent(const word &w, word &nextW, int32 limitLine=100)
Definition: iIstream.cpp:168
pFlow::IOfileHeader::readIfPresent
bool readIfPresent() const
Definition: IOfileHeader.cpp:116
pFlow::IOfileHeader::readHeader
bool readHeader(iIstream &is, bool silent=false)
Definition: IOfileHeader.cpp:142
warningInFunction
#define warningInFunction
Definition: error.hpp:55
pFlow::IOfileHeader::fileExist
bool fileExist() const
Definition: IOfileHeader.cpp:111
pFlow::word
std::string word
Definition: builtinTypes.hpp:63
pFlow::endl
iOstream & endl(iOstream &os)
Definition: iOstream.hpp:312
pFlow::fileSystem
Definition: fileSystem.hpp:63
repository.hpp
pFlow::IOfileHeader::outStream
uniquePtr< oFstream > outStream() const
Definition: IOfileHeader.cpp:32
pFlow::iIstream
Definition: iIstream.hpp:33
pFlow::IOfileHeader::writeHeader
bool writeHeader(iOstream &os, const word &typeName) const
Definition: IOfileHeader.cpp:122
pFlow::fileSystem::path
const pathType & path() const
Definition: fileSystem.hpp:121
pFlow::IOfileHeader::path
fileSystem path() const
Definition: IOfileHeader.cpp:55
pFlow::IOfileHeader::IOfileHeader
IOfileHeader(const objectFile &objf, const repository *owner=nullptr)
Definition: IOfileHeader.cpp:46
pFlow::IOstream::fatalCheck
bool fatalCheck(const char *operation) const
Definition: IOstream.cpp:48
pFlow::objectFile
Definition: objectFile.hpp:33
IOfileHeader.hpp
pFlow::IOstream::name
virtual const word & name() const
Definition: IOstream.cpp:31
pFlow::IOfileHeader::inStream
uniquePtr< iFstream > inStream() const
Definition: IOfileHeader.cpp:24
pFlow::IOfileHeader::implyRead
bool implyRead() const
Definition: IOfileHeader.cpp:100
pFlow::IOfileHeader::writeSeparator
bool writeSeparator(iOstream &os) const
Definition: IOfileHeader.cpp:181
pFlow::IOfileHeader::headerOk
bool headerOk(bool silent=false)
Definition: IOfileHeader.cpp:71
pFlow::uniquePtr
Definition: uniquePtr.hpp:44
pFlow::repository
Definition: repository.hpp:34
pFlow::IOfileHeader::implyWrite
bool implyWrite() const
Definition: IOfileHeader.cpp:106
pFlow::iOstream
Definition: iOstream.hpp:53
pFlow::iOstream::writeWordEntry
iOstream & writeWordEntry(const word &key, const T &value)
Definition: iOstream.hpp:217
pFlow::IOfileHeader::writeBanner
bool writeBanner(iOstream &os) const
Definition: IOfileHeader.cpp:170