File.hpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. void createNewFile();
  34. bool exists();
  35. std::string getAbsoluteFilePath();
  36. std::string getName();
  37. std::string getParent();
  38. long getSize();
  39. bool isDirectory();
  40. bool isFile();
  41. time_t lastModified();
  42. std::list<std::string> list();
  43. std::list<std::string> listFiles();
  44. void makeDirectory();
  45. void makeDirectories();
  46. void remove();
  47. bool renameTo(const std::string& _newName);
  48. private:
  49. std::string absoluteFilePath {};
  50. #ifdef _WIN32
  51. static void _addToFileListWindows(const std::string& _path, bool _withDirectories, WIN32_FIND_DATA _data, std::list<std::string>& _list);
  52. #endif
  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. static bool _exists(const std::string& _path);
  57. static std::string _getParent(const std::string& _path);
  58. static bool _isDirectory(const std::string& _path);
  59. static bool _isExecutable(const std::string& _path);
  60. static bool _isFile(const std::string& _path);
  61. static time_t _lastModified(const std::string& _path);
  62. static std::list<std::string> _list(const std::string& _path);
  63. static std::list<std::string> _listFiles(const std::string& _path);
  64. #if defined(unix) || defined(__APPLE__)
  65. static std::list<std::string> _listUnix(const std::string& _path, bool withDirectories);
  66. #endif
  67. #ifdef _WIN32
  68. static std::list<std::string> _listWindows(const std::string& _path, bool withDirectories);
  69. #endif
  70. static int _mkdir(const std::string& _path);
  71. static std::string _normalizePath(std::string _path);
  72. static std::string _reduceSeparators(const std::string& _path);
  73. static bool _renameTo(const std::string& _oldName, const std::string& _newName);
  74. static std::string _replaceWrongSeparator(std::string _path);
  75. static std::vector<std::string> _splitIntoSubDirectoryNames(const std::string& _path);
  76. };
  77. }
  78. #endif