| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- #include <cstdint>
- #include <sys/stat.h>
- // filesystem includes
- #include <wallpaperengine/fs/fileResolver.h>
- // engine includes
- #include <wallpaperengine/irrlicht.h>
- namespace wp
- {
- namespace fs
- {
- fileResolver::fileResolver ()
- {
- this->m_environment.push_back (".");
- }
- fileResolver::fileResolver (std::vector<irr::io::path> environment)
- {
- this->m_environment.push_back (".");
- this->m_environment.insert (this->m_environment.end (), environment.begin (), environment.end ());
- }
- void fileResolver::appendEnvironment (irr::io::path path)
- {
- this->m_environment.push_back (wp::irrlicht::device->getFileSystem ()->getAbsolutePath (path));
- }
- void fileResolver::removeEnvironment(irr::io::path path)
- {
- std::vector<irr::io::path>::const_iterator cur = this->m_environment.begin ();
- std::vector<irr::io::path>::const_iterator end = this->m_environment.end ();
- irr::io::path absolute = wp::irrlicht::device->getFileSystem ()->getAbsolutePath (path);
- for (; cur != end; cur ++)
- {
- if (*cur == path)
- {
- this->m_environment.erase (cur);
- return;
- }
- }
- }
- void fileResolver::prependEnvironment (irr::io::path path)
- {
- this->m_environment.insert (
- this->m_environment.begin (),
- wp::irrlicht::device->getFileSystem ()->getAbsolutePath (path)
- );
- }
- void fileResolver::changeWorkingDirectory (irr::io::path newpath)
- {
- this->m_environment.erase (this->m_environment.begin ());
- this->prependEnvironment (newpath);
- }
- irr::io::path fileResolver::getWorkingDirectory ()
- {
- return *this->m_environment.begin ();
- }
- fs::fileResolver fileResolver::clone ()
- {
- return fileResolver (this->m_environment);
- }
- irr::io::path fileResolver::resolve (irr::io::path name)
- {
- std::vector<irr::io::path>::const_iterator cur = this->m_environment.begin ();
- std::vector<irr::io::path>::const_iterator end = this->m_environment.end ();
- irr::io::path tmp = "";
- // try to resolve the path
- for (; cur != end; cur ++)
- {
- tmp = *cur;
- tmp += "/";
- tmp += name;
- if (wp::irrlicht::device->getFileSystem ()->existFile (tmp) == true)
- {
- wp::irrlicht::device->getLogger ()->log ("Resolved file to", tmp.c_str ());
- return tmp;
- }
- }
- wp::irrlicht::device->getLogger ()->log ("Failed resolving file ", name.c_str (), irr::ELL_ERROR);
- return "";
- }
- irr::io::path fileResolver::resolveOnWorkingDirectory (irr::io::path name)
- {
- irr::io::path file = *this->m_environment.begin () + "/" + name;
- if (wp::irrlicht::device->getFileSystem ()->existFile (file) == true)
- {
- wp::irrlicht::device->getLogger ()->log ("Resolved file in working directory to", file.c_str ());
- // perform stat over the file to check for directory
- return file;
- }
- wp::irrlicht::device->getLogger ()->log ("Failed resolving file on working directory for ", name.c_str (), irr::ELL_ERROR);
- return "";
- }
- irr::io::path fileResolver::resolve (json name)
- {
- std::string tmp = name;
- return this->resolve (tmp.c_str ());
- }
- irr::io::path fileResolver::resolveOnWorkingDirectory (json name)
- {
- std::string tmp = name;
- return this->resolveOnWorkingDirectory (tmp.c_str ());
- }
- irr::io::path fileResolver::resolve (const char* name)
- {
- return this->resolve (irr::io::path(name));
- }
- irr::io::path fileResolver::resolveOnWorkingDirectory (const char* name)
- {
- return this->resolveOnWorkingDirectory(irr::io::path (name));
- }
- fileResolver resolver;
- }
- }
|