*
**************************************************************************/
+#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
-
-#if USE_HOTLOADING
-#include <sys/inotify.h>
-#include <sys/ioctl.h>
-#endif
#ifndef BUF_SIZE
#define BUF_SIZE 4096
namespace moof {
+void resource::print_types()
+{
+}
-static std::vector<std::string> 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<std::string,loader_ptr>::iterator it;
+ it = lookup.find(extension);
+ if (it != lookup.end()) loader = (*it).second;
+ }
+}
+
+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_;
+//resource::type_lookup_ptr resource::type_lookup_;
+//resource::type_lookup 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
{
int num_resources = 0;
-#if USE_HOTLOADING
+#ifdef USE_HOTLOADING
log_info("hotloading?");
char bytes[BUF_SIZE];
int num_bytes;
resource::~resource()
{
-#if USE_HOTLOADING
+#ifdef USE_HOTLOADING
inotify_rm_watch(monitor_fd_, wd_);
#endif
}
{
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<std::string,resource_weakptr,hash_function>::iterator it;
- it = resource_table_.find(path);
+ it = resource_table_.find(path1);
if (it != resource_table_.end())
{
resource_weakptr rsrc = (*it).second;
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;
+ 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());
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
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;
+ //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) != "";
}
-FILE* resource::open_file(const std::string& path, const std::string& mode)
+std::string resource::find_file(const std::string& name)
{
-#if defined(_WIN32)
- // windows always has to be a little different
- //boost::replace_all(path, "/", "\\");
-#endif
+ //log_info("looking for", name, "in", search_paths_);
+ //return stlplus::lookup(name, search_paths_, ":");
+
+ std::vector<std::string> paths;
+ boost::split(paths, search_paths_, boost::is_any_of(":"));
std::vector<std::string>::iterator it;
- for (it = search_paths_.begin(); it != search_paths_.end(); ++it)
+ for (it = paths.begin(); it != 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;
+ *it += "/";
+ *it += name;
+ log_info("looking for", name, "in", *it);
+ if (stlplus::file_exists(*it)) return *it;
}
- // last ditch effort; maybe it's already a path to a valid resource
- return fopen(path.c_str(), mode.c_str());
+ return std::string();
+}
+
+FILE* resource::open_file(const std::string& path, const std::string& mode)
+{
+ return fopen(find_file(path).c_str(), mode.c_str());
}