From fa40f6facc5c500cae8bf890e66a7bf5d490387e Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Wed, 3 Jan 2018 12:39:06 +0000 Subject: Rename util to json_helper --- CMakeLists.txt | 2 +- src/main/game/sol_rules.cpp | 32 ++++++++++++++++---------------- src/main/input-output/deal_parser.cpp | 6 +++--- src/main/input-output/json_helper.cpp | 35 +++++++++++++++++++++++++++++++++++ src/main/input-output/json_helper.h | 19 +++++++++++++++++++ src/main/main.cpp | 4 ++-- src/main/util/util.cpp | 35 ----------------------------------- src/main/util/util.h | 19 ------------------- src/test/test_helper.cpp | 4 ++-- 9 files changed, 78 insertions(+), 78 deletions(-) create mode 100644 src/main/input-output/json_helper.cpp create mode 100644 src/main/input-output/json_helper.h delete mode 100644 src/main/util/util.cpp delete mode 100644 src/main/util/util.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 9afe98b..93420bc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,7 +16,7 @@ set(sources src/main/input-output/command_line_helper.cpp src/main/game/card.cpp src/main/input-output/deal_parser.h src/main/input-output/deal_parser.cpp src/main/solver/solver.cpp src/main/solver/solver.h src/main/game/game_state.cpp src/main/game/game_state.h src/main/game/sol_rules.h src/main/game/sol_rules.cpp - src/main/game/pile.cpp src/main/game/pile.h src/main/util/util.cpp src/main/util/util.h src/main/input-output/sol_preset_types.h src/main/input-output/sol_preset_types.cpp) + src/main/game/pile.cpp src/main/game/pile.h src/main/input-output/json_helper.cpp src/main/input-output/json_helper.h src/main/input-output/sol_preset_types.h src/main/input-output/sol_preset_types.cpp) set(sources_test src/test/test_helper.cpp src/test/test_helper.h src/test/spanish_patience_test.cpp diff --git a/src/main/game/sol_rules.cpp b/src/main/game/sol_rules.cpp index a1567f7..28c2af7 100644 --- a/src/main/game/sol_rules.cpp +++ b/src/main/game/sol_rules.cpp @@ -9,7 +9,7 @@ #include "rapidjson/error/en.h" #include "sol_rules.h" -#include "../util/util.h" +#include "../input-output/json_helper.h" #include "../input-output/sol_preset_types.h" using namespace std; @@ -24,7 +24,7 @@ typedef sol_rules::build_policy pol; const sol_rules sol_rules::from_file(const string rules_file) { sol_rules sr = get_default(); - Document d = util::get_file_json(rules_file); + Document d = json_helper::get_file_json(rules_file); modify_sol_rules(sr, d); @@ -59,7 +59,7 @@ sol_rules sol_rules::get_default() { void sol_rules::modify_sol_rules(sol_rules& sr, Document& d) { if (!d.IsObject()) { - util::json_parse_err("JSON doc must be object"); + json_helper::json_parse_err("JSON doc must be object"); } if (d.HasMember("tableau piles")) { @@ -69,7 +69,7 @@ void sol_rules::modify_sol_rules(sol_rules& sr, Document& d) { if (d["tableau piles"]["count"].IsInt()) { sr.tableau_pile_count = static_cast(d["tableau piles"]["count"].GetInt()); } else { - util::json_parse_err("[tableau piles][count] must be an integer"); + json_helper::json_parse_err("[tableau piles][count] must be an integer"); } } @@ -82,11 +82,11 @@ void sol_rules::modify_sol_rules(sol_rules& sr, Document& d) { } else if (bo_str == "descending") { sr.build_ord = build_order::DESCENDING; } else { - util::json_parse_err("[tableau piles][build order] is invalid"); + json_helper::json_parse_err("[tableau piles][build order] is invalid"); } } else { - util::json_parse_err("[tableau piles][build order] must be an string"); + json_helper::json_parse_err("[tableau piles][build order] must be an string"); } } @@ -103,11 +103,11 @@ void sol_rules::modify_sol_rules(sol_rules& sr, Document& d) { } else if (bp_str == "no-build") { sr.build_pol = build_policy::NO_BUILD; } else { - util::json_parse_err("[tableau piles][build policy] is invalid"); + json_helper::json_parse_err("[tableau piles][build policy] is invalid"); } } else { - util::json_parse_err("[tableau piles][build policy] must be an string"); + json_helper::json_parse_err("[tableau piles][build policy] must be an string"); } } @@ -120,16 +120,16 @@ void sol_rules::modify_sol_rules(sol_rules& sr, Document& d) { } else if (sp_str == "no-build") { sr.spaces_pol = spaces_policy::NO_BUILD; } else { - util::json_parse_err("[tableau piles][spaces policy] is invalid"); + json_helper::json_parse_err("[tableau piles][spaces policy] is invalid"); } } else { - util::json_parse_err("[tableau piles][spaces policy] must be an string"); + json_helper::json_parse_err("[tableau piles][spaces policy] must be an string"); } } } else { - util::json_parse_err("[tableau piles] must be an object"); + json_helper::json_parse_err("[tableau piles] must be an object"); } } @@ -137,7 +137,7 @@ void sol_rules::modify_sol_rules(sol_rules& sr, Document& d) { if (d["max rank"].IsInt()) { sr.max_rank = static_cast(d["max rank"].GetInt()); } else { - util::json_parse_err("[max rank] must be an integer"); + json_helper::json_parse_err("[max rank] must be an integer"); } } @@ -145,7 +145,7 @@ void sol_rules::modify_sol_rules(sol_rules& sr, Document& d) { if (d["hole"].IsBool()) { sr.hole = d["hole"].GetBool(); } else { - util::json_parse_err("[hole] must be a boolean"); + json_helper::json_parse_err("[hole] must be a boolean"); } } @@ -153,7 +153,7 @@ void sol_rules::modify_sol_rules(sol_rules& sr, Document& d) { if (d["foundations"].IsBool()) { sr.foundations = d["foundations"].GetBool(); } else { - util::json_parse_err("[foundations] must be a boolean"); + json_helper::json_parse_err("[foundations] must be a boolean"); } } @@ -161,7 +161,7 @@ void sol_rules::modify_sol_rules(sol_rules& sr, Document& d) { if (d["cells"].IsInt()) { sr.cells = static_cast(d["cells"].GetInt()); } else { - util::json_parse_err("[cells] must be an integer"); + json_helper::json_parse_err("[cells] must be an integer"); } } @@ -169,7 +169,7 @@ void sol_rules::modify_sol_rules(sol_rules& sr, Document& d) { if (d["stock size"].IsInt()) { sr.stock_size = static_cast(d["stock size"].GetInt()); } else { - util::json_parse_err("[stock size] must be an integer"); + json_helper::json_parse_err("[stock size] must be an integer"); } } } 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 +#include + +#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 + +#include + +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 diff --git a/src/main/main.cpp b/src/main/main.cpp index e07e566..342cdd0 100644 --- a/src/main/main.cpp +++ b/src/main/main.cpp @@ -5,7 +5,7 @@ #include "input-output/command_line_helper.h" #include "solver/solver.h" #include "input-output/log_helper.h" -#include "util/util.h" +#include "input-output/json_helper.h" using namespace rapidjson; @@ -72,7 +72,7 @@ void solve_input_files(const vector input_files, const sol_rules& rules) for (const string& input_file : input_files) { try { // Reads in the input file to a json doc - const Document in_doc = util::get_file_json(input_file); + const Document in_doc = json_helper::get_file_json(input_file); // Attempts to create a game state object from the json game_state gs(rules, in_doc); diff --git a/src/main/util/util.cpp b/src/main/util/util.cpp deleted file mode 100644 index 7e7de8d..0000000 --- a/src/main/util/util.cpp +++ /dev/null @@ -1,35 +0,0 @@ -// -// Created by thecharlesblake on 11/13/17. -// - -#include -#include - -#include "util.h" - -using namespace std; -using namespace rapidjson; - -Document util::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 util::json_parse_err(const string& msg) { - throw runtime_error("Error in JSON doc: " + msg); -} diff --git a/src/main/util/util.h b/src/main/util/util.h deleted file mode 100644 index de2a94d..0000000 --- a/src/main/util/util.h +++ /dev/null @@ -1,19 +0,0 @@ -// -// Created by thecharlesblake on 11/13/17. -// - -#ifndef SOLVITAIRE_UTIL_H -#define SOLVITAIRE_UTIL_H - -#include - -#include - -class util { -public: - static rapidjson::Document get_file_json(const std::string&); - static void json_parse_err(const std::string& msg); -}; - - -#endif //SOLVITAIRE_UTIL_H diff --git a/src/test/test_helper.cpp b/src/test/test_helper.cpp index 9c258a3..59e7584 100644 --- a/src/test/test_helper.cpp +++ b/src/test/test_helper.cpp @@ -7,13 +7,13 @@ #include "test_helper.h" #include "../main/game/game_state.h" #include "../main/solver/solver.h" -#include "../main/util/util.h" +#include "../main/input-output/json_helper.h" using namespace rapidjson; using namespace std; bool test_helper::is_solvable(const std::string& input_file, const std::string& preset_type) { - const Document in_doc = util::get_file_json(input_file); + const Document in_doc = json_helper::get_file_json(input_file); const sol_rules rules = sol_rules::from_preset(preset_type); game_state gs(rules, in_doc); -- cgit v1.2.1