fileResolver.cpp 4.3 KB

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