File.cpp 5.0 KB

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