summaryrefslogtreecommitdiff
path: root/src/ModuleCache.cc
diff options
context:
space:
mode:
authorMarius Kintel <marius@kintel.net>2012-02-09 00:44:29 (GMT)
committerMarius Kintel <marius@kintel.net>2012-02-15 00:32:38 (GMT)
commitabcd702a6812abb77317d6fab4f3bdacc273b463 (patch)
treefd84bae1719e3b8a5b3655bbf4d1fef69531031b /src/ModuleCache.cc
parentaccb71b5463b8265b7540a0c310aa827d1d0838a (diff)
Minor clarification, cleanup file reading
Diffstat (limited to 'src/ModuleCache.cc')
-rw-r--r--src/ModuleCache.cc74
1 files changed, 35 insertions, 39 deletions
diff --git a/src/ModuleCache.cc b/src/ModuleCache.cc
index dd953c5..59424d4 100644
--- a/src/ModuleCache.cc
+++ b/src/ModuleCache.cc
@@ -5,6 +5,7 @@
#include "openscad.h"
#include <stdio.h>
+#include <fstream>
#include <sstream>
#include <sys/stat.h>
@@ -12,8 +13,9 @@ ModuleCache *ModuleCache::inst = NULL;
Module *ModuleCache::evaluate(const std::string &filename)
{
- Module *cached = NULL;
+ Module *lib_mod = NULL;
+ // Create cache ID
struct stat st;
memset(&st, 0, sizeof(struct stat));
stat(filename.c_str(), &st);
@@ -22,61 +24,55 @@ Module *ModuleCache::evaluate(const std::string &filename)
idstream << std::hex << st.st_mtime << "." << st.st_size;
std::string cache_id = idstream.str();
+ // Lookup in cache
if (this->entries.find(filename) != this->entries.end() &&
this->entries[filename].cache_id == cache_id) {
#ifdef DEBUG
PRINTB("Using cached library: %s (%s)", filename % cache_id);
#endif
PRINTB("%s", this->entries[filename].msg);
- cached = &(*this->entries[filename].module);
+ lib_mod = &(*this->entries[filename].module);
}
-
- if (cached) {
- cached->handleDependencies();
- return cached;
- }
- else {
+
+ // If cache lookup failed (non-existing or old timestamp), compile module
+ if (!lib_mod) {
+#ifdef DEBUG
if (this->entries.find(filename) != this->entries.end()) {
PRINTB("Recompiling cached library: %s (%s)", filename % cache_id);
}
else {
PRINTB("Compiling library '%s'.", filename);
}
- }
-
- FILE *fp = fopen(filename.c_str(), "rt");
- if (!fp) {
- fprintf(stderr, "WARNING: Can't open library file '%s'\n", filename.c_str());
- return NULL;
- }
- std::stringstream text;
- char buffer[513];
- int ret;
- while ((ret = fread(buffer, 1, 512, fp)) > 0) {
- buffer[ret] = 0;
- text << buffer;
- }
- fclose(fp);
-
- print_messages_push();
-
- cache_entry e = { NULL, cache_id, std::string("WARNING: Library `") + filename + "' tries to recursively use itself!" };
- if (this->entries.find(filename) != this->entries.end())
- delete this->entries[filename].module;
- this->entries[filename] = e;
+#endif
- std::string pathname = boosty::stringy(fs::path(filename).parent_path());
- Module *lib_mod = dynamic_cast<Module*>(parse(text.str().c_str(), pathname.c_str(), 0));
+ std::ifstream ifs(filename.c_str());
+ if (!ifs.is_open()) {
+ PRINTB("WARNING: Can't open library file '%s'\n", filename);
+ return NULL;
+ }
+ std::string text((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());
- if (lib_mod) {
- this->entries[filename].module = lib_mod;
- this->entries[filename].msg = print_messages_stack.back();
- lib_mod->handleDependencies();
- } else {
- this->entries.erase(filename);
+ print_messages_push();
+
+ cache_entry e = { NULL, cache_id, std::string("WARNING: Library `") + filename + "' tries to recursively use itself!" };
+ if (this->entries.find(filename) != this->entries.end())
+ delete this->entries[filename].module;
+ this->entries[filename] = e;
+
+ std::string pathname = boosty::stringy(fs::path(filename).parent_path());
+ lib_mod = dynamic_cast<Module*>(parse(text.c_str(), pathname.c_str(), 0));
+
+ if (lib_mod) {
+ this->entries[filename].module = lib_mod;
+ this->entries[filename].msg = print_messages_stack.back();
+ } else {
+ this->entries.erase(filename);
+ }
+
+ print_messages_pop();
}
- print_messages_pop();
+ if (lib_mod) lib_mod->handleDependencies();
return lib_mod;
}
contact: Jan Huwald // Impressum