File.hpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-08-15
  6. * Changed: 2020-08-19
  7. *
  8. * */
  9. #ifndef FILE_HPP
  10. #define FILE_HPP
  11. #include "../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. void createNewFile();
  35. bool exists();
  36. std::string getAbsoluteFilePath();
  37. std::string getName();
  38. std::string getParent();
  39. long getSize();
  40. bool isDirectory();
  41. bool isFile();
  42. time_t lastModified();
  43. std::list<std::string> list();
  44. std::list<std::string> listFiles();
  45. void makeDirectory();
  46. void makeDirectories();
  47. void remove();
  48. bool renameTo(const std::string& _newName);
  49. private:
  50. std::string absoluteFilePath {};
  51. #if defined(unix) || defined(__APPLE__)
  52. static void _addToFileListUnix(const std::string& _path, bool _withDirectories, dirent* directoryEntity, std::list<std::string>& _list);
  53. #endif
  54. #ifdef _WIN32
  55. static void _addToFileListWindows(const std::string& _path, bool _withDirectories, WIN32_FIND_DATA _data, std::list<std::string>& _list);
  56. #endif
  57. static bool _exists(const std::string& _path);
  58. static std::string _getParent(const std::string& _path);
  59. static bool _isDirectory(const std::string& _path);
  60. static bool _isExecutable(const std::string& _path);
  61. static bool _isFile(const std::string& _path);
  62. #if defined(unix) || defined(__APPLE__)
  63. static bool _isReadableUnix(const std::string& _path);
  64. #endif
  65. #ifdef _WIN32
  66. static bool _isReadableWindows(const std::string& _path);
  67. #endif
  68. static time_t _lastModified(const std::string& _path);
  69. static std::list<std::string> _list(const std::string& _path);
  70. static std::list<std::string> _listFiles(const std::string& _path);
  71. #if defined(unix) || defined(__APPLE__)
  72. static std::list<std::string> _listUnix(const std::string& _path, bool withDirectories);
  73. #endif
  74. #ifdef _WIN32
  75. static std::list<std::string> _listWindows(const std::string& _path, bool withDirectories);
  76. #endif
  77. static int _mkdir(const std::string& _path);
  78. static std::string _normalizePath(std::string _path);
  79. static std::string _reduceSeparators(const std::string& _path);
  80. static bool _renameTo(const std::string& _oldName, const std::string& _newName);
  81. static std::string _replaceWrongSeparator(std::string _path);
  82. static std::vector<std::string> _splitIntoSubDirectoryNames(const std::string& _path);
  83. };
  84. }
  85. #endif