www.cemf.ir
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 "streams.hpp"
25 
26 
28 {
29 
30  if(!validFileName(name))
31  {
33  "Invalid file name supplied " << name <<
34  "the following characters are not allowd: " <<
36  return false;
37  }
38 
39  return true;
40 }
41 
42 
45 :
46  path_(wPath),
47  isDir_(std::filesystem::is_directory(path_))
48 {
49  if( !isDir_ && !checkFileName(fileName()))
50  {
51  fatalExit;
52  }
53 }
54 
55 pFlow::fileSystem::fileSystem( const word& dir, const word& file)
56 {
57  isDir_ = file.empty();
58 
59  if( !isDir_ && !checkFileName(file))
60  {
61  fatalExit;
62  }
63 
64  try
65  {
66  path_ = pathType(dir)/file;
67  }
68  catch (std::filesystem::filesystem_error & ec)
69  {
71  "Invalid fileSystem input:" <<
72  ec.what()<<endl;
73  fatalExit;
74  }
75 
76 }
77 
78 pFlow::fileSystem::fileSystem( const char* dir, const char* file)
79 :
80  fileSystem(word(dir),word(file))
81 {
82 
83 }
84 
86 :
87  path_(path),
88  isDir_(std::filesystem::is_directory(path_))
89 {}
90 
92 {
93 
94  if( isDir())
95  {
96  return *this;
97  }
98  else
99  {
100  return fileSystem(path_.parent_path().c_str());
101  }
102 
103 }
104 
106 {
107  if( isDir())
108  return word("");
109 
110  try
111  {
112  return word( path_.filename());
113  }
114  catch (std::filesystem::filesystem_error & ec)
115  {
117  ec.what()<<endl;
118  fatalExit;
119  }
120 
121  return word("");
122 }
123 
125 (
126 ) const
127 {
128 
129  std::error_code ec;
130  pathType abPath;
131  if( abPath = std::filesystem::absolute( path_, ec); ec )
132  {
134  "The absolute path of "<< path_.c_str() <<" is invalid: "<<
135  ec.message()<<endl;
136  fatalExit;
137  }
138 
139  fileSystem res;
140 
141  res.path_ = abPath;
142  res.isDir_ = isDir_;
143 
144  return res;
145 
146 }
147 
149 (
150 )const
151 {
152  std::error_code ec;
153  pathType cnPath;
154  if( cnPath = std::filesystem::canonical( path_, ec); ec )
155  {
157  "The canonical path of "<< path_.c_str() <<" cannot be obtained: "<<
158  ec.message()<<endl;
159  fatalExit;
160  }
161 
162  fileSystem res;
163 
164  res.path_ = cnPath;
165  res.isDir_ = isDir_;
166 
167  return res;
168 }
169 
171 {
172  return fileSystem( std::filesystem::relative(path_, base.path_));
173 }
174 
176 (
177 ) const
178 {
179  if(!isDir())
180  {
182  "This function only operates on dir path, the path is "<<
183  path_.c_str()<<endl;
184  fatalExit;
185  return false;
186  }
187 
188  return exist();
189 
190 }
191 
193 {
194  try
195  {
196  return std::filesystem::exists(path_);
197  }
198  catch (std::filesystem::filesystem_error & ec)
199  {
201  ec.what()<<endl;
202  fatalExit;
203  return false;
204  }
205 }
206 
208 (
209 ) const
210 {
211  if(! isDir())
212  {
214  "This function only operates on dir path, the path is "<<
215  path_.c_str()<<endl;
216  fatalExit;
217  }
218 
219  try
220  {
221  std::filesystem::create_directories(path_);
222  return canonical();
223  }
224  catch (std::filesystem::filesystem_error & ec)
225  {
227  ec.what()<<endl;
228  fatalExit;
229  return *this;
230  }
231 }
232 
233 
234 pFlow::fileSystem pFlow::fileSystem::operator()
235 (
236  bool retDir
237 ) const
238 {
239  if(retDir)
240  return dirPath();
241  else
242  return *this;
243 }
244 
246 {
247  if(!checkFileName(fileName))
248  {
249  fatalExit;
250  }
251 
252  if( isDir())
253  {
254  path_ /= fileName;
255  isDir_ = false;
256  }
257  else
258  {
259  path_ = path_.parent_path()/fileName;
260  }
261 }
262 
263 
264 void pFlow::fileSystem::operator /=
265 (
266  const fileSystem& fs
267 )
268 {
269  if(!isDir())
270  {
272  "This operator should be used on dir path only"<<endl;
273  fatalExit;
274  }
275 
276  path_/= fs.dirPath().path_;
277 }
278 
279 pFlow::fileSystem pFlow::operator /
280 (
281  const fileSystem& fs1,
282  const fileSystem& fs2
283 )
284 {
285  fileSystem::pathType cmbPath(fs1.dirPath().path_ / fs2.path_);
286 
287  return fileSystem( cmbPath.c_str() );
288 
289 }
290 
291 pFlow::fileSystem pFlow::operator /
292 (
293  const fileSystem& fs1,
294  const word& dir2
295 )
296 {
297  return fs1/fileSystem(dir2, "");
298 }
299 
300 pFlow::fileSystem pFlow::operator +
301 (
302  const fileSystem& fs1,
303  const word fName
304 )
305 {
306  fileSystem path = fs1;
307  path+= fName;
308  return path;
309 }
310 
312 {
313  os << fs.path_.c_str();
314  return os;
315 }
316 
317 std::ostream& pFlow::operator << (std::ostream& os, const fileSystem& fs)
318 {
319  os << fs.path_.c_str();
320  return os;
321 }
322 
324 (
325  const fileSystem& path
326 )
327 {
328  return std::filesystem::is_directory(path.path());
329 }
330 
332 {
333  return std::filesystem::is_regular_file(path.path());
334 }
335 
337 (
338  const fileSystem& path
339 )
340 {
341  fileSystemList dirs;
342 
343  if( isDirectory(path) )
344  {
345  auto dOps = std::filesystem::directory_options::skip_permission_denied;
346  for( auto& subPath: std::filesystem::directory_iterator(path.path(), dOps) )
347  {
348  if(isDirectory( fileSystem(subPath.path()) ) )
349  {
350  dirs.emplace_back(subPath.path());
351  }
352  }
353  }
354 
355  return dirs;
356 }
357 
359 (
360  const fileSystem& path
361 )
362 {
363  fileSystemList files;
364 
365  if( isDirectory(path) )
366  {
367  auto dOps = std::filesystem::directory_options::skip_permission_denied;
368  for( auto& subPath: std::filesystem::directory_iterator(path.path(), dOps) )
369  {
370  if( std::filesystem::is_regular_file(subPath.path()) )
371  {
372  files.emplace_back(subPath.path());
373  }
374  }
375 
376  }
377 
378  return files;
379 }
pFlow::List
Definition: List.hpp:39
pFlow::fileSystem::canonical
fileSystem canonical() const
Canonical path of this (it should exist)
Definition: fileSystem.cpp:149
fatalExit
#define fatalExit
Fatal exit.
Definition: error.hpp:98
pFlow::fileSystem::fileName
word fileName() const
File name part of the path (if any)
Definition: fileSystem.cpp:105
pFlow::fileSystem::fileSystem
fileSystem()
Default.
Definition: fileSystem.hpp:111
pFlow::fileSystem::dirExist
bool dirExist() const
Only operate on dir path Check if the dir path exists.
Definition: fileSystem.cpp:176
pFlow::fileSystem::exist
bool exist() const
Check if the path exists.
Definition: fileSystem.cpp:192
pFlow::fileSystem::operator+=
void operator+=(const word &fileName)
If this is dir path, add the filename to dir path if it is file path, replace the file name
Definition: fileSystem.cpp:245
pFlow::word
std::string word
Definition: builtinTypes.hpp:64
pFlow::containingFiles
fileSystemList containingFiles(const fileSystem &path)
A list of file paths that exist in the path.
Definition: fileSystem.cpp:359
pFlow::fileSystem::isDir_
bool isDir_
Is this a directory path?
Definition: fileSystem.hpp:83
pFlow::endl
iOstream & endl(iOstream &os)
Add newline and flush stream.
Definition: iOstream.hpp:341
fileSystem.hpp
pFlow::isRegularFile
bool isRegularFile(const fileSystem &path)
free function to check if the path is regular file
Definition: fileSystem.cpp:331
pFlow::fileSystem
Manages file pathes, manupulate and combines them.
Definition: fileSystem.hpp:71
pFlow::subDirectories
fileSystemList subDirectories(const fileSystem &path)
A list of sub-directories that exist in path.
Definition: fileSystem.cpp:337
pFlow::fileSystem::path_
pathType path_
File path.
Definition: fileSystem.hpp:80
pFlow::fileSystem::notPermittedCharsFile
static word notPermittedCharsFile
Not premitted chars in file name
Definition: fileSystem.hpp:87
fatalErrorInFunction
#define fatalErrorInFunction
Report a fatal error and function name and exit the application.
Definition: error.hpp:77
pFlow::fileSystem::path
const pathType & path() const
Const access to path.
Definition: fileSystem.hpp:154
pFlow::fileSystem::dirPath
fileSystem dirPath() const
Dir part of the path.
Definition: fileSystem.cpp:91
pFlow::isDirectory
bool isDirectory(const fileSystem &path)
Free function to check if the path is dir path.
Definition: fileSystem.cpp:324
pFlow::operator<<
INLINE_FUNCTION iOstream & operator<<(iOstream &str, const AB3History &ab3)
Definition: AdamsBashforth3.hpp:57
pFlow::fileSystem::absolute
fileSystem absolute() const
Absolute path of this.
Definition: fileSystem.cpp:125
pFlow::fileSystem::validFileName
static bool validFileName(const word &name)
Is name is valid for a file?
Definition: fileSystem.hpp:90
streams.hpp
pFlow::fileSystem::checkFileName
static bool checkFileName(const word &name)
Is a valid file name?
Definition: fileSystem.cpp:27
pFlow::fileSystem::relative
fileSystem relative(const fileSystem &base) const
relative path of this this with respect to base
Definition: fileSystem.cpp:170
pFlow::fileSystem::pathType
std::filesystem::path pathType
Definition: fileSystem.hpp:75
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::iOstream
Interface class for any output stream.
Definition: iOstream.hpp:59
error.hpp