File.hpp 3.5 KB

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