File.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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-16
  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. #include <sstream>
  16. #include <vector>
  17. #ifdef unix
  18. #include <unistd.h>
  19. #endif
  20. #ifdef __APPLE__
  21. #include <unistd.h>
  22. #endif
  23. ls_std::File::File(std::string _absoluteFilePath) : Class("File"),
  24. absoluteFilePath(ls_std::File::_normalizePath(std::move(_absoluteFilePath)))
  25. {}
  26. bool ls_std::File::operator==(File &_file)
  27. {
  28. return this->absoluteFilePath == _file.getAbsoluteFilePath();
  29. }
  30. bool ls_std::File::operator!=(File &_file)
  31. {
  32. return this->absoluteFilePath != _file.getAbsoluteFilePath();
  33. }
  34. bool ls_std::File::canExecute()
  35. {
  36. bool executable {};
  37. struct stat _stat {};
  38. if(stat(this->absoluteFilePath.c_str(), &_stat) == 0) {
  39. executable = (_stat.st_mode & (unsigned short) S_IEXEC) != 0;
  40. }
  41. return executable;
  42. }
  43. void ls_std::File::createNewFile()
  44. {
  45. if(!this->_exists(this->absoluteFilePath)) {
  46. std::ofstream file {this->absoluteFilePath};
  47. file.close();
  48. } else {
  49. throw ls_std::FileOperationException{this->absoluteFilePath};
  50. }
  51. }
  52. bool ls_std::File::exists()
  53. {
  54. return this->_exists(this->absoluteFilePath);
  55. }
  56. std::string ls_std::File::getAbsoluteFilePath() {
  57. return this->absoluteFilePath;
  58. }
  59. std::string ls_std::File::getName()
  60. {
  61. std::string copy = this->absoluteFilePath;
  62. // if it's a directory, remove separator from end, if it does exist
  63. if(this->_isDirectory(this->absoluteFilePath)) {
  64. copy.erase(std::remove_if(copy.end() - 1, copy.end(), ls_std::FilePathSeparatorMatch()), copy.end());
  65. }
  66. // now get the file / directory name
  67. auto base = std::find_if(copy.rbegin(), copy.rend(), ls_std::FilePathSeparatorMatch()).base();
  68. return std::string(base, copy.end());
  69. }
  70. long ls_std::File::getSize()
  71. {
  72. std::streampos fileSize {};
  73. if(this->_exists(this->absoluteFilePath)) {
  74. std::ifstream fileHandler{this->absoluteFilePath, std::ios::in};
  75. fileSize = fileHandler.tellg();
  76. fileHandler.seekg(0, std::ios::end);
  77. fileSize = fileHandler.tellg() - fileSize;
  78. fileHandler.close();
  79. }
  80. return fileSize;
  81. }
  82. bool ls_std::File::isDirectory()
  83. {
  84. return this->_isDirectory(this->absoluteFilePath);
  85. }
  86. bool ls_std::File::isFile()
  87. {
  88. return this->_isFile(this->absoluteFilePath);
  89. }
  90. void ls_std::File::makeDirectory()
  91. {
  92. if(ls_std::File::_mkdir(this->absoluteFilePath)) {
  93. throw ls_std::FileOperationException {this->absoluteFilePath};
  94. }
  95. }
  96. void ls_std::File::makeDirectories() {
  97. std::vector<std::string> subDirectories = ls_std::File::_splitIntoSubDirectoryNames(this->absoluteFilePath);
  98. const char separator = ls_std::FilePathSeparator::getOperatingSystemSpecificSeparator();
  99. std::string currentHierarchy {};
  100. for(const auto& subDirectory : subDirectories) {
  101. currentHierarchy += subDirectory;
  102. if(!this->_exists(currentHierarchy)) {
  103. ls_std::File::_mkdir(currentHierarchy);
  104. }
  105. currentHierarchy += separator;
  106. }
  107. }
  108. void ls_std::File::remove()
  109. {
  110. if(this->_isFile(this->absoluteFilePath)) {
  111. if(std::remove(this->absoluteFilePath.c_str())) {
  112. throw ls_std::FileOperationException{this->absoluteFilePath};
  113. }
  114. }
  115. if(this->_isDirectory(this->absoluteFilePath)) {
  116. if(rmdir(this->absoluteFilePath.c_str())) {
  117. throw ls_std::FileOperationException{this->absoluteFilePath};
  118. }
  119. }
  120. }
  121. bool ls_std::File::_exists(const std::string& _path)
  122. {
  123. struct stat _stat {};
  124. return (stat(this->absoluteFilePath.c_str(), &_stat) == 0);
  125. }
  126. bool ls_std::File::_isDirectory(const std::string& _path)
  127. {
  128. bool match {};
  129. struct stat _stat {};
  130. if(stat(this->absoluteFilePath.c_str(), &_stat) == 0) {
  131. match = _stat.st_mode & (unsigned short) S_IFDIR;
  132. }
  133. return match;
  134. }
  135. bool ls_std::File::_isFile(const std::string& _path)
  136. {
  137. bool match {};
  138. struct stat _stat {};
  139. if(stat(this->absoluteFilePath.c_str(), &_stat) == 0) {
  140. match = _stat.st_mode & (unsigned short) S_IFREG;
  141. }
  142. return match;
  143. }
  144. int ls_std::File::_mkdir(const std::string& _path) {
  145. int result;
  146. #ifdef _WIN32
  147. result = mkdir(_path.c_str());
  148. #endif
  149. #ifdef unix
  150. result = mkdir(_path.c_str(), 0777);
  151. #endif
  152. #ifdef __APPLE__
  153. result = mkdir(_path.c_str(), 0777);
  154. #endif
  155. return result;
  156. }
  157. std::string ls_std::File::_normalizePath(std::string _path)
  158. {
  159. const char linuxSeparator = ls_std::FilePathSeparator::getLinuxFilePathSeparator();
  160. const char windowsSeparator = ls_std::FilePathSeparator::getUnixFilePathSeparator();
  161. #ifdef unix
  162. std::replace(_path.begin(), _path.end(), windowsSeparator, linuxSeparator);
  163. #endif
  164. #ifdef __APPLE__
  165. std::replace(_path.begin(), _path.end(), windowsSeparator, linuxSeparator);
  166. #endif
  167. #ifdef _WIN32
  168. std::replace(_path.begin(), _path.end(), linuxSeparator, windowsSeparator);
  169. #endif
  170. return _path;
  171. }
  172. std::vector<std::string> ls_std::File::_splitIntoSubDirectoryNames(const std::string& _path) {
  173. std::vector<std::string> subDirectoryNames {};
  174. std::stringstream _stream {_path};
  175. std::string subDirectoryName {};
  176. const char separator = ls_std::FilePathSeparator::getOperatingSystemSpecificSeparator();
  177. while(std::getline(_stream, subDirectoryName, separator)) {
  178. subDirectoryNames.push_back(subDirectoryName);
  179. }
  180. return subDirectoryNames;
  181. }