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 
21 #include "fileStream.hpp"
22 #include "error.hpp"
23 
24 #include "streams.hpp"
25 
27 (
28  const fileSystem& path
29 )
30 {
31 
32  if( !path.exist() )
33  {
35  "File " << path << " does not exist for opening. \n";
36  fatalExit;
37  }
38 
39  if(binary_)
40  inStream_ = makeUnique<std::ifstream>(
41  path.wordPath(), std::ios_base::in | std::ios_base::binary);
42  else
43  inStream_ = makeUnique<std::ifstream>(
44  path.wordPath(), std::ios_base::in);
45 
46  if( !inStream_->is_open())
47  {
48 
50  "File " << path << " cannot be opened. \n";
51  fatalExit;
52 
53  }
54 }
55 
56 
58 (
59  const fileSystem& path
60 )
61 {
62 
63  // - check if the Dir exists
64  auto dir = path.dirPath();
65 
66  if(!dir.exist())
67  {
68  dir.createDirs();
69  }
70 
71  if(binary_)
72  {
73  if(append_)
74  {
75  outStream_ = makeUnique< std::ofstream>(
76  path.wordPath(), std::ios_base::out| std::ios::binary|std::ios::app);
77  }
78  else
79  {
80  outStream_ = makeUnique< std::ofstream>(
81  path.wordPath(), std::ios_base::out| std::ios::binary);
82  }
83  }
84  else
85  {
86  if(append_)
87  {
88  outStream_ = makeUnique< std::ofstream>(
89  path.wordPath(), std::ios_base::out|std::ios::app);
90  }
91  else
92  {
93  outStream_ = makeUnique< std::ofstream>(
94  path.wordPath(), std::ios_base::out);
95  }
96  }
97 
98  if(!outStream_->is_open())
99  {
101  "File " << path << " cannot be opened. \n";
102  fatalExit;
103  }
104 }
105 
107 {
108  if(inStream_)
109  {
110  inStream_.reset(nullptr);
111  }
112 
113  if(outStream_)
114  {
115  outStream_.reset(nullptr);
116  }
117 }
118 
120 (
121  const fileSystem& path,
122  bool outStream,
123  bool binary,
124  bool append
125 )
126 :
127  inStream_(nullptr),
128  outStream_(nullptr),
129  binary_(binary),
130  append_(append)
131 {
132 
133  if(outStream)
134  {
135  openOutFile(path);
136  }else
137  {
138  openInFile(path);
139  }
140 
141 }
142 
144 {
145  return inStream_();
146 }
147 
149 {
150  return outStream_();
151 }
pFlow::fileStream::outStream_
uniquePtr< std::ofstream > outStream_
out file stream
Definition: fileStream.hpp:51
fatalExit
#define fatalExit
Fatal exit.
Definition: error.hpp:98
pFlow::fileSystem::exist
bool exist() const
Check if the path exists.
Definition: fileSystem.cpp:192
pFlow::fileStream::openInFile
void openInFile(const fileSystem &path)
open input file
Definition: fileStream.cpp:27
pFlow::fileStream::close
void close()
close streams
Definition: fileStream.cpp:106
pFlow::fileSystem
Manages file pathes, manupulate and combines them.
Definition: fileSystem.hpp:71
pFlow::fileStream::outStream
std::ofstream & outStream()
Access output file stream.
Definition: fileStream.cpp:148
fatalErrorInFunction
#define fatalErrorInFunction
Report a fatal error and function name and exit the application.
Definition: error.hpp:77
pFlow::fileSystem::dirPath
fileSystem dirPath() const
Dir part of the path.
Definition: fileSystem.cpp:91
pFlow::fileStream::inStream_
uniquePtr< std::ifstream > inStream_
in file stream
Definition: fileStream.hpp:48
streams.hpp
pFlow::fileStream::fileStream
fileStream(const fileSystem &path, bool outStream=false, bool binary=false, bool append=false)
From file path and input type and format.
Definition: fileStream.cpp:120
pFlow::fileSystem::wordPath
word wordPath() const
Path in word type.
Definition: fileSystem.hpp:160
pFlow::fileSystem::createDirs
fileSystem createDirs() const
Operate on dir path only Create dir based on the path and returns the canonical path.
Definition: fileSystem.cpp:208
pFlow::fileStream::openOutFile
void openOutFile(const fileSystem &path)
open output file
Definition: fileStream.cpp:58
pFlow::fileStream::inStream
std::ifstream & inStream()
Access input file stream.
Definition: fileStream.cpp:143
fileStream.hpp
error.hpp