File.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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-03
  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::std::io
  25. {
  26. class LS_STD_DYNAMIC_GOAL File : public ls::std::core::Class
  27. {
  28. public:
  29. explicit File(::std::string _absoluteFilePath);
  30. ~File() override = default;
  31. // comparison operators
  32. bool operator==(ls::std::io::File &_file);
  33. bool operator!=(ls::std::io::File &_file);
  34. // additional functionality
  35. bool canExecute();
  36. bool canRead();
  37. bool canWrite();
  38. void createNewFile();
  39. bool exists();
  40. ::std::string getAbsoluteFilePath();
  41. ::std::string getName();
  42. ::std::string getParent();
  43. static ::std::string getWorkingDirectory();
  44. long getSize();
  45. bool isDirectory();
  46. bool isFile();
  47. time_t lastModified();
  48. ::std::list<::std::string> list();
  49. ::std::list<::std::string> listFiles();
  50. void makeDirectory();
  51. void makeDirectories();
  52. void remove();
  53. bool renameTo(const ::std::string &_newName);
  54. void reset(const ::std::string &_newPath);
  55. private:
  56. ::std::string absoluteFilePath{};
  57. #if defined(unix) || defined(__APPLE__)
  58. static void _addToFileListUnix(const ::std::string &_path, bool _withDirectories, dirent *directoryEntity, ::std::list<::std::string> &_list);
  59. #endif
  60. #ifdef _WIN32
  61. static void _addToFileListWindows(const ::std::string &_path, bool _withDirectories, WIN32_FIND_DATA _data, ::std::list<::std::string> &_list);
  62. #endif
  63. static bool _equals(ls::std::io::File &_file, ls::std::io::File &_foreignFile);
  64. static bool _exists(const ::std::string &_path);
  65. static ::std::string _getParent(const ::std::string &_path);
  66. #if defined(unix) || defined(__APPLE__)
  67. static ::std::string _getWorkingDirectoryUnix();
  68. #endif
  69. #ifdef _WIN32
  70. static ::std::string _getWorkingDirectoryWindows();
  71. #endif
  72. static bool _isDirectory(const ::std::string &_path);
  73. static bool _isExecutable(const ::std::string &_path);
  74. static bool _isFile(const ::std::string &_path);
  75. #if defined(unix) || defined(__APPLE__)
  76. static bool _isReadableUnix(const ::std::string &_path);
  77. #endif
  78. #ifdef _WIN32
  79. static bool _isReadableWindows(const ::std::string &_path);
  80. #endif
  81. static bool _isWritable(const ::std::string &_path);
  82. static time_t _lastModified(const ::std::string &_path);
  83. static ::std::list<::std::string> _list(const ::std::string &_path);
  84. static ::std::list<::std::string> _listFiles(const ::std::string &_path);
  85. #if defined(unix) || defined(__APPLE__)
  86. static ::std::list<::std::string> _listUnix(const ::std::string &_path, bool withDirectories);
  87. #endif
  88. #ifdef _WIN32
  89. static ::std::list<::std::string> _listWindows(const ::std::string &_path, bool withDirectories);
  90. #endif
  91. static int _mkdir(const ::std::string &_path);
  92. static ::std::string _normalizePath(::std::string _path);
  93. static ::std::string _reduceSeparators(const ::std::string &_path);
  94. static void _remove(const ::std::string &_path);
  95. #if defined(unix) || defined(__APPLE__)
  96. static void _removeUnix(const ::std::string &_path);
  97. #endif
  98. #ifdef _WIN32
  99. static void _removeWindows(const ::std::string &_path);
  100. #endif
  101. static bool _renameTo(const ::std::string &_oldName, const ::std::string &_newName);
  102. static ::std::string _replaceWrongSeparator(::std::string _path);
  103. static ::std::vector<::std::string> _splitIntoSubDirectoryNames(const ::std::string &_path);
  104. };
  105. }
  106. #endif