fileResolver.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #include <sys/stat.h>
  2. #include <wallpaperengine/irrlicht.h>
  3. #include "fileResolver.h"
  4. namespace wp
  5. {
  6. namespace fs
  7. {
  8. fileResolver::fileResolver ()
  9. {
  10. this->m_environment.push_back (".");
  11. }
  12. fileResolver::fileResolver (std::vector<irr::io::path> environment)
  13. {
  14. this->m_environment.push_back (".");
  15. this->m_environment.insert (this->m_environment.end (), environment.begin (), environment.end ());
  16. }
  17. void fileResolver::appendEnvironment (irr::io::path path)
  18. {
  19. this->m_environment.push_back (wp::irrlicht::device->getFileSystem ()->getAbsolutePath (path));
  20. }
  21. void fileResolver::removeEnvironment(irr::io::path path)
  22. {
  23. std::vector<irr::io::path>::const_iterator cur = this->m_environment.begin ();
  24. std::vector<irr::io::path>::const_iterator end = this->m_environment.end ();
  25. irr::io::path absolute = wp::irrlicht::device->getFileSystem ()->getAbsolutePath (path);
  26. for (; cur != end; cur ++)
  27. {
  28. if (*cur == path)
  29. {
  30. this->m_environment.erase (cur);
  31. return;
  32. }
  33. }
  34. }
  35. void fileResolver::prependEnvironment (irr::io::path path)
  36. {
  37. this->m_environment.insert (
  38. this->m_environment.begin (),
  39. wp::irrlicht::device->getFileSystem ()->getAbsolutePath (path)
  40. );
  41. }
  42. void fileResolver::changeWorkingDirectory (irr::io::path newpath)
  43. {
  44. this->m_environment.erase (this->m_environment.begin ());
  45. this->prependEnvironment (newpath);
  46. }
  47. fileResolver fileResolver::clone ()
  48. {
  49. return fileResolver (this->m_environment);
  50. }
  51. irr::io::path fileResolver::resolve (irr::io::path name)
  52. {
  53. std::vector<irr::io::path>::const_iterator cur = this->m_environment.begin ();
  54. std::vector<irr::io::path>::const_iterator end = this->m_environment.end ();
  55. irr::io::path tmp = "";
  56. // try to resolve the path
  57. for (; cur != end; cur ++)
  58. {
  59. tmp = *cur;
  60. tmp += "/";
  61. tmp += name;
  62. struct stat buffer;
  63. if (stat (tmp.c_str (), &buffer) == 0)
  64. {
  65. wp::irrlicht::device->getLogger ()->log ("Resolved file to", tmp.c_str ());
  66. return tmp;
  67. }
  68. }
  69. wp::irrlicht::device->getLogger ()->log ("Failed resolving file ", name.c_str ());
  70. return "";
  71. }
  72. }
  73. }