www.cemf.ir
fileSystem.hpp
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 #ifndef __fileSystem_hpp__
22 #define __fileSystem_hpp__
23 
24 #include <filesystem>
25 #include "bTypes.hpp"
26 #include "List.hpp"
27 
28 
29 namespace pFlow
30 {
31 
32 // - Forward
33 class iOstream;
34 class fileSystem;
35 
36 iOstream& operator <<
37 (
38  iOstream& os,
39  const fileSystem& fs
40 );
41 
42 std::ostream& operator <<
43 (
44  std::ostream& os,
45  const fileSystem& fs
46 );
47 
48 
49 fileSystem operator /
50 (
51  const fileSystem& fs1,
52  const fileSystem& fs2
53 );
54 
55 fileSystem operator /
56 (
57  const fileSystem& fs1,
58  const word& dir2
59 );
60 
61 fileSystem operator +
62 (
63  const fileSystem& fs1,
64  const word fName
65 );
66 
72 {
73 public:
74 
75  using pathType = std::filesystem::path;
76 
77 protected:
78 
81 
83  bool isDir_;
84 
85 
87  inline static word notPermittedCharsFile = word(" ") + word("&\t\n;:?*/<>\"?\'");
88 
90  bool static validFileName(const word& name)
91  {
92  return name.find_first_of(notPermittedCharsFile)==word::npos;
93  }
94 
96  bool static checkFileName(const word& name);
97 
98 public:
99 
101  inline static fileSystem CWD()
102  {
103  return fileSystem(std::filesystem::current_path().c_str());
104  }
105 
106 public:
107 
109 
112  path_(),
113  isDir_(true)
114  {}
115 
117  explicit
118  fileSystem(const word & wPath);
119 
121  fileSystem( const word& dir, const word& file);
122 
124  fileSystem( const char* dir, const char* file="");
125 
127  explicit
128  fileSystem( const pathType& path );
129 
131  fileSystem(const fileSystem&) = default;
132 
134  fileSystem(fileSystem&&) = default;
135 
137  fileSystem& operator = (const fileSystem&) = default;
138 
140  fileSystem& operator = (fileSystem&&) = default;
141 
143  ~fileSystem() = default;
144 
146 
148  bool isDir() const
149  {
150  return isDir_;
151  }
152 
154  const pathType& path()const
155  {
156  return path_;
157  }
158 
160  word wordPath()const
161  {
162  return word(path_.c_str());
163  }
164 
166  fileSystem dirPath() const;
167 
169  word fileName() const;
170 
172  fileSystem absolute()const;
173 
174 
176  fileSystem canonical()const;
177 
179  fileSystem relative(const fileSystem& base)const;
180 
183  bool dirExist()const;
184 
186  bool exist()const;
187 
190  fileSystem createDirs()const;
191 
192 
195  void operator += (const word& fileName);
196 
199  void operator /=(const fileSystem& fs );
200 
203  friend fileSystem operator /(const fileSystem& fs1, const fileSystem& fs2 );
204 
205 
207  fileSystem operator()(bool retDir = true) const;
208 
210  friend iOstream& operator << (iOstream& os, const fileSystem& fs);
211 
212  friend std::ostream& operator << (std::ostream& os, const fileSystem& fs);
213 
214 };
215 
216 
218 
219 
221 inline fileSystem CWD()
222 {
223  return fileSystem::CWD();
224 }
225 
227 bool isDirectory(const fileSystem& path);
228 
230 bool isRegularFile(const fileSystem& path);
231 
233 fileSystemList subDirectories(const fileSystem& path);
234 
236 fileSystemList containingFiles(const fileSystem& path);
237 
238 } // pFlow
239 
240 #endif
pFlow::List
Definition: List.hpp:39
pFlow::fileSystem::canonical
fileSystem canonical() const
Canonical path of this (it should exist)
Definition: fileSystem.cpp:149
pFlow::fileSystem::operator/
friend fileSystem operator/(const fileSystem &fs1, const fileSystem &fs2)
Create a dir path from dir path of fs1 and fas2 in the form of fs1/fs2
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::CWD
static fileSystem CWD()
return current working directory
Definition: fileSystem.hpp:101
pFlow::fileSystem::exist
bool exist() const
Check if the path exists.
Definition: fileSystem.cpp:192
pFlow::fileSystem::operator/=
void operator/=(const fileSystem &fs)
It operates on dir path only Add the dir path of fs to this
Definition: fileSystem.cpp:265
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::fileSystem::operator()
fileSystem operator()(bool retDir=true) const
Return the dir path of this.
Definition: fileSystem.cpp:235
List.hpp
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::fileSystem::operator=
fileSystem & operator=(const fileSystem &)=default
Copy assignment.
pFlow::fileSystem::isDir
bool isDir() const
Is directory?
Definition: fileSystem.hpp:148
bTypes.hpp
pFlow
Definition: demGeometry.hpp:27
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
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::fileSystem::operator<<
friend iOstream & operator<<(iOstream &os, const fileSystem &fs)
pFlow::isDirectory
bool isDirectory(const fileSystem &path)
Free function to check if the path is dir path.
Definition: fileSystem.cpp:324
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
pFlow::fileSystem::checkFileName
static bool checkFileName(const word &name)
Is a valid file name?
Definition: fileSystem.cpp:27
pFlow::fileSystemList
List< fileSystem > fileSystemList
Definition: fileSystem.hpp:217
pFlow::CWD
fileSystem CWD()
Free function to reture current working directory.
Definition: fileSystem.hpp:221
pFlow::fileSystem::~fileSystem
~fileSystem()=default
Destructor.
pFlow::fileSystem::relative
fileSystem relative(const fileSystem &base) const
relative path of this this with respect to base
Definition: fileSystem.cpp:170
pFlow::fileSystem::wordPath
word wordPath() const
Path in word type.
Definition: fileSystem.hpp:160
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