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 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 "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 
35 
36  auto osPtr = makeUnique<oFstream>(path(), outFileBinary());
37 
38  if(osPtr && owner_)
39  {
40  auto outPrecision = owner_->outFilePrecision();
41  osPtr->precision(outPrecision);
42  }
43 
44  return osPtr;
45 }
46 
48 (
49  const objectFile& objf,
50  const repository* owner
51 )
52 :
53  objectFile(objf),
54  owner_(owner)
55 {}
56 
58 {
59  fileSystem f;
60 
61  if( owner_ )
62  {
63  f = owner_->path()/localPath();
64 
65  }else
66  {
67  f = localPath();
68  }
69  f += name_;
70  return f;
71 }
72 
74 {
75  if(owner_)
76  return owner_->outFileBinary();
77  else
78  return false;
79 }
80 
82 {
83  return toUpper(fileFormat_) == "BINARY";
84 }
85 
87 {
88  if(!fileExist())
89  {
90  if(!silent)
92  " the file "<< path() << " does not exist. \n";
93  return false;
94  }
95 
96  if( implyRead() )
97  {
98 
99  if( auto ptrIS = inStream(); ptrIS )
100  {
101  return readHeader( ptrIS(), silent );
102  }
103  else
104  {
105  if(!silent)
107  "could not open file " << path() <<endl;
108  return false;
109  }
110  }
111 
112  return true;
113 }
114 
116 {
117  if (isReadAlways()) return true;
118  return readIfPresent();
119 }
120 
122 {
123  return isWriteAlways();
124 }
125 
127 {
128  return path().exist();
129 }
130 
132 {
133  return fileExist() && isReadIfPresent();
134 }
135 
136 
137 bool pFlow::IOfileHeader::writeHeader(iOstream& os, const word& typeName) const
138 {
139 
140  writeBanner(os);
141 
142  os.writeWordEntry("objectType", typeName );
143  os.fatalCheck("writing objectType");
144 
145  os.writeWordEntry("objectName", name() );
146  os.fatalCheck("writing objectName");
147 
148  word fileFormat;
149  if(outFileBinary())
150  fileFormat = "Binary";
151  else
152  fileFormat = "ASCII";
153 
154  os.writeWordEntry("fileFormat", fileFormat);
155  os.fatalCheck("writing fileFormat");
156 
157  writeSeparator(os);
158  return true;
159 }
160 
162 {
163  return writeHeader(os, objectType_);
164 }
165 
167 {
168 
169  if( !is.findTokenAndNextSilent("objectName", objectName_) )
170  {
171  if(!silent)
172  {
174  "cannot find/error in reading objectName in file " <<
175  is.name()<<endl;
176  }
177  return false;
178  }
179 
180  if( !is.findTokenAndNextSilent("objectType", objectType_) )
181  {
182  if(!silent)
183  {
185  "cannot find/error in reading objectType in file "<<
186  is.name()<<endl;
187  }
188  return false;
189  }
190 
191 
192  if( !is.findTokenAndNextSilent("fileFormat", fileFormat_) )
193  {
194  if(!silent)
195  {
197  "cannot find/error in reading fileFormat in file "<<
198  is.name()<<endl;
199  }
200  return false;
201  }
202 
203  return true;
204 }
205 
207 {
208  os<<
209 "/* -------------------------------*- C++ -*---------------------------------- *\\ \n"<<
210 "| phasicFlow File | \n"<<
211 "| copyright: www.cemf.ir | \n"<<
212 "\\* ------------------------------------------------------------------------- */ \n \n";
213 
214  return true;
215 }
216 
218 {
219  os<< "\n" <<
220 "// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // \n \n";
221 
222  return true;
223 }
pFlow::iIstream::findTokenAndNextSilent
virtual bool findTokenAndNextSilent(const word &w, word &nextW, int32 limitLine=100)
Definition: iIstream.cpp:176
pFlow::IOfileHeader::readIfPresent
bool readIfPresent() const
Definition: IOfileHeader.cpp:131
pFlow::IOfileHeader::outFileBinary
bool outFileBinary() const
Definition: IOfileHeader.cpp:73
pFlow::IOfileHeader::readHeader
bool readHeader(iIstream &is, bool silent=false)
Definition: IOfileHeader.cpp:166
warningInFunction
#define warningInFunction
Definition: error.hpp:55
pFlow::toUpper
word toUpper(const word &inStr)
Definition: bTypesFunctions.cpp:107
pFlow::IOfileHeader::fileExist
bool fileExist() const
Definition: IOfileHeader.cpp:126
pFlow::word
std::string word
Definition: builtinTypes.hpp:63
pFlow::endl
iOstream & endl(iOstream &os)
Add newline and flush stream.
Definition: iOstream.hpp:320
pFlow::fileSystem
Definition: fileSystem.hpp:63
repository.hpp
pFlow::IOfileHeader::outStream
uniquePtr< oFstream > outStream() const
Definition: IOfileHeader.cpp:32
pFlow::iIstream
Definition: iIstream.hpp:33
pFlow::IOfileHeader::writeHeader
bool writeHeader(iOstream &os, const word &typeName) const
Definition: IOfileHeader.cpp:137
pFlow::fileSystem::path
const pathType & path() const
Definition: fileSystem.hpp:121
pFlow::IOfileHeader::path
fileSystem path() const
Definition: IOfileHeader.cpp:57
pFlow::IOfileHeader::IOfileHeader
IOfileHeader(const objectFile &objf, const repository *owner=nullptr)
Definition: IOfileHeader.cpp:48
pFlow::IOstream::fatalCheck
bool fatalCheck(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.cpp:48
pFlow::objectFile
Definition: objectFile.hpp:33
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
Definition: IOfileHeader.cpp:115
pFlow::IOfileHeader::writeSeparator
bool writeSeparator(iOstream &os) const
Definition: IOfileHeader.cpp:217
pFlow::IOfileHeader::headerOk
bool headerOk(bool silent=false)
Definition: IOfileHeader.cpp:86
pFlow::uniquePtr
Definition: uniquePtr.hpp:44
pFlow::IOfileHeader::inFileBinary
bool inFileBinary() const
Definition: IOfileHeader.cpp:81
pFlow::repository
Definition: repository.hpp:34
pFlow::IOfileHeader::implyWrite
bool implyWrite() const
Definition: IOfileHeader.cpp:121
pFlow::iOstream
Definition: iOstream.hpp:53
pFlow::iOstream::writeWordEntry
iOstream & writeWordEntry(const word &key, const T &value)
Write a keyword/value entry.
Definition: iOstream.hpp:224
pFlow::IOfileHeader::writeBanner
bool writeBanner(iOstream &os) const
Definition: IOfileHeader.cpp:206