CFilesystemLoader.cpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #include "StdInc.h"
  2. #include "CFilesystemLoader.h"
  3. #include "CFileInfo.h"
  4. #include "CFileInputStream.h"
  5. CFilesystemLoader::CFilesystemLoader(const std::string & baseDirectory, size_t depth, bool initial):
  6. baseDirectory(baseDirectory),
  7. fileList(listFiles(depth, initial))
  8. {
  9. }
  10. std::unique_ptr<CInputStream> CFilesystemLoader::load(const std::string & resourceName) const
  11. {
  12. std::unique_ptr<CInputStream> stream(new CFileInputStream(getOrigin() + '/' + resourceName));
  13. return stream;
  14. }
  15. bool CFilesystemLoader::existsEntry(const std::string & resourceName) const
  16. {
  17. for(auto it = fileList.begin(); it != fileList.end(); ++it)
  18. {
  19. if(it->second == resourceName)
  20. {
  21. return true;
  22. }
  23. }
  24. return false;
  25. }
  26. boost::unordered_map<ResourceID, std::string> CFilesystemLoader::getEntries() const
  27. {
  28. return fileList;
  29. }
  30. std::string CFilesystemLoader::getOrigin() const
  31. {
  32. return baseDirectory;
  33. }
  34. bool CFilesystemLoader::createEntry(std::string filename)
  35. {
  36. ResourceID res(filename);
  37. if (fileList.find(res) != fileList.end())
  38. return false;
  39. std::ofstream file(baseDirectory + '/' + filename);
  40. if (!file.good())
  41. return false;
  42. fileList[res] = filename;
  43. return true;
  44. }
  45. boost::unordered_map<ResourceID, std::string> CFilesystemLoader::listFiles(size_t depth, bool initial) const
  46. {
  47. assert(boost::filesystem::is_directory(baseDirectory));
  48. boost::unordered_map<ResourceID, std::string> fileList;
  49. std::vector<std::string> path;//vector holding relative path to our file
  50. boost::filesystem::recursive_directory_iterator enddir;
  51. boost::filesystem::recursive_directory_iterator it(baseDirectory, boost::filesystem::symlink_option::recurse);
  52. for(; it != enddir; ++it)
  53. {
  54. EResType::Type type;
  55. if (boost::filesystem::is_directory(it->status()))
  56. {
  57. path.resize(it.level()+1);
  58. path.back() = it->path().leaf().string();
  59. it.no_push(depth <= it.level()); // don't iterate into directory if depth limit reached
  60. type = EResType::DIRECTORY;
  61. }
  62. else
  63. type = EResTypeHelper::getTypeFromExtension(boost::filesystem::extension(*it));
  64. if (!initial || type == EResType::DIRECTORY || type == EResType::ARCHIVE || type == EResType::TEXT)
  65. {
  66. //reconstruct relative filename (not possible via boost AFAIK)
  67. std::string filename;
  68. for (size_t i=0; i<it.level() && i<path.size(); i++)
  69. filename += path[i] + '/';
  70. filename += it->path().leaf().string();
  71. fileList[ResourceID(filename, type)] = filename;
  72. }
  73. }
  74. return fileList;
  75. }