File.cpp 9.9 KB

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