From f3230836ca2e263e84b58ffec6645c0f77439117 Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Sat, 1 Sep 2012 18:28:50 +0200 Subject: [PATCH] resolve inclusion path name in Parser --- Debug/src/loader/subdir.mk | 9 +++++--- Release/src/loader/subdir.mk | 9 +++++--- src/loader/Parser.cpp | 9 +++++--- src/loader/Parser.h | 12 ++++++----- src/loader/utility.cpp | 41 ++++++++++++++++++++++++++++++++++++ src/loader/utility.h | 21 ++++++++++++++++++ test-data/test.l2s | 6 +++--- 7 files changed, 90 insertions(+), 17 deletions(-) create mode 100644 src/loader/utility.cpp create mode 100644 src/loader/utility.h diff --git a/Debug/src/loader/subdir.mk b/Debug/src/loader/subdir.mk index 89118df..19b1d87 100644 --- a/Debug/src/loader/subdir.mk +++ b/Debug/src/loader/subdir.mk @@ -7,19 +7,22 @@ CPP_SRCS += \ ../src/loader/Interpreter.cpp \ ../src/loader/ParsedSource.cpp \ ../src/loader/Parser.cpp \ -../src/loader/Tokenizer.cpp +../src/loader/Tokenizer.cpp \ +../src/loader/utility.cpp OBJS += \ ./src/loader/Interpreter.o \ ./src/loader/ParsedSource.o \ ./src/loader/Parser.o \ -./src/loader/Tokenizer.o +./src/loader/Tokenizer.o \ +./src/loader/utility.o CPP_DEPS += \ ./src/loader/Interpreter.d \ ./src/loader/ParsedSource.d \ ./src/loader/Parser.d \ -./src/loader/Tokenizer.d +./src/loader/Tokenizer.d \ +./src/loader/utility.d # Each subdirectory must supply rules for building sources it contributes diff --git a/Release/src/loader/subdir.mk b/Release/src/loader/subdir.mk index 50f5baf..c619e85 100644 --- a/Release/src/loader/subdir.mk +++ b/Release/src/loader/subdir.mk @@ -7,19 +7,22 @@ CPP_SRCS += \ ../src/loader/Interpreter.cpp \ ../src/loader/ParsedSource.cpp \ ../src/loader/Parser.cpp \ -../src/loader/Tokenizer.cpp +../src/loader/Tokenizer.cpp \ +../src/loader/utility.cpp OBJS += \ ./src/loader/Interpreter.o \ ./src/loader/ParsedSource.o \ ./src/loader/Parser.o \ -./src/loader/Tokenizer.o +./src/loader/Tokenizer.o \ +./src/loader/utility.o CPP_DEPS += \ ./src/loader/Interpreter.d \ ./src/loader/ParsedSource.d \ ./src/loader/Parser.d \ -./src/loader/Tokenizer.d +./src/loader/Tokenizer.d \ +./src/loader/utility.d # Each subdirectory must supply rules for building sources it contributes diff --git a/src/loader/Parser.cpp b/src/loader/Parser.cpp index d6abcc3..bd65200 100644 --- a/src/loader/Parser.cpp +++ b/src/loader/Parser.cpp @@ -7,6 +7,8 @@ #include "Parser.h" +#include "utility.h" + #include #include @@ -17,9 +19,10 @@ using std::vector; namespace loader { -Parser::Parser(const char *file, ParsedSource &product) +Parser::Parser(const string &file, ParsedSource &product) : file(file) -, in(file) +, dirname(Dirname(file)) +, in(this->file.c_str()) , tok(in) , product(product) { if (!in) { @@ -74,7 +77,7 @@ void Parser::ParseExportDirective() { void Parser::ParseIncludeDirective() { Tokenizer::Token t(GetToken()); AssertTokenType(t.type, Tokenizer::Token::STRING); - Parser sub(t.str.c_str(), product); // TODO: resolve path name + Parser sub(CatPath(dirname, t.str), product); sub.Parse(); } diff --git a/src/loader/Parser.h b/src/loader/Parser.h index 9e4cbf4..ebfeafa 100644 --- a/src/loader/Parser.h +++ b/src/loader/Parser.h @@ -26,7 +26,7 @@ class PropertyList; class Parser { public: - Parser(const char *file, ParsedSource &product); + Parser(const std::string &file, ParsedSource &product); ~Parser() { } private: Parser(const Parser &); @@ -38,12 +38,13 @@ public: public: class Error: public std::runtime_error { public: - Error(const char *file, int line, const std::string &msg) + Error(const std::string &file, int line, const std::string &msg) : std::runtime_error(msg), file(file), line(line) { }; - const char *File() const { return file; } + ~Error() throw() { } + const std::string &File() const { return file; } int Line() const { return line; } private: - const char *file; + std::string file; int line; }; @@ -73,7 +74,8 @@ private: bool BeginOfPropertyList(const Tokenizer::Token &) const; private: - const char *file; + std::string file; + std::string dirname; std::ifstream in; Tokenizer tok; ParsedSource &product; diff --git a/src/loader/utility.cpp b/src/loader/utility.cpp new file mode 100644 index 0000000..ee88ea0 --- /dev/null +++ b/src/loader/utility.cpp @@ -0,0 +1,41 @@ +/* + * utility.cpp + * + * Created on: Sep 1, 2012 + * Author: holy + */ + +#include "utility.h" + +#include +#include + +using std::string; + +namespace loader { + +string Dirname(const string &path) { + // unix version + char *str(new char[path.size() + 1]); + std::memcpy(str, path.c_str(), path.size()); + str[path.size()] = '\0'; + string dn(dirname(str)); + delete str; + return dn; +} + +string CatPath(const string &lhs, const string &rhs) { + // unix version + string path(lhs); + if (!path.empty() && path[path.size() - 1] != '/') { + path += '/'; + } + if (!rhs.empty() && rhs[0] == '/') { + path.append(rhs, 1, string::npos); + } else { + path += rhs; + } + return path; +} + +} diff --git a/src/loader/utility.h b/src/loader/utility.h new file mode 100644 index 0000000..b8ccd10 --- /dev/null +++ b/src/loader/utility.h @@ -0,0 +1,21 @@ +/* + * utility.h + * + * Created on: Sep 1, 2012 + * Author: holy + */ + +#ifndef LOADER_UTILITY_H_ +#define LOADER_UTILITY_H_ + +#include + +namespace loader { + +std::string Dirname(const std::string &path); + +std::string CatPath(const std::string &lhs, const std::string &rhs); + +} + +#endif /* LOADER_UTILITY_H_ */ diff --git a/test-data/test.l2s b/test-data/test.l2s index 5958c74..6b03e29 100644 --- a/test-data/test.l2s +++ b/test-data/test.l2s @@ -510,11 +510,11 @@ export Sprite itemTargetCursor { offset: <0,64> } -include "test-data/spells.l2s" +include "spells.l2s" export String spellMenuHeadline "Please choose a spell." -include "test-data/ikaris.l2s" -include "test-data/items.l2s" +include "ikaris.l2s" +include "items.l2s" export String itemMenuHeadline "Please choose an item." -- 2.39.2