fileSystem.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 
22 #include "fileSystem.hpp"
23 #include "error.hpp"
24 #include "iOstream.hpp"
25 
26 
28 {
29 
30  if(!validFileName(name))
31  {
33  "Invalid file name supplied " << name <<
34  "the following characters are not allowd: " <<
36  fatalExit;
37  return false;
38  }
39 
40  return true;
41 }
42 
43 
44 
45 pFlow::fileSystem::fileSystem( const word& dir, const word& file)
46 {
47  isDir_ = file.empty();
48 
49  if( !isDir_)
50  {
51  checkFileName(file);
52  }
53 
54  try
55  {
56  path_ = pathType(dir)/file;
57  }
58  catch (std::filesystem::filesystem_error & ec)
59  {
61  "Invalid fileSystem input:" <<
62  ec.what()<<endl;
63  fatalExit;
64  }
65 
66 }
67 
68 pFlow::fileSystem::fileSystem( const char* dir, const char* file)
69 :
70  fileSystem(word(dir),word(file))
71 {
72 
73 }
74 
76 :
77  path_(path)
78 {
79  isDir_ = isDirectory(*this);
80 }
81 
83 {
84 
85  if( isDir())
86  {
87  return *this;
88  }
89  else
90  {
91  return fileSystem(path_.parent_path().c_str());
92  }
93 
94 }
95 
97 {
98  if( isDir())
99  return word("");
100 
101  try
102  {
103  return word( path_.filename());
104  }
105  catch (std::filesystem::filesystem_error & ec)
106  {
108  ec.what()<<endl;
109  fatalExit;
110  }
111 
112  return word("");
113 }
114 
116 (
117 ) const
118 {
119 
120  std::error_code ec;
121  pathType abPath;
122  if( abPath = std::filesystem::absolute( path_, ec); ec )
123  {
125  "The absolute path of "<< path_.c_str() <<" is invalid: "<<
126  ec.message()<<endl;
127  fatalExit;
128  }
129 
130  fileSystem res;
131 
132  res.path_ = abPath;
133  res.isDir_ = isDir_;
134 
135  return res;
136 
137 }
138 
140 (
141 )const
142 {
143  std::error_code ec;
144  pathType cnPath;
145  if( cnPath = std::filesystem::canonical( path_, ec); ec )
146  {
148  "The canonical path of "<< path_.c_str() <<" cannot be obtained: "<<
149  ec.message()<<endl;
150  fatalExit;
151  }
152 
153  fileSystem res;
154 
155  res.path_ = cnPath;
156  res.isDir_ = isDir_;
157 
158  return res;
159 }
160 
162 (
163 ) const
164 {
165  if(!isDir())
166  {
168  "This function only operates on dir path, the path is "<<
169  path_.c_str()<<endl;
170  fatalExit;
171  return false;
172  }
173 
174  return exist();
175 
176 }
177 
179 (
180 )const
181 {
182  try
183  {
184  return std::filesystem::exists(path_);
185  }
186  catch (std::filesystem::filesystem_error & ec)
187  {
189  ec.what()<<endl;
190  fatalExit;
191  return false;
192  }
193 }
194 
196 (
197 ) const
198 {
199  if(! isDir())
200  {
202  "This function only operates on dir path, the path is "<<
203  path_.c_str()<<endl;
204  fatalExit;
205  }
206 
207  try
208  {
209  std::filesystem::create_directories(path_);
210  return canonical();
211  }
212  catch (std::filesystem::filesystem_error & ec)
213  {
215  ec.what()<<endl;
216  fatalExit;
217  return *this;
218  }
219 }
220 
221 
222 pFlow::fileSystem pFlow::fileSystem::operator()
223 (
224  bool retDir
225 ) const
226 {
227  if(retDir)
228  return dirPath();
229  else
230  return *this;
231 }
232 
234 {
235  checkFileName(fileName);
236 
237  if( isDir())
238  {
239  path_ /= fileName;
240  isDir_ = false;
241  }
242  else
243  {
244  path_ = path_.parent_path()/fileName;
245  }
246 }
247 
248 
249 void pFlow::fileSystem::operator /=
250 (
251  const fileSystem& fs
252 )
253 {
254  if(!isDir())
255  {
257  "This operator should be used on dir path only"<<endl;
258  fatalExit;
259  }
260 
261  path_/= fs.dirPath().path_;
262 }
263 
264 pFlow::fileSystem pFlow::operator /
265 (
266  const fileSystem& fs1,
267  const fileSystem& fs2
268 )
269 {
270  fileSystem::pathType cmbPath(fs1.dirPath().path_ / fs2.dirPath().path_);
271 
272  return fileSystem( cmbPath.c_str() );
273 
274 }
275 
276 pFlow::fileSystem pFlow::operator +
277 (
278  const fileSystem& fs1,
279  const word fName
280 )
281 {
282  fileSystem path = fs1;
283  path+= fName;
284  return path;
285 }
286 
288 {
289  os << fs.path_.c_str();
290  return os;
291 }
292 
293 std::ostream& pFlow::operator << (std::ostream& os, fileSystem fs)
294 {
295  os << fs.path_.c_str();
296  return os;
297 }
298 
300 (
301  const fileSystem& path
302 )
303 {
304  return std::filesystem::is_directory(path.path());
305 }
306 
308 {
309  return std::filesystem::is_regular_file(path.path());
310 }
311 
313 (
314  const fileSystem& path
315 )
316 {
317  fileSystemList dirs;
318 
319  if( isDirectory(path) )
320  {
321  auto dOps = std::filesystem::directory_options::skip_permission_denied;
322  for( auto& subPath: std::filesystem::directory_iterator(path.path(), dOps) )
323  {
324  if(isDirectory( subPath.path() ) )
325  {
326  dirs.emplace_back(subPath.path());
327  }
328  }
329  }
330 
331  return dirs;
332 }
333 
335 (
336  const fileSystem& path
337 )
338 {
339  fileSystemList files;
340 
341  if( isDirectory(path) )
342  {
343  auto dOps = std::filesystem::directory_options::skip_permission_denied;
344  for( auto& subPath: std::filesystem::directory_iterator(path.path(), dOps) )
345  {
346  if( std::filesystem::is_regular_file(subPath.path()) )
347  {
348  files.emplace_back(subPath.path());
349  }
350  }
351 
352  }
353 
354  return files;
355 }
pFlow::List
Definition: List.hpp:39
pFlow::fileSystem::canonical
fileSystem canonical() const
Definition: fileSystem.cpp:140
fatalExit
#define fatalExit
Definition: error.hpp:57
pFlow::fileSystem::fileName
word fileName() const
Definition: fileSystem.cpp:96
pFlow::fileSystem::fileSystem
fileSystem()
Definition: fileSystem.hpp:96
pFlow::fileSystem::dirExist
bool dirExist() const
Definition: fileSystem.cpp:162
pFlow::fileSystem::exist
bool exist() const
Definition: fileSystem.cpp:179
pFlow::fileSystem::operator+=
void operator+=(const word &fileName)
Definition: fileSystem.cpp:233
pFlow::word
std::string word
Definition: builtinTypes.hpp:63
pFlow::containingFiles
fileSystemList containingFiles(const fileSystem &path)
Definition: fileSystem.cpp:335
pFlow::fileSystem::isDir_
bool isDir_
Definition: fileSystem.hpp:68
pFlow::endl
iOstream & endl(iOstream &os)
Definition: iOstream.hpp:312
fileSystem.hpp
pFlow::isRegularFile
bool isRegularFile(const fileSystem &path)
Definition: fileSystem.cpp:307
pFlow::fileSystem
Definition: fileSystem.hpp:63
pFlow::subDirectories
fileSystemList subDirectories(const fileSystem &path)
Definition: fileSystem.cpp:313
pFlow::fileSystem::notPermittedCharsFile
static word notPermittedCharsFile
Definition: fileSystem.hpp:72
fatalErrorInFunction
#define fatalErrorInFunction
Definition: error.hpp:42
pFlow::fileSystem::path
const pathType & path() const
Definition: fileSystem.hpp:121
pFlow::fileSystem::dirPath
fileSystem dirPath() const
Definition: fileSystem.cpp:82
pFlow::fileSystem::path_
std::filesystem::path path_
Definition: fileSystem.hpp:67
pFlow::isDirectory
bool isDirectory(const fileSystem &path)
Definition: fileSystem.cpp:300
pFlow::operator<<
INLINE_FUNCTION iOstream & operator<<(iOstream &str, const AB3History &ab3)
Definition: AdamsBashforth3.hpp:57
pFlow::fileSystem::absolute
fileSystem absolute() const
Definition: fileSystem.cpp:116
pFlow::fileSystem::validFileName
static bool validFileName(const word &name)
Definition: fileSystem.hpp:76
pFlow::fileSystem::checkFileName
static bool checkFileName(const word &name)
Definition: fileSystem.cpp:27
iOstream.hpp
pFlow::fileSystem::pathType
std::filesystem::path pathType
Definition: fileSystem.hpp:93
pFlow::fileSystem::createDirs
fileSystem createDirs() const
Definition: fileSystem.cpp:196
pFlow::iOstream
Definition: iOstream.hpp:53
error.hpp