]> Dogcows Code - chaz/yoink/blobdiff - src/Moof/Resource.cc
refactoring needed for win32 crash
[chaz/yoink] / src / Moof / Resource.cc
index d958c3aa9e551cc584d9fe14fe02cf3275846041..ec940257045ea6650bf479f197eed0df9f91b60c 100644 (file)
@@ -28,6 +28,9 @@
 
 #include <unistd.h>
 
+#include <boost/algorithm/string.hpp>
+
+#include "Log.hh"
 #include "Resource.hh"
 
 
@@ -35,41 +38,63 @@ namespace Mf {
 
 
 // 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::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();
 }
This page took 0.020679 seconds and 4 git commands to generate.