aboutsummaryrefslogtreecommitdiff
path: root/src/main/input-output
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/input-output')
-rw-r--r--src/main/input-output/deal_parser.cpp6
-rw-r--r--src/main/input-output/json_helper.cpp35
-rw-r--r--src/main/input-output/json_helper.h19
3 files changed, 57 insertions, 3 deletions
diff --git a/src/main/input-output/deal_parser.cpp b/src/main/input-output/deal_parser.cpp
index c876e91..8637649 100644
--- a/src/main/input-output/deal_parser.cpp
+++ b/src/main/input-output/deal_parser.cpp
@@ -4,7 +4,7 @@
#include "deal_parser.h"
#include "../game/game_state.h"
-#include "../util/util.h"
+#include "json_helper.h"
using namespace std;
using namespace rapidjson;
@@ -35,7 +35,7 @@ void deal_parser::parse_tableau_piles(game_state &gs, const rapidjson::Document&
assert(json_tab_piles.IsArray());
if (gs.tableau_piles.size() != gs.rules.tableau_pile_count) {
- util::json_parse_err("Incorrect number of tableau piles");
+ json_helper::json_parse_err("Incorrect number of tableau piles");
}
for (auto p = std::make_pair(begin(json_tab_piles.GetArray()), begin(gs.tableau_piles));
@@ -67,7 +67,7 @@ void deal_parser::parse_cells(game_state &gs, const Document& doc) {
if (json_cell_arr.Empty()) return;
else if (gs.rules.cells != json_cell_arr.Size()) {
- util::json_parse_err("Incorrect number of cells");
+ json_helper::json_parse_err("Incorrect number of cells");
}
for (auto p = std::make_pair(begin(json_cell_arr), begin(gs.cells));
diff --git a/src/main/input-output/json_helper.cpp b/src/main/input-output/json_helper.cpp
new file mode 100644
index 0000000..89f4564
--- /dev/null
+++ b/src/main/input-output/json_helper.cpp
@@ -0,0 +1,35 @@
+//
+// Created by thecharlesblake on 11/13/17.
+//
+
+#include <fstream>
+#include <sstream>
+
+#include "json_helper.h"
+
+using namespace std;
+using namespace rapidjson;
+
+Document json_helper::get_file_json(const string& filename) {
+ // Reads the file into a string
+ std::ifstream ifstr(filename);
+ std::stringstream buf;
+ buf << ifstr.rdbuf();
+
+ if (ifstr.fail()) {
+ throw runtime_error("could not read file " + filename);
+ } else {
+ Document d;
+
+ d.Parse(buf.str().c_str());
+ if (d.HasParseError()) {
+ throw runtime_error(filename + " not valid json");
+ } else {
+ return d;
+ }
+ }
+}
+
+void json_helper::json_parse_err(const string& msg) {
+ throw runtime_error("Error in JSON doc: " + msg);
+}
diff --git a/src/main/input-output/json_helper.h b/src/main/input-output/json_helper.h
new file mode 100644
index 0000000..5ab24bc
--- /dev/null
+++ b/src/main/input-output/json_helper.h
@@ -0,0 +1,19 @@
+//
+// Created by thecharlesblake on 11/13/17.
+//
+
+#ifndef SOLVITAIRE_UTIL_H
+#define SOLVITAIRE_UTIL_H
+
+#include <string>
+
+#include <rapidjson/schema.h>
+
+class json_helper {
+public:
+ static rapidjson::Document get_file_json(const std::string&);
+ static void json_parse_err(const std::string& msg);
+};
+
+
+#endif //SOLVITAIRE_UTIL_H