FileSystem.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #include "Base.h"
  2. #include "FileSystem.h"
  3. #ifdef WIN32
  4. #include <windows.h>
  5. #include <tchar.h>
  6. #include <stdio.h>
  7. #else
  8. #include <dirent.h>
  9. #include <sys/stat.h>
  10. #endif
  11. namespace gameplay
  12. {
  13. static std::string __resourcePath("./");
  14. FileSystem::FileSystem()
  15. {
  16. }
  17. FileSystem::~FileSystem()
  18. {
  19. }
  20. void FileSystem::setResourcePath(const char* path)
  21. {
  22. __resourcePath = path == NULL ? "" : path;
  23. }
  24. const char* FileSystem::getResourcePath()
  25. {
  26. return __resourcePath.c_str();
  27. }
  28. bool FileSystem::listFiles(const char* dirPath, std::vector<std::string>& files)
  29. {
  30. // TODO make this method work with absolute and relative paths.
  31. #ifdef WIN32
  32. std::string path(FileSystem::getResourcePath());
  33. if (dirPath && strlen(dirPath) > 0)
  34. {
  35. path.append(dirPath);
  36. }
  37. path.append("/*");
  38. // Convert char to wchar
  39. std::basic_string<TCHAR> wPath;
  40. wPath.assign(path.begin(), path.end());
  41. WIN32_FIND_DATA FindFileData;
  42. HANDLE hFind = FindFirstFile(wPath.c_str(), &FindFileData);
  43. if (hFind == INVALID_HANDLE_VALUE)
  44. {
  45. return false;
  46. }
  47. do
  48. {
  49. // Add to the list if this is not a directory
  50. if ((FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0)
  51. {
  52. // Convert wchar to char
  53. std::basic_string<TCHAR> wfilename(FindFileData.cFileName);
  54. std::string filename;
  55. filename.assign(wfilename.begin(), wfilename.end());
  56. files.push_back(filename);
  57. }
  58. } while(FindNextFile(hFind, &FindFileData) != 0);
  59. FindClose(hFind);
  60. return true;
  61. #else
  62. std::string path(FileSystem::getResourcePath());
  63. if (dirPath && strlen(dirPath) > 0)
  64. {
  65. path.append(dirPath);
  66. }
  67. path.append("/.");
  68. struct dirent* dp;
  69. DIR* dir = opendir(path.c_str());
  70. if (!dir)
  71. {
  72. return false;
  73. }
  74. while ((dp = readdir(dir)) != NULL)
  75. {
  76. std::string filepath(path);
  77. filepath.append("/");
  78. filepath.append(dp->d_name);
  79. struct stat buf;
  80. if (!stat(filepath.c_str(), &buf))
  81. {
  82. // Add to the list if this is not a directory
  83. if (!S_ISDIR(buf.st_mode))
  84. {
  85. files.push_back(dp->d_name);
  86. }
  87. }
  88. }
  89. closedir(dir);
  90. return true;
  91. #endif
  92. }
  93. FILE* FileSystem::openFile(const char* path, const char* mode)
  94. {
  95. std::string fullPath(__resourcePath);
  96. fullPath += path;
  97. FILE* fp = fopen(fullPath.c_str(), mode);
  98. // Win32 doesnt support a asset or bundle definitions.
  99. #ifdef WIN32
  100. if (fp == NULL)
  101. {
  102. fullPath = __resourcePath;
  103. fullPath += "../../gameplay/";
  104. fullPath += path;
  105. fp = fopen(fullPath.c_str(), mode);
  106. }
  107. #endif
  108. return fp;
  109. }
  110. char* FileSystem::readAll(const char* filePath, int* fileSize)
  111. {
  112. // Open file for reading.
  113. FILE* file = openFile(filePath, "rb");
  114. if (file == NULL)
  115. {
  116. LOG_ERROR_VARG("Failed to load file: %s", filePath);
  117. return NULL;
  118. }
  119. // Obtain file length.
  120. fseek(file, 0, SEEK_END);
  121. int size = (int)ftell(file);
  122. fseek(file, 0, SEEK_SET);
  123. // Read entire file contents.
  124. char* buffer = new char[size + 1];
  125. int read = (int)fread(buffer, 1, size, file);
  126. assert(read == size);
  127. if (read != size)
  128. {
  129. LOG_ERROR_VARG("Read error for file: %s (%d < %d)", filePath, (int)read, (int)size);
  130. SAFE_DELETE_ARRAY(buffer);
  131. return NULL;
  132. }
  133. // Force the character buffer to be NULL-terminated.
  134. buffer[size] = '\0';
  135. // Close file and return.
  136. fclose(file);
  137. if (fileSize)
  138. {
  139. *fileSize = size;
  140. }
  141. return buffer;
  142. }
  143. }