www.cemf.ir
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 #include "streams.hpp"
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  if(binary_)
43  inStream_ = makeUnique<std::ifstream>(
44  path.wordPath(), std::ios_base::in | std::ios_base::binary);
45  else
46  inStream_ = makeUnique<std::ifstream>(
47  path.wordPath(), std::ios_base::in);
48 
49  if( !inStream_->is_open())
50  {
51 
53  "File " << path << " cannot be opened. \n";
54  fatalExit;
55 
56  }
57 }
58 
59 
61 (
62  const fileSystem& path
63 )
64 {
65 
66  // - check if the Dir exists
67  auto dir = path.dirPath();
68 
69  if(!dir.exist())
70  {
71  dir.createDirs();
72  }
73 
74  if(binary_)
75  {
76  outStream_ = makeUnique< std::ofstream>(
77  path.wordPath(), std::ios_base::out| std::ios::binary);
78  }
79  else
80  outStream_ = makeUnique< std::ofstream>(
81  path.wordPath(), std::ios_base::out);
82 
83 
84  if(!outStream_->is_open())
85  {
87  "File " << path << " cannot be opened. \n";
88  fatalExit;
89  }
90 }
91 
93 {
94  if(inStream_)
95  {
96  inStream_.reset(nullptr);
97  }
98 
99  if(outStream_)
100  {
101  outStream_.reset(nullptr);
102  }
103 }
104 
106 (
107  const fileSystem& path,
108  bool outStream,
109  bool binary
110 )
111 :
112  inStream_(nullptr),
113  outStream_(nullptr),
114  binary_(binary)
115 {
116 
117  if(outStream)
118  {
119  openOutFile(path);
120  }else
121  {
122  openInFile(path);
123  }
124 
125 }
126 
128 {
129  return inStream_();
130 }
131 
133 {
134  return outStream_();
135 }
136 
137 
138 
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::close
void close()
Definition: fileStream.cpp:92
pFlow::fileStream::fileStream
fileStream(const fileSystem &path, bool outStream=false, bool binary=false)
Definition: fileStream.cpp:106
pFlow::fileSystem
Definition: fileSystem.hpp:63
pFlow::fileStream::outStream
std::ofstream & outStream()
Definition: fileStream.cpp:132
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
streams.hpp
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:61
pFlow::fileStream::inStream
std::ifstream & inStream()
Definition: fileStream.cpp:127
fileStream.hpp
error.hpp