File.cpp 13 KB

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