File.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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. #include <cstdio>
  18. #if defined(unix) || defined(__APPLE__)
  19. #include <unistd.h>
  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::createNewFile()
  42. {
  43. if(!ls_std::File::_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 ls_std::File::_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(ls_std::File::_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. std::string ls_std::File::getParent()
  69. {
  70. return ls_std::File::_getParent(this->absoluteFilePath);
  71. }
  72. long ls_std::File::getSize()
  73. {
  74. std::streampos fileSize {};
  75. if(ls_std::File::_exists(this->absoluteFilePath)) {
  76. std::ifstream fileHandler{this->absoluteFilePath, std::ios::in};
  77. fileSize = fileHandler.tellg();
  78. fileHandler.seekg(0, std::ios::end);
  79. fileSize = fileHandler.tellg() - fileSize;
  80. fileHandler.close();
  81. }
  82. return fileSize;
  83. }
  84. bool ls_std::File::isDirectory()
  85. {
  86. return ls_std::File::_isDirectory(this->absoluteFilePath);
  87. }
  88. bool ls_std::File::isFile()
  89. {
  90. return ls_std::File::_isFile(this->absoluteFilePath);
  91. }
  92. time_t ls_std::File::lastModified()
  93. {
  94. return ls_std::File::_lastModified(this->absoluteFilePath);
  95. }
  96. std::list<std::string> ls_std::File::list()
  97. {
  98. std::list<std::string> fileList {};
  99. if(ls_std::File::_isDirectory(this->absoluteFilePath)) {
  100. fileList = ls_std::File::_list(this->absoluteFilePath);
  101. }
  102. return fileList;
  103. }
  104. std::list<std::string> ls_std::File::listFiles()
  105. {
  106. std::list<std::string> fileList {};
  107. if(ls_std::File::_isDirectory(this->absoluteFilePath)) {
  108. fileList = ls_std::File::_listFiles(this->absoluteFilePath);
  109. }
  110. return fileList;
  111. }
  112. void ls_std::File::makeDirectory()
  113. {
  114. if(ls_std::File::_mkdir(this->absoluteFilePath)) {
  115. throw ls_std::FileOperationException {this->absoluteFilePath};
  116. }
  117. }
  118. void ls_std::File::makeDirectories() {
  119. std::vector<std::string> subDirectories = ls_std::File::_splitIntoSubDirectoryNames(this->absoluteFilePath);
  120. const char separator = ls_std::FilePathSeparator::getOperatingSystemSpecificSeparator();
  121. std::string currentHierarchy {};
  122. for(const auto& subDirectory : subDirectories) {
  123. currentHierarchy += subDirectory;
  124. if(!ls_std::File::_exists(currentHierarchy)) {
  125. ls_std::File::_mkdir(currentHierarchy);
  126. }
  127. currentHierarchy += separator;
  128. }
  129. }
  130. void ls_std::File::remove()
  131. {
  132. if(ls_std::File::_isFile(this->absoluteFilePath)) {
  133. if(std::remove(this->absoluteFilePath.c_str())) {
  134. throw ls_std::FileOperationException{this->absoluteFilePath};
  135. }
  136. }
  137. if(ls_std::File::_isDirectory(this->absoluteFilePath)) {
  138. if(rmdir(this->absoluteFilePath.c_str())) {
  139. throw ls_std::FileOperationException{this->absoluteFilePath};
  140. }
  141. }
  142. }
  143. bool ls_std::File::renameTo(const std::string &_newName)
  144. {
  145. bool renamed = ls_std::File::_renameTo(this->absoluteFilePath, _newName);
  146. if(renamed) {
  147. this->absoluteFilePath = _newName;
  148. }
  149. return renamed;
  150. }
  151. #ifdef _WIN32
  152. void ls_std::File::_addToFileListWindows(const std::string& _path, bool _withDirectories, WIN32_FIND_DATA _data, std::list<std::string>& _list)
  153. {
  154. const char separator = ls_std::FilePathSeparator::getOperatingSystemSpecificSeparator();
  155. std::string absolutePath = _path + separator + _data.cFileName;
  156. if(_withDirectories) {
  157. _list.emplace_back(absolutePath);
  158. } else {
  159. if(ls_std::File::_isFile(absolutePath)) {
  160. _list.emplace_back(absolutePath);
  161. }
  162. }
  163. }
  164. #endif
  165. #if defined(unix) || defined(__APPLE__)
  166. void ls_std::File::_addToFileListUnix(const std::string& _path, bool _withDirectories, dirent* directoryEntity, std::list<std::string>& _list)
  167. {
  168. const char separator = ls_std::FilePathSeparator::getOperatingSystemSpecificSeparator();
  169. std::string absolutePath = _path + separator + directoryEntity->d_name;
  170. if(_withDirectories) {
  171. _list.emplace_back(absolutePath);
  172. } else {
  173. if(ls_std::File::_isFile(absolutePath)) {
  174. _list.emplace_back(absolutePath);
  175. }
  176. }
  177. }
  178. #endif
  179. bool ls_std::File::_exists(const std::string& _path)
  180. {
  181. struct stat _stat {};
  182. return (stat(_path.c_str(), &_stat) == 0);
  183. }
  184. std::string ls_std::File::_getParent(const std::string &_path)
  185. {
  186. std::string parent {};
  187. std::vector<std::string> subDirectoryNames = ls_std::File::_splitIntoSubDirectoryNames(_path);
  188. const char separator = ls_std::FilePathSeparator::getOperatingSystemSpecificSeparator();
  189. subDirectoryNames.pop_back();
  190. for(auto const& subDirectoryName : subDirectoryNames) {
  191. parent += subDirectoryName + separator;
  192. }
  193. return parent;
  194. }
  195. bool ls_std::File::_isDirectory(const std::string& _path)
  196. {
  197. bool match {};
  198. struct stat _stat {};
  199. if(stat(_path.c_str(), &_stat) == 0) {
  200. match = _stat.st_mode & (unsigned short) S_IFDIR;
  201. }
  202. return match;
  203. }
  204. bool ls_std::File::_isFile(const std::string& _path)
  205. {
  206. bool match {};
  207. struct stat _stat {};
  208. if(stat(_path.c_str(), &_stat) == 0) {
  209. match = _stat.st_mode & (unsigned short) S_IFREG;
  210. }
  211. return match;
  212. }
  213. time_t ls_std::File::_lastModified(const std::string &_path)
  214. {
  215. time_t lastModifiedTimeStamp {};
  216. struct stat _stat {};
  217. if(stat(_path.c_str(), &_stat) == 0) {
  218. lastModifiedTimeStamp = _stat.st_mtime;
  219. }
  220. return lastModifiedTimeStamp;
  221. }
  222. std::list<std::string> ls_std::File::_list(const std::string &_path)
  223. {
  224. std::list<std::string> filesInDirectory {};
  225. #if defined(unix) || defined(__APPLE__)
  226. filesInDirectory = ls_std::File::_listUnix(_path, true);
  227. #endif
  228. #ifdef _WIN32
  229. filesInDirectory = ls_std::File::_listWindows(_path, true);
  230. #endif
  231. return filesInDirectory;
  232. }
  233. std::list<std::string> ls_std::File::_listFiles(const std::string &_path)
  234. {
  235. std::list<std::string> filesInDirectory {};
  236. #if defined(unix) || defined(__APPLE__)
  237. filesInDirectory = ls_std::File::_listUnix(_path, false);
  238. #endif
  239. #ifdef _WIN32
  240. filesInDirectory = ls_std::File::_listWindows(_path, false);
  241. #endif
  242. return filesInDirectory;
  243. }
  244. #if defined(unix) || defined(__APPLE__)
  245. std::list<std::string> ls_std::File::_listUnix(const std::string &_path, bool withDirectories)
  246. {
  247. std::list<std::string> filesInDirectory {};
  248. DIR* directory = opendir(_path.c_str());
  249. struct dirent* directoryEntity;
  250. std::string absolutePath {};
  251. while((directoryEntity = readdir(directory)) != nullptr) {
  252. ls_std::File::_addToFileListUnix(_path, withDirectories, directoryEntity, filesInDirectory);
  253. }
  254. return filesInDirectory;
  255. }
  256. #endif
  257. #ifdef _WIN32
  258. std::list<std::string> ls_std::File::_listWindows(const std::string &_path, bool withDirectories)
  259. {
  260. std::list<std::string> filesInDirectory {};
  261. WIN32_FIND_DATA data {};
  262. HANDLE hFind;
  263. std::string pattern {_path + ls_std::FilePathSeparator::getOperatingSystemSpecificSeparator() + "*"};
  264. if((hFind = FindFirstFile(pattern.c_str(), &data)) != INVALID_HANDLE_VALUE) {
  265. do {
  266. ls_std::File::_addToFileListWindows(_path, withDirectories, data, filesInDirectory);
  267. }
  268. while(FindNextFile(hFind, &data) != 0);
  269. FindClose(hFind);
  270. }
  271. return filesInDirectory;
  272. }
  273. #endif
  274. int ls_std::File::_mkdir(const std::string& _path) {
  275. int result;
  276. #ifdef _WIN32
  277. result = mkdir(_path.c_str());
  278. #endif
  279. #if defined(unix) || defined(__APPLE__)
  280. result = mkdir(_path.c_str(), 0777);
  281. #endif
  282. return result;
  283. }
  284. std::string ls_std::File::_normalizePath(std::string _path)
  285. {
  286. const char unixSeparator = ls_std::FilePathSeparator::getUnixFilePathSeparator();
  287. const char windowsSeparator = ls_std::FilePathSeparator::getWindowsFilePathSeparator();
  288. #if defined(unix) || defined(__APPLE__)
  289. std::replace(_path.begin(), _path.end(), windowsSeparator, unixSeparator);
  290. #endif
  291. #ifdef _WIN32
  292. std::replace(_path.begin(), _path.end(), unixSeparator, windowsSeparator);
  293. #endif
  294. return _path;
  295. }
  296. bool ls_std::File::_renameTo(const std::string &_oldName, const std::string &_newName)
  297. {
  298. return std::rename(_oldName.c_str(), _newName.c_str()) == 0;
  299. }
  300. std::vector<std::string> ls_std::File::_splitIntoSubDirectoryNames(const std::string& _path) {
  301. std::vector<std::string> subDirectoryNames {};
  302. std::stringstream _stream {_path};
  303. std::string subDirectoryName {};
  304. const char separator = ls_std::FilePathSeparator::getOperatingSystemSpecificSeparator();
  305. while(std::getline(_stream, subDirectoryName, separator)) {
  306. subDirectoryNames.push_back(subDirectoryName);
  307. }
  308. return subDirectoryNames;
  309. }