File.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  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-17
  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. return readable;
  290. }
  291. #endif
  292. #ifdef _WIN32
  293. bool ls_std::File::_isReadableWindows(const std::string &_path)
  294. {
  295. bool readable;
  296. WIN32_FIND_DATA data{};
  297. HANDLE handleFind = FindFirstFile(_path.c_str(), &data);
  298. if (handleFind != INVALID_HANDLE_VALUE)
  299. {
  300. readable = GetFileAttributes(data.cFileName) & (unsigned) FILE_ATTRIBUTE_READONLY;
  301. }
  302. else
  303. {
  304. throw ls_std::FileOperationException{};
  305. }
  306. return readable;
  307. }
  308. #endif
  309. bool ls_std::File::_isWritable(const std::string &_path)
  310. {
  311. bool writable{};
  312. if (ls_std::File::_exists(_path))
  313. {
  314. struct stat _stat{};
  315. if (stat(_path.c_str(), &_stat) == 0)
  316. {
  317. writable = (_stat.st_mode & (unsigned) S_IWRITE) != 0;
  318. }
  319. }
  320. return writable;
  321. }
  322. time_t ls_std::File::_lastModified(const std::string &_path)
  323. {
  324. time_t lastModifiedTimeStamp{};
  325. struct stat _stat{};
  326. if (stat(_path.c_str(), &_stat) == 0)
  327. {
  328. lastModifiedTimeStamp = _stat.st_mtime;
  329. }
  330. return lastModifiedTimeStamp;
  331. }
  332. std::list<std::string> ls_std::File::_list(const std::string &_path)
  333. {
  334. std::list<std::string> filesInDirectory{};
  335. #if defined(unix) || defined(__APPLE__)
  336. filesInDirectory = ls_std::File::_listUnix(_path, true);
  337. #endif
  338. #ifdef _WIN32
  339. filesInDirectory = ls_std::File::_listWindows(_path, true);
  340. #endif
  341. return filesInDirectory;
  342. }
  343. std::list<std::string> ls_std::File::_listFiles(const std::string &_path)
  344. {
  345. std::list<std::string> filesInDirectory{};
  346. #if defined(unix) || defined(__APPLE__)
  347. filesInDirectory = ls_std::File::_listUnix(_path, false);
  348. #endif
  349. #ifdef _WIN32
  350. filesInDirectory = ls_std::File::_listWindows(_path, false);
  351. #endif
  352. return filesInDirectory;
  353. }
  354. #if defined(unix) || defined(__APPLE__)
  355. std::list<std::string> ls_std::File::_listUnix(const std::string &_path, bool withDirectories)
  356. {
  357. std::list<std::string> filesInDirectory{};
  358. DIR *directory = opendir(_path.c_str());
  359. struct dirent *directoryEntity;
  360. std::string absolutePath{};
  361. while ((directoryEntity = readdir(directory)) != nullptr)
  362. {
  363. ls_std::File::_addToFileListUnix(_path, withDirectories, directoryEntity, filesInDirectory);
  364. }
  365. closedir(directory);
  366. return filesInDirectory;
  367. }
  368. #endif
  369. #ifdef _WIN32
  370. std::list<std::string> ls_std::File::_listWindows(const std::string &_path, bool withDirectories)
  371. {
  372. std::list<std::string> filesInDirectory{};
  373. WIN32_FIND_DATA data{};
  374. HANDLE hFind;
  375. std::string pattern{_path + ls_std::FilePathSeparator::get() + "*"};
  376. if ((hFind = FindFirstFile(pattern.c_str(), &data)) != INVALID_HANDLE_VALUE)
  377. {
  378. do
  379. {
  380. ls_std::File::_addToFileListWindows(_path, withDirectories, data, filesInDirectory);
  381. } while (FindNextFile(hFind, &data) != 0);
  382. FindClose(hFind);
  383. }
  384. return filesInDirectory;
  385. }
  386. #endif
  387. int ls_std::File::_mkdir(const std::string &_path)
  388. {
  389. int result;
  390. #ifdef _WIN32
  391. result = mkdir(_path.c_str());
  392. #endif
  393. #if defined(unix) || defined(__APPLE__)
  394. result = mkdir(_path.c_str(), 0777);
  395. #endif
  396. return result;
  397. }
  398. std::string ls_std::File::_normalizePath(std::string _path)
  399. {
  400. _path = ls_std::File::_replaceWrongSeparator(_path);
  401. _path = ls_std::File::_reduceSeparators(_path);
  402. return _path;
  403. }
  404. std::string ls_std::File::_reduceSeparators(const std::string &_path)
  405. {
  406. static const char separator = {ls_std::FilePathSeparator::get()};
  407. std::string normalizedPath{};
  408. int index{};
  409. while (index < _path.size())
  410. {
  411. if (_path[index] == separator)
  412. {
  413. normalizedPath += _path[index];
  414. do
  415. {
  416. index++;
  417. } while (_path[index] == separator);
  418. }
  419. else
  420. {
  421. normalizedPath += _path[index];
  422. index++;
  423. }
  424. }
  425. return normalizedPath;
  426. }
  427. void ls_std::File::_remove(const std::string &_path)
  428. {
  429. #if defined(unix) || defined(__APPLE__)
  430. ls_std::File::_removeUnix(_path);
  431. #endif
  432. #ifdef _WIN32
  433. ls_std::File::_removeWindows(_path);
  434. #endif
  435. }
  436. #if defined(unix) || defined(__APPLE__)
  437. void ls_std::File::_removeUnix(const std::string &_path)
  438. {
  439. rmdir(_path.c_str());
  440. }
  441. #endif
  442. #ifdef _WIN32
  443. void ls_std::File::_removeWindows(const std::string &_path)
  444. {
  445. _rmdir(_path.c_str());
  446. }
  447. #endif
  448. bool ls_std::File::_renameTo(const std::string &_oldName, const std::string &_newName)
  449. {
  450. return std::rename(_oldName.c_str(), _newName.c_str()) == 0;
  451. }
  452. std::string ls_std::File::_replaceWrongSeparator(std::string _path)
  453. {
  454. static const char unixSeparator = ls_std::FilePathSeparator::getUnixFilePathSeparator();
  455. static const char windowsSeparator = ls_std::FilePathSeparator::getWindowsFilePathSeparator();
  456. #if defined(unix) || defined(__APPLE__)
  457. std::replace(_path.begin(), _path.end(), windowsSeparator, unixSeparator);
  458. #endif
  459. #ifdef _WIN32
  460. std::replace(_path.begin(), _path.end(), unixSeparator, windowsSeparator);
  461. #endif
  462. return _path;
  463. }
  464. std::vector<std::string> ls_std::File::_splitIntoSubDirectoryNames(const std::string &_path)
  465. {
  466. std::vector<std::string> subDirectoryNames{};
  467. std::stringstream _stream{_path};
  468. std::string subDirectoryName{};
  469. const char separator = ls_std::FilePathSeparator::get();
  470. while (std::getline(_stream, subDirectoryName, separator))
  471. {
  472. subDirectoryNames.push_back(subDirectoryName);
  473. }
  474. return subDirectoryNames;
  475. }