File.cpp 15 KB

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