File.cpp 13 KB

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