C++ mpi module for stochmagnet_main Package
CORE_IO.h
1 #ifndef CORE_IO_H
2 #define CORE_IO_H
3 
4 
5 //base classe header
6 #include "CORE_Object.h"
7 
8 //include the iostream
9 #include <filesystem>
10 
11 //include vector
12 #include <vector>
13 
14 //strings functions header
15 #include "functions_string.h"
16 
23 class CORE_IO : public CORE_Object {
24  // ATTRIBUTES
25 
26  public:
27 
28 
29 private:
30  // ASSOCIATIONS
31 
32 
33  // METHODS
34 
35 
36 public:
37  // CONSTRUCTORS
40  CORE_IO() {
41  }
42 
43 
44 
45 
46  // DESTRUCTORS
50  virtual ~CORE_IO(void) {
51  }
52 
53 
54 public:
55 
56 
57 private:
58 
59 
60 public:
61 
69  virtual tMemSize getMemorySize() const override {
70  return sizeof(*this)+getContentsMemorySize();
71  }
72 
77  inline static tBoolean IsFile(const tString& f) {
78  return std::filesystem::is_regular_file(f);
79  }
84  inline static tBoolean IsPath(const tString& p) {
85  return std::filesystem::is_directory(p);
86  }
91  inline static tBoolean Exists(const tString& f) {
92  return std::filesystem::exists(f);
93  }
94 
99  inline static tBoolean CreatePath(const tString& path) {
100  std::filesystem::path p(path);
101  return std::filesystem::create_directories(p);
102  }
107  inline static tBoolean Rename(const tString& file, const tString& newFile) {
108  if (Exists(file)) {
109  std::filesystem::rename(file,newFile);
110  return true;
111  }
112  return false;
113  }
114 
120  inline static tBoolean RemovePath(const tString& path,const tBoolean& force) {
121  try {
122  if (force) {
123  return (std::filesystem::remove_all(path)!=0);
124  } else {
125  return std::filesystem::remove(path);
126  }
127  } catch(std::exception& e) {
128  std::cerr<<e.what()<<"\n";
129  return false;
130  }
131  }
136  inline static tBoolean RemovePath(const tString& path) {
137  return RemovePath(path,false);
138  }
139 
144  inline static tBoolean RemoveFile(const tString& file) {
145  return std::filesystem::remove(file);
146  }
147 
151  inline static void RemoveAllFiles(const tString& path) {
152  std::vector<tString> files;
153  //get all the files
154  GetFiles(path,files);
155  for_each(files.begin(),files.end(),[&](const auto& file) {
156  std::filesystem::remove(file);
157  });
158  }
163  inline static void RemoveAllFiles(const tString& path,const tString& exts) {
164  std::vector<tString> files;
165  //get all the files
166  GetFiles(path,exts,files);
167  for_each(files.begin(),files.end(),[&](const auto& file) {
168  std::filesystem::remove(file);
169  });
170  }
171 
176  inline static tString GetSystemPath(const tString& posix_path) {
177  return std::filesystem::path(posix_path).make_preferred().string();
178  }
179 
184  inline static void GetPaths(const tString& path,std::vector<tString>& paths) {
185  paths.clear();
186  if (std::filesystem::exists(path)) {
187  for (const auto & entry : std::filesystem::directory_iterator(path))
188  if (std::filesystem::is_directory(entry.path())) paths.push_back(entry.path());
189  }
190  }
195  inline static tString GetPathName(const tString& aPath) {
196  std::filesystem::path p(aPath);
197  return p.filename();
198 
199  }
204  inline static void GetFiles(const tString& path,std::vector<tString>& files) {
205  files.clear();
206  if (std::filesystem::exists(path)) {
207  for (const auto & entry : std::filesystem::directory_iterator(path))
208  if (std::filesystem::is_regular_file(entry.path())) files.push_back(entry.path());
209  }
210  }
216  inline static void GetFiles(const tString& path,const tString& exts,std::vector<tString>& files) {
217  files.clear();
218  if (std::filesystem::exists(path)) {
219  tString ext;
220  for (const auto & entry : std::filesystem::directory_iterator(path)) {
221  ext=GetExtension(entry.path());
222  functions_string::toLower(ext);
223  if ( (ext.compare("")!=0) &&
224  (exts.find(ext)!=tString::npos) &&
225  (std::filesystem::is_regular_file(entry.path()))) files.push_back(entry.path());
226  }
227  }
228  }
235  inline static void GetFiles(const tString& path,const tString& prefix,const tString& exts,std::vector<tString>& files) {
236  files.clear();
237  if (std::filesystem::exists(path)) {
238  tString ext;
239  for (const auto & entry : std::filesystem::directory_iterator(path)) {
240  ext=GetExtension(entry.path());
241  functions_string::toLower(ext);
242  if ( (ext.compare("")!=0) &&
243  (exts.find(ext)!=tString::npos) &&
244  (std::filesystem::is_regular_file(entry.path())) &&
245  (entry.path().filename().string().find(prefix)==0)
246  ) files.push_back(entry.path().filename());
247  }
248  }
249  }
250 
258  inline static tBoolean CopyFiles(const tString& sourcePath,const tString& exts,
259  const tString& destPath,
260  const tBoolean& overwrite) {
261 
262  if (std::filesystem::exists(sourcePath)&&std::filesystem::exists(destPath)) {
263  tString ext;
264 
265  std::filesystem::copy_options copyOptions;
266  if (overwrite)
267  copyOptions=std::filesystem::copy_options::update_existing;//Replace the existing file only if it is older than the file being copied
268 
269  for (const auto & entry : std::filesystem::directory_iterator(sourcePath)) {
270  ext=GetExtension(entry.path());
271  functions_string::toLower(ext);
272  if ( (ext.compare("")!=0) && (exts.find(ext)!=tString::npos) &&
273  (std::filesystem::is_regular_file(entry.path()))) std::filesystem::copy(entry.path(),destPath,copyOptions);
274  }
275  return true;
276  }
277  return false;
278  }
285  inline static tBoolean CopyFiles(const tString& sourcePath,
286  const tString& destPath,
287  const tBoolean& overwrite) {
288  if (std::filesystem::exists(sourcePath)&&std::filesystem::exists(destPath)) {
289  std::filesystem::copy_options copyOptions=std::filesystem::copy_options::skip_existing;
290  if (overwrite) {
291  //copyOptions=std::filesystem::copy_options::update_existing;//Replace the existing file only if it is older than the file being copied
292  copyOptions=std::filesystem::copy_options::overwrite_existing;//Replace the existing file
293  }
294  std::filesystem::copy(sourcePath,destPath,copyOptions);
295  return true;
296  }
297  return false;
298  }
305  inline static tBoolean CopyFile(const tString& source,
306  const tString& dest,
307  const tBoolean& overwrite) {
308  if (std::filesystem::exists(dest)) {
309  std::filesystem::copy_options copyOptions;
310  if (overwrite)
311  copyOptions=std::filesystem::copy_options::overwrite_existing;
312  if (!std::filesystem::equivalent(source,dest)) std::filesystem::copy(source,dest,copyOptions);
313  return true;
314  }
315  std::filesystem::copy(source,dest);
316  return true;
317  }
323  inline static tBoolean CopyFile(const tString& source,
324  const tString& dest) {
325  return CopyFile(source,dest,true);
326  }
327 
333  inline static tBoolean CopyPath(const tString& sourcePath,const tString& destPath) {
334  if (std::filesystem::exists(sourcePath)) {
335  const auto copyOptions = std::filesystem::copy_options::update_existing //Replace the existing file only if it is older than the file being copied
336  | std::filesystem::copy_options::recursive; //Recursively copy subdirectories and their content
337 
338  std::filesystem::copy(sourcePath,destPath,copyOptions);
339  return true;
340  }
341  return false;
342  }
343 
348  inline static void GetContent(const tString& path,std::vector<tString>& content) {
349  content.clear();
350  for (const auto & entry : std::filesystem::directory_iterator(path))
351  content.push_back(entry.path());
352 
353  }
354 
358  inline static tString GetCurrentPath() {
359  return std::filesystem::current_path() ;
360  }
365  inline static tString GetAbsolutePath(const tString& path) {
366  return std::filesystem::absolute(path) ;
367  }
372  inline static tString GetBaseName(const tString& file) {
373  return std::filesystem::path(file).stem();
374  }
375 
380  inline static tString GetExtension(const tString& file) {
381  return std::filesystem::path(file).extension();
382  }
383 
388  inline static tString GetPath(const tString& file) {
389  return std::filesystem::path(file).parent_path();;
390  }
395  inline static tString GetFileName(const tString& file) {
396  return std::filesystem::path(file).filename();
397  }
398 
399 
400 
401 };
402 
403 #endif
this class describes the file system IO by default write on standart output
Definition: CORE_IO.h:23
static tString GetPath(const tString &file)
get path containing the file
Definition: CORE_IO.h:388
static tBoolean RemovePath(const tString &path)
remove an empty path
Definition: CORE_IO.h:136
static tString GetCurrentPath()
get the current path
Definition: CORE_IO.h:358
static tString GetFileName(const tString &file)
get path containing the file
Definition: CORE_IO.h:395
static void GetFiles(const tString &path, std::vector< tString > &files)
get all the files within the directory
Definition: CORE_IO.h:204
virtual ~CORE_IO(void)
destroy a CORE_IO
Definition: CORE_IO.h:50
static tBoolean RemoveFile(const tString &file)
remove a file
Definition: CORE_IO.h:144
static void GetFiles(const tString &path, const tString &prefix, const tString &exts, std::vector< tString > &files)
get all the files within the directory with extension of the form .ext
Definition: CORE_IO.h:235
static tBoolean CopyFiles(const tString &sourcePath, const tString &destPath, const tBoolean &overwrite)
copy all the files within the directory to destination path
Definition: CORE_IO.h:285
static tBoolean CopyFile(const tString &source, const tString &dest, const tBoolean &overwrite)
copy file
Definition: CORE_IO.h:305
static tString GetSystemPath(const tString &posix_path)
get the system path corresponding to posix path
Definition: CORE_IO.h:176
static tString GetPathName(const tString &aPath)
get all the paths within the directory
Definition: CORE_IO.h:195
static tString GetAbsolutePath(const tString &path)
get the absolute path of the path
Definition: CORE_IO.h:365
static tBoolean IsPath(const tString &p)
return true if the path designed by p is a path
Definition: CORE_IO.h:84
static void GetFiles(const tString &path, const tString &exts, std::vector< tString > &files)
get all the files within the directory with extension of the form .ext
Definition: CORE_IO.h:216
static tBoolean CopyFiles(const tString &sourcePath, const tString &exts, const tString &destPath, const tBoolean &overwrite)
copy all the files with extension within the directory to destination path
Definition: CORE_IO.h:258
static void RemoveAllFiles(const tString &path, const tString &exts)
remove all files with extension within the path
Definition: CORE_IO.h:163
static tBoolean CopyPath(const tString &sourcePath, const tString &destPath)
copy path
Definition: CORE_IO.h:333
static tBoolean Exists(const tString &f)
return true if the file or the path exists
Definition: CORE_IO.h:91
static tBoolean CopyFile(const tString &source, const tString &dest)
copy file
Definition: CORE_IO.h:323
static void GetContent(const tString &path, std::vector< tString > &content)
get all the content files or paths within the directory
Definition: CORE_IO.h:348
virtual tMemSize getMemorySize() const override
return the memory size of the class
Definition: CORE_IO.h:69
static tBoolean CreatePath(const tString &path)
create a path
Definition: CORE_IO.h:99
static tString GetExtension(const tString &file)
get extension of the file
Definition: CORE_IO.h:380
static tBoolean IsFile(const tString &f)
return true if the file is a regular file
Definition: CORE_IO.h:77
static tBoolean Rename(const tString &file, const tString &newFile)
Definition: CORE_IO.h:107
static void RemoveAllFiles(const tString &path)
remove all files within a path
Definition: CORE_IO.h:151
static tString GetBaseName(const tString &file)
get base name of the file
Definition: CORE_IO.h:372
CORE_IO()
build a CORE_IO
Definition: CORE_IO.h:40
static void GetPaths(const tString &path, std::vector< tString > &paths)
get all the paths within the directory
Definition: CORE_IO.h:184
static tBoolean RemovePath(const tString &path, const tBoolean &force)
remove a path
Definition: CORE_IO.h:120
abstract base class for most classes.
Definition: CORE_Object.h:65
virtual tMemSize getContentsMemorySize() const
return nthe memory size of the included associations
Definition: CORE_Object.h:278