File.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. std::string ls_std::File::getParent()
  71. {
  72. std::string parent {};
  73. std::vector<std::string> subDirectoryNames = ls_std::File::_splitIntoSubDirectoryNames(this->absoluteFilePath);
  74. const char separator = ls_std::FilePathSeparator::getOperatingSystemSpecificSeparator();
  75. subDirectoryNames.pop_back();
  76. for(auto const& subDirectoryName : subDirectoryNames) {
  77. parent += subDirectoryName + separator;
  78. }
  79. return parent;
  80. }
  81. long ls_std::File::getSize()
  82. {
  83. std::streampos fileSize {};
  84. if(this->_exists(this->absoluteFilePath)) {
  85. std::ifstream fileHandler{this->absoluteFilePath, std::ios::in};
  86. fileSize = fileHandler.tellg();
  87. fileHandler.seekg(0, std::ios::end);
  88. fileSize = fileHandler.tellg() - fileSize;
  89. fileHandler.close();
  90. }
  91. return fileSize;
  92. }
  93. bool ls_std::File::isDirectory()
  94. {
  95. return this->_isDirectory(this->absoluteFilePath);
  96. }
  97. bool ls_std::File::isFile()
  98. {
  99. return this->_isFile(this->absoluteFilePath);
  100. }
  101. void ls_std::File::makeDirectory()
  102. {
  103. if(ls_std::File::_mkdir(this->absoluteFilePath)) {
  104. throw ls_std::FileOperationException {this->absoluteFilePath};
  105. }
  106. }
  107. void ls_std::File::makeDirectories() {
  108. std::vector<std::string> subDirectories = ls_std::File::_splitIntoSubDirectoryNames(this->absoluteFilePath);
  109. const char separator = ls_std::FilePathSeparator::getOperatingSystemSpecificSeparator();
  110. std::string currentHierarchy {};
  111. for(const auto& subDirectory : subDirectories) {
  112. currentHierarchy += subDirectory;
  113. if(!this->_exists(currentHierarchy)) {
  114. ls_std::File::_mkdir(currentHierarchy);
  115. }
  116. currentHierarchy += separator;
  117. }
  118. }
  119. void ls_std::File::remove()
  120. {
  121. if(this->_isFile(this->absoluteFilePath)) {
  122. if(std::remove(this->absoluteFilePath.c_str())) {
  123. throw ls_std::FileOperationException{this->absoluteFilePath};
  124. }
  125. }
  126. if(this->_isDirectory(this->absoluteFilePath)) {
  127. if(rmdir(this->absoluteFilePath.c_str())) {
  128. throw ls_std::FileOperationException{this->absoluteFilePath};
  129. }
  130. }
  131. }
  132. bool ls_std::File::_exists(const std::string& _path)
  133. {
  134. struct stat _stat {};
  135. return (stat(this->absoluteFilePath.c_str(), &_stat) == 0);
  136. }
  137. bool ls_std::File::_isDirectory(const std::string& _path)
  138. {
  139. bool match {};
  140. struct stat _stat {};
  141. if(stat(this->absoluteFilePath.c_str(), &_stat) == 0) {
  142. match = _stat.st_mode & (unsigned short) S_IFDIR;
  143. }
  144. return match;
  145. }
  146. bool ls_std::File::_isFile(const std::string& _path)
  147. {
  148. bool match {};
  149. struct stat _stat {};
  150. if(stat(this->absoluteFilePath.c_str(), &_stat) == 0) {
  151. match = _stat.st_mode & (unsigned short) S_IFREG;
  152. }
  153. return match;
  154. }
  155. int ls_std::File::_mkdir(const std::string& _path) {
  156. int result;
  157. #ifdef _WIN32
  158. result = mkdir(_path.c_str());
  159. #endif
  160. #ifdef unix
  161. result = mkdir(_path.c_str(), 0777);
  162. #endif
  163. #ifdef __APPLE__
  164. result = mkdir(_path.c_str(), 0777);
  165. #endif
  166. return result;
  167. }
  168. std::string ls_std::File::_normalizePath(std::string _path)
  169. {
  170. const char linuxSeparator = ls_std::FilePathSeparator::getLinuxFilePathSeparator();
  171. const char windowsSeparator = ls_std::FilePathSeparator::getUnixFilePathSeparator();
  172. #ifdef unix
  173. std::replace(_path.begin(), _path.end(), windowsSeparator, linuxSeparator);
  174. #endif
  175. #ifdef __APPLE__
  176. std::replace(_path.begin(), _path.end(), windowsSeparator, linuxSeparator);
  177. #endif
  178. #ifdef _WIN32
  179. std::replace(_path.begin(), _path.end(), linuxSeparator, windowsSeparator);
  180. #endif
  181. return _path;
  182. }
  183. std::vector<std::string> ls_std::File::_splitIntoSubDirectoryNames(const std::string& _path) {
  184. std::vector<std::string> subDirectoryNames {};
  185. std::stringstream _stream {_path};
  186. std::string subDirectoryName {};
  187. const char separator = ls_std::FilePathSeparator::getOperatingSystemSpecificSeparator();
  188. while(std::getline(_stream, subDirectoryName, separator)) {
  189. subDirectoryNames.push_back(subDirectoryName);
  190. }
  191. return subDirectoryNames;
  192. }