C++ main module for stochmagnet Package  1.0
CORE_IO.h
Go to the documentation of this file.
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 //incelude vector
12 #include <vector>
13 
20 class CORE_IO : public CORE_Object {
21  // ATTRIBUTES
22 
23  public:
24 
25 
26 private:
27  // ASSOCIATIONS
28 
29 
30  // METHODS
31 
32 
33 public:
34  // CONSTRUCTORS
37  CORE_IO() {
38  }
39 
40 
41 
42 
43  // DESTRUCTORS
47  virtual ~CORE_IO(void) {
48  }
49 
50 
51 public:
52 
53 
54 private:
55 
56 
57 public:
58 
66  virtual tMemSize getMemorySize() const override {
67  return sizeof(*this)+getContentsMemorySize();
68  }
69 
74  inline static tBoolean IsFile(const tString& f) {
75  return std::filesystem::is_regular_file(f);
76  }
81  inline static tBoolean IsPath(const tString& p) {
82  return std::filesystem::is_directory(p);
83  }
88  inline static tBoolean Exists(const tString& f) {
89  return std::filesystem::exists(f);
90  }
91 
96  inline static tBoolean CreatePath(const tString& path) {
97  std::filesystem::path p(path);
98  return std::filesystem::create_directories(p);
99  }
104  inline static tBoolean Rename(const tString& file, const tString& newFile) {
105  if (Exists(file)) {
106  std::filesystem::rename(file,newFile);
107  return true;
108  }
109  return false;
110  }
111 
117  inline static tBoolean RemovePath(const tString& path,const tBoolean& force) {
118  try {
119  if (force) {
120  return (std::filesystem::remove_all(path)!=0);
121  } else {
122  return std::filesystem::remove(path);
123  }
124  } catch(std::exception& e) {
125  std::cerr<<e.what()<<"\n";
126  return false;
127  }
128  }
133  inline static tBoolean RemovePath(const tString& path) {
134  return RemovePath(path,false);
135  }
136 
141  inline static tBoolean RemoveFile(const tString& file) {
142  return std::filesystem::remove(file);
143  }
148  inline static void RemoveAllFiles(const tString& path) {
149  std::vector<tString> files;
150  //get all the files
151  GetFiles(path,files);
152  for_each(files.begin(),files.end(),[&](const auto& file) {
153  std::filesystem::remove(file);
154  });
155  }
161  inline static void RemoveAllFiles(const tString& path,const tString& exts) {
162  std::vector<tString> files;
163  //get all the files
164  GetFiles(path,exts,files);
165  for_each(files.begin(),files.end(),[&](const auto& file) {
166  std::filesystem::remove(file);
167  });
168  }
169 
174  inline static tString GetSystemPath(const tString& posix_path) {
175  return std::filesystem::path(posix_path).make_preferred().string();
176  }
177 
182  inline static void GetPaths(const tString& path,std::vector<tString>& paths) {
183  paths.clear();
184  if (std::filesystem::exists(path)) {
185  for (const auto & entry : std::filesystem::directory_iterator(path))
186  if (std::filesystem::is_directory(entry.path())) paths.push_back(entry.path());
187  }
188  }
193  inline static tString GetPathName(const tString& aPath) {
194  std::filesystem::path p(aPath);
195  return p.filename();
196 
197  }
202  inline static void GetFiles(const tString& path,std::vector<tString>& files) {
203  files.clear();
204  if (std::filesystem::exists(path)) {
205  for (const auto & entry : std::filesystem::directory_iterator(path))
206  if (std::filesystem::is_regular_file(entry.path())) files.push_back(entry.path());
207  }
208  }
214  inline static void GetFiles(const tString& path,const tString& exts,std::vector<tString>& files) {
215  files.clear();
216  if (std::filesystem::exists(path)) {
217  tString ext;
218  for (const auto & entry : std::filesystem::directory_iterator(path)) {
219  ext=GetExtension(entry.path());
221  if ( (ext.compare("")!=0) &&
222  (exts.find(ext)!=tString::npos) &&
223  (std::filesystem::is_regular_file(entry.path()))) files.push_back(entry.path());
224  }
225  }
226  }
227 
235  inline static tBoolean CopyFiles(const tString& sourcePath,const tString& exts,
236  const tString& destPath,
237  const tBoolean& overwrite) {
238 
239  if (std::filesystem::exists(sourcePath)&&std::filesystem::exists(destPath)) {
240  tString ext;
241 
242  std::filesystem::copy_options copyOptions;
243  if (overwrite)
244  copyOptions=std::filesystem::copy_options::update_existing;//Replace the existing file only if it is older than the file being copied
245 
246  for (const auto & entry : std::filesystem::directory_iterator(sourcePath)) {
247  ext=GetExtension(entry.path());
249  if ( (ext.compare("")!=0) && (exts.find(ext)!=tString::npos) &&
250  (std::filesystem::is_regular_file(entry.path()))) std::filesystem::copy(entry.path(),destPath,copyOptions);
251  }
252  return true;
253  }
254  return false;
255  }
262  inline static tBoolean CopyFiles(const tString& sourcePath,
263  const tString& destPath,
264  const tBoolean& overwrite) {
265  if (std::filesystem::exists(sourcePath)&&std::filesystem::exists(destPath)) {
266  std::filesystem::copy_options copyOptions;
267  if (overwrite)
268  copyOptions=std::filesystem::copy_options::update_existing;//Replace the existing file only if it is older than the file being copied
269  std::filesystem::copy(sourcePath,destPath,copyOptions);
270  return true;
271  }
272  return false;
273  }
280  inline static tBoolean CopyFile(const tString& source,
281  const tString& dest,
282  const tBoolean& overwrite) {
283  if (std::filesystem::exists(dest)) {
284  std::filesystem::copy_options copyOptions;
285  if (overwrite)
286  copyOptions=std::filesystem::copy_options::overwrite_existing;
287  if (!std::filesystem::equivalent(source,dest)) std::filesystem::copy(source,dest,copyOptions);
288  return true;
289  }
290  std::filesystem::copy(source,dest);
291  return true;
292  }
299  inline static tBoolean CopyFile(const tString& source,
300  const tString& dest) {
301  return CopyFile(source,dest,true);
302  }
303 
309  inline static tBoolean CopyPath(const tString& sourcePath,const tString& destPath) {
310  if (std::filesystem::exists(sourcePath)) {
311  const auto copyOptions = std::filesystem::copy_options::update_existing //Replace the existing file only if it is older than the file being copied
312  | std::filesystem::copy_options::recursive; //Recursively copy subdirectories and their content
313 
314  std::filesystem::copy(sourcePath,destPath,copyOptions);
315  return true;
316  }
317  return false;
318  }
319 
324  inline static void GetContent(const tString& path,std::vector<tString>& content) {
325  content.clear();
326  for (const auto & entry : std::filesystem::directory_iterator(path))
327  content.push_back(entry.path());
328 
329  }
330 
334  inline static tString GetCurrentPath() {
335  return std::filesystem::current_path() ;
336  }
341  inline static tString GetAbsolutePath(const tString& path) {
342  return std::filesystem::absolute(path) ;
343  }
348  inline static tString GetBaseName(const tString& file) {
349  return std::filesystem::path(file).stem();
350  }
351 
356  inline static tString GetExtension(const tString& file) {
357  return std::filesystem::path(file).extension();
358  }
359 
364  inline static tString GetPath(const tString& file) {
365  return std::filesystem::path(file).parent_path();;
366  }
371  inline static tString GetFileName(const tString& file) {
372  return std::filesystem::path(file).filename();
373  }
374 
375 
376 
377 };
378 
379 #endif
this class describes the file system IO by default write on standart output
Definition: CORE_IO.h:20
static tString GetPath(const tString &file)
get path containing the file
Definition: CORE_IO.h:364
static tBoolean RemovePath(const tString &path)
remove an empty path
Definition: CORE_IO.h:133
static tString GetCurrentPath()
get the current path
Definition: CORE_IO.h:334
static tString GetFileName(const tString &file)
get path containing the file
Definition: CORE_IO.h:371
static void GetFiles(const tString &path, std::vector< tString > &files)
get all the files within the directory
Definition: CORE_IO.h:202
virtual ~CORE_IO(void)
destroy a CORE_IO
Definition: CORE_IO.h:47
static tBoolean RemoveFile(const tString &file)
remove a file
Definition: CORE_IO.h:141
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:262
static tBoolean CopyFile(const tString &source, const tString &dest, const tBoolean &overwrite)
copy file
Definition: CORE_IO.h:280
static tString GetSystemPath(const tString &posix_path)
get the system path corresponding to posix path
Definition: CORE_IO.h:174
static tString GetPathName(const tString &aPath)
get all the paths within the directory
Definition: CORE_IO.h:193
static tString GetAbsolutePath(const tString &path)
get the absolute path of the path
Definition: CORE_IO.h:341
static tBoolean IsPath(const tString &p)
return true if the path designed by p is a path
Definition: CORE_IO.h:81
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:214
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:235
static void RemoveAllFiles(const tString &path, const tString &exts)
remove all files with extension within the path
Definition: CORE_IO.h:161
static tBoolean CopyPath(const tString &sourcePath, const tString &destPath)
copy path
Definition: CORE_IO.h:309
static tBoolean Exists(const tString &f)
return true if the file or the path exists
Definition: CORE_IO.h:88
static tBoolean CopyFile(const tString &source, const tString &dest)
copy file
Definition: CORE_IO.h:299
static void GetContent(const tString &path, std::vector< tString > &content)
get all the content files or paths within the directory
Definition: CORE_IO.h:324
virtual tMemSize getMemorySize() const override
return the memory size of the class
Definition: CORE_IO.h:66
static tBoolean CreatePath(const tString &path)
create a path
Definition: CORE_IO.h:96
static tString GetExtension(const tString &file)
get extension of the file
Definition: CORE_IO.h:356
static tBoolean IsFile(const tString &f)
return true if the file is a regular file
Definition: CORE_IO.h:74
static tBoolean Rename(const tString &file, const tString &newFile)
Definition: CORE_IO.h:104
static void RemoveAllFiles(const tString &path)
remove all files within a path
Definition: CORE_IO.h:148
static tString GetBaseName(const tString &file)
get base name of the file
Definition: CORE_IO.h:348
CORE_IO()
build a CORE_IO
Definition: CORE_IO.h:37
static void GetPaths(const tString &path, std::vector< tString > &paths)
get all the paths within the directory
Definition: CORE_IO.h:182
static tBoolean RemovePath(const tString &path, const tBoolean &force)
remove a path
Definition: CORE_IO.h:117
abstract base class for most classes.
Definition: CORE_Object.h:48
virtual tMemSize getContentsMemorySize() const
return nthe memory size of the included associations
Definition: CORE_Object.h:259
void toLower(tString &s)
turn a string to lower value
Definition: functions.h:375
#define tString
Definition: types.h:147
#define tMemSize
Definition: types.h:166
#define tBoolean
Definition: types.h:151