X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fyoink;a=blobdiff_plain;f=src%2Fmoof%2Fresource.cc;h=314f80b5db81707e73b4cb3d91300f6d5b5a8ea0;hp=ac200f7831e380866665bc490a48ed416340a7e7;hb=1da520638918096276158ecdfaeebc14a3d70be7;hpb=6b0a0d0efafe34d48ab344fca3b479553bd4e62c diff --git a/src/moof/resource.cc b/src/moof/resource.cc index ac200f7..314f80b 100644 --- a/src/moof/resource.cc +++ b/src/moof/resource.cc @@ -9,24 +9,22 @@ * **************************************************************************/ +#include "config.h" + +#ifdef USE_HOTLOADING +#include +#include +#endif + #include #include #include - #include #include "hash.hh" #include "resource.hh" -#if HAVE_CONFIG_H -#include "../config.h" -#endif - -#if USE_HOTLOADING -#include -#include -#endif #ifndef BUF_SIZE #define BUF_SIZE 4096 @@ -35,17 +33,37 @@ namespace moof { +void resource::print_types() +{ +} -static std::vector search_paths_; +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_; 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_; -#if USE_HOTLOADING +#ifdef USE_HOTLOADING static hash monitor_lookup_; static int monitor_fd_ = inotify_init1(IN_NONBLOCK); #endif @@ -54,7 +72,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 +117,7 @@ int resource::reload_as_needed() resource::~resource() { -#if USE_HOTLOADING +#ifdef USE_HOTLOADING inotify_rm_watch(monitor_fd_, wd_); #endif } @@ -109,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 = find_file(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; @@ -120,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; -#if USE_HOTLOADING - int wd = inotify_add_watch(monitor_fd_, - path.c_str(), IN_MODIFY); +#ifdef USE_HOTLOADING + 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,9 +194,8 @@ void resource::reload() typeinfo_ = resource->typeinfo_; unloader_ = resource->unloader_; -#if USE_HOTLOADING - int wd = inotify_add_watch(monitor_fd_, - path_.c_str(), IN_MODIFY); +#ifdef USE_HOTLOADING + int wd = inotify_add_watch(monitor_fd_, path_.c_str(), IN_MODIFY); set_watch_descriptor(wd); monitor_lookup_[wd] = path_; #endif @@ -185,66 +206,27 @@ void resource::reload() void resource::add_search_paths(const std::string& paths) { - std::vector pathList; - boost::split(pathList, paths, boost::is_any_of(":")); - - add_search_paths(pathList); -} - -void resource::add_search_paths(const std::vector& pathList) -{ - std::vector::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; - } + std::string file = stlplus::lookup(path, search_paths_, ":"); + log_info("found file", file, "in", search_paths_); + return !stlplus::lookup(path, search_paths_, ":").empty(); +} - return false; +std::string resource::find_file(const std::string& name) +{ + log_info("looking for", name, "in", search_paths_); + return stlplus::lookup(name, search_paths_, ":"); } 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::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()); }