aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitmodules3
-rw-r--r--CMakeLists.txt2
-rw-r--r--Deserializer.cpp16
-rw-r--r--Deserializer.h8
-rw-r--r--XMLParser.cpp47
-rw-r--r--docs/class-diag.puml2
m---------lib/pugixml0
-rw-r--r--main.cpp16
8 files changed, 86 insertions, 8 deletions
diff --git a/.gitmodules b/.gitmodules
index b260b07..de46813 100644
--- a/.gitmodules
+++ b/.gitmodules
@@ -6,3 +6,6 @@
path = lib/cpr
url = https://github.com/libcpr/cpr
shallow = true
+[submodule "lib/pugixml"]
+ path = lib/pugixml
+ url = https://github.com/zeux/pugixml
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 6e47625..8217d1d 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -9,6 +9,7 @@ set(CMAKE_BUILD_TYPE Debug)
# find_package(cpr REQUIRED)
add_subdirectory(lib/SDL)
add_subdirectory(lib/cpr)
+add_subdirectory(lib/pugixml)
project(main C CXX)
@@ -31,5 +32,6 @@ add_executable(main
target_link_libraries(main
SDL2
cpr
+ pugixml
)
diff --git a/Deserializer.cpp b/Deserializer.cpp
index ecadbbe..48c4da9 100644
--- a/Deserializer.cpp
+++ b/Deserializer.cpp
@@ -5,6 +5,15 @@ void Deserializer::set_target(Museum * museum) {
this->museum = museum;
}
+Deserializer::~Deserializer() {
+ this->finalize();
+}
+
+void Deserializer::finalize() {
+ // TODO: fill empty squares in canvas with white ones
+ // TODO: remove artists outside the canvas
+}
+
Museum & Deserializer::get_target() {
if (this->museum == nullptr)
throw Exception("no museum given to Deserializer");
@@ -30,3 +39,10 @@ void Deserializer::add_tile(unsigned int x, unsigned int y, TileData data) {
printf("add tile(%d,%d) data(%s)...\n", x, y, data.type.c_str());
}
+void Deserializer::add_type(std::string type, Color color, unsigned int weight) {
+ if (type.length() == 0) return;
+
+ printf("add type(%s) color(#%02x%02x%02x) weight(%d)...\n", type.c_str(),
+ color.red, color.green, color.blue, weight);
+}
+
diff --git a/Deserializer.h b/Deserializer.h
index 78b7920..950692c 100644
--- a/Deserializer.h
+++ b/Deserializer.h
@@ -10,14 +10,20 @@
class Deserializer {
public:
+ Deserializer() = default;
+ virtual ~Deserializer();
+
+public:
void set_target(Museum * m);
- void add_type(std::string, Color, unsigned int);
+ void add_type(std::string type, Color color, unsigned int weight);
void set_canvas(CanvasData);
void add_tile(unsigned int x, unsigned int y, TileData);
void add_artist(ArtistData);
+ void finalize();
+
private:
Museum & get_target();
Museum * museum = nullptr;
diff --git a/XMLParser.cpp b/XMLParser.cpp
index 779f51f..8dda552 100644
--- a/XMLParser.cpp
+++ b/XMLParser.cpp
@@ -1,6 +1,9 @@
#include <cstdlib>
+#include <pugixml.hpp>
+
#include "XMLParser.h"
+#include "Exception.h"
#include "Parser.h"
using namespace std;
@@ -24,6 +27,48 @@ unsigned int XMLParser::heuristic(File & f) {
}
void XMLParser::parse(File & f, Deserializer & d) {
- printf("%s\n", __PRETTY_FUNCTION__);
+ using namespace pugi;
+
+ const string content = f.read();
+ xml_document doc;
+ xml_parse_result result = doc.load_string(content.c_str());
+ if (!result)
+ throw Exception("parsing XML failed");
+
+ xml_node canvas = doc.child("canvas");
+ if (!canvas)
+ throw Exception("missing <canvas>");
+ d.set_canvas({
+ .rows = canvas.attribute("rows").as_uint(),
+ .columns = canvas.attribute("cols").as_uint(),
+ });
+
+ xml_node node_types = canvas.child("nodeTypes");
+ if (!node_types)
+ throw Exception("missing <nodeTypes>");
+ for (xml_node node_type : node_types) {
+ d.add_type(
+ node_type.attribute("tag").as_string(),
+ {
+ .red = node_type.attribute("red").as_uint(),
+ .green = node_type.attribute("green").as_uint(),
+ .blue = node_type.attribute("blue").as_uint(),
+ },
+ node_type.attribute("weight").as_int()
+ );
+ }
+
+ xml_node nodes = canvas.child("nodes");
+ if (!nodes)
+ throw Exception("missing <nodes>");
+ for (xml_node node : nodes) {
+ d.add_tile(
+ node.attribute("x").as_uint(),
+ node.attribute("y").as_uint(),
+ {
+ .type = node.name(),
+ }
+ );
+ }
}
diff --git a/docs/class-diag.puml b/docs/class-diag.puml
index 8d4fa68..697e32c 100644
--- a/docs/class-diag.puml
+++ b/docs/class-diag.puml
@@ -9,7 +9,7 @@
!include style.ipuml
-!include hide-groups.ipuml
+' !include hide-groups.ipuml
' !include hide-details.ipuml
class main as "main()"
diff --git a/lib/pugixml b/lib/pugixml
new file mode 160000
+Subproject 3b17184379fcaaeb7f1fbe08018b7fedf2640b3
diff --git a/main.cpp b/main.cpp
index b258a49..322a691 100644
--- a/main.cpp
+++ b/main.cpp
@@ -6,10 +6,8 @@
#include "Deserializer.h"
#include "Parser.h"
-int main(int argc, char** argv) {
- Museum m {};
+void load_museum(Museum & m, int argc, char** argv) {
Deserializer d {};
-
d.set_target(&m);
for (int i = 1; i < argc; i++) {
@@ -20,19 +18,27 @@ int main(int argc, char** argv) {
f = &FileReader::open(url);
} catch (Exception & e) {
printf("File open error: %s\n", e.what());
- return EXIT_FAILURE;
+ exit(EXIT_FAILURE);
}
try {
Parser::parse(*f, d);
} catch (Exception & e) {
printf("Parser error: %s (%s)\n", e.what(), url);
- return EXIT_FAILURE;
+ exit(EXIT_FAILURE);
}
f->close();
}
+ d.finalize();
+}
+
+int main(int argc, char** argv) {
+ Museum m {};
+
+ load_museum(m, argc, argv);
+
return EXIT_SUCCESS;
}