fileStream.cpp
Go to the documentation of this file.
1 /*------------------------------- phasicFlow ---------------------------------
2  O C enter of
3  O O E ngineering and
4  O O M ultiscale modeling of
5  OOOOOOO F luid flow
6 ------------------------------------------------------------------------------
7  Copyright (C): www.cemf.ir
8  email: hamid.r.norouzi AT gmail.com
9 ------------------------------------------------------------------------------
10 Licence:
11  This file is part of phasicFlow code. It is a free software for simulating
12  granular and multiphase flows. You can redistribute it and/or modify it under
13  the terms of GNU General Public License v3 or any other later versions.
14 
15  phasicFlow is distributed to help others in their research in the field of
16  granular and multiphase flows, but WITHOUT ANY WARRANTY; without even the
17  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 
19 -----------------------------------------------------------------------------*/
20 // based on OpenFOAM stream, with some modifications/simplifications
21 // to be tailored to our needs
22 
23 
24 #include "fileStream.hpp"
25 #include "error.hpp"
26 
27 
28 
30 (
31  const fileSystem& path
32 )
33 {
34 
35  if( !path.exist() )
36  {
38  "File " << path << " does not exist for opening. \n";
39  fatalExit;
40  }
41 
42  inStream_ = makeUnique<std::ifstream>( path.wordPath(), std::ios_base::in);
43 
44  if( !inStream_->is_open())
45  {
46 
48  "File " << path << " cannot be opened. \n";
49  fatalExit;
50 
51  }
52 }
53 
54 
56 (
57  const fileSystem& path
58 )
59 {
60 
61  // - check if the Dir exists
62  auto dir = path.dirPath();
63 
64  if(!dir.exist())
65  {
66  dir.createDirs();
67  }
68 
69  outStream_ = makeUnique< std::ofstream>(path.wordPath(), std::ios_base::out);
70 
71  if(!outStream_->is_open())
72  {
74  "File " << path << " cannot be opened. \n";
75  fatalExit;
76  }
77 }
78 
80 {
81  if(inStream_)
82  {
83  inStream_.reset(nullptr);
84  }
85 
86  if(outStream_)
87  {
88  outStream_.reset(nullptr);
89  }
90 }
91 
93 (
94  const fileSystem& path,
95  bool outStream
96 )
97 :
98  inStream_(nullptr),
99  outStream_(nullptr)
100 {
101 
102  if(outStream)
103  {
104  openOutFile(path);
105  }else
106  {
107  openInFile(path);
108  }
109 
110 }
111 
113 {
114  return inStream_();
115 }
116 
118 {
119  return outStream_();
120 }
121 
122 
123 
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