]> Dogcows Code - chaz/yoink/blobdiff - src/moof/resource.cc
remove some deprected stuff
[chaz/yoink] / src / moof / resource.cc
index 5cd75360d85d5c8c5041d76da80987bc830cccf0..57bd99b927df9fabfdee70f22faeae208c2d823b 100644 (file)
@@ -9,24 +9,22 @@
 *
 **************************************************************************/
 
+#include "config.h"
+
+#ifdef USE_HOTLOADING
+#include <sys/inotify.h>
+#include <sys/ioctl.h>
+#endif
+
 #include <queue>
 
 #include <boost/algorithm/string.hpp>
 #include <boost/weak_ptr.hpp>
-
 #include <stlplus/portability/file_system.hpp>
 
 #include "hash.hh"
 #include "resource.hh"
 
-#if HAVE_CONFIG_H
-#include "../config.h"
-#endif
-
-#ifdef USE_HOTLOADING
-#include <sys/inotify.h>
-#include <sys/ioctl.h>
-#endif
 
 #ifndef BUF_SIZE
 #define BUF_SIZE 4096
 namespace moof {
        
 
-static std::vector<std::string> search_paths_;
+bool resource::call_registry(const std::string& extension,
+                                                        loader_ptr& loader,
+                                                        registry_action action)
+{
+       static type_lookup table;
+
+       switch (action)
+       {
+               case set:
+               {
+                       if (loader) table[extension] = loader;
+                       else        table.erase(extension);
+                       break;
+               }
+
+               case lookup:
+               {
+                       std::map<std::string,loader_ptr>::iterator it;
+                       it = table.find(extension);
+                       if (it != table.end()) loader = (*it).second;
+                       break;
+               }
+       }
+
+       return loader;
+}
+
+static std::string search_paths_;
 
 typedef boost::weak_ptr<resource> resource_weakptr;
 static hash<std::string,resource_weakptr,hash_function> resource_table_;
 
-// static member
-resource::type_lookup_ptr resource::type_lookup_;
+static hash<std::string,std::string,hash_function> prefix_table_;
 
 
 #ifdef USE_HOTLOADING
@@ -105,11 +129,21 @@ resource::~resource()
 }
 
 
-resource_ptr resource::load(const std::string& path)
+resource_ptr resource::load(const std::string& name,
+                                                       const std::string& ext)
 {
-       std::string extension = stlplus::extension_part(path);
+       return load_with_path(find_file(name, ext));
+}
+
+resource_ptr resource::load(const std::string& name)
+{
+       return load_with_path(find_file(name));
+}
 
-       if (!find(path)) return resource_ptr();
+
+resource_ptr resource::load_with_path(const std::string& path)
+{
+       std::string extension = stlplus::extension_part(path);
 
        hash<std::string,resource_weakptr,hash_function>::iterator it;
        it = resource_table_.find(path);
@@ -120,17 +154,16 @@ resource_ptr resource::load(const std::string& path)
                if (locked) return locked;
        }
 
-       std::map<std::string,loader_ptr>::iterator jt;
-       jt = type_lookup_->find(extension);
-       if (jt != type_lookup_->end())
+       loader_ptr loader;
+       call_registry(extension, loader, lookup);
+       if (loader)
        {
-               resource_ptr rsrc((*jt).second->load(path));
-               rsrc->set_loader(path, (*jt).second);
+               resource_ptr rsrc(loader->load(path));
+               rsrc->set_loader(path, loader);
                resource_table_[path] = rsrc;
 
 #ifdef USE_HOTLOADING
-               int wd = inotify_add_watch(monitor_fd_,
-                               path.c_str(), IN_MODIFY);
+               int wd = inotify_add_watch(monitor_fd_, path.c_str(), IN_MODIFY);
                rsrc->set_watch_descriptor(wd);
                monitor_lookup_[wd] = path;
 #endif
@@ -139,6 +172,7 @@ resource_ptr resource::load(const std::string& path)
                return rsrc;
        }
 
+       log_warning("cannot load resource of unknown type:", path);
        return resource_ptr();
 }
 
@@ -173,8 +207,7 @@ void resource::reload()
        unloader_ = resource->unloader_;
 
 #ifdef USE_HOTLOADING
-       int wd = inotify_add_watch(monitor_fd_,
-                       path_.c_str(), IN_MODIFY);
+       int wd = inotify_add_watch(monitor_fd_, path_.c_str(), IN_MODIFY);
        set_watch_descriptor(wd);
        monitor_lookup_[wd] = path_;
 #endif
@@ -185,66 +218,55 @@ 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);
+       search_paths_ = paths;
 }
 
-void resource::add_search_paths(const std::vector<std::string>& pathList)
+
+std::string resource::find_file(const std::string& name)
 {
-       std::vector<std::string>::const_iterator it;
-       for (it = pathList.begin(); it != pathList.end(); ++it)
-       {
-               std::string path(*it);
+       std::vector<std::string> paths;
+       boost::split(paths, search_paths_, boost::is_any_of(":"));
 
-               ASSERT(!path.empty() && "empty search path string");
+       std::string ext = stlplus::extension_part(name);
+       std::string prefix("hi");
 
-               // add a slash if there isn't one already
-               if (*path.rbegin() != '/') path += '/';
+       loader_ptr loader;
+       call_registry(ext, loader, lookup);
+       if (loader) prefix = loader->prefix();
 
-#if defined(_WIN32)
-               //boost::replace_all(path, "/", "\\");
-#endif
+       log_info("find_file:", ext, prefix);
 
-               search_paths_.push_back(path);
-               log_info << "added search path " << path << std::endl;
-       }
-}
-
-
-bool resource::find(const std::string& path)
-{
-       FILE* file = open_file(path);
-       if (file)
+       std::vector<std::string>::iterator it;
+       for (it = paths.begin(); it != paths.end(); ++it)
        {
-               fclose(file);
-               return true;
+               std::string path = stlplus::create_filespec(*it, name);
+               log_info("looking for", name, "at", path);
+               if (stlplus::file_exists(path)) return path;
+               
+               // try it with the prefix added
+               if (!prefix.empty())
+               {
+                       *it = stlplus::create_filespec(*it, prefix);
+                       path = stlplus::create_filespec(*it, name);
+                       log_info("looking for", name, "at", path);
+                       if (stlplus::file_exists(path)) return path;
+               }
        }
 
-       return false;
+
+       log_error("cannot find resource file:", name);
+       return std::string();
 }
 
-FILE* resource::open_file(const std::string& path, const std::string& mode)
+std::string resource::find_file(const std::string& name,
+                                                               const std::string& ext)
 {
-#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)
+       std::string actual_ext = stlplus::extension_part(name);
+       if (actual_ext != ext)
        {
-               // 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;
+               return find_file(stlplus::create_filename(name, ext));
        }
-
-       // last ditch effort; maybe it's already a path to a valid resource
-       return fopen(path.c_str(), mode.c_str());
+       return find_file(name);
 }
 
 
This page took 0.026427 seconds and 4 git commands to generate.