File.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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-15
  7. *
  8. * */
  9. #include "File.hpp"
  10. #include "../exception/FileOperationException.hpp"
  11. #include "FilePathSeparatorMatch.hpp"
  12. #include <fstream>
  13. #include <sys/stat.h>
  14. #include <algorithm>
  15. ls_std::File::File(std::string _absoluteFilePath) : Class("File"),
  16. absoluteFilePath(std::move(_absoluteFilePath))
  17. {}
  18. bool ls_std::File::operator==(File &_file)
  19. {
  20. std::string normalizedFilePath = ls_std::File::normalizePath(this->absoluteFilePath);
  21. std::string normalizedForeignFilePath = ls_std::File::normalizePath(_file.getAbsoluteFilePath());
  22. return normalizedFilePath == normalizedForeignFilePath;
  23. }
  24. bool ls_std::File::operator!=(File &_file)
  25. {
  26. std::string normalizedFilePath = ls_std::File::normalizePath(this->absoluteFilePath);
  27. std::string normalizedForeignFilePath = ls_std::File::normalizePath(_file.getAbsoluteFilePath());
  28. return normalizedFilePath != normalizedForeignFilePath;
  29. }
  30. bool ls_std::File::canExecute()
  31. {
  32. bool executable {};
  33. struct stat _stat {};
  34. if(stat(this->absoluteFilePath.c_str(), &_stat) == 0) {
  35. executable = (_stat.st_mode & (unsigned short) S_IEXEC) != 0;
  36. }
  37. return executable;
  38. }
  39. void ls_std::File::create()
  40. {
  41. if(!this->_exists()) {
  42. std::ofstream file {this->absoluteFilePath};
  43. file.close();
  44. } else {
  45. throw ls_std::FileOperationException{this->absoluteFilePath};
  46. }
  47. }
  48. bool ls_std::File::exists()
  49. {
  50. return this->_exists();
  51. }
  52. std::string ls_std::File::getAbsoluteFilePath() {
  53. return this->absoluteFilePath;
  54. }
  55. std::string ls_std::File::getName()
  56. {
  57. std::string copy = this->absoluteFilePath;
  58. // if it's a directory, remove separator from end, if it does exist
  59. if(this->_isDirectory()) {
  60. copy.erase(std::remove_if(copy.end() - 1, copy.end(), ls_std::FilePathSeparatorMatch()), copy.end());
  61. }
  62. // now get the file / directory name
  63. auto base = std::find_if(copy.rbegin(), copy.rend(), ls_std::FilePathSeparatorMatch()).base();
  64. return std::string(base, copy.end());
  65. }
  66. long ls_std::File::getSize()
  67. {
  68. std::streampos fileSize {};
  69. if(this->_exists()) {
  70. std::ifstream fileHandler{this->absoluteFilePath, std::ios::in};
  71. fileSize = fileHandler.tellg();
  72. fileHandler.seekg(0, std::ios::end);
  73. fileSize = fileHandler.tellg() - fileSize;
  74. fileHandler.close();
  75. }
  76. return fileSize;
  77. }
  78. bool ls_std::File::isDirectory()
  79. {
  80. return this->_isDirectory();
  81. }
  82. bool ls_std::File::isFile()
  83. {
  84. return this->_isFile();
  85. }
  86. void ls_std::File::makeDirectory()
  87. {
  88. if(mkdir(this->absoluteFilePath.c_str())) {
  89. throw ls_std::FileOperationException {this->absoluteFilePath};
  90. }
  91. }
  92. void ls_std::File::remove()
  93. {
  94. if(this->_isFile()) {
  95. if(std::remove(this->absoluteFilePath.c_str())) {
  96. throw ls_std::FileOperationException{this->absoluteFilePath};
  97. }
  98. }
  99. if(this->_isDirectory()) {
  100. if(rmdir(this->absoluteFilePath.c_str())) {
  101. throw ls_std::FileOperationException{this->absoluteFilePath};
  102. }
  103. }
  104. }
  105. bool ls_std::File::_exists()
  106. {
  107. struct stat _stat {};
  108. return (stat(this->absoluteFilePath.c_str(), &_stat) == 0);
  109. }
  110. bool ls_std::File::_isDirectory()
  111. {
  112. bool match {};
  113. struct stat _stat {};
  114. if(stat(this->absoluteFilePath.c_str(), &_stat) == 0) {
  115. match = _stat.st_mode & (unsigned short) S_IFDIR;
  116. }
  117. return match;
  118. }
  119. bool ls_std::File::_isFile()
  120. {
  121. bool match {};
  122. struct stat _stat {};
  123. if(stat(this->absoluteFilePath.c_str(), &_stat) == 0) {
  124. match = _stat.st_mode & (unsigned short) S_IFREG;
  125. }
  126. return match;
  127. }
  128. std::string ls_std::File::normalizePath(std::string path)
  129. {
  130. std::replace(path.begin(), path.end(), '\\', '/');
  131. return path;
  132. }