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 
33 class iOstream;
34 class ostream;
35 class fileSystem;
36 
37 iOstream& operator <<
38 (
39  iOstream& os,
40  fileSystem fs
41 );
42 
43 std::ostream& operator <<
44 (
45  std::ostream& os,
46  fileSystem fs
47 );
48 
49 
50 fileSystem operator /
51 (
52  const fileSystem& fs1,
53  const fileSystem& fs2
54  );
55 
56 fileSystem operator +
57 (
58  const fileSystem& fs1,
59  const word fName
60 );
61 
62 // a class to manage file/directory names
64 {
65 protected:
66 
67  std::filesystem::path path_;
68  bool isDir_;
69 
70 
71  // protected static variables
72  inline static word notPermittedCharsFile = word(" ") + word("\t\n\0;:?*/<>\"?\'");
73 
74  // protected methods
75 
76  bool static validFileName(const word& name)
77  {
78  return name.find_first_of(notPermittedCharsFile);
79  }
80 
81  bool static checkFileName(const word& name);
82 
83 public:
84 
85  inline static fileSystem CWD()
86  {
87  return fileSystem(std::filesystem::current_path().c_str());
88  }
89 
90 public:
91 
92 
93  typedef std::filesystem::path pathType;
94 
95 
97  path_(),
98  isDir_(true)
99  {}
100  // Constructors
101  fileSystem( const word& dir, const word& file = "");
102  fileSystem( const char* dir, const char* file = "");
103  fileSystem( const pathType& path );
104 
105  fileSystem(const fileSystem&) = default;
106 
107  fileSystem(fileSystem&&) = default;
108 
109  fileSystem& operator = (const fileSystem&) = default;
110 
111  fileSystem& operator = (fileSystem&&) = default;
112 
113  ~fileSystem() = default;
114 
115  // Methods
116  bool isDir() const
117  {
118  return isDir_;
119  }
120 
121  const pathType& path()const
122  {
123  return path_;
124  }
125 
126  word wordPath()const
127  {
128  return word(path_.c_str());
129  }
130 
131  // dir path of this
132  fileSystem dirPath() const;
133 
134  // file name of this (if any)
135  word fileName() const;
136 
137  // absolute path of this
138  fileSystem absolute()const;
139 
140 
141  //fileSystem relative()const;
142 
143  // canonical path of this (it should exist)
144  fileSystem canonical()const;
145 
146  // only operate on dir path
147  // check if the dir path exists
148  bool dirExist()const;
149 
150  // check if the path exists
151  bool exist()const;
152 
153  // operate on dir path only
154  // create dir based on the path and returns the canonical path
155  fileSystem createDirs()const;
156 
157 
158  // if this is dir path, add the filename to dir path
159  // if it is file path, replace the file name
160  void operator += (const word& fileName);
161 
162  // it operates on dir path only
163  // it adds the dir path of fs to this
164  void operator /=(const fileSystem& fs );
165 
166  // Create a dir path from dir path of fs1 and fas2 in the
167  // form of fs1/fs2
168  friend fileSystem operator /(const fileSystem& fs1, const fileSystem& fs2 );
169 
170 
171  // return the dir path of this
172  fileSystem operator()(bool retDir = true) const;
173 
174 
175  friend iOstream& operator << (iOstream& os, fileSystem fs);
176 
177  friend std::ostream& operator << (std::ostream& os, fileSystem fs);
178 
179 };
180 
181 
183 
184 
185 
186 inline fileSystem CWD()
187 {
188  return fileSystem::CWD();
189 }
190 
191 bool isDirectory(const fileSystem& path);
192 
193 bool isRegularFile(const fileSystem& path);
194 
195 fileSystemList subDirectories(const fileSystem& path);
196 
197 fileSystemList containingFiles(const fileSystem& path);
198 
199 } // pFlow
200 
201 #endif
pFlow::List
Definition: List.hpp:39
pFlow::fileSystem::canonical
fileSystem canonical() const
Definition: fileSystem.cpp:140
pFlow::fileSystem::operator/
friend fileSystem operator/(const fileSystem &fs1, const fileSystem &fs2)
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::CWD
static fileSystem CWD()
Definition: fileSystem.hpp:85
pFlow::fileSystem::exist
bool exist() const
Definition: fileSystem.cpp:179
pFlow::fileSystem::operator/=
void operator/=(const fileSystem &fs)
Definition: fileSystem.cpp:250
pFlow::fileSystem::operator+=
void operator+=(const word &fileName)
Definition: fileSystem.cpp:233
pFlow::fileSystem::operator()
fileSystem operator()(bool retDir=true) const
Definition: fileSystem.cpp:223
List.hpp
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::fileSystem::operator=
fileSystem & operator=(const fileSystem &)=default
pFlow::fileSystem::isDir
bool isDir() const
Definition: fileSystem.hpp:116
bTypes.hpp
pFlow
Definition: demComponent.hpp:28
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
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::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
pFlow::fileSystemList
List< fileSystem > fileSystemList
Definition: fileSystem.hpp:182
pFlow::CWD
fileSystem CWD()
Definition: fileSystem.hpp:186
pFlow::fileSystem::~fileSystem
~fileSystem()=default
pFlow::fileSystem::wordPath
word wordPath() const
Definition: fileSystem.hpp:126
pFlow::fileSystem::operator<<
friend iOstream & operator<<(iOstream &os, fileSystem fs)
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