File.cpp 12 KB

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