File.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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-18
  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. bool executable {};
  38. struct stat _stat {};
  39. if(stat(this->absoluteFilePath.c_str(), &_stat) == 0) {
  40. executable = (_stat.st_mode & (unsigned short) S_IEXEC) != 0;
  41. }
  42. return executable;
  43. }
  44. void ls_std::File::createNewFile()
  45. {
  46. if(!ls_std::File::_exists(this->absoluteFilePath)) {
  47. std::ofstream file {this->absoluteFilePath};
  48. file.close();
  49. } else {
  50. throw ls_std::FileOperationException{};
  51. }
  52. }
  53. bool ls_std::File::exists()
  54. {
  55. return ls_std::File::_exists(this->absoluteFilePath);
  56. }
  57. std::string ls_std::File::getAbsoluteFilePath() {
  58. return this->absoluteFilePath;
  59. }
  60. std::string ls_std::File::getName()
  61. {
  62. std::string copy = this->absoluteFilePath;
  63. // if it's a directory, remove separator from end, if it does exist
  64. if(ls_std::File::_isDirectory(this->absoluteFilePath)) {
  65. copy.erase(std::remove_if(copy.end() - 1, copy.end(), ls_std::FilePathSeparatorMatch()), copy.end());
  66. }
  67. // now get the file / directory name
  68. auto base = std::find_if(copy.rbegin(), copy.rend(), ls_std::FilePathSeparatorMatch()).base();
  69. return std::string(base, copy.end());
  70. }
  71. std::string ls_std::File::getParent()
  72. {
  73. return ls_std::File::_getParent(this->absoluteFilePath);
  74. }
  75. long ls_std::File::getSize()
  76. {
  77. std::streampos fileSize {};
  78. if(ls_std::File::_exists(this->absoluteFilePath)) {
  79. std::ifstream fileHandler{this->absoluteFilePath, std::ios::in};
  80. fileSize = fileHandler.tellg();
  81. fileHandler.seekg(0, std::ios::end);
  82. fileSize = fileHandler.tellg() - fileSize;
  83. fileHandler.close();
  84. }
  85. return fileSize;
  86. }
  87. bool ls_std::File::isDirectory()
  88. {
  89. return ls_std::File::_isDirectory(this->absoluteFilePath);
  90. }
  91. bool ls_std::File::isFile()
  92. {
  93. return ls_std::File::_isFile(this->absoluteFilePath);
  94. }
  95. time_t ls_std::File::lastModified()
  96. {
  97. return ls_std::File::_lastModified(this->absoluteFilePath);
  98. }
  99. std::list<std::string> ls_std::File::list()
  100. {
  101. std::list<std::string> fileList {};
  102. if(ls_std::File::_isDirectory(this->absoluteFilePath)) {
  103. fileList = ls_std::File::_list(this->absoluteFilePath);
  104. }
  105. return fileList;
  106. }
  107. std::list<std::string> ls_std::File::listFiles()
  108. {
  109. std::list<std::string> fileList {};
  110. if(ls_std::File::_isDirectory(this->absoluteFilePath)) {
  111. fileList = ls_std::File::_listFiles(this->absoluteFilePath);
  112. }
  113. return fileList;
  114. }
  115. void ls_std::File::makeDirectory()
  116. {
  117. if(ls_std::File::_mkdir(this->absoluteFilePath)) {
  118. throw ls_std::FileOperationException {};
  119. }
  120. }
  121. void ls_std::File::makeDirectories() {
  122. std::vector<std::string> subDirectories = ls_std::File::_splitIntoSubDirectoryNames(this->absoluteFilePath);
  123. const char separator = ls_std::FilePathSeparator::get();
  124. std::string currentHierarchy {};
  125. for(const auto& subDirectory : subDirectories) {
  126. currentHierarchy += subDirectory;
  127. if(!ls_std::File::_exists(currentHierarchy)) {
  128. ls_std::File::_mkdir(currentHierarchy);
  129. }
  130. currentHierarchy += separator;
  131. }
  132. }
  133. void ls_std::File::remove()
  134. {
  135. if(ls_std::File::_isFile(this->absoluteFilePath)) {
  136. if(std::remove(this->absoluteFilePath.c_str())) {
  137. throw ls_std::FileOperationException{};
  138. }
  139. }
  140. if(ls_std::File::_isDirectory(this->absoluteFilePath)) {
  141. if(rmdir(this->absoluteFilePath.c_str())) {
  142. throw ls_std::FileOperationException{};
  143. }
  144. }
  145. }
  146. bool ls_std::File::renameTo(const std::string &_newName)
  147. {
  148. bool renamed = ls_std::File::_renameTo(this->absoluteFilePath, _newName);
  149. if(renamed) {
  150. this->absoluteFilePath = _newName;
  151. }
  152. return renamed;
  153. }
  154. #ifdef _WIN32
  155. void ls_std::File::_addToFileListWindows(const std::string& _path, bool _withDirectories, WIN32_FIND_DATA _data, std::list<std::string>& _list)
  156. {
  157. const char separator = ls_std::FilePathSeparator::get();
  158. std::string absolutePath = _path + separator + _data.cFileName;
  159. if(_withDirectories) {
  160. _list.emplace_back(absolutePath);
  161. } else {
  162. if(ls_std::File::_isFile(absolutePath)) {
  163. _list.emplace_back(absolutePath);
  164. }
  165. }
  166. }
  167. #endif
  168. #if defined(unix) || defined(__APPLE__)
  169. void ls_std::File::_addToFileListUnix(const std::string& _path, bool _withDirectories, dirent* directoryEntity, std::list<std::string>& _list)
  170. {
  171. const char separator = ls_std::FilePathSeparator::get();
  172. std::string absolutePath = _path + separator + directoryEntity->d_name;
  173. if(_withDirectories) {
  174. _list.emplace_back(absolutePath);
  175. } else {
  176. if(ls_std::File::_isFile(absolutePath)) {
  177. _list.emplace_back(absolutePath);
  178. }
  179. }
  180. }
  181. #endif
  182. bool ls_std::File::_exists(const std::string& _path)
  183. {
  184. struct stat _stat {};
  185. return (stat(_path.c_str(), &_stat) == 0);
  186. }
  187. std::string ls_std::File::_getParent(const std::string &_path)
  188. {
  189. std::string parent {};
  190. std::vector<std::string> subDirectoryNames = ls_std::File::_splitIntoSubDirectoryNames(_path);
  191. const char separator = ls_std::FilePathSeparator::get();
  192. subDirectoryNames.pop_back();
  193. for(auto const& subDirectoryName : subDirectoryNames) {
  194. parent += subDirectoryName + separator;
  195. }
  196. return parent;
  197. }
  198. bool ls_std::File::_isDirectory(const std::string& _path)
  199. {
  200. bool match {};
  201. struct stat _stat {};
  202. if(stat(_path.c_str(), &_stat) == 0) {
  203. match = _stat.st_mode & (unsigned short) S_IFDIR;
  204. }
  205. return match;
  206. }
  207. bool ls_std::File::_isFile(const std::string& _path)
  208. {
  209. bool match {};
  210. struct stat _stat {};
  211. if(stat(_path.c_str(), &_stat) == 0) {
  212. match = _stat.st_mode & (unsigned short) S_IFREG;
  213. }
  214. return match;
  215. }
  216. time_t ls_std::File::_lastModified(const std::string &_path)
  217. {
  218. time_t lastModifiedTimeStamp {};
  219. struct stat _stat {};
  220. if(stat(_path.c_str(), &_stat) == 0) {
  221. lastModifiedTimeStamp = _stat.st_mtime;
  222. }
  223. return lastModifiedTimeStamp;
  224. }
  225. std::list<std::string> ls_std::File::_list(const std::string &_path)
  226. {
  227. std::list<std::string> filesInDirectory {};
  228. #if defined(unix) || defined(__APPLE__)
  229. filesInDirectory = ls_std::File::_listUnix(_path, true);
  230. #endif
  231. #ifdef _WIN32
  232. filesInDirectory = ls_std::File::_listWindows(_path, true);
  233. #endif
  234. return filesInDirectory;
  235. }
  236. std::list<std::string> ls_std::File::_listFiles(const std::string &_path)
  237. {
  238. std::list<std::string> filesInDirectory {};
  239. #if defined(unix) || defined(__APPLE__)
  240. filesInDirectory = ls_std::File::_listUnix(_path, false);
  241. #endif
  242. #ifdef _WIN32
  243. filesInDirectory = ls_std::File::_listWindows(_path, false);
  244. #endif
  245. return filesInDirectory;
  246. }
  247. #if defined(unix) || defined(__APPLE__)
  248. std::list<std::string> ls_std::File::_listUnix(const std::string &_path, bool withDirectories)
  249. {
  250. std::list<std::string> filesInDirectory {};
  251. DIR* directory = opendir(_path.c_str());
  252. struct dirent* directoryEntity;
  253. std::string absolutePath {};
  254. while((directoryEntity = readdir(directory)) != nullptr) {
  255. ls_std::File::_addToFileListUnix(_path, withDirectories, directoryEntity, filesInDirectory);
  256. }
  257. return filesInDirectory;
  258. }
  259. #endif
  260. #ifdef _WIN32
  261. std::list<std::string> ls_std::File::_listWindows(const std::string &_path, bool withDirectories)
  262. {
  263. std::list<std::string> filesInDirectory {};
  264. WIN32_FIND_DATA data {};
  265. HANDLE hFind;
  266. std::string pattern {_path + ls_std::FilePathSeparator::get() + "*"};
  267. if((hFind = FindFirstFile(pattern.c_str(), &data)) != INVALID_HANDLE_VALUE) {
  268. do {
  269. ls_std::File::_addToFileListWindows(_path, withDirectories, data, filesInDirectory);
  270. }
  271. while(FindNextFile(hFind, &data) != 0);
  272. FindClose(hFind);
  273. }
  274. return filesInDirectory;
  275. }
  276. #endif
  277. int ls_std::File::_mkdir(const std::string& _path) {
  278. int result;
  279. #ifdef _WIN32
  280. result = mkdir(_path.c_str());
  281. #endif
  282. #if defined(unix) || defined(__APPLE__)
  283. result = mkdir(_path.c_str(), 0777);
  284. #endif
  285. return result;
  286. }
  287. std::string ls_std::File::_normalizePath(std::string _path)
  288. {
  289. _path = ls_std::File::_replaceWrongSeparator(_path);
  290. _path = ls_std::File::_reduceSeparators(_path);
  291. return _path;
  292. }
  293. std::string ls_std::File::_reduceSeparators(const std::string& _path)
  294. {
  295. static const char separator = {ls_std::FilePathSeparator::get()};
  296. std::string normalizedPath {};
  297. int index {};
  298. while(index < _path.size()) {
  299. if(_path[index] == separator) {
  300. normalizedPath += _path[index];
  301. do {
  302. index++;
  303. }
  304. while(_path[index] == separator);
  305. } else {
  306. normalizedPath += _path[index];
  307. index++;
  308. }
  309. }
  310. return normalizedPath;
  311. }
  312. bool ls_std::File::_renameTo(const std::string &_oldName, const std::string &_newName)
  313. {
  314. return std::rename(_oldName.c_str(), _newName.c_str()) == 0;
  315. }
  316. std::string ls_std::File::_replaceWrongSeparator(std::string _path)
  317. {
  318. static const char unixSeparator = ls_std::FilePathSeparator::getUnixFilePathSeparator();
  319. static const char windowsSeparator = ls_std::FilePathSeparator::getWindowsFilePathSeparator();
  320. #if defined(unix) || defined(__APPLE__)
  321. std::replace(_path.begin(), _path.end(), windowsSeparator, unixSeparator);
  322. #endif
  323. #ifdef _WIN32
  324. std::replace(_path.begin(), _path.end(), unixSeparator, windowsSeparator);
  325. #endif
  326. return _path;
  327. }
  328. std::vector<std::string> ls_std::File::_splitIntoSubDirectoryNames(const std::string& _path) {
  329. std::vector<std::string> subDirectoryNames {};
  330. std::stringstream _stream {_path};
  331. std::string subDirectoryName {};
  332. const char separator = ls_std::FilePathSeparator::get();
  333. while(std::getline(_stream, subDirectoryName, separator)) {
  334. subDirectoryNames.push_back(subDirectoryName);
  335. }
  336. return subDirectoryNames;
  337. }