File.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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-19
  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 ls_std::File::_equals(*this, _file);
  27. }
  28. bool ls_std::File::operator!=(File &_file)
  29. {
  30. return !ls_std::File::_equals(*this, _file);
  31. }
  32. bool ls_std::File::canExecute()
  33. {
  34. return ls_std::File::_isExecutable(this->absoluteFilePath);
  35. }
  36. bool ls_std::File::canRead()
  37. {
  38. bool readable;
  39. #if defined(unix) || defined(__APPLE__)
  40. readable = ls_std::File::_isReadableUnix(this->absoluteFilePath);
  41. #endif
  42. #ifdef _WIN32
  43. readable = ls_std::File::_isReadableWindows(this->absoluteFilePath);
  44. #endif
  45. return readable;
  46. }
  47. bool ls_std::File::canWrite()
  48. {
  49. return ls_std::File::_isWritable(this->absoluteFilePath);
  50. }
  51. void ls_std::File::createNewFile()
  52. {
  53. if(!ls_std::File::_exists(this->absoluteFilePath)) {
  54. std::ofstream file {this->absoluteFilePath};
  55. file.close();
  56. } else {
  57. throw ls_std::FileOperationException{};
  58. }
  59. }
  60. bool ls_std::File::exists()
  61. {
  62. return ls_std::File::_exists(this->absoluteFilePath);
  63. }
  64. std::string ls_std::File::getAbsoluteFilePath() {
  65. return this->absoluteFilePath;
  66. }
  67. std::string ls_std::File::getName()
  68. {
  69. std::string copy = this->absoluteFilePath;
  70. // if it's a directory, remove separator from end, if it does exist
  71. if(ls_std::File::_isDirectory(this->absoluteFilePath)) {
  72. copy.erase(std::remove_if(copy.end() - 1, copy.end(), ls_std::FilePathSeparatorMatch()), copy.end());
  73. }
  74. // now get the file / directory name
  75. auto base = std::find_if(copy.rbegin(), copy.rend(), ls_std::FilePathSeparatorMatch()).base();
  76. return std::string(base, copy.end());
  77. }
  78. std::string ls_std::File::getParent()
  79. {
  80. return ls_std::File::_getParent(this->absoluteFilePath);
  81. }
  82. long ls_std::File::getSize()
  83. {
  84. std::streampos fileSize {};
  85. if(ls_std::File::_exists(this->absoluteFilePath)) {
  86. std::ifstream fileHandler{this->absoluteFilePath, std::ios::in};
  87. fileSize = fileHandler.tellg();
  88. fileHandler.seekg(0, std::ios::end);
  89. fileSize = fileHandler.tellg() - fileSize;
  90. fileHandler.close();
  91. }
  92. return fileSize;
  93. }
  94. bool ls_std::File::isDirectory()
  95. {
  96. return ls_std::File::_isDirectory(this->absoluteFilePath);
  97. }
  98. bool ls_std::File::isFile()
  99. {
  100. return ls_std::File::_isFile(this->absoluteFilePath);
  101. }
  102. time_t ls_std::File::lastModified()
  103. {
  104. return ls_std::File::_lastModified(this->absoluteFilePath);
  105. }
  106. std::list<std::string> ls_std::File::list()
  107. {
  108. std::list<std::string> fileList {};
  109. if(ls_std::File::_isDirectory(this->absoluteFilePath)) {
  110. fileList = ls_std::File::_list(this->absoluteFilePath);
  111. }
  112. return fileList;
  113. }
  114. std::list<std::string> ls_std::File::listFiles()
  115. {
  116. std::list<std::string> fileList {};
  117. if(ls_std::File::_isDirectory(this->absoluteFilePath)) {
  118. fileList = ls_std::File::_listFiles(this->absoluteFilePath);
  119. }
  120. return fileList;
  121. }
  122. void ls_std::File::makeDirectory()
  123. {
  124. if(ls_std::File::_mkdir(this->absoluteFilePath)) {
  125. throw ls_std::FileOperationException {};
  126. }
  127. }
  128. void ls_std::File::makeDirectories() {
  129. std::vector<std::string> subDirectories = ls_std::File::_splitIntoSubDirectoryNames(this->absoluteFilePath);
  130. const char separator = ls_std::FilePathSeparator::get();
  131. std::string currentHierarchy {};
  132. for(const auto& subDirectory : subDirectories) {
  133. currentHierarchy += subDirectory;
  134. if(!ls_std::File::_exists(currentHierarchy)) {
  135. ls_std::File::_mkdir(currentHierarchy);
  136. }
  137. currentHierarchy += separator;
  138. }
  139. }
  140. void ls_std::File::remove()
  141. {
  142. if(ls_std::File::_isFile(this->absoluteFilePath)) {
  143. if(std::remove(this->absoluteFilePath.c_str())) {
  144. throw ls_std::FileOperationException{};
  145. }
  146. }
  147. if(ls_std::File::_isDirectory(this->absoluteFilePath)) {
  148. if(rmdir(this->absoluteFilePath.c_str())) {
  149. throw ls_std::FileOperationException{};
  150. }
  151. }
  152. }
  153. bool ls_std::File::renameTo(const std::string &_newName)
  154. {
  155. bool renamed = ls_std::File::_renameTo(this->absoluteFilePath, _newName);
  156. if(renamed) {
  157. this->absoluteFilePath = _newName;
  158. }
  159. return renamed;
  160. }
  161. void ls_std::File::reset(const std::string &_newPath)
  162. {
  163. this->absoluteFilePath = ls_std::File::_normalizePath(_newPath);
  164. }
  165. #ifdef _WIN32
  166. void ls_std::File::_addToFileListWindows(const std::string& _path, bool _withDirectories, WIN32_FIND_DATA _data, std::list<std::string>& _list)
  167. {
  168. const char separator = ls_std::FilePathSeparator::get();
  169. std::string absolutePath = _path + separator + _data.cFileName;
  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. #if defined(unix) || defined(__APPLE__)
  180. void ls_std::File::_addToFileListUnix(const std::string& _path, bool _withDirectories, dirent* directoryEntity, std::list<std::string>& _list)
  181. {
  182. const char separator = ls_std::FilePathSeparator::get();
  183. std::string absolutePath = _path + separator + directoryEntity->d_name;
  184. if(_withDirectories) {
  185. _list.emplace_back(absolutePath);
  186. } else {
  187. if(ls_std::File::_isFile(absolutePath)) {
  188. _list.emplace_back(absolutePath);
  189. }
  190. }
  191. }
  192. #endif
  193. bool ls_std::File::_equals(File &_file, File &_foreignFile)
  194. {
  195. bool isEqual = _file.getAbsoluteFilePath() == _foreignFile.getAbsoluteFilePath();
  196. if(_file.exists() && _foreignFile.exists()) {
  197. isEqual = isEqual && _file.canRead() == _foreignFile.canRead();
  198. isEqual = isEqual && _file.canWrite() == _foreignFile.canWrite();
  199. isEqual = isEqual && _file.canExecute() == _foreignFile.canExecute();
  200. }
  201. return isEqual;
  202. }
  203. bool ls_std::File::_exists(const std::string& _path)
  204. {
  205. struct stat _stat {};
  206. return (stat(_path.c_str(), &_stat) == 0);
  207. }
  208. std::string ls_std::File::_getParent(const std::string &_path)
  209. {
  210. std::string parent {};
  211. std::vector<std::string> subDirectoryNames = ls_std::File::_splitIntoSubDirectoryNames(_path);
  212. const char separator = ls_std::FilePathSeparator::get();
  213. subDirectoryNames.pop_back();
  214. for(auto const& subDirectoryName : subDirectoryNames) {
  215. parent += subDirectoryName + separator;
  216. }
  217. return parent;
  218. }
  219. bool ls_std::File::_isDirectory(const std::string& _path)
  220. {
  221. bool match {};
  222. struct stat _stat {};
  223. if(stat(_path.c_str(), &_stat) == 0) {
  224. match = _stat.st_mode & (unsigned short) S_IFDIR;
  225. }
  226. return match;
  227. }
  228. bool ls_std::File::_isExecutable(const std::string &_path)
  229. {
  230. bool executable {};
  231. if(ls_std::File::_exists(_path)) {
  232. struct stat _stat {};
  233. if(stat(_path.c_str(), &_stat) == 0) {
  234. executable = (_stat.st_mode & (unsigned short) S_IEXEC) != 0;
  235. }
  236. }
  237. return executable;
  238. }
  239. bool ls_std::File::_isFile(const std::string& _path)
  240. {
  241. bool match {};
  242. struct stat _stat {};
  243. if(stat(_path.c_str(), &_stat) == 0) {
  244. match = _stat.st_mode & (unsigned) S_IFREG;
  245. }
  246. return match;
  247. }
  248. #if defined(unix) || defined(__APPLE__)
  249. bool ls_std::File::_isReadableUnix(const std::string &_path)
  250. {
  251. bool readable {};
  252. if(ls_std::File::_exists(_path)) {
  253. struct stat _stat {};
  254. if(stat(_path.c_str(), &_stat) == 0) {
  255. readable = (_stat.st_mode & (unsigned) S_IREAD) != 0;
  256. }
  257. }
  258. return readable;
  259. }
  260. #endif
  261. #ifdef _WIN32
  262. bool ls_std::File::_isReadableWindows(const std::string &_path)
  263. {
  264. bool readable;
  265. WIN32_FIND_DATA data {};
  266. HANDLE handleFind = FindFirstFile(_path.c_str(), &data);
  267. if(handleFind != INVALID_HANDLE_VALUE) {
  268. readable = GetFileAttributes(data.cFileName) & (unsigned) FILE_ATTRIBUTE_READONLY;
  269. } else {
  270. throw ls_std::FileOperationException {};
  271. }
  272. return readable;
  273. }
  274. #endif
  275. bool ls_std::File::_isWritable(const std::string &_path)
  276. {
  277. bool writable {};
  278. if(ls_std::File::_exists(_path)) {
  279. struct stat _stat {};
  280. if(stat(_path.c_str(), &_stat) == 0) {
  281. writable = (_stat.st_mode & (unsigned) S_IWRITE) != 0;
  282. }
  283. }
  284. return writable;
  285. }
  286. time_t ls_std::File::_lastModified(const std::string &_path)
  287. {
  288. time_t lastModifiedTimeStamp {};
  289. struct stat _stat {};
  290. if(stat(_path.c_str(), &_stat) == 0) {
  291. lastModifiedTimeStamp = _stat.st_mtime;
  292. }
  293. return lastModifiedTimeStamp;
  294. }
  295. std::list<std::string> ls_std::File::_list(const std::string &_path)
  296. {
  297. std::list<std::string> filesInDirectory {};
  298. #if defined(unix) || defined(__APPLE__)
  299. filesInDirectory = ls_std::File::_listUnix(_path, true);
  300. #endif
  301. #ifdef _WIN32
  302. filesInDirectory = ls_std::File::_listWindows(_path, true);
  303. #endif
  304. return filesInDirectory;
  305. }
  306. std::list<std::string> ls_std::File::_listFiles(const std::string &_path)
  307. {
  308. std::list<std::string> filesInDirectory {};
  309. #if defined(unix) || defined(__APPLE__)
  310. filesInDirectory = ls_std::File::_listUnix(_path, false);
  311. #endif
  312. #ifdef _WIN32
  313. filesInDirectory = ls_std::File::_listWindows(_path, false);
  314. #endif
  315. return filesInDirectory;
  316. }
  317. #if defined(unix) || defined(__APPLE__)
  318. std::list<std::string> ls_std::File::_listUnix(const std::string &_path, bool withDirectories)
  319. {
  320. std::list<std::string> filesInDirectory {};
  321. DIR* directory = opendir(_path.c_str());
  322. struct dirent* directoryEntity;
  323. std::string absolutePath {};
  324. while((directoryEntity = readdir(directory)) != nullptr) {
  325. ls_std::File::_addToFileListUnix(_path, withDirectories, directoryEntity, filesInDirectory);
  326. }
  327. return filesInDirectory;
  328. }
  329. #endif
  330. #ifdef _WIN32
  331. std::list<std::string> ls_std::File::_listWindows(const std::string &_path, bool withDirectories)
  332. {
  333. std::list<std::string> filesInDirectory {};
  334. WIN32_FIND_DATA data {};
  335. HANDLE hFind;
  336. std::string pattern {_path + ls_std::FilePathSeparator::get() + "*"};
  337. if((hFind = FindFirstFile(pattern.c_str(), &data)) != INVALID_HANDLE_VALUE) {
  338. do {
  339. ls_std::File::_addToFileListWindows(_path, withDirectories, data, filesInDirectory);
  340. }
  341. while(FindNextFile(hFind, &data) != 0);
  342. FindClose(hFind);
  343. }
  344. return filesInDirectory;
  345. }
  346. #endif
  347. int ls_std::File::_mkdir(const std::string& _path) {
  348. int result;
  349. #ifdef _WIN32
  350. result = mkdir(_path.c_str());
  351. #endif
  352. #if defined(unix) || defined(__APPLE__)
  353. result = mkdir(_path.c_str(), 0777);
  354. #endif
  355. return result;
  356. }
  357. std::string ls_std::File::_normalizePath(std::string _path)
  358. {
  359. _path = ls_std::File::_replaceWrongSeparator(_path);
  360. _path = ls_std::File::_reduceSeparators(_path);
  361. return _path;
  362. }
  363. std::string ls_std::File::_reduceSeparators(const std::string& _path)
  364. {
  365. static const char separator = {ls_std::FilePathSeparator::get()};
  366. std::string normalizedPath {};
  367. int index {};
  368. while(index < _path.size()) {
  369. if(_path[index] == separator) {
  370. normalizedPath += _path[index];
  371. do {
  372. index++;
  373. }
  374. while(_path[index] == separator);
  375. } else {
  376. normalizedPath += _path[index];
  377. index++;
  378. }
  379. }
  380. return normalizedPath;
  381. }
  382. bool ls_std::File::_renameTo(const std::string &_oldName, const std::string &_newName)
  383. {
  384. return std::rename(_oldName.c_str(), _newName.c_str()) == 0;
  385. }
  386. std::string ls_std::File::_replaceWrongSeparator(std::string _path)
  387. {
  388. static const char unixSeparator = ls_std::FilePathSeparator::getUnixFilePathSeparator();
  389. static const char windowsSeparator = ls_std::FilePathSeparator::getWindowsFilePathSeparator();
  390. #if defined(unix) || defined(__APPLE__)
  391. std::replace(_path.begin(), _path.end(), windowsSeparator, unixSeparator);
  392. #endif
  393. #ifdef _WIN32
  394. std::replace(_path.begin(), _path.end(), unixSeparator, windowsSeparator);
  395. #endif
  396. return _path;
  397. }
  398. std::vector<std::string> ls_std::File::_splitIntoSubDirectoryNames(const std::string& _path) {
  399. std::vector<std::string> subDirectoryNames {};
  400. std::stringstream _stream {_path};
  401. std::string subDirectoryName {};
  402. const char separator = ls_std::FilePathSeparator::get();
  403. while(std::getline(_stream, subDirectoryName, separator)) {
  404. subDirectoryNames.push_back(subDirectoryName);
  405. }
  406. return subDirectoryNames;
  407. }