summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/dxfdim.cc17
-rw-r--r--src/import.cc2
-rw-r--r--src/linearextrude.cc2
-rw-r--r--src/module.cc22
-rw-r--r--src/module.h8
-rw-r--r--src/parser.y50
-rw-r--r--src/rotateextrude.cc2
-rw-r--r--src/surface.cc2
8 files changed, 70 insertions, 35 deletions
diff --git a/src/dxfdim.cc b/src/dxfdim.cc
index 1ed37fa..53bc480 100644
--- a/src/dxfdim.cc
+++ b/src/dxfdim.cc
@@ -49,6 +49,9 @@ Value builtin_dxf_dim(const Context *ctx, const std::vector<std::string> &argnam
double yorigin = 0;
double scale = 1;
+ // FIXME: We don't lookup the file relative to where this function was instantiated
+ // since the path is only available for ModuleInstantiations, not function expressions.
+ // See isse #217
for (size_t i = 0; i < argnames.size() && i < args.size(); i++) {
if (argnames[i] == "file")
filename = ctx->getAbsolutePath(args[i].toString());
@@ -63,9 +66,16 @@ Value builtin_dxf_dim(const Context *ctx, const std::vector<std::string> &argnam
}
std::stringstream keystream;
+ fs::path filepath(filename);
+ uintmax_t filesize = -1;
+ time_t lastwritetime = -1;
+ if (fs::exists(filepath) && fs::is_regular_file(filepath)) {
+ filesize = fs::file_size(filepath);
+ lastwritetime = fs::last_write_time(filepath);
+ }
keystream << filename << "|" << layername << "|" << name << "|" << xorigin
- << "|" << yorigin <<"|" << scale << "|" << fs::last_write_time(filename)
- << "|" << fs::file_size(filename);
+ << "|" << yorigin <<"|" << scale << "|" << lastwritetime
+ << "|" << filesize;
std::string key = keystream.str();
if (dxf_dim_cache.find(key) != dxf_dim_cache.end())
return dxf_dim_cache.find(key)->second;
@@ -133,6 +143,9 @@ Value builtin_dxf_cross(const Context *ctx, const std::vector<std::string> &argn
double yorigin = 0;
double scale = 1;
+ // FIXME: We don't lookup the file relative to where this function was instantiated
+ // since the path is only available for ModuleInstantiations, not function expressions.
+ // See isse #217
for (size_t i = 0; i < argnames.size() && i < args.size(); i++) {
if (argnames[i] == "file")
filename = ctx->getAbsolutePath(args[i].toString());
diff --git a/src/import.cc b/src/import.cc
index e693401..627fb56 100644
--- a/src/import.cc
+++ b/src/import.cc
@@ -80,7 +80,7 @@ AbstractNode *ImportModule::evaluate(const Context *ctx, const ModuleInstantiati
c.args(argnames, argexpr, inst_argnames, inst->argvalues);
Value v = c.lookup_variable("file");
- std::string filename = c.getAbsolutePath(v.isUndefined() ? "" : v.toString());
+ std::string filename = inst->getAbsolutePath(v.isUndefined() ? "" : v.toString());
import_type_e actualtype = this->type;
if (actualtype == TYPE_UNKNOWN) {
std::string extraw = boosty::extension_str( fs::path(filename) );
diff --git a/src/linearextrude.cc b/src/linearextrude.cc
index 43db907..ff9682e 100644
--- a/src/linearextrude.cc
+++ b/src/linearextrude.cc
@@ -75,7 +75,7 @@ AbstractNode *LinearExtrudeModule::evaluate(const Context *ctx, const ModuleInst
if (!file.isUndefined()) {
PRINT("DEPRECATED: Support for reading files in linear_extrude will be removed in future releases. Use a child import() instead.");
- node->filename = c.getAbsolutePath(file.toString());
+ node->filename = inst->getAbsolutePath(file.toString());
}
// if height not given, and first argument is a number,
diff --git a/src/module.cc b/src/module.cc
index e6dcb57..322085b 100644
--- a/src/module.cc
+++ b/src/module.cc
@@ -32,6 +32,9 @@
#include "function.h"
#include "printutils.h"
+#include <boost/filesystem.hpp>
+namespace fs = boost::filesystem;
+#include "boosty.h"
#include <boost/foreach.hpp>
#include <sstream>
#include <sys/stat.h>
@@ -67,6 +70,19 @@ IfElseModuleInstantiation::~IfElseModuleInstantiation()
BOOST_FOREACH (ModuleInstantiation *v, else_children) delete v;
}
+/*!
+ Returns the absolute path to the given filename, unless it's empty.
+ */
+std::string ModuleInstantiation::getAbsolutePath(const std::string &filename) const
+{
+ if (!filename.empty() && !boosty::is_absolute(fs::path(filename))) {
+ return boosty::absolute(fs::path(this->modpath) / filename).string();
+ }
+ else {
+ return filename;
+ }
+}
+
std::string ModuleInstantiation::dump(const std::string &indent) const
{
std::stringstream dump;
@@ -152,15 +168,15 @@ AbstractNode *Module::evaluate(const Context *ctx, const ModuleInstantiation *in
c.functions_p = &functions;
c.modules_p = &modules;
-
+
if (!usedlibs.empty())
c.usedlibs_p = &usedlibs;
else
c.usedlibs_p = NULL;
-
+
BOOST_FOREACH(const std::string &var, assignments_var) {
c.set_variable(var, assignments.at(var)->evaluate(&c));
- }
+ }
AbstractNode *node = new AbstractNode(inst);
for (size_t i = 0; i < children.size(); i++) {
diff --git a/src/module.h b/src/module.h
index cc82f81..1f9e303 100644
--- a/src/module.h
+++ b/src/module.h
@@ -10,7 +10,7 @@
class ModuleInstantiation
{
public:
- ModuleInstantiation(const std::string &name = "")
+ ModuleInstantiation(const std::string &name = "")
: ctx(NULL),
tag_root(false), tag_highlight(false), tag_background(false), modname(name) { }
virtual ~ModuleInstantiation();
@@ -19,6 +19,10 @@ public:
class AbstractNode *evaluate(const class Context *ctx) const;
std::vector<AbstractNode*> evaluateChildren(const Context *ctx = NULL) const;
+ void setPath(const std::string &path) { this->modpath = path; }
+ const std::string &path() const { return this->modpath; }
+ std::string getAbsolutePath(const std::string &filename) const;
+
const std::string &name() const { return this->modname; }
bool isBackground() const { return this->tag_background; }
bool isHighlight() const { return this->tag_highlight; }
@@ -35,6 +39,7 @@ public:
bool tag_background;
protected:
std::string modname;
+ std::string modpath;
friend class Module;
};
@@ -61,6 +66,7 @@ class Module : public AbstractModule
public:
Module() : is_handling_dependencies(false) { }
virtual ~Module();
+
virtual AbstractNode *evaluate(const Context *ctx, const ModuleInstantiation *inst) const;
virtual std::string dump(const std::string &indent, const std::string &name) const;
diff --git a/src/parser.y b/src/parser.y
index 536f4ef..70aebba 100644
--- a/src/parser.y
+++ b/src/parser.y
@@ -58,6 +58,12 @@ int lexerlex(void);
std::vector<Module*> module_stack;
Module *currmodule;
+extern void lexerdestroy();
+extern FILE *lexerin;
+extern const char *parser_input_buffer;
+const char *parser_input_buffer;
+std::string parser_source_path;
+
class ArgContainer {
public:
std::string argname;
@@ -77,6 +83,7 @@ public:
class Value *value;
class Expression *expr;
class ModuleInstantiation *inst;
+ std::vector<ModuleInstantiation*> *instvec;
class IfElseModuleInstantiation *ifelse;
class ArgContainer *arg;
class ArgsContainer *args;
@@ -117,8 +124,8 @@ public:
%type <inst> module_instantiation
%type <ifelse> if_statement
%type <ifelse> ifelse_statement
-%type <inst> children_instantiation
-%type <inst> module_instantiation_list
+%type <instvec> children_instantiation
+%type <instvec> module_instantiation_list
%type <inst> single_module_instantiation
%type <args> arguments_call
@@ -182,9 +189,9 @@ statement:
/* Will return a dummy parent node with zero or more children */
children_instantiation:
module_instantiation {
- $$ = new ModuleInstantiation();
+ $$ = new std::vector<ModuleInstantiation*>;
if ($1) {
- $$->children.push_back($1);
+ $$->push_back($1);
}
} |
'{' module_instantiation_list '}' {
@@ -196,14 +203,14 @@ if_statement:
$$ = new IfElseModuleInstantiation();
$$->argnames.push_back("");
$$->argexpr.push_back($3);
+ $$->setPath(parser_source_path);
if ($$) {
- $$->children = $5->children;
+ $$->children = *$5;
} else {
- for (size_t i = 0; i < $5->children.size(); i++)
- delete $5->children[i];
+ for (size_t i = 0; i < $5->size(); i++)
+ delete (*$5)[i];
}
- $5->children.clear();
delete $5;
} ;
@@ -214,12 +221,11 @@ ifelse_statement:
if_statement TOK_ELSE children_instantiation {
$$ = $1;
if ($$) {
- $$->else_children = $3->children;
+ $$->else_children = *$3;
} else {
- for (size_t i = 0; i < $3->children.size(); i++)
- delete $3->children[i];
+ for (size_t i = 0; i < $3->size(); i++)
+ delete (*$3)[i];
}
- $3->children.clear();
delete $3;
} ;
@@ -246,12 +252,11 @@ module_instantiation:
single_module_instantiation children_instantiation {
$$ = $1;
if ($$) {
- $$->children = $2->children;
+ $$->children = *$2;
} else {
- for (size_t i = 0; i < $2->children.size(); i++)
- delete $2->children[i];
+ for (size_t i = 0; i < $2->size(); i++)
+ delete (*$2)[i];
}
- $2->children.clear();
delete $2;
} |
ifelse_statement {
@@ -260,12 +265,12 @@ module_instantiation:
module_instantiation_list:
/* empty */ {
- $$ = new ModuleInstantiation();
+ $$ = new std::vector<ModuleInstantiation*>;
} |
module_instantiation_list module_instantiation {
$$ = $1;
if ($$) {
- if ($2) $$->children.push_back($2);
+ if ($2) $$->push_back($2);
} else {
delete $2;
}
@@ -276,6 +281,7 @@ single_module_instantiation:
$$ = new ModuleInstantiation($1);
$$->argnames = $3->argnames;
$$->argexpr = $3->argexpr;
+ $$->setPath(parser_source_path);
free($1);
delete $3;
}
@@ -536,18 +542,12 @@ void yyerror (char const *s)
currmodule = NULL;
}
-extern void lexerdestroy();
-extern FILE *lexerin;
-extern const char *parser_input_buffer;
-const char *parser_input_buffer;
-std::string parser_source_path;
-
Module *parse(const char *text, const char *path, int debug)
{
lexerin = NULL;
parser_error_pos = -1;
parser_input_buffer = text;
- parser_source_path = std::string(path);
+ parser_source_path = boosty::absolute(std::string(path)).string();
module_stack.clear();
Module *rootmodule = currmodule = new Module();
diff --git a/src/rotateextrude.cc b/src/rotateextrude.cc
index dc8ea34..c4d9342 100644
--- a/src/rotateextrude.cc
+++ b/src/rotateextrude.cc
@@ -71,7 +71,7 @@ AbstractNode *RotateExtrudeModule::evaluate(const Context *ctx, const ModuleInst
if (!file.isUndefined()) {
PRINT("DEPRECATED: Support for reading files in rotate_extrude will be removed in future releases. Use a child import() instead.");
- node->filename = c.getAbsolutePath(file.toString());
+ node->filename = inst->getAbsolutePath(file.toString());
}
node->layername = layer.isUndefined() ? "" : layer.toString();
diff --git a/src/surface.cc b/src/surface.cc
index 4339ead..ca5031e 100644
--- a/src/surface.cc
+++ b/src/surface.cc
@@ -83,7 +83,7 @@ AbstractNode *SurfaceModule::evaluate(const Context *ctx, const ModuleInstantiat
c.args(argnames, argexpr, inst->argnames, inst->argvalues);
Value fileval = c.lookup_variable("file");
- node->filename = c.getAbsolutePath(fileval.isUndefined() ? "" : fileval.toString());
+ node->filename = inst->getAbsolutePath(fileval.isUndefined() ? "" : fileval.toString());
Value center = c.lookup_variable("center", true);
if (center.type() == Value::BOOL) {
contact: Jan Huwald // Impressum