FileSystem.cpp 3.7 KB

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