#include <unistd.h>
+#include <boost/algorithm/string.hpp>
+
+#include "Log.hh"
#include "Resource.hh"
// static member
-std::vector<std::string> Resource::searchPaths_;
+std::vector<std::string> Resource::gSearchPaths;
-Resource::~Resource() {}
+void Resource::addSearchPaths(const std::string& path)
+{
+ std::vector<std::string> paths;
+ boost::split(paths, path, boost::is_any_of(":"));
+ addSearchPaths(paths);
+}
-void Resource::addSearchPath(const std::string& directory)
+void Resource::addSearchPaths(const std::vector<std::string>& path)
{
- // add a slash if there isn't one already
- if (directory[directory.length() - 1] != '/')
- {
- searchPaths_.push_back(directory + '/');
- }
- else
+ std::vector<std::string>::const_iterator it;
+
+ for (it = path.begin(); it != path.end(); ++it)
{
- searchPaths_.push_back(directory);
+ std::string onePath(*it);
+
+ ASSERT(!onePath.empty() && "empty search path string");
+
+ // add a slash if there isn't one already
+ if (*onePath.rbegin() != '/')
+ {
+ onePath += '/';
+ }
+
+#if defined(_WIN32) || defined(__WIN32__)
+ boost::replace_all(onePath, "/", "\\");
+#endif
+
+ gSearchPaths.push_back(onePath);
+ logInfo << "added search path " << onePath << std::endl;
}
}
-std::string Resource::getPathToResource(const std::string& name)
+
+std::string Resource::getPath(const std::string& name)
{
std::vector<std::string>::iterator it;
- for (it = searchPaths_.begin(); it != searchPaths_.end(); ++it)
+ std::string path(name);
+
+#if defined(_WIN32) || defined(__WIN32__)
+ boost::replace_all(path, "/", "\\");
+#endif
+
+ for (it = gSearchPaths.begin(); it != gSearchPaths.end(); ++it)
{
std::string fullPath(*it);
- fullPath += name;
+ fullPath += path;
- // TODO this could be more portable
- if (access(fullPath.c_str(), R_OK) == 0)
- {
- return fullPath;
- }
+ if (access(fullPath.c_str(), R_OK) == 0) return fullPath;
}
+ logWarning << "cannot find resource " << name << std::endl;
+
// empty string
return std::string();
}