]> Dogcows Code - chaz/yoink/blobdiff - src/moof/resource.cc
begin cleaning up resource management
[chaz/yoink] / src / moof / resource.cc
index ac200f7831e380866665bc490a48ed416340a7e7..d22ae217a28d81269460fa8fda3201764bccc37d 100644 (file)
@@ -9,8 +9,15 @@
 *
 **************************************************************************/
 
+#include "../config.h"
+
 #include <queue>
 
+#ifdef USE_HOTLOADING
+#include <sys/inotify.h>
+#include <sys/ioctl.h>
+#endif
+
 #include <boost/algorithm/string.hpp>
 #include <boost/weak_ptr.hpp>
 
 #include "hash.hh"
 #include "resource.hh"
 
-#if HAVE_CONFIG_H
-#include "../config.h"
-#endif
-
-#if USE_HOTLOADING
-#include <sys/inotify.h>
-#include <sys/ioctl.h>
-#endif
 
 #ifndef BUF_SIZE
 #define BUF_SIZE 4096
@@ -36,7 +35,7 @@
 namespace moof {
        
 
-static std::vector<std::string> search_paths_;
+static std::string search_paths_;
 
 typedef boost::weak_ptr<resource> resource_weakptr;
 static hash<std::string,resource_weakptr,hash_function> resource_table_;
@@ -45,7 +44,7 @@ static hash<std::string,resource_weakptr,hash_function> resource_table_;
 resource::type_lookup_ptr resource::type_lookup_;
 
 
-#if USE_HOTLOADING
+#ifdef USE_HOTLOADING
 static hash<int,std::string,hash_function> monitor_lookup_;
 static int monitor_fd_ = inotify_init1(IN_NONBLOCK);
 #endif
@@ -54,7 +53,7 @@ int resource::reload_as_needed()
 {
        int num_resources = 0;
 
-#if USE_HOTLOADING
+#ifdef USE_HOTLOADING
        log_info("hotloading?");
        char bytes[BUF_SIZE];
        int num_bytes;
@@ -99,7 +98,7 @@ int resource::reload_as_needed()
 
 resource::~resource()
 {
-#if USE_HOTLOADING
+#ifdef USE_HOTLOADING
        inotify_rm_watch(monitor_fd_, wd_);
 #endif
 }
@@ -128,7 +127,7 @@ resource_ptr resource::load(const std::string& path)
                rsrc->set_loader(path, (*jt).second);
                resource_table_[path] = rsrc;
 
-#if USE_HOTLOADING
+#ifdef USE_HOTLOADING
                int wd = inotify_add_watch(monitor_fd_,
                                path.c_str(), IN_MODIFY);
                rsrc->set_watch_descriptor(wd);
@@ -172,7 +171,7 @@ void resource::reload()
        typeinfo_ = resource->typeinfo_;
        unloader_ = resource->unloader_;
 
-#if USE_HOTLOADING
+#ifdef USE_HOTLOADING
        int wd = inotify_add_watch(monitor_fd_,
                        path_.c_str(), IN_MODIFY);
        set_watch_descriptor(wd);
@@ -185,66 +184,19 @@ void resource::reload()
 
 void resource::add_search_paths(const std::string& paths)
 {
-       std::vector<std::string> pathList;
-       boost::split(pathList, paths, boost::is_any_of(":"));
-
-       add_search_paths(pathList);
-}
-
-void resource::add_search_paths(const std::vector<std::string>& pathList)
-{
-       std::vector<std::string>::const_iterator it;
-       for (it = pathList.begin(); it != pathList.end(); ++it)
-       {
-               std::string path(*it);
-
-               ASSERT(!path.empty() && "empty search path string");
-
-               // add a slash if there isn't one already
-               if (*path.rbegin() != '/') path += '/';
-
-#if defined(_WIN32)
-               //boost::replace_all(path, "/", "\\");
-#endif
-
-               search_paths_.push_back(path);
-               log_info << "added search path " << path << std::endl;
-       }
+       search_paths_ = paths;
 }
 
 
 bool resource::find(const std::string& path)
 {
-       FILE* file = open_file(path);
-       if (file)
-       {
-               fclose(file);
-               return true;
-       }
-
-       return false;
+       return !stlplus::lookup(path, search_paths_, ":").empty();
 }
 
 FILE* resource::open_file(const std::string& path, const std::string& mode)
 {
-#if defined(_WIN32)
-       // windows always has to be a little different
-       //boost::replace_all(path, "/", "\\");
-#endif
-
-       std::vector<std::string>::iterator it;
-       for (it = search_paths_.begin(); it != search_paths_.end(); ++it)
-       {
-               // check path relative to search path
-               std::string complete_path(*it);
-               complete_path += path;
-
-               FILE* file = fopen(complete_path.c_str(), mode.c_str());
-               if (file) return file;
-       }
-
-       // last ditch effort; maybe it's already a path to a valid resource
-       return fopen(path.c_str(), mode.c_str());
+       std::string file = stlplus::lookup(path, search_paths_, ":");
+       return fopen(file.c_str(), mode.c_str());
 }
 
 
This page took 0.020702 seconds and 4 git commands to generate.