File.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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-17
  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. #if defined(unix) || defined(__APPLE__)
  18. #include <unistd.h>
  19. #include <dirent.h>
  20. #endif
  21. #ifdef _WIN32
  22. #include <windows.h>
  23. #endif
  24. ls_std::File::File(std::string _absoluteFilePath) : Class("File"),
  25. absoluteFilePath(ls_std::File::_normalizePath(std::move(_absoluteFilePath)))
  26. {}
  27. bool ls_std::File::operator==(File &_file)
  28. {
  29. return this->absoluteFilePath == _file.getAbsoluteFilePath();
  30. }
  31. bool ls_std::File::operator!=(File &_file)
  32. {
  33. return this->absoluteFilePath != _file.getAbsoluteFilePath();
  34. }
  35. bool ls_std::File::canExecute()
  36. {
  37. bool executable {};
  38. struct stat _stat {};
  39. if(stat(this->absoluteFilePath.c_str(), &_stat) == 0) {
  40. executable = (_stat.st_mode & (unsigned short) S_IEXEC) != 0;
  41. }
  42. return executable;
  43. }
  44. void ls_std::File::createNewFile()
  45. {
  46. if(!ls_std::File::_exists(this->absoluteFilePath)) {
  47. std::ofstream file {this->absoluteFilePath};
  48. file.close();
  49. } else {
  50. throw ls_std::FileOperationException{this->absoluteFilePath};
  51. }
  52. }
  53. bool ls_std::File::exists()
  54. {
  55. return ls_std::File::_exists(this->absoluteFilePath);
  56. }
  57. std::string ls_std::File::getAbsoluteFilePath() {
  58. return this->absoluteFilePath;
  59. }
  60. std::string ls_std::File::getName()
  61. {
  62. std::string copy = this->absoluteFilePath;
  63. // if it's a directory, remove separator from end, if it does exist
  64. if(ls_std::File::_isDirectory(this->absoluteFilePath)) {
  65. copy.erase(std::remove_if(copy.end() - 1, copy.end(), ls_std::FilePathSeparatorMatch()), copy.end());
  66. }
  67. // now get the file / directory name
  68. auto base = std::find_if(copy.rbegin(), copy.rend(), ls_std::FilePathSeparatorMatch()).base();
  69. return std::string(base, copy.end());
  70. }
  71. std::string ls_std::File::getParent()
  72. {
  73. return ls_std::File::_getParent(this->absoluteFilePath);
  74. }
  75. long ls_std::File::getSize()
  76. {
  77. std::streampos fileSize {};
  78. if(ls_std::File::_exists(this->absoluteFilePath)) {
  79. std::ifstream fileHandler{this->absoluteFilePath, std::ios::in};
  80. fileSize = fileHandler.tellg();
  81. fileHandler.seekg(0, std::ios::end);
  82. fileSize = fileHandler.tellg() - fileSize;
  83. fileHandler.close();
  84. }
  85. return fileSize;
  86. }
  87. bool ls_std::File::isDirectory()
  88. {
  89. return ls_std::File::_isDirectory(this->absoluteFilePath);
  90. }
  91. bool ls_std::File::isFile()
  92. {
  93. return ls_std::File::_isFile(this->absoluteFilePath);
  94. }
  95. time_t ls_std::File::lastModified()
  96. {
  97. return ls_std::File::_lastModified(this->absoluteFilePath);
  98. }
  99. std::list<std::string> ls_std::File::list()
  100. {
  101. std::list<std::string> fileList {};
  102. if(ls_std::File::_isDirectory(this->absoluteFilePath)) {
  103. fileList = ls_std::File::_list(this->absoluteFilePath);
  104. }
  105. return fileList;
  106. }
  107. std::list<std::string> ls_std::File::listFiles()
  108. {
  109. std::list<std::string> fileList {};
  110. if(ls_std::File::_isDirectory(this->absoluteFilePath)) {
  111. fileList = ls_std::File::_listFiles(this->absoluteFilePath);
  112. }
  113. return fileList;
  114. }
  115. void ls_std::File::makeDirectory()
  116. {
  117. if(ls_std::File::_mkdir(this->absoluteFilePath)) {
  118. throw ls_std::FileOperationException {this->absoluteFilePath};
  119. }
  120. }
  121. void ls_std::File::makeDirectories() {
  122. std::vector<std::string> subDirectories = ls_std::File::_splitIntoSubDirectoryNames(this->absoluteFilePath);
  123. const char separator = ls_std::FilePathSeparator::getOperatingSystemSpecificSeparator();
  124. std::string currentHierarchy {};
  125. for(const auto& subDirectory : subDirectories) {
  126. currentHierarchy += subDirectory;
  127. if(!ls_std::File::_exists(currentHierarchy)) {
  128. ls_std::File::_mkdir(currentHierarchy);
  129. }
  130. currentHierarchy += separator;
  131. }
  132. }
  133. void ls_std::File::remove()
  134. {
  135. if(ls_std::File::_isFile(this->absoluteFilePath)) {
  136. if(std::remove(this->absoluteFilePath.c_str())) {
  137. throw ls_std::FileOperationException{this->absoluteFilePath};
  138. }
  139. }
  140. if(ls_std::File::_isDirectory(this->absoluteFilePath)) {
  141. if(rmdir(this->absoluteFilePath.c_str())) {
  142. throw ls_std::FileOperationException{this->absoluteFilePath};
  143. }
  144. }
  145. }
  146. bool ls_std::File::_exists(const std::string& _path)
  147. {
  148. struct stat _stat {};
  149. return (stat(_path.c_str(), &_stat) == 0);
  150. }
  151. std::string ls_std::File::_getParent(const std::string &_path)
  152. {
  153. std::string parent {};
  154. std::vector<std::string> subDirectoryNames = ls_std::File::_splitIntoSubDirectoryNames(_path);
  155. const char separator = ls_std::FilePathSeparator::getOperatingSystemSpecificSeparator();
  156. subDirectoryNames.pop_back();
  157. for(auto const& subDirectoryName : subDirectoryNames) {
  158. parent += subDirectoryName + separator;
  159. }
  160. return parent;
  161. }
  162. bool ls_std::File::_isDirectory(const std::string& _path)
  163. {
  164. bool match {};
  165. struct stat _stat {};
  166. if(stat(_path.c_str(), &_stat) == 0) {
  167. match = _stat.st_mode & (unsigned short) S_IFDIR;
  168. }
  169. return match;
  170. }
  171. bool ls_std::File::_isFile(const std::string& _path)
  172. {
  173. bool match {};
  174. struct stat _stat {};
  175. if(stat(_path.c_str(), &_stat) == 0) {
  176. match = _stat.st_mode & (unsigned short) S_IFREG;
  177. }
  178. return match;
  179. }
  180. time_t ls_std::File::_lastModified(const std::string &_path)
  181. {
  182. time_t lastModifiedTimeStamp {};
  183. struct stat _stat {};
  184. if(stat(_path.c_str(), &_stat) == 0) {
  185. lastModifiedTimeStamp = _stat.st_mtime;
  186. }
  187. return lastModifiedTimeStamp;
  188. }
  189. std::list<std::string> ls_std::File::_list(const std::string &_path)
  190. {
  191. std::list<std::string> filesInDirectory {};
  192. #if defined(unix) || defined(__APPLE__)
  193. filesInDirectory = ls_std::File::_listUnix(_path, true);
  194. #endif
  195. #ifdef _WIN32
  196. filesInDirectory = ls_std::File::_listWindows(_path, true);
  197. #endif
  198. return filesInDirectory;
  199. }
  200. std::list<std::string> ls_std::File::_listFiles(const std::string &_path)
  201. {
  202. std::list<std::string> filesInDirectory {};
  203. #if defined(unix) || defined(__APPLE__)
  204. filesInDirectory = ls_std::File::_listUnix(_path, false);
  205. #endif
  206. #ifdef _WIN32
  207. filesInDirectory = ls_std::File::_listWindows(_path, false);
  208. #endif
  209. return filesInDirectory;
  210. }
  211. #if defined(unix) || defined(__APPLE__)
  212. std::list<std::string> ls_std::File::_listUnix(const std::string &_path, bool withDirectories)
  213. {
  214. std::list<std::string> filesInDirectory {};
  215. DIR* directory = opendir(_path.c_str());
  216. struct dirent* directoryEntity;
  217. std::string parent = ls_std::File::_getParent(_path);
  218. std::string absolutePath {};
  219. while((directoryEntity = readdir(directory)) != nullptr) {
  220. absolutePath = parent + directoryEntity->d_name;
  221. if(withDirectories) {
  222. filesInDirectory.emplace_back(absolutePath);
  223. } else {
  224. if(ls_std::File::_isFile(absolutePath)) {
  225. filesInDirectory.emplace_back(absolutePath);
  226. }
  227. }
  228. }
  229. return filesInDirectory;
  230. }
  231. #endif
  232. #ifdef _WIN32
  233. std::list<std::string> ls_std::File::_listWindows(const std::string &_path, bool withDirectories)
  234. {
  235. std::list<std::string> filesInDirectory {};
  236. WIN32_FIND_DATA data {};
  237. HANDLE hFind;
  238. std::string parent = ls_std::File::_getParent(_path);
  239. std::string pattern {_path + ls_std::FilePathSeparator::getOperatingSystemSpecificSeparator() + "*"};
  240. std::string absolutePath {};
  241. if((hFind = FindFirstFile(pattern.c_str(), &data)) != INVALID_HANDLE_VALUE) {
  242. do {
  243. absolutePath = parent + data.cFileName;
  244. if(withDirectories) {
  245. filesInDirectory.emplace_back(absolutePath);
  246. } else {
  247. if(ls_std::File::_isFile(absolutePath)) {
  248. filesInDirectory.emplace_back(absolutePath);
  249. }
  250. }
  251. }
  252. while(FindNextFile(hFind, &data) != 0);
  253. FindClose(hFind);
  254. }
  255. return filesInDirectory;
  256. }
  257. #endif
  258. int ls_std::File::_mkdir(const std::string& _path) {
  259. int result;
  260. #ifdef _WIN32
  261. result = mkdir(_path.c_str());
  262. #endif
  263. #if defined(unix) || defined(__APPLE__)
  264. result = mkdir(_path.c_str(), 0777);
  265. #endif
  266. return result;
  267. }
  268. std::string ls_std::File::_normalizePath(std::string _path)
  269. {
  270. const char unixSeparator = ls_std::FilePathSeparator::getUnixFilePathSeparator();
  271. const char windowsSeparator = ls_std::FilePathSeparator::getWindowsFilePathSeparator();
  272. #if defined(unix) || defined(__APPLE__)
  273. std::replace(_path.begin(), _path.end(), windowsSeparator, unixSeparator);
  274. #endif
  275. #ifdef _WIN32
  276. std::replace(_path.begin(), _path.end(), unixSeparator, windowsSeparator);
  277. #endif
  278. return _path;
  279. }
  280. std::vector<std::string> ls_std::File::_splitIntoSubDirectoryNames(const std::string& _path) {
  281. std::vector<std::string> subDirectoryNames {};
  282. std::stringstream _stream {_path};
  283. std::string subDirectoryName {};
  284. const char separator = ls_std::FilePathSeparator::getOperatingSystemSpecificSeparator();
  285. while(std::getline(_stream, subDirectoryName, separator)) {
  286. subDirectoryNames.push_back(subDirectoryName);
  287. }
  288. return subDirectoryNames;
  289. }