From 7830acc2ab78d0c82a72948c4eb87eeb6463693c Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Sat, 15 Sep 2012 20:30:10 +0200 Subject: [PATCH] added argument interpreter --- Debug/src/app/subdir.mk | 3 +++ Release/src/app/subdir.mk | 3 +++ src/app/Arguments.cpp | 43 +++++++++++++++++++++++++++++++++++++++ src/app/Arguments.h | 37 +++++++++++++++++++++++++++++++++ src/main.cpp | 42 ++++++++++++++++++++++++++++++-------- 5 files changed, 120 insertions(+), 8 deletions(-) create mode 100644 src/app/Arguments.cpp create mode 100644 src/app/Arguments.h diff --git a/Debug/src/app/subdir.mk b/Debug/src/app/subdir.mk index 645d8ed..8fad388 100644 --- a/Debug/src/app/subdir.mk +++ b/Debug/src/app/subdir.mk @@ -5,14 +5,17 @@ # Add inputs and outputs from these tool invocations to the build variables CPP_SRCS += \ ../src/app/Application.cpp \ +../src/app/Arguments.cpp \ ../src/app/Input.cpp OBJS += \ ./src/app/Application.o \ +./src/app/Arguments.o \ ./src/app/Input.o CPP_DEPS += \ ./src/app/Application.d \ +./src/app/Arguments.d \ ./src/app/Input.d diff --git a/Release/src/app/subdir.mk b/Release/src/app/subdir.mk index 3255ffa..cf54190 100644 --- a/Release/src/app/subdir.mk +++ b/Release/src/app/subdir.mk @@ -5,14 +5,17 @@ # Add inputs and outputs from these tool invocations to the build variables CPP_SRCS += \ ../src/app/Application.cpp \ +../src/app/Arguments.cpp \ ../src/app/Input.cpp OBJS += \ ./src/app/Application.o \ +./src/app/Arguments.o \ ./src/app/Input.o CPP_DEPS += \ ./src/app/Application.d \ +./src/app/Arguments.d \ ./src/app/Input.d diff --git a/src/app/Arguments.cpp b/src/app/Arguments.cpp new file mode 100644 index 0000000..bdfdf18 --- /dev/null +++ b/src/app/Arguments.cpp @@ -0,0 +1,43 @@ +/* + * Arguments.cpp + * + * Created on: Sep 15, 2012 + * Author: holy + */ + +#include "Arguments.h" + +#include +#include + +namespace app { + +Arguments::Arguments() +: outfile(0) { + +} + + +void Arguments::Read(int argc, char **argv) { + for (int i(1); i < argc; ++i) { + char *arg(argv[i]); + if (arg[0] == '-') { + switch (arg[1]) { + case 'o': + if (i + 1 >= argc) { + throw std::runtime_error("missing argument to -o"); + } + ++i; + outfile = argv[i]; + break; + default: + throw std::runtime_error(std::string("unknown option ") + arg[1]); + break; + } + } else { + infiles.push_back(arg); + } + } +} + +} diff --git a/src/app/Arguments.h b/src/app/Arguments.h new file mode 100644 index 0000000..c7e37ee --- /dev/null +++ b/src/app/Arguments.h @@ -0,0 +1,37 @@ +/* + * Arguments.h + * + * Created on: Sep 15, 2012 + * Author: holy + */ + +#ifndef APP_ARGUMENTS_H_ +#define APP_ARGUMENTS_H_ + +#include + +namespace app { + +class Arguments { + +public: + Arguments(); + ~Arguments() { } + +public: + void Read(int argc, char **argv); + + const std::vector &Infiles() const { return infiles; } + + bool OutfileSet() const { return outfile; } + const char *OutfilePath() const { return outfile; } + +private: + std::vector infiles; + const char *outfile; + +}; + +} + +#endif /* APP_ARGUMENTS_H_ */ diff --git a/src/main.cpp b/src/main.cpp index 2ac0423..9770626 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,6 +6,7 @@ */ #include "app/Application.h" +#include "app/Arguments.h" #include "app/Input.h" #include "battle/BattleState.h" #include "battle/Hero.h" @@ -38,10 +39,12 @@ #include #include #include +#include #include #include using app::Application; +using app::Arguments; using app::Input; using battle::BattleState; using battle::Hero; @@ -73,6 +76,8 @@ using std::cerr; using std::cout; using std::endl; using std::exception; +using std::string; +using std::vector; int main(int argc, char **argv) { const int width = 800; @@ -102,18 +107,39 @@ int main(int argc, char **argv) { Stats::CreateTypeDescription(); common::TargetingMode::CreateTypeDescription(); + Arguments args; + args.Read(argc, argv); + ParsedSource source; - Parser("test-data/test.l2s", source).Parse(); - Parser("test-data/ikaris.l2s", source).Parse(); - Parser("test-data/items.l2s", source).Parse(); - Parser("test-data/spells.l2s", source).Parse(); - Parser("test-data/constants.l2s", source).Parse(); + + for (vector::const_iterator i(args.Infiles().begin()), end(args.Infiles().end()); i != end; ++i) { + string filePath(*i); + switch (filePath[filePath.size() - 1]) { + case 'o': + // TODO: load object file + break; + case 's': + Parser(filePath, source).Parse(); + break; + default: + throw std::runtime_error("don't know what to do with " + filePath); + } + } + +// Parser("test-data/test.l2s", source).Parse(); +// Parser("test-data/ikaris.l2s", source).Parse(); +// Parser("test-data/items.l2s", source).Parse(); +// Parser("test-data/spells.l2s", source).Parse(); +// Parser("test-data/constants.l2s", source).Parse(); + Interpreter intp(source); intp.ReadSource(); -// std::ofstream testOut("test-data/test.l2o"); -// Compiler(intp).Write(testOut); -// return 0; + if (args.OutfileSet()) { + std::ofstream testOut(args.OutfilePath()); + Compiler(intp).Write(testOut); + return 0; + } int battleResId(TypeDescription::GetTypeId("BattleResources")); int heroId(TypeDescription::GetTypeId("Hero")); -- 2.39.2