File.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-08-15
  6. * Changed: 2021-04-23
  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 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. if (std::remove(this->absoluteFilePath.c_str()))
  161. {
  162. throw ls_std::FileOperationException{};
  163. }
  164. }
  165. if (ls_std::File::_isDirectory(this->absoluteFilePath))
  166. {
  167. ls_std::File::_remove(this->absoluteFilePath);
  168. }
  169. }
  170. bool ls_std::File::renameTo(const std::string &_newName)
  171. {
  172. bool renamed = ls_std::File::_renameTo(this->absoluteFilePath, _newName);
  173. if (renamed)
  174. {
  175. this->absoluteFilePath = _newName;
  176. }
  177. return renamed;
  178. }
  179. void ls_std::File::reset(const std::string &_newPath)
  180. {
  181. this->absoluteFilePath = ls_std::File::_normalizePath(_newPath);
  182. }
  183. #ifdef _WIN32
  184. void ls_std::File::_addToFileListWindows(const std::string& _path, bool _withDirectories, WIN32_FIND_DATA _data, std::list<std::string>& _list)
  185. {
  186. const char separator = ls_std::FilePathSeparator::get();
  187. std::string absolutePath = _path + separator + _data.cFileName;
  188. if(_withDirectories) {
  189. _list.emplace_back(absolutePath);
  190. } else {
  191. if(ls_std::File::_isFile(absolutePath)) {
  192. _list.emplace_back(absolutePath);
  193. }
  194. }
  195. }
  196. #endif
  197. #if defined(unix) || defined(__APPLE__)
  198. void ls_std::File::_addToFileListUnix(const std::string &_path, bool _withDirectories, dirent *directoryEntity, std::list<std::string> &_list)
  199. {
  200. const char separator = ls_std::FilePathSeparator::get();
  201. std::string absolutePath = _path + separator + directoryEntity->d_name;
  202. if (_withDirectories)
  203. {
  204. _list.emplace_back(absolutePath);
  205. }
  206. else
  207. {
  208. if (ls_std::File::_isFile(absolutePath))
  209. {
  210. _list.emplace_back(absolutePath);
  211. }
  212. }
  213. }
  214. #endif
  215. bool ls_std::File::_equals(ls_std::File &_file, ls_std::File &_foreignFile)
  216. {
  217. bool isEqual = _file.getAbsoluteFilePath() == _foreignFile.getAbsoluteFilePath();
  218. if (_file.exists() && _foreignFile.exists())
  219. {
  220. isEqual = isEqual && _file.canRead() == _foreignFile.canRead();
  221. isEqual = isEqual && _file.canWrite() == _foreignFile.canWrite();
  222. isEqual = isEqual && _file.canExecute() == _foreignFile.canExecute();
  223. }
  224. return isEqual;
  225. }
  226. bool ls_std::File::_exists(const std::string &_path)
  227. {
  228. struct stat _stat{};
  229. return (stat(_path.c_str(), &_stat) == 0);
  230. }
  231. std::string ls_std::File::_getParent(const std::string &_path)
  232. {
  233. std::string parent{};
  234. std::vector<std::string> subDirectoryNames = ls_std::File::_splitIntoSubDirectoryNames(_path);
  235. const char separator = ls_std::FilePathSeparator::get();
  236. subDirectoryNames.pop_back();
  237. for (auto const &subDirectoryName : subDirectoryNames)
  238. {
  239. parent += subDirectoryName + separator;
  240. }
  241. return parent;
  242. }
  243. bool ls_std::File::_isDirectory(const std::string &_path)
  244. {
  245. bool match{};
  246. struct stat _stat{};
  247. if (stat(_path.c_str(), &_stat) == 0)
  248. {
  249. match = _stat.st_mode & (unsigned short) S_IFDIR;
  250. }
  251. return match;
  252. }
  253. bool ls_std::File::_isExecutable(const std::string &_path)
  254. {
  255. bool executable{};
  256. if (ls_std::File::_exists(_path))
  257. {
  258. struct stat _stat{};
  259. if (stat(_path.c_str(), &_stat) == 0)
  260. {
  261. executable = (_stat.st_mode & (unsigned short) S_IEXEC) != 0;
  262. }
  263. }
  264. return executable;
  265. }
  266. bool ls_std::File::_isFile(const std::string &_path)
  267. {
  268. bool match{};
  269. struct stat _stat{};
  270. if (stat(_path.c_str(), &_stat) == 0)
  271. {
  272. match = _stat.st_mode & (unsigned) S_IFREG;
  273. }
  274. return match;
  275. }
  276. #if defined(unix) || defined(__APPLE__)
  277. bool ls_std::File::_isReadableUnix(const std::string &_path)
  278. {
  279. bool readable{};
  280. if (ls_std::File::_exists(_path))
  281. {
  282. struct stat _stat{};
  283. if (stat(_path.c_str(), &_stat) == 0)
  284. {
  285. readable = (_stat.st_mode & (unsigned) S_IREAD) != 0;
  286. }
  287. }
  288. return readable;
  289. }
  290. #endif
  291. #ifdef _WIN32
  292. bool ls_std::File::_isReadableWindows(const std::string &_path)
  293. {
  294. bool readable;
  295. WIN32_FIND_DATA data {};
  296. HANDLE handleFind = FindFirstFile(_path.c_str(), &data);
  297. if(handleFind != INVALID_HANDLE_VALUE) {
  298. readable = GetFileAttributes(data.cFileName) & (unsigned) FILE_ATTRIBUTE_READONLY;
  299. } else {
  300. throw ls_std::FileOperationException {};
  301. }
  302. return readable;
  303. }
  304. #endif
  305. bool ls_std::File::_isWritable(const std::string &_path)
  306. {
  307. bool writable{};
  308. if (ls_std::File::_exists(_path))
  309. {
  310. struct stat _stat{};
  311. if (stat(_path.c_str(), &_stat) == 0)
  312. {
  313. writable = (_stat.st_mode & (unsigned) S_IWRITE) != 0;
  314. }
  315. }
  316. return writable;
  317. }
  318. time_t ls_std::File::_lastModified(const std::string &_path)
  319. {
  320. time_t lastModifiedTimeStamp{};
  321. struct stat _stat{};
  322. if (stat(_path.c_str(), &_stat) == 0)
  323. {
  324. lastModifiedTimeStamp = _stat.st_mtime;
  325. }
  326. return lastModifiedTimeStamp;
  327. }
  328. std::list<std::string> ls_std::File::_list(const std::string &_path)
  329. {
  330. std::list<std::string> filesInDirectory{};
  331. #if defined(unix) || defined(__APPLE__)
  332. filesInDirectory = ls_std::File::_listUnix(_path, true);
  333. #endif
  334. #ifdef _WIN32
  335. filesInDirectory = ls_std::File::_listWindows(_path, true);
  336. #endif
  337. return filesInDirectory;
  338. }
  339. std::list<std::string> ls_std::File::_listFiles(const std::string &_path)
  340. {
  341. std::list<std::string> filesInDirectory{};
  342. #if defined(unix) || defined(__APPLE__)
  343. filesInDirectory = ls_std::File::_listUnix(_path, false);
  344. #endif
  345. #ifdef _WIN32
  346. filesInDirectory = ls_std::File::_listWindows(_path, false);
  347. #endif
  348. return filesInDirectory;
  349. }
  350. #if defined(unix) || defined(__APPLE__)
  351. std::list<std::string> ls_std::File::_listUnix(const std::string &_path, bool withDirectories)
  352. {
  353. std::list<std::string> filesInDirectory{};
  354. DIR *directory = opendir(_path.c_str());
  355. struct dirent *directoryEntity;
  356. std::string absolutePath{};
  357. while ((directoryEntity = readdir(directory)) != nullptr)
  358. {
  359. ls_std::File::_addToFileListUnix(_path, withDirectories, directoryEntity, filesInDirectory);
  360. }
  361. closedir(directory);
  362. return filesInDirectory;
  363. }
  364. #endif
  365. #ifdef _WIN32
  366. std::list<std::string> ls_std::File::_listWindows(const std::string &_path, bool withDirectories)
  367. {
  368. std::list<std::string> filesInDirectory {};
  369. WIN32_FIND_DATA data {};
  370. HANDLE hFind;
  371. std::string pattern {_path + ls_std::FilePathSeparator::get() + "*"};
  372. if((hFind = FindFirstFile(pattern.c_str(), &data)) != INVALID_HANDLE_VALUE) {
  373. do {
  374. ls_std::File::_addToFileListWindows(_path, withDirectories, data, filesInDirectory);
  375. }
  376. while(FindNextFile(hFind, &data) != 0);
  377. FindClose(hFind);
  378. }
  379. return filesInDirectory;
  380. }
  381. #endif
  382. int ls_std::File::_mkdir(const std::string &_path)
  383. {
  384. int result;
  385. #ifdef _WIN32
  386. result = mkdir(_path.c_str());
  387. #endif
  388. #if defined(unix) || defined(__APPLE__)
  389. result = mkdir(_path.c_str(), 0777);
  390. #endif
  391. return result;
  392. }
  393. std::string ls_std::File::_normalizePath(std::string _path)
  394. {
  395. _path = ls_std::File::_replaceWrongSeparator(_path);
  396. _path = ls_std::File::_reduceSeparators(_path);
  397. return _path;
  398. }
  399. std::string ls_std::File::_reduceSeparators(const std::string &_path)
  400. {
  401. static const char separator = {ls_std::FilePathSeparator::get()};
  402. std::string normalizedPath{};
  403. int index{};
  404. while (index < _path.size())
  405. {
  406. if (_path[index] == separator)
  407. {
  408. normalizedPath += _path[index];
  409. do
  410. {
  411. index++;
  412. } while (_path[index] == separator);
  413. }
  414. else
  415. {
  416. normalizedPath += _path[index];
  417. index++;
  418. }
  419. }
  420. return normalizedPath;
  421. }
  422. void ls_std::File::_remove(const std::string &_path)
  423. {
  424. #if defined(unix) || defined(__APPLE__)
  425. ls_std::File::_removeUnix(_path);
  426. #endif
  427. #ifdef _WIN32
  428. ls_std::File::_removeWindows(_path);
  429. #endif
  430. }
  431. #if defined(unix) || defined(__APPLE__)
  432. void ls_std::File::_removeUnix(const std::string &_path)
  433. {
  434. rmdir(_path.c_str());
  435. }
  436. #endif
  437. #ifdef _WIN32
  438. void ls_std::File::_removeWindows(const std::string &_path)
  439. {
  440. _rmdir(_path.c_str());
  441. }
  442. #endif
  443. bool ls_std::File::_renameTo(const std::string &_oldName, const std::string &_newName)
  444. {
  445. return std::rename(_oldName.c_str(), _newName.c_str()) == 0;
  446. }
  447. std::string ls_std::File::_replaceWrongSeparator(std::string _path)
  448. {
  449. static const char unixSeparator = ls_std::FilePathSeparator::getUnixFilePathSeparator();
  450. static const char windowsSeparator = ls_std::FilePathSeparator::getWindowsFilePathSeparator();
  451. #if defined(unix) || defined(__APPLE__)
  452. std::replace(_path.begin(), _path.end(), windowsSeparator, unixSeparator);
  453. #endif
  454. #ifdef _WIN32
  455. std::replace(_path.begin(), _path.end(), unixSeparator, windowsSeparator);
  456. #endif
  457. return _path;
  458. }
  459. std::vector<std::string> ls_std::File::_splitIntoSubDirectoryNames(const std::string &_path)
  460. {
  461. std::vector<std::string> subDirectoryNames{};
  462. std::stringstream _stream{_path};
  463. std::string subDirectoryName{};
  464. const char separator = ls_std::FilePathSeparator::get();
  465. while (std::getline(_stream, subDirectoryName, separator))
  466. {
  467. subDirectoryNames.push_back(subDirectoryName);
  468. }
  469. return subDirectoryNames;
  470. }