File.hpp 4.6 KB

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