File.hpp 3.5 KB

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