www.cemf.ir
IOfileHeader.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 distribute+d 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 "IOfileHeader.hpp"
22 #include "repository.hpp"
23 
25 {
26  if( fileExist() )
27  return makeUnique<iFstream>(path(), inFileBinary());
28  else
29  return nullptr;
30 }
31 
33 {
34  auto osPtr = makeUnique<oFstream>(path(), outFileBinary());
35 
36  if(osPtr && owner())
37  {
38  auto outPrecision = owner()->outFilePrecision();
39  osPtr->precision(static_cast<int>(outPrecision));
40  }
41 
42  return osPtr;
43 }
44 
46 {
47  auto osPtr = makeUnique<oFstream>( CWD()+word("dummyFile") , outFileBinary());
48 
49  if(osPtr && owner())
50  {
51  auto outPrecision = owner()->outFilePrecision();
52  osPtr->precision(static_cast<int>(outPrecision));
53  }
54 
55  return osPtr;
56 }
57 
59  const objectFile &objf)
60  : objectFile(objf)
61 {}
62 
64 {
65  fileSystem f;
66 
67  if( owner() )
68  {
69  f = owner()->path()/localPath();
70 
71  }else
72  {
73  f = localPath();
74  }
75  f += name();
76  return f;
77 }
78 
80 {
81  if(owner())
82  return owner()->outFileBinary();
83  else
84  return false;
85 }
86 
88 {
89  return toUpper(fileFormat_) == "BINARY";
90 }
91 
93 {
94  if(!fileExist())
95  {
96  if(!silent)
98  " the file "<< path() << " does not exist. \n";
99  return false;
100  }
101 
102  if( implyRead() )
103  {
104 
105  if( auto ptrIS = inStream(); ptrIS )
106  {
107  return readHeader( ptrIS(), silent );
108  }
109  else
110  {
111  if(!silent)
113  "could not open file " << path() <<endl;
114  return false;
115  }
116  }
117 
118  return true;
119 }
120 
122 {
123  if (isReadAlways()) return true;
124  return readIfPresent();
125 }
126 
128 {
129  if( isExcluded( name() ) ) return false;
130  if( isIncluded( name() ) ) return true;
131  return isWriteAlways();
132 }
133 
135 {
136  return path().exist();
137 }
138 
140 {
141  return fileExist() && isReadIfPresent();
142 }
143 
145 {
146 
147  if( !this->readWriteHeader() ) return false;
148  if( !implyWrite() ) return false;
149 
150  return true;
151 }
152 
154 (
155  iOstream& os,
156  const word& typeName,
157  bool forceWrite
158 ) const
159 {
160 
161  if(!forceWrite && !writeHeader()) return true;
162 
163  writeBanner(os);
164 
165  os.writeWordEntry("objectType", typeName );
166  os.fatalCheck("writing objectType");
167 
168  os.writeWordEntry("objectName", name() );
169  os.fatalCheck("writing objectName");
170 
171  word fileFormat;
172  if(outFileBinary())
173  fileFormat = "Binary";
174  else
175  fileFormat = "ASCII";
176 
177  os.writeWordEntry("fileFormat", fileFormat);
178  os.fatalCheck("writing fileFormat");
179 
180  writeSeparator(os);
181  return true;
182 }
183 
184 bool pFlow::IOfileHeader::writeHeader(iOstream& os, bool forceWrite) const
185 {
186  return writeHeader(os, objectType_, forceWrite);
187 }
188 
190 {
191  if( !implyRead())return false;
192  if( !this->readWriteHeader() ) return false;
193  return true;
194 }
195 
197 {
198 
199  if(!readHeader()) return true;
200 
201  if( !is.findTokenAndNextSilent("objectName", objectName_) )
202  {
203  if(!silent)
204  {
206  "cannot find/error in reading objectName in file " <<
207  is.name()<<endl;
208  }
209  return false;
210  }
211 
212  if( !is.findTokenAndNextSilent("objectType", objectType_) )
213  {
214  if(!silent)
215  {
217  "cannot find/error in reading objectType in file "<<
218  is.name()<<endl;
219  }
220  return false;
221  }
222 
223 
224  if( !is.findTokenAndNextSilent("fileFormat", fileFormat_) )
225  {
226  if(!silent)
227  {
229  "cannot find/error in reading fileFormat in file "<<
230  is.name()<<endl;
231  }
232  return false;
233  }
234 
235  return true;
236 }
237 
239 {
240  os<<
241 "/* -------------------------------*- C++ -*---------------------------------- *\\ \n"<<
242 "| phasicFlow File | \n"<<
243 "| copyright: www.cemf.ir | \n"<<
244 "\\* ------------------------------------------------------------------------- */ \n \n";
245 
246  return true;
247 }
248 
250 {
251  os<< "\n" <<
252 "// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // \n \n";
253 
254  return true;
255 }
pFlow::iIstream::findTokenAndNextSilent
virtual bool findTokenAndNextSilent(const word &w, word &nextW, int32 limitLine=100)
Definition: iIstream.cpp:203
pFlow::IOfileHeader::readIfPresent
bool readIfPresent() const
Check read if present.
Definition: IOfileHeader.cpp:139
pFlow::IOfileHeader::outFileBinary
bool outFileBinary() const
Definition: IOfileHeader.cpp:79
warningInFunction
#define warningInFunction
Report a warning.
Definition: error.hpp:95
pFlow::toUpper
word toUpper(const word &inStr)
convert a word to all caps
Definition: bTypesFunctions.cpp:43
pFlow::IOfileHeader::fileExist
bool fileExist() const
Check if file exists.
Definition: IOfileHeader.cpp:134
pFlow::word
std::string word
Definition: builtinTypes.hpp:64
pFlow::IOfileHeader::writeHeader
bool writeHeader() const
Check if the header should be written to file True: on master + implyWrite + readWriteHeader = true F...
Definition: IOfileHeader.cpp:144
pFlow::endl
iOstream & endl(iOstream &os)
Add newline and flush stream.
Definition: iOstream.hpp:341
pFlow::fileSystem
Manages file pathes, manupulate and combines them.
Definition: fileSystem.hpp:71
repository.hpp
pFlow::IOfileHeader::outStream
uniquePtr< oFstream > outStream() const
Definition: IOfileHeader.cpp:32
pFlow::iIstream
Interface class for any input stream
Definition: iIstream.hpp:37
pFlow::fileSystem::path
const pathType & path() const
Const access to path.
Definition: fileSystem.hpp:154
pFlow::IOfileHeader::path
fileSystem path() const
Definition: IOfileHeader.cpp:63
pFlow::IOstream::fatalCheck
bool fatalCheck(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.cpp:48
pFlow::objectFile
Definition: objectFile.hpp:30
IOfileHeader.hpp
pFlow::IOstream::name
virtual const word & name() const
Return the name of the stream.
Definition: IOstream.cpp:31
pFlow::IOfileHeader::inStream
uniquePtr< iFstream > inStream() const
Definition: IOfileHeader.cpp:24
pFlow::IOfileHeader::implyRead
bool implyRead() const
Imply read.
Definition: IOfileHeader.cpp:121
pFlow::IOfileHeader::writeSeparator
bool writeSeparator(iOstream &os) const
wirte a separator line
Definition: IOfileHeader.cpp:249
pFlow::IOfileHeader::headerOk
bool headerOk(bool silent=false)
Definition: IOfileHeader.cpp:92
pFlow::uniquePtr
Definition: uniquePtr.hpp:42
pFlow::IOfileHeader::readHeader
bool readHeader() const
Check if header should be read from file.
Definition: IOfileHeader.cpp:189
pFlow::IOfileHeader::IOfileHeader
IOfileHeader(const objectFile &objf)
Definition: IOfileHeader.cpp:58
pFlow::CWD
fileSystem CWD()
Free function to reture current working directory.
Definition: fileSystem.hpp:221
pFlow::IOfileHeader::inFileBinary
bool inFileBinary() const
Definition: IOfileHeader.cpp:87
pFlow::IOfileHeader::dummyOutStream
uniquePtr< oFstream > dummyOutStream() const
Definition: IOfileHeader.cpp:45
pFlow::IOfileHeader::implyWrite
bool implyWrite() const
Imply write.
Definition: IOfileHeader.cpp:127
pFlow::iOstream
Interface class for any output stream.
Definition: iOstream.hpp:59
pFlow::iOstream::writeWordEntry
iOstream & writeWordEntry(const word &key, const T &value)
Write a keyword/value entry.
Definition: iOstream.hpp:239
pFlow::IOfileHeader::writeBanner
bool writeBanner(iOstream &os) const
write the banner
Definition: IOfileHeader.cpp:238