File.hpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-08-15
  6. * Changed: 2022-07-01
  7. *
  8. * */
  9. #ifndef LS_STD_FILE_HPP
  10. #define LS_STD_FILE_HPP
  11. #include <ls_std/core/Class.hpp>
  12. #include <string>
  13. #include <vector>
  14. #include <list>
  15. #include <ctime>
  16. #include <regex>
  17. #include <ls_std/os/dynamic_goal.hpp>
  18. #if defined(unix) || defined(__APPLE__)
  19. #include <dirent.h>
  20. #endif
  21. #ifdef _WIN32
  22. #include <windows.h>
  23. #endif
  24. namespace ls
  25. {
  26. namespace std
  27. {
  28. namespace io
  29. {
  30. class DYNAMIC_GOAL File : public ls::std::core::Class
  31. {
  32. public:
  33. explicit File(::std::string _absoluteFilePath);
  34. ~File() override = default;
  35. // comparison operators
  36. bool operator==(ls::std::io::File &_file);
  37. bool operator!=(ls::std::io::File &_file);
  38. // additional functionality
  39. bool canExecute();
  40. bool canRead();
  41. bool canWrite();
  42. void createNewFile();
  43. bool exists();
  44. ::std::string getAbsoluteFilePath();
  45. ::std::string getName();
  46. ::std::string getParent();
  47. static ::std::string getWorkingDirectory();
  48. long getSize();
  49. bool isDirectory();
  50. bool isFile();
  51. time_t lastModified();
  52. ::std::list<::std::string> list();
  53. ::std::list<::std::string> listFiles();
  54. void makeDirectory();
  55. void makeDirectories();
  56. void remove();
  57. bool renameTo(const ::std::string &_newName);
  58. void reset(const ::std::string &_newPath);
  59. private:
  60. ::std::string absoluteFilePath{};
  61. #if defined(unix) || defined(__APPLE__)
  62. static void _addToFileListUnix(const ::std::string &_path, bool _withDirectories, dirent *directoryEntity, ::std::list<::std::string> &_list);
  63. #endif
  64. #ifdef _WIN32
  65. static void _addToFileListWindows(const ::std::string &_path, bool _withDirectories, WIN32_FIND_DATA _data, ::std::list<::std::string> &_list);
  66. #endif
  67. static bool _equals(ls::std::io::File &_file, ls::std::io::File &_foreignFile);
  68. static bool _exists(const ::std::string &_path);
  69. static ::std::string _getParent(const ::std::string &_path);
  70. #if defined(unix) || defined(__APPLE__)
  71. static ::std::string _getWorkingDirectoryUnix();
  72. #endif
  73. #ifdef _WIN32
  74. static ::std::string _getWorkingDirectoryWindows();
  75. #endif
  76. static bool _isDirectory(const ::std::string &_path);
  77. static bool _isExecutable(const ::std::string &_path);
  78. static bool _isFile(const ::std::string &_path);
  79. #if defined(unix) || defined(__APPLE__)
  80. static bool _isReadableUnix(const ::std::string &_path);
  81. #endif
  82. #ifdef _WIN32
  83. static bool _isReadableWindows(const ::std::string &_path);
  84. #endif
  85. static bool _isWritable(const ::std::string &_path);
  86. static time_t _lastModified(const ::std::string &_path);
  87. static ::std::list<::std::string> _list(const ::std::string &_path);
  88. static ::std::list<::std::string> _listFiles(const ::std::string &_path);
  89. #if defined(unix) || defined(__APPLE__)
  90. static ::std::list<::std::string> _listUnix(const ::std::string &_path, bool withDirectories);
  91. #endif
  92. #ifdef _WIN32
  93. static ::std::list<::std::string> _listWindows(const ::std::string &_path, bool withDirectories);
  94. #endif
  95. static int _mkdir(const ::std::string &_path);
  96. static ::std::string _normalizePath(::std::string _path);
  97. static ::std::string _reduceSeparators(const ::std::string &_path);
  98. static void _remove(const ::std::string &_path);
  99. #if defined(unix) || defined(__APPLE__)
  100. static void _removeUnix(const ::std::string &_path);
  101. #endif
  102. #ifdef _WIN32
  103. static void _removeWindows(const ::std::string &_path);
  104. #endif
  105. static bool _renameTo(const ::std::string &_oldName, const ::std::string &_newName);
  106. static ::std::string _replaceWrongSeparator(::std::string _path);
  107. static ::std::vector<::std::string> _splitIntoSubDirectoryNames(const ::std::string &_path);
  108. };
  109. }
  110. }
  111. }
  112. #endif