# PhasicFlow: src/phasicFlow/streams/Fstream/fileStream.cpp Source File

fileStream.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 "fileStream.hpp"
#include "error.hpp"
void pFlow::fileStream::openInFile
(
const fileSystem& path
)
{
if( !path.exist() )
{
fatalErrorInFunction <<
"File " << path << " does not exist for opening. \n";
fatalExit;
}
inStream_ = makeUnique<std::ifstream>( path.wordPath(), std::ios_base::in);
if( !inStream_->is_open())
{
fatalErrorInFunction <<
"File " << path << " cannot be opened. \n";
fatalExit;
}
}
void pFlow::fileStream::openOutFile
(
const fileSystem& path
)
{
// - check if the Dir exists
auto dir = path.dirPath();
if(!dir.exist())
{
dir.createDirs();
}
outStream_ = makeUnique< std::ofstream>(path.wordPath(), std::ios_base::out);
if(!outStream_->is_open())
{
fatalErrorInFunction <<
"File " << path << " cannot be opened. \n";
fatalExit;
}
}
void pFlow::fileStream::close()
{
if(inStream_)
{
inStream_.reset(nullptr);
}
if(outStream_)
{
outStream_.reset(nullptr);
}
}
pFlow::fileStream::fileStream
(
const fileSystem& path,
bool outStream
)
:
inStream_(nullptr),
outStream_(nullptr)
{
if(outStream)
{
openOutFile(path);
}else
{
openInFile(path);
}
}
std::ifstream& pFlow::fileStream::inStream()
{
return inStream_();
}
std::ofstream& pFlow::fileStream::outStream()
{
return outStream_();
}
pFlow::fileStream::outStream_
uniquePtr< std::ofstream > outStream_
Definition: fileStream.hpp:44
fatalExit
#define fatalExit
Definition: error.hpp:57
pFlow::fileSystem::exist
bool exist() const
Definition: fileSystem.cpp:179
pFlow::fileStream::openInFile
void openInFile(const fileSystem &path)
Definition: fileStream.cpp:30
pFlow::fileStream::fileStream
fileStream(const fileSystem &path, bool outStream=false)
Definition: fileStream.cpp:93
pFlow::fileStream::close
void close()
Definition: fileStream.cpp:79
pFlow::fileSystem
Definition: fileSystem.hpp:63
pFlow::fileStream::outStream
std::ofstream & outStream()
Definition: fileStream.cpp:117
fatalErrorInFunction
#define fatalErrorInFunction
Definition: error.hpp:42
pFlow::fileSystem::dirPath
fileSystem dirPath() const
Definition: fileSystem.cpp:82
pFlow::fileStream::inStream_
uniquePtr< std::ifstream > inStream_
Definition: fileStream.hpp:41
pFlow::fileSystem::wordPath
word wordPath() const
Definition: fileSystem.hpp:126
pFlow::fileSystem::createDirs
fileSystem createDirs() const
Definition: fileSystem.cpp:196
pFlow::fileStream::openOutFile
void openOutFile(const fileSystem &path)
Definition: fileStream.cpp:56
pFlow::fileStream::inStream
std::ifstream & inStream()
Definition: fileStream.cpp:112
fileStream.hpp
error.hpp