Filesystem.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #include "anki/util/Filesystem.h"
  2. #include "anki/util/Exception.h"
  3. #include "anki/util/Assert.h"
  4. #include <fstream>
  5. #include <cstring>
  6. #include <sys/stat.h>
  7. #include <sys/types.h>
  8. #include <ftw.h>
  9. namespace anki {
  10. //==============================================================================
  11. const char* getFileExtension(const char* filename)
  12. {
  13. ANKI_ASSERT(filename);
  14. const char* pc = strrchr(filename, '.');
  15. if(pc == nullptr)
  16. {
  17. return nullptr;
  18. }
  19. ++pc;
  20. return (*pc == '\0') ? nullptr : pc;
  21. }
  22. //==============================================================================
  23. std::string readFile(const char* filename)
  24. {
  25. std::ifstream file(filename);
  26. if (!file.is_open())
  27. {
  28. throw ANKI_EXCEPTION("Cannot open file: " + filename);
  29. }
  30. return std::string((std::istreambuf_iterator<char>(file)),
  31. std::istreambuf_iterator<char>());
  32. }
  33. //==============================================================================
  34. StringList readFileLines(const char* filename)
  35. {
  36. std::ifstream ifs(filename);
  37. if(!ifs.is_open())
  38. {
  39. throw ANKI_EXCEPTION("Cannot open file: " + filename);
  40. }
  41. StringList lines;
  42. std::string temp;
  43. while(getline(ifs, temp))
  44. {
  45. lines.push_back(temp);
  46. }
  47. return lines;
  48. }
  49. //==============================================================================
  50. bool fileExists(const char* filename)
  51. {
  52. ANKI_ASSERT(filename);
  53. struct stat s;
  54. stat(filename, &s);
  55. return S_ISREG(s.st_mode);
  56. }
  57. //==============================================================================
  58. bool directoryExists(const char* filename)
  59. {
  60. ANKI_ASSERT(filename);
  61. struct stat s;
  62. stat(filename, &s);
  63. return S_ISDIR(s.st_mode);
  64. }
  65. //==============================================================================
  66. static int rmDir(const char* fpath, const struct stat* sb, int typeflag,
  67. struct FTW* ftwbuf)
  68. {
  69. int rv = remove(fpath);
  70. if(rv)
  71. {
  72. throw ANKI_EXCEPTION(strerror(errno) + ": " + filename);
  73. }
  74. return rv;
  75. }
  76. void removeDirectory(const char* dir)
  77. {
  78. nftw(path, rmDir, 64, FTW_DEPTH | FTW_PHYS);
  79. }
  80. //==============================================================================
  81. void createDirectory(const char* dir)
  82. {
  83. if(directoryExists(dir))
  84. {
  85. return;
  86. }
  87. if(mkdir(path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH))
  88. {
  89. throw ANKI_EXCEPTION(strerror(errno) + ": " + filename);
  90. }
  91. }
  92. //==============================================================================
  93. void toNativePath(const char* path)
  94. {
  95. ANKI_ASSERT(path);
  96. }
  97. } // end namespace anki