File.cpp 3.7 KB

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