File.cpp 15 KB

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