]> git.localhorst.tv Git - l2e.git/commitdiff
added argument interpreter
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Sat, 15 Sep 2012 18:30:10 +0000 (20:30 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Sat, 15 Sep 2012 19:45:10 +0000 (21:45 +0200)
Debug/src/app/subdir.mk
Release/src/app/subdir.mk
src/app/Arguments.cpp [new file with mode: 0644]
src/app/Arguments.h [new file with mode: 0644]
src/main.cpp

index 645d8eda5d62884af8b66cca8386a4b13019570b..8fad388622e8a78baa916580f11755e2e458e935 100644 (file)
@@ -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 
 
 
index 3255ffa8f698ddf8e8e278ef4d1868e075540d94..cf5419093b43305265b5d8d626c8baca39f4c90c 100644 (file)
@@ -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 (file)
index 0000000..bdfdf18
--- /dev/null
@@ -0,0 +1,43 @@
+/*
+ * Arguments.cpp
+ *
+ *  Created on: Sep 15, 2012
+ *      Author: holy
+ */
+
+#include "Arguments.h"
+
+#include <stdexcept>
+#include <string>
+
+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 (file)
index 0000000..c7e37ee
--- /dev/null
@@ -0,0 +1,37 @@
+/*
+ * Arguments.h
+ *
+ *  Created on: Sep 15, 2012
+ *      Author: holy
+ */
+
+#ifndef APP_ARGUMENTS_H_
+#define APP_ARGUMENTS_H_
+
+#include <vector>
+
+namespace app {
+
+class Arguments {
+
+public:
+       Arguments();
+       ~Arguments() { }
+
+public:
+       void Read(int argc, char **argv);
+
+       const std::vector<char *> &Infiles() const { return infiles; }
+
+       bool OutfileSet() const { return outfile; }
+       const char *OutfilePath() const { return outfile; }
+
+private:
+       std::vector<char *> infiles;
+       const char *outfile;
+
+};
+
+}
+
+#endif /* APP_ARGUMENTS_H_ */
index 2ac04233370a496f7cf469ceb5963a9f33578a25..97706269dcedf978f7ee12d9a90f79763ad6f67c 100644 (file)
@@ -6,6 +6,7 @@
  */
 
 #include "app/Application.h"
+#include "app/Arguments.h"
 #include "app/Input.h"
 #include "battle/BattleState.h"
 #include "battle/Hero.h"
 #include <ctime>
 #include <exception>
 #include <iostream>
+#include <string>
 #include <SDL.h>
 #include <SDL_image.h>
 
 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<char *>::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"));