fileResolver.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #include <sys/stat.h>
  2. #include <wallpaperengine/irrlicht.h>
  3. #include "fileResolver.h"
  4. namespace wp
  5. {
  6. fs::fileResolver::fileResolver ()
  7. {
  8. this->m_environment.push_back (".");
  9. }
  10. fs::fileResolver::fileResolver (std::vector<irr::io::path> environment)
  11. {
  12. this->m_environment.push_back (".");
  13. this->m_environment.insert (this->m_environment.end (), environment.begin (), environment.end ());
  14. }
  15. void fs::fileResolver::appendEnvironment (irr::io::path path)
  16. {
  17. this->m_environment.push_back (wp::irrlicht::device->getFileSystem ()->getAbsolutePath (path));
  18. }
  19. void fs::fileResolver::removeEnvironment(irr::io::path path)
  20. {
  21. std::vector<irr::io::path>::const_iterator cur = this->m_environment.begin ();
  22. std::vector<irr::io::path>::const_iterator end = this->m_environment.end ();
  23. irr::io::path absolute = wp::irrlicht::device->getFileSystem ()->getAbsolutePath (path);
  24. for (; cur != end; cur ++)
  25. {
  26. if (*cur == path)
  27. {
  28. this->m_environment.erase (cur);
  29. return;
  30. }
  31. }
  32. }
  33. void fs::fileResolver::prependEnvironment (irr::io::path path)
  34. {
  35. this->m_environment.insert (
  36. this->m_environment.begin (),
  37. wp::irrlicht::device->getFileSystem ()->getAbsolutePath (path)
  38. );
  39. }
  40. void fs::fileResolver::changeWorkingDirectory (irr::io::path newpath)
  41. {
  42. this->m_environment.erase (this->m_environment.begin ());
  43. this->prependEnvironment (newpath);
  44. }
  45. irr::io::path fs::fileResolver::getWorkingDirectory ()
  46. {
  47. return *this->m_environment.begin ();
  48. }
  49. fs::fileResolver fs::fileResolver::clone ()
  50. {
  51. return fileResolver (this->m_environment);
  52. }
  53. irr::io::path fs::fileResolver::resolve (irr::io::path name)
  54. {
  55. std::vector<irr::io::path>::const_iterator cur = this->m_environment.begin ();
  56. std::vector<irr::io::path>::const_iterator end = this->m_environment.end ();
  57. irr::io::path tmp = "";
  58. // try to resolve the path
  59. for (; cur != end; cur ++)
  60. {
  61. tmp = *cur;
  62. tmp += "/";
  63. tmp += name;
  64. if (wp::irrlicht::device->getFileSystem ()->existFile (tmp) == true)
  65. {
  66. wp::irrlicht::device->getLogger ()->log ("Resolved file to", tmp.c_str ());
  67. return tmp;
  68. }
  69. }
  70. wp::irrlicht::device->getLogger ()->log ("Failed resolving file ", name.c_str (), irr::ELL_ERROR);
  71. return "";
  72. }
  73. irr::io::path fs::fileResolver::resolveOnWorkingDirectory (irr::io::path name)
  74. {
  75. irr::io::path file = *this->m_environment.begin () + "/" + name;
  76. if (wp::irrlicht::device->getFileSystem ()->existFile (file) == true)
  77. {
  78. wp::irrlicht::device->getLogger ()->log ("Resolved file in working directory to", file.c_str ());
  79. return file;
  80. }
  81. return "";
  82. }
  83. irr::io::path fs::fileResolver::resolve (json name)
  84. {
  85. std::string tmp = name;
  86. return this->resolve (tmp.c_str ());
  87. }
  88. irr::io::path fs::fileResolver::resolveOnWorkingDirectory (json name)
  89. {
  90. std::string tmp = name;
  91. return this->resolveOnWorkingDirectory (tmp.c_str ());
  92. }
  93. irr::io::path fs::fileResolver::resolve (const char* name)
  94. {
  95. return this->resolve (irr::io::path(name));
  96. }
  97. irr::io::path fs::fileResolver::resolveOnWorkingDirectory (const char* name)
  98. {
  99. return this->resolveOnWorkingDirectory(irr::io::path (name));
  100. }
  101. fs::fileResolver fs::resolver;
  102. }