X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fyoink;a=blobdiff_plain;f=src%2Fmoof%2Fresource.cc;h=053fc0b31849e9b37991d90f671e71ee7983800d;hp=d22ae217a28d81269460fa8fda3201764bccc37d;hb=382626aad0a683ed8642a6a807eea743db45f7f8;hpb=bc795376d093ee097040f029b891b5435eb3bd21 diff --git a/src/moof/resource.cc b/src/moof/resource.cc index d22ae21..053fc0b 100644 --- a/src/moof/resource.cc +++ b/src/moof/resource.cc @@ -9,18 +9,17 @@ * **************************************************************************/ -#include "../config.h" - -#include +#include "config.h" #ifdef USE_HOTLOADING #include #include #endif +#include + #include #include - #include #include "hash.hh" @@ -34,6 +33,25 @@ namespace moof { +void resource::print_types() +{ +} + +void resource::manage_loader(const std::string& extension, loader_ptr& loader, bool set) +{ + static type_lookup lookup; + + if (loader || set) + { + lookup[extension] = loader; + } + else + { + std::map::iterator it; + it = lookup.find(extension); + if (it != lookup.end()) loader = (*it).second; + } +} static std::string search_paths_; @@ -41,7 +59,8 @@ typedef boost::weak_ptr resource_weakptr; static hash resource_table_; // static member -resource::type_lookup_ptr resource::type_lookup_; +//resource::type_lookup_ptr resource::type_lookup_; +//resource::type_lookup resource::type_lookup_; #ifdef USE_HOTLOADING @@ -108,10 +127,15 @@ resource_ptr resource::load(const std::string& path) { std::string extension = stlplus::extension_part(path); - if (!find(path)) return resource_ptr(); + std::string path1 = path; + if (!find(path1)) + { + log_error("trying to load missing resource:", path1); + return resource_ptr(); + } hash::iterator it; - it = resource_table_.find(path); + it = resource_table_.find(path1); if (it != resource_table_.end()) { resource_weakptr rsrc = (*it).second; @@ -119,19 +143,18 @@ resource_ptr resource::load(const std::string& path) if (locked) return locked; } - std::map::iterator jt; - jt = type_lookup_->find(extension); - if (jt != type_lookup_->end()) + loader_ptr loader; + manage_loader(extension, loader); + if (loader) { - resource_ptr rsrc((*jt).second->load(path)); - rsrc->set_loader(path, (*jt).second); - resource_table_[path] = rsrc; + resource_ptr rsrc(loader->load(path1)); + rsrc->set_loader(path1, loader); + resource_table_[path1] = rsrc; #ifdef USE_HOTLOADING - int wd = inotify_add_watch(monitor_fd_, - path.c_str(), IN_MODIFY); + int wd = inotify_add_watch(monitor_fd_, path1.c_str(), IN_MODIFY); rsrc->set_watch_descriptor(wd); - monitor_lookup_[wd] = path; + monitor_lookup_[wd] = path1; #endif log_info("loaded", rsrc.get()); @@ -172,8 +195,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 @@ -190,13 +212,35 @@ void resource::add_search_paths(const std::string& paths) bool resource::find(const std::string& path) { - return !stlplus::lookup(path, search_paths_, ":").empty(); + //std::string file = stlplus::lookup(path, search_paths_, ":"); + //log_info("found file", file, "in", search_paths_); + //return !stlplus::lookup(path, search_paths_, ":").empty(); + return find_file(path) != ""; +} + +std::string resource::find_file(const std::string& name) +{ + //log_info("looking for", name, "in", search_paths_); + //return stlplus::lookup(name, search_paths_, ":"); + + std::vector paths; + boost::split(paths, search_paths_, boost::is_any_of(":")); + + std::vector::iterator it; + for (it = paths.begin(); it != paths.end(); ++it) + { + *it += "/"; + *it += name; + log_info("looking for", name, "in", *it); + if (stlplus::file_exists(*it)) return *it; + } + + return std::string(); } FILE* resource::open_file(const std::string& path, const std::string& mode) { - std::string file = stlplus::lookup(path, search_paths_, ":"); - return fopen(file.c_str(), mode.c_str()); + return fopen(find_file(path).c_str(), mode.c_str()); }