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());
28  else
29  return nullptr;
30 }
31 
33 {
34  auto osPtr = makeUnique<oFstream>(path());
35 
36  if(osPtr && owner_)
37  {
38  auto outPrecision = owner_->outFilePrecision();
39  osPtr->precision(outPrecision);
40  }
41 
42  return osPtr;
43 }
44 
46 (
47  const objectFile& objf,
48  const repository* owner
49 )
50 :
51  objectFile(objf),
52  owner_(owner)
53 {}
54 
56 {
57  fileSystem f;
58 
59  if( owner_ )
60  {
61  f = owner_->path()/localPath();
62 
63  }else
64  {
65  f = localPath();
66  }
67  f += name_;
68  return f;
69 }
70 
72 {
73  if(!fileExist())
74  {
75  if(!silent)
77  " the file "<< path() << " does not exist. \n";
78  return false;
79  }
80 
81  if( implyRead() )
82  {
83 
84  if( auto ptrIS = inStream(); ptrIS )
85  {
86  return readHeader( ptrIS(), silent );
87  }
88  else
89  {
90  if(!silent)
92  "could not open file " << path() <<endl;
93  return false;
94  }
95  }
96 
97  return true;
98 }
99 
101 {
102  if (isReadAlways()) return true;
103  return readIfPresent();
104 }
105 
107 {
108  return isWriteAlways();
109 }
110 
112 {
113  return path().exist();
114 }
115 
117 {
118  return fileExist() && isReadIfPresent();
119 }
120 
121 
122 bool pFlow::IOfileHeader::writeHeader(iOstream& os, const word& typeName) const
123 {
124 
125  writeBanner(os);
126 
127  os.writeWordEntry("objectType", typeName );
128  os.fatalCheck("writing objectType");
129 
130  os.writeWordEntry("objectName", name() );
131  os.fatalCheck("writing objectName");
132 
133  writeSeparator(os);
134  return true;
135 }
136 
138 {
139  return writeHeader(os, objectType_);
140 }
141 
143 {
144 
145  if( !is.findTokenAndNextSilent("objectName", objectName_) )
146  {
147  if(!silent)
148  {
150  "cannot find/error in reading objectName in file " <<
151  is.name()<<endl;
152  }
153  return false;
154  }
155 
156  if( !is.findTokenAndNextSilent("objectType", objectType_) )
157  {
158  if(!silent)
159  {
161  "cannot find/error in reading objectType in file "<<
162  is.name()<<endl;
163  }
164  return false;
165  }
166 
167  return true;
168 }
169 
171 {
172  os<<
173 "/* -------------------------------*- C++ -*---------------------------------- *\\ \n"<<
174 "| phasicFlow File | \n"<<
175 "| copyright: www.cemf.ir | \n"<<
176 "\\* ------------------------------------------------------------------------- */ \n \n";
177 
178  return true;
179 }
180 
182 {
183  os<< "\n" <<
184 "// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // \n \n";
185 
186  return true;
187 }
pFlow::iIstream::findTokenAndNextSilent
virtual bool findTokenAndNextSilent(const word &w, word &nextW, int32 limitLine=100)
Definition: iIstream.cpp:168
pFlow::IOfileHeader::readIfPresent
bool readIfPresent() const
Definition: IOfileHeader.cpp:116
pFlow::IOfileHeader::readHeader
bool readHeader(iIstream &is, bool silent=false)
Definition: IOfileHeader.cpp:142
warningInFunction
#define warningInFunction
Definition: error.hpp:55
pFlow::IOfileHeader::fileExist
bool fileExist() const
Definition: IOfileHeader.cpp:111
pFlow::word
std::string word
Definition: builtinTypes.hpp:63
pFlow::endl
iOstream & endl(iOstream &os)
Definition: iOstream.hpp:312
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:122
pFlow::fileSystem::path
const pathType & path() const
Definition: fileSystem.hpp:121
pFlow::IOfileHeader::path
fileSystem path() const
Definition: IOfileHeader.cpp:55
pFlow::IOfileHeader::IOfileHeader
IOfileHeader(const objectFile &objf, const repository *owner=nullptr)
Definition: IOfileHeader.cpp:46
pFlow::IOstream::fatalCheck
bool fatalCheck(const char *operation) const
Definition: IOstream.cpp:48
pFlow::objectFile
Definition: objectFile.hpp:33
IOfileHeader.hpp
pFlow::IOstream::name
virtual const word & name() const
Definition: IOstream.cpp:31
pFlow::IOfileHeader::inStream
uniquePtr< iFstream > inStream() const
Definition: IOfileHeader.cpp:24
pFlow::IOfileHeader::implyRead
bool implyRead() const
Definition: IOfileHeader.cpp:100
pFlow::IOfileHeader::writeSeparator
bool writeSeparator(iOstream &os) const
Definition: IOfileHeader.cpp:181
pFlow::IOfileHeader::headerOk
bool headerOk(bool silent=false)
Definition: IOfileHeader.cpp:71
pFlow::uniquePtr
Definition: uniquePtr.hpp:44
pFlow::repository
Definition: repository.hpp:34
pFlow::IOfileHeader::implyWrite
bool implyWrite() const
Definition: IOfileHeader.cpp:106
pFlow::iOstream
Definition: iOstream.hpp:53
pFlow::iOstream::writeWordEntry
iOstream & writeWordEntry(const word &key, const T &value)
Definition: iOstream.hpp:217
pFlow::IOfileHeader::writeBanner
bool writeBanner(iOstream &os) const
Definition: IOfileHeader.cpp:170