fileResolver.cpp 4.2 KB

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