File.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-08-15
  6. * Changed: 2021-09-24
  7. *
  8. * */
  9. #include <ls_std/io/File.hpp>
  10. #include <ls_std/exception/FileOperationException.hpp>
  11. #include <ls_std/io/FilePathSeparatorMatch.hpp>
  12. #include <fstream>
  13. #include <algorithm>
  14. #include <sstream>
  15. #include <vector>
  16. #include <cstdio>
  17. #include <sys/stat.h>
  18. #if defined(unix) || defined(__APPLE__)
  19. #include <unistd.h>
  20. #endif
  21. #ifdef _WIN32
  22. #include <direct.h>
  23. #endif
  24. ls_std::File::File(std::string _absoluteFilePath)
  25. : ls_std::Class("File"),
  26. absoluteFilePath(ls_std::File::_normalizePath(std::move(_absoluteFilePath)))
  27. {}
  28. bool ls_std::File::operator==(ls_std::File &_file)
  29. {
  30. return ls_std::File::_equals(*this, _file);
  31. }
  32. bool ls_std::File::operator!=(ls_std::File &_file)
  33. {
  34. return !ls_std::File::_equals(*this, _file);
  35. }
  36. bool ls_std::File::canExecute()
  37. {
  38. return ls_std::File::_isExecutable(this->absoluteFilePath);
  39. }
  40. bool ls_std::File::canRead()
  41. {
  42. bool readable;
  43. #if defined(unix) || defined(__APPLE__)
  44. readable = ls_std::File::_isReadableUnix(this->absoluteFilePath);
  45. #endif
  46. #ifdef _WIN32
  47. readable = ls_std::File::_isReadableWindows(this->absoluteFilePath);
  48. #endif
  49. return readable;
  50. }
  51. bool ls_std::File::canWrite()
  52. {
  53. return ls_std::File::_isWritable(this->absoluteFilePath);
  54. }
  55. void ls_std::File::createNewFile()
  56. {
  57. if (!ls_std::File::_exists(this->absoluteFilePath))
  58. {
  59. std::ofstream file{this->absoluteFilePath};
  60. file.close();
  61. }
  62. else
  63. {
  64. throw ls_std::FileOperationException{};
  65. }
  66. }
  67. bool ls_std::File::exists()
  68. {
  69. return ls_std::File::_exists(this->absoluteFilePath);
  70. }
  71. std::string ls_std::File::getAbsoluteFilePath()
  72. {
  73. return this->absoluteFilePath;
  74. }
  75. std::string ls_std::File::getName()
  76. {
  77. std::string copy = this->absoluteFilePath;
  78. // if it's a directory, remove separator from end, if it does exist
  79. if (ls_std::File::_isDirectory(this->absoluteFilePath))
  80. {
  81. copy.erase(std::remove_if(copy.end() - 1, copy.end(), ls_std::FilePathSeparatorMatch()), copy.end());
  82. }
  83. // now get the file / directory name
  84. auto base = std::find_if(copy.rbegin(), copy.rend(), ls_std::FilePathSeparatorMatch()).base();
  85. return std::string{base, copy.end()};
  86. }
  87. std::string ls_std::File::getParent()
  88. {
  89. return ls_std::File::_getParent(this->absoluteFilePath);
  90. }
  91. long ls_std::File::getSize()
  92. {
  93. std::streampos fileSize{};
  94. if (ls_std::File::_exists(this->absoluteFilePath))
  95. {
  96. std::ifstream fileHandler{this->absoluteFilePath, std::ios::in};
  97. fileSize = fileHandler.tellg();
  98. fileHandler.seekg(0, std::ios::end);
  99. fileSize = fileHandler.tellg() - fileSize;
  100. fileHandler.close();
  101. }
  102. return (long) fileSize;
  103. }
  104. bool ls_std::File::isDirectory()
  105. {
  106. return ls_std::File::_isDirectory(this->absoluteFilePath);
  107. }
  108. bool ls_std::File::isFile()
  109. {
  110. return ls_std::File::_isFile(this->absoluteFilePath);
  111. }
  112. time_t ls_std::File::lastModified()
  113. {
  114. return ls_std::File::_lastModified(this->absoluteFilePath);
  115. }
  116. std::list<std::string> ls_std::File::list()
  117. {
  118. std::list<std::string> fileList{};
  119. if (ls_std::File::_isDirectory(this->absoluteFilePath))
  120. {
  121. fileList = ls_std::File::_list(this->absoluteFilePath);
  122. }
  123. return fileList;
  124. }
  125. std::list<std::string> ls_std::File::listFiles()
  126. {
  127. std::list<std::string> fileList{};
  128. if (ls_std::File::_isDirectory(this->absoluteFilePath))
  129. {
  130. fileList = ls_std::File::_listFiles(this->absoluteFilePath);
  131. }
  132. return fileList;
  133. }
  134. void ls_std::File::makeDirectory()
  135. {
  136. if (ls_std::File::_mkdir(this->absoluteFilePath))
  137. {
  138. throw ls_std::FileOperationException{};
  139. }
  140. }
  141. void ls_std::File::makeDirectories()
  142. {
  143. std::vector<std::string> subDirectories = ls_std::File::_splitIntoSubDirectoryNames(this->absoluteFilePath);
  144. const char separator = ls_std::FilePathSeparator::get();
  145. std::string currentHierarchy{};
  146. for (const auto &subDirectory: subDirectories)
  147. {
  148. currentHierarchy += subDirectory;
  149. if (!ls_std::File::_exists(currentHierarchy))
  150. {
  151. ls_std::File::_mkdir(currentHierarchy);
  152. }
  153. currentHierarchy += separator;
  154. }
  155. }
  156. void ls_std::File::remove()
  157. {
  158. if (ls_std::File::_isFile(this->absoluteFilePath))
  159. {
  160. std::remove(this->absoluteFilePath.c_str());
  161. }
  162. if (ls_std::File::_isDirectory(this->absoluteFilePath))
  163. {
  164. ls_std::File::_remove(this->absoluteFilePath);
  165. }
  166. }
  167. bool ls_std::File::renameTo(const std::string &_newName)
  168. {
  169. bool renamed = ls_std::File::_renameTo(this->absoluteFilePath, _newName);
  170. if (renamed)
  171. {
  172. this->absoluteFilePath = _newName;
  173. }
  174. return renamed;
  175. }
  176. void ls_std::File::reset(const std::string &_newPath)
  177. {
  178. this->absoluteFilePath = ls_std::File::_normalizePath(_newPath);
  179. }
  180. #ifdef _WIN32
  181. void ls_std::File::_addToFileListWindows(const std::string &_path, bool _withDirectories, WIN32_FIND_DATA _data, std::list<std::string> &_list)
  182. {
  183. const char separator = ls_std::FilePathSeparator::get();
  184. std::string absolutePath = _path + separator + _data.cFileName;
  185. if (_withDirectories)
  186. {
  187. _list.emplace_back(absolutePath);
  188. }
  189. else
  190. {
  191. if (ls_std::File::_isFile(absolutePath))
  192. {
  193. _list.emplace_back(absolutePath);
  194. }
  195. }
  196. }
  197. #endif
  198. #if defined(unix) || defined(__APPLE__)
  199. void ls_std::File::_addToFileListUnix(const std::string &_path, bool _withDirectories, dirent *directoryEntity, std::list<std::string> &_list)
  200. {
  201. const char separator = ls_std::FilePathSeparator::get();
  202. std::string absolutePath = _path + separator + directoryEntity->d_name;
  203. if (_withDirectories)
  204. {
  205. _list.emplace_back(absolutePath);
  206. }
  207. else
  208. {
  209. if (ls_std::File::_isFile(absolutePath))
  210. {
  211. _list.emplace_back(absolutePath);
  212. }
  213. }
  214. }
  215. #endif
  216. bool ls_std::File::_equals(ls_std::File &_file, ls_std::File &_foreignFile)
  217. {
  218. bool isEqual = _file.getAbsoluteFilePath() == _foreignFile.getAbsoluteFilePath();
  219. if (_file.exists() && _foreignFile.exists())
  220. {
  221. isEqual = isEqual && _file.canRead() == _foreignFile.canRead();
  222. isEqual = isEqual && _file.canWrite() == _foreignFile.canWrite();
  223. isEqual = isEqual && _file.canExecute() == _foreignFile.canExecute();
  224. }
  225. return isEqual;
  226. }
  227. bool ls_std::File::_exists(const std::string &_path)
  228. {
  229. struct stat _stat{};
  230. return (stat(_path.c_str(), &_stat) == 0);
  231. }
  232. std::string ls_std::File::_getParent(const std::string &_path)
  233. {
  234. std::string parent{};
  235. std::vector<std::string> subDirectoryNames = ls_std::File::_splitIntoSubDirectoryNames(_path);
  236. const char separator = ls_std::FilePathSeparator::get();
  237. subDirectoryNames.pop_back();
  238. for (auto const &subDirectoryName: subDirectoryNames)
  239. {
  240. parent += subDirectoryName + separator;
  241. }
  242. return parent;
  243. }
  244. bool ls_std::File::_isDirectory(const std::string &_path)
  245. {
  246. bool match{};
  247. struct stat _stat{};
  248. if (stat(_path.c_str(), &_stat) == 0)
  249. {
  250. match = _stat.st_mode & (unsigned short) S_IFDIR;
  251. }
  252. return match;
  253. }
  254. bool ls_std::File::_isExecutable(const std::string &_path)
  255. {
  256. bool executable{};
  257. if (ls_std::File::_exists(_path))
  258. {
  259. struct stat _stat{};
  260. if (stat(_path.c_str(), &_stat) == 0)
  261. {
  262. executable = (_stat.st_mode & (unsigned short) S_IEXEC) != 0;
  263. }
  264. }
  265. return executable;
  266. }
  267. bool ls_std::File::_isFile(const std::string &_path)
  268. {
  269. bool match{};
  270. struct stat _stat{};
  271. if (stat(_path.c_str(), &_stat) == 0)
  272. {
  273. match = _stat.st_mode & (unsigned) S_IFREG;
  274. }
  275. return match;
  276. }
  277. #if defined(unix) || defined(__APPLE__)
  278. bool ls_std::File::_isReadableUnix(const std::string &_path)
  279. {
  280. bool readable{};
  281. if (ls_std::File::_exists(_path))
  282. {
  283. struct stat _stat{};
  284. if (stat(_path.c_str(), &_stat) == 0)
  285. {
  286. readable = (_stat.st_mode & (unsigned) S_IREAD) != 0;
  287. }
  288. }
  289. else
  290. {
  291. throw ls_std::FileOperationException{};
  292. }
  293. return readable;
  294. }
  295. #endif
  296. #ifdef _WIN32
  297. bool ls_std::File::_isReadableWindows(const std::string &_path)
  298. {
  299. bool readable;
  300. WIN32_FIND_DATA data{};
  301. HANDLE handleFind = FindFirstFile(_path.c_str(), &data);
  302. if (handleFind != INVALID_HANDLE_VALUE)
  303. {
  304. readable = GetFileAttributes(data.cFileName) & (unsigned) FILE_ATTRIBUTE_READONLY;
  305. }
  306. else
  307. {
  308. throw ls_std::FileOperationException{};
  309. }
  310. return readable;
  311. }
  312. #endif
  313. bool ls_std::File::_isWritable(const std::string &_path)
  314. {
  315. bool writable{};
  316. if (ls_std::File::_exists(_path))
  317. {
  318. struct stat _stat{};
  319. if (stat(_path.c_str(), &_stat) == 0)
  320. {
  321. writable = (_stat.st_mode & (unsigned) S_IWRITE) != 0;
  322. }
  323. }
  324. return writable;
  325. }
  326. time_t ls_std::File::_lastModified(const std::string &_path)
  327. {
  328. time_t lastModifiedTimeStamp{};
  329. struct stat _stat{};
  330. if (stat(_path.c_str(), &_stat) == 0)
  331. {
  332. lastModifiedTimeStamp = _stat.st_mtime;
  333. }
  334. return lastModifiedTimeStamp;
  335. }
  336. std::list<std::string> ls_std::File::_list(const std::string &_path)
  337. {
  338. std::list<std::string> filesInDirectory{};
  339. #if defined(unix) || defined(__APPLE__)
  340. filesInDirectory = ls_std::File::_listUnix(_path, true);
  341. #endif
  342. #ifdef _WIN32
  343. filesInDirectory = ls_std::File::_listWindows(_path, true);
  344. #endif
  345. return filesInDirectory;
  346. }
  347. std::list<std::string> ls_std::File::_listFiles(const std::string &_path)
  348. {
  349. std::list<std::string> filesInDirectory{};
  350. #if defined(unix) || defined(__APPLE__)
  351. filesInDirectory = ls_std::File::_listUnix(_path, false);
  352. #endif
  353. #ifdef _WIN32
  354. filesInDirectory = ls_std::File::_listWindows(_path, false);
  355. #endif
  356. return filesInDirectory;
  357. }
  358. #if defined(unix) || defined(__APPLE__)
  359. std::list<std::string> ls_std::File::_listUnix(const std::string &_path, bool withDirectories)
  360. {
  361. std::list<std::string> filesInDirectory{};
  362. DIR *directory = opendir(_path.c_str());
  363. struct dirent *directoryEntity;
  364. std::string absolutePath{};
  365. while ((directoryEntity = readdir(directory)) != nullptr)
  366. {
  367. ls_std::File::_addToFileListUnix(_path, withDirectories, directoryEntity, filesInDirectory);
  368. }
  369. closedir(directory);
  370. return filesInDirectory;
  371. }
  372. #endif
  373. #ifdef _WIN32
  374. std::list<std::string> ls_std::File::_listWindows(const std::string &_path, bool withDirectories)
  375. {
  376. std::list<std::string> filesInDirectory{};
  377. WIN32_FIND_DATA data{};
  378. HANDLE hFind;
  379. std::string pattern{_path + ls_std::FilePathSeparator::get() + "*"};
  380. if ((hFind = FindFirstFile(pattern.c_str(), &data)) != INVALID_HANDLE_VALUE)
  381. {
  382. do
  383. {
  384. ls_std::File::_addToFileListWindows(_path, withDirectories, data, filesInDirectory);
  385. } while (FindNextFile(hFind, &data) != 0);
  386. FindClose(hFind);
  387. }
  388. return filesInDirectory;
  389. }
  390. #endif
  391. int ls_std::File::_mkdir(const std::string &_path)
  392. {
  393. int result;
  394. #ifdef _WIN32
  395. result = mkdir(_path.c_str());
  396. #endif
  397. #if defined(unix) || defined(__APPLE__)
  398. result = mkdir(_path.c_str(), 0777);
  399. #endif
  400. return result;
  401. }
  402. std::string ls_std::File::_normalizePath(std::string _path)
  403. {
  404. _path = ls_std::File::_replaceWrongSeparator(_path);
  405. _path = ls_std::File::_reduceSeparators(_path);
  406. return _path;
  407. }
  408. std::string ls_std::File::_reduceSeparators(const std::string &_path)
  409. {
  410. static const char separator = {ls_std::FilePathSeparator::get()};
  411. std::string normalizedPath{};
  412. int index{};
  413. while (index < _path.size())
  414. {
  415. if (_path[index] == separator)
  416. {
  417. normalizedPath += _path[index];
  418. do
  419. {
  420. index++;
  421. } while (_path[index] == separator);
  422. }
  423. else
  424. {
  425. normalizedPath += _path[index];
  426. index++;
  427. }
  428. }
  429. return normalizedPath;
  430. }
  431. void ls_std::File::_remove(const std::string &_path)
  432. {
  433. #if defined(unix) || defined(__APPLE__)
  434. ls_std::File::_removeUnix(_path);
  435. #endif
  436. #ifdef _WIN32
  437. ls_std::File::_removeWindows(_path);
  438. #endif
  439. }
  440. #if defined(unix) || defined(__APPLE__)
  441. void ls_std::File::_removeUnix(const std::string &_path)
  442. {
  443. rmdir(_path.c_str());
  444. }
  445. #endif
  446. #ifdef _WIN32
  447. void ls_std::File::_removeWindows(const std::string &_path)
  448. {
  449. _rmdir(_path.c_str());
  450. }
  451. #endif
  452. bool ls_std::File::_renameTo(const std::string &_oldName, const std::string &_newName)
  453. {
  454. return std::rename(_oldName.c_str(), _newName.c_str()) == 0;
  455. }
  456. std::string ls_std::File::_replaceWrongSeparator(std::string _path)
  457. {
  458. static const char unixSeparator = ls_std::FilePathSeparator::getUnixFilePathSeparator();
  459. static const char windowsSeparator = ls_std::FilePathSeparator::getWindowsFilePathSeparator();
  460. #if defined(unix) || defined(__APPLE__)
  461. std::replace(_path.begin(), _path.end(), windowsSeparator, unixSeparator);
  462. #endif
  463. #ifdef _WIN32
  464. std::replace(_path.begin(), _path.end(), unixSeparator, windowsSeparator);
  465. #endif
  466. return _path;
  467. }
  468. std::vector<std::string> ls_std::File::_splitIntoSubDirectoryNames(const std::string &_path)
  469. {
  470. std::vector<std::string> subDirectoryNames{};
  471. std::stringstream _stream{_path};
  472. std::string subDirectoryName{};
  473. const char separator = ls_std::FilePathSeparator::get();
  474. while (std::getline(_stream, subDirectoryName, separator))
  475. {
  476. subDirectoryNames.push_back(subDirectoryName);
  477. }
  478. return subDirectoryNames;
  479. }