From bba8775ce7c63783303240470d4a4b8404c9498b Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Sat, 11 Nov 2017 11:36:27 +0000 Subject: Add src file directory structure --- src/main/input-output/command_line_helper.cpp | 146 ++++++++++++++++++++++++++ src/main/input-output/command_line_helper.h | 36 +++++++ src/main/input-output/deal_parser.cpp | 96 +++++++++++++++++ src/main/input-output/deal_parser.h | 27 +++++ src/main/input-output/log_helper.h | 19 ++++ 5 files changed, 324 insertions(+) create mode 100644 src/main/input-output/command_line_helper.cpp create mode 100644 src/main/input-output/command_line_helper.h create mode 100644 src/main/input-output/deal_parser.cpp create mode 100644 src/main/input-output/deal_parser.h create mode 100644 src/main/input-output/log_helper.h (limited to 'src/main/input-output') diff --git a/src/main/input-output/command_line_helper.cpp b/src/main/input-output/command_line_helper.cpp new file mode 100644 index 0000000..b31761a --- /dev/null +++ b/src/main/input-output/command_line_helper.cpp @@ -0,0 +1,146 @@ +// +// Created by thecharlesblake on 10/16/17. +// + +#include +#include +#include + +#include + +#include "command_line_helper.h" +#include "game/sol_rules.h" + +using namespace std; + +namespace po = boost::program_options; + +command_line_helper::command_line_helper() + : main_options("options"), random_deal(-1) { + + main_options.add_options() + ("help", "produce help message") + ("type", po::value(), + "specify the type of the solitaire game to be solved (REQUIRED)") + ("random", po::value(), "create and solve a random solitaire " + "deal based on a seed"); + + po::options_description hidden_options("Hidden options"); + hidden_options.add_options() + ("input-files", po::value>(), + "the file containing the deal to be solved"); + + cmdline_options.add(main_options).add(hidden_options); + + p.add("input-files", -1); +} + +bool command_line_helper::parse(int argc, const char* argv[]) { + po::variables_map vm; + try { + store(po::command_line_parser(argc, argv).options(cmdline_options) + .positional(p).run(), vm); + } catch(po::error& e) { + cerr << "Error: " << e.what() << "\n"; + return false; + } + + notify(vm); + + help = vm.count("help"); + + if (vm.count("input-files")) { + input_files = vm["input-files"].as>(); + } + + if (vm.count("type")) { + solitaire_type = vm["type"].as(); + } + + if (vm.count("random")) { + random_deal = vm["random"].as(); + } + + // Handle logic error scenarios + return assess_errors(); +} + +bool command_line_helper::assess_errors() { + if (help) { + print_help(); + return false; + } + + if (solitaire_type.empty()) { + print_no_sol_type_error(); + return false; + } + + bool valid_sol_type = assess_sol_type(); + if (!valid_sol_type) return false; + + if (random_deal != -1 && !input_files.empty()) { + print_rand_plus_input_err(); + return false; + } + + if (input_files.empty() && random_deal == -1) { + print_no_input_error(); + return false; + } + return true; +} + +bool command_line_helper::assess_sol_type() { + if (find(sol_rules::valid_sol_strs.begin(), + sol_rules::valid_sol_strs.end(), + solitaire_type) != sol_rules::valid_sol_strs.end()) { + return true; + } else { + cerr << "Error: Solitaire type is not valid: " << solitaire_type + << "\nValid solitaire types are: "; + copy(begin(sol_rules::valid_sol_strs), end(sol_rules::valid_sol_strs), + ostream_iterator(cerr, ", ")); + cerr << "\n"; + print_help(); + return false; + } +} + +void command_line_helper::print_help() { + cerr << "Usage: solvitaire [options] input-file1 input-file2 ...\n" + << main_options << "\n"; +} + +void command_line_helper::print_no_input_error() { + cerr << "Error: User must supply input file(s) or the '--random' " + "option\n"; + print_help(); +} + +void command_line_helper::print_no_sol_type_error() { + cerr << "Error: User must supply a solitaire type\n"; + print_help(); +} + +void command_line_helper::print_rand_plus_input_err() { + cerr << "Error: User must supply input file(s) or the '--random' option, " + "not both\n"; + print_help(); +} + +const vector command_line_helper::get_input_files() { + return input_files; +} + +const string command_line_helper::get_solitaire_type() { + return solitaire_type; +} + +int command_line_helper::get_random_deal() { + return random_deal; +} + +bool command_line_helper::get_help() { + return help; +} diff --git a/src/main/input-output/command_line_helper.h b/src/main/input-output/command_line_helper.h new file mode 100644 index 0000000..8cc67cb --- /dev/null +++ b/src/main/input-output/command_line_helper.h @@ -0,0 +1,36 @@ +// +// Created by thecharlesblake on 10/16/17. +// + +#ifndef SOLVITAIRE_COMMAND_LINE_HELPER_H +#define SOLVITAIRE_COMMAND_LINE_HELPER_H + +#include + +class command_line_helper { +public: + command_line_helper(); + bool parse(int argc, const char* argv[]); + const std::vector get_input_files(); + const std::string get_solitaire_type(); + int get_random_deal(); + bool get_help(); +private: + bool assess_errors(); + bool assess_sol_type(); + void print_help(); + void print_no_input_error(); + void print_no_sol_type_error(); + void print_rand_plus_input_err(); + + boost::program_options::options_description cmdline_options; + boost::program_options::options_description main_options; + boost::program_options::positional_options_description p; + + std::vector input_files; + std::string solitaire_type; + int random_deal; + bool help; +}; + +#endif //SOLVITAIRE_COMMAND_LINE_HELPER_H diff --git a/src/main/input-output/deal_parser.cpp b/src/main/input-output/deal_parser.cpp new file mode 100644 index 0000000..8a9cef1 --- /dev/null +++ b/src/main/input-output/deal_parser.cpp @@ -0,0 +1,96 @@ +// +// Created by thecharlesblake on 10/21/17. +// + +#include +#include +#include +#include + +#include +#include +#include +#include + +#include "deal_parser.h" + +using namespace std; +using namespace rapidjson; +using namespace boost; + +void deal_parser::parse(Document& doc, const std::string filename) { + const optional in_json = read_file(filename); + + if (!in_json) { + throw runtime_error("could not read file " + filename); + } + + Document sd; + gen_schema_doc(sd); + SchemaDocument schema(sd); + SchemaValidator validator(schema); + + if(!to_json(doc, *in_json)) { + throw runtime_error(filename + " not valid json"); + } + + if (!doc.Accept(validator)) { + throw runtime_error(schema_err_str(validator)); + } +} + +const optional deal_parser::read_file(const string filename) { + // Reads the file into a string + std::ifstream ifstr(filename); + std::stringstream buf; + buf << ifstr.rdbuf(); + + if (ifstr.fail()) { + return none; + } + + return buf.str(); +} + +void deal_parser::gen_schema_doc(Document& d) { + const char* schema_json = "{\"$schema\":\"http://json-schema.org/draft-04/sc" + "hema#\",\"definitions\":{\"card\":{\"type\":\"string\",\"pattern" + "\":\"^(?i)([A|[1-9]|1[0-3]|J|Q|K])([C|D|H|S])$\"}},\"type\":\"" + "object\",\"properties\":{\"tableau piles\":{\"type\":\"array\"" + ",\"items\":{\"type\":\"array\",\"items\":{\"$ref\":\"#/definit" + "ions/card\"}}},\"hole card\":{\"$ref\":\"#/definitions/card\"}}" + ",\"additionalProperties\":false,\"anyOf\":[{\"required\":[\"tableau" + " piles\"]}]}"; + + to_json(d, schema_json); +} + +bool deal_parser::to_json(rapidjson::Document& d, const char* in) { + d.Parse(in); + return !d.HasParseError(); +} + +bool deal_parser::to_json(rapidjson::Document& d, const string in) { + return to_json(d, in.c_str()); +} + +const string deal_parser::schema_err_str(const SchemaValidator& validator) { + // Input JSON is invalid according to the schema + // Output diagnostic information + string ret = "input JSON failed to match the required schema.\n"; + + StringBuffer sb; + validator.GetInvalidSchemaPointer().StringifyUriFragment(sb); + ret += "Invalid schema keyword: "; + ret += validator.GetInvalidSchemaKeyword(); + ret += "\n"; + + sb.Clear(); + validator.GetInvalidDocumentPointer().StringifyUriFragment(sb); + + ret += "Invalid document: "; + ret += sb.GetString(); + ret += "\n"; + + return ret; +} \ No newline at end of file diff --git a/src/main/input-output/deal_parser.h b/src/main/input-output/deal_parser.h new file mode 100644 index 0000000..60eb8fb --- /dev/null +++ b/src/main/input-output/deal_parser.h @@ -0,0 +1,27 @@ +// +// Created by thecharlesblake on 10/21/17. +// + +#ifndef SOLVITAIRE_DEAL_PARSER_H +#define SOLVITAIRE_DEAL_PARSER_H + +#include + +#include +#include +#include + +#include "game/card.h" + +class deal_parser { +public: + static void parse(rapidjson::Document&, const std::string); +private: + static const boost::optional read_file(const std::string); + static void gen_schema_doc(rapidjson::Document&); + static bool to_json(rapidjson::Document&, const char*); + static bool to_json(rapidjson::Document&, const std::string); + static const std::string schema_err_str(const rapidjson::SchemaValidator&); +}; + +#endif //SOLVITAIRE_DEAL_PARSER_H diff --git a/src/main/input-output/log_helper.h b/src/main/input-output/log_helper.h new file mode 100644 index 0000000..889a5ae --- /dev/null +++ b/src/main/input-output/log_helper.h @@ -0,0 +1,19 @@ +// +// Created by thecharlesblake on 10/16/17. +// + +#ifndef SOLVITAIRE_LOG_HELPER_H +#define SOLVITAIRE_LOG_HELPER_H + +#include + +#if defined NDEBUG + #define LOG_DEBUG +#else + #define LOG_DEBUG(msg) clog << "[debug]\n" << msg +#endif +#define LOG_INFO(msg) clog << "[info]\n" << msg +#define LOG_WARNING(msg) clog << "[warning]\n" << msg +#define LOG_ERROR(msg) cerr << "[error]\n" << msg + +#endif //SOLVITAIRE_LOG_HELPER_H -- cgit v1.2.1 From 34cc6c6d68728d21d49b639f448030a8c2df58c4 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Sat, 11 Nov 2017 12:03:35 +0000 Subject: Refactor command line helper --- src/main/input-output/command_line_helper.cpp | 15 +++++++++++++-- src/main/input-output/command_line_helper.h | 1 + 2 files changed, 14 insertions(+), 2 deletions(-) (limited to 'src/main/input-output') diff --git a/src/main/input-output/command_line_helper.cpp b/src/main/input-output/command_line_helper.cpp index b31761a..2228d51 100644 --- a/src/main/input-output/command_line_helper.cpp +++ b/src/main/input-output/command_line_helper.cpp @@ -16,7 +16,7 @@ using namespace std; namespace po = boost::program_options; command_line_helper::command_line_helper() - : main_options("options"), random_deal(-1) { + : main_options("options") { main_options.add_options() ("help", "produce help message") @@ -35,8 +35,10 @@ command_line_helper::command_line_helper() p.add("input-files", -1); } +// Returns true if we can continue solving the supplied solitaire(s) bool command_line_helper::parse(int argc, const char* argv[]) { po::variables_map vm; + // Attempts to parse cmdln options. Catches basic errors try { store(po::command_line_parser(argc, argv).options(cmdline_options) .positional(p).run(), vm); @@ -45,9 +47,12 @@ bool command_line_helper::parse(int argc, const char* argv[]) { return false; } + // Updates the variables map which the user's options are stored in notify(vm); - help = vm.count("help"); + // Converts the variables map into flags and values + + help = (vm.count("help") != 0); if (vm.count("input-files")) { input_files = vm["input-files"].as>(); @@ -59,6 +64,8 @@ bool command_line_helper::parse(int argc, const char* argv[]) { if (vm.count("random")) { random_deal = vm["random"].as(); + } else { + random_deal = -1; } // Handle logic error scenarios @@ -79,6 +86,8 @@ bool command_line_helper::assess_errors() { bool valid_sol_type = assess_sol_type(); if (!valid_sol_type) return false; + // The user must either supply input files or a random seed + if (random_deal != -1 && !input_files.empty()) { print_rand_plus_input_err(); return false; @@ -91,11 +100,13 @@ bool command_line_helper::assess_errors() { return true; } +// Checks if the supplied solitaire type is in the list of valid solitaires bool command_line_helper::assess_sol_type() { if (find(sol_rules::valid_sol_strs.begin(), sol_rules::valid_sol_strs.end(), solitaire_type) != sol_rules::valid_sol_strs.end()) { return true; + } else { cerr << "Error: Solitaire type is not valid: " << solitaire_type << "\nValid solitaire types are: "; diff --git a/src/main/input-output/command_line_helper.h b/src/main/input-output/command_line_helper.h index 8cc67cb..9cb772b 100644 --- a/src/main/input-output/command_line_helper.h +++ b/src/main/input-output/command_line_helper.h @@ -15,6 +15,7 @@ public: const std::string get_solitaire_type(); int get_random_deal(); bool get_help(); + private: bool assess_errors(); bool assess_sol_type(); -- cgit v1.2.1 From 14f534452ac99913931c0513532dfbad2e277493 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Sat, 11 Nov 2017 12:20:31 +0000 Subject: Update logging --- src/main/input-output/command_line_helper.cpp | 16 ++++++++-------- src/main/input-output/log_helper.h | 8 ++++---- 2 files changed, 12 insertions(+), 12 deletions(-) (limited to 'src/main/input-output') diff --git a/src/main/input-output/command_line_helper.cpp b/src/main/input-output/command_line_helper.cpp index 2228d51..b66cc39 100644 --- a/src/main/input-output/command_line_helper.cpp +++ b/src/main/input-output/command_line_helper.cpp @@ -4,12 +4,12 @@ #include #include -#include #include #include "command_line_helper.h" #include "game/sol_rules.h" +#include "input-output/log_helper.h" using namespace std; @@ -43,7 +43,7 @@ bool command_line_helper::parse(int argc, const char* argv[]) { store(po::command_line_parser(argc, argv).options(cmdline_options) .positional(p).run(), vm); } catch(po::error& e) { - cerr << "Error: " << e.what() << "\n"; + LOG_ERROR << "Error: " << e.what() << "\n"; return false; } @@ -108,34 +108,34 @@ bool command_line_helper::assess_sol_type() { return true; } else { - cerr << "Error: Solitaire type is not valid: " << solitaire_type + LOG_ERROR << "Error: Solitaire type is not valid: " << solitaire_type << "\nValid solitaire types are: "; copy(begin(sol_rules::valid_sol_strs), end(sol_rules::valid_sol_strs), ostream_iterator(cerr, ", ")); - cerr << "\n"; + LOG_ERROR << "\n"; print_help(); return false; } } void command_line_helper::print_help() { - cerr << "Usage: solvitaire [options] input-file1 input-file2 ...\n" + LOG_ERROR << "Usage: solvitaire [options] input-file1 input-file2 ...\n" << main_options << "\n"; } void command_line_helper::print_no_input_error() { - cerr << "Error: User must supply input file(s) or the '--random' " + LOG_ERROR << "Error: User must supply input file(s) or the '--random' " "option\n"; print_help(); } void command_line_helper::print_no_sol_type_error() { - cerr << "Error: User must supply a solitaire type\n"; + LOG_ERROR << "Error: User must supply a solitaire type\n"; print_help(); } void command_line_helper::print_rand_plus_input_err() { - cerr << "Error: User must supply input file(s) or the '--random' option, " + LOG_ERROR << "Error: User must supply input file(s) or the '--random' option, " "not both\n"; print_help(); } diff --git a/src/main/input-output/log_helper.h b/src/main/input-output/log_helper.h index 889a5ae..441381f 100644 --- a/src/main/input-output/log_helper.h +++ b/src/main/input-output/log_helper.h @@ -10,10 +10,10 @@ #if defined NDEBUG #define LOG_DEBUG #else - #define LOG_DEBUG(msg) clog << "[debug]\n" << msg + #define LOG_DEBUG clog << "[debug]\n" #endif -#define LOG_INFO(msg) clog << "[info]\n" << msg -#define LOG_WARNING(msg) clog << "[warning]\n" << msg -#define LOG_ERROR(msg) cerr << "[error]\n" << msg +#define LOG_INFO clog << "[info] " +#define LOG_WARNING clog << "[warning] " +#define LOG_ERROR cerr << "[ERROR] " #endif //SOLVITAIRE_LOG_HELPER_H -- cgit v1.2.1 From 2b6b5d1046c69a43656cc8990426176ad20ed597 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Sun, 12 Nov 2017 09:51:32 +0000 Subject: Make log macros proper functions --- src/main/input-output/log_helper.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'src/main/input-output') diff --git a/src/main/input-output/log_helper.h b/src/main/input-output/log_helper.h index 441381f..71cc5f4 100644 --- a/src/main/input-output/log_helper.h +++ b/src/main/input-output/log_helper.h @@ -8,12 +8,12 @@ #include #if defined NDEBUG - #define LOG_DEBUG + #define LOG_DEBUG(msg) #else - #define LOG_DEBUG clog << "[debug]\n" + #define LOG_DEBUG(msg) clog << "[debug]\n" << msg << "\n" #endif -#define LOG_INFO clog << "[info] " -#define LOG_WARNING clog << "[warning] " -#define LOG_ERROR cerr << "[ERROR] " +#define LOG_INFO(msg) clog << "[info] " << msg << "\n" +#define LOG_WARNING clog(msg) << "[warning] " << msg << "\n" +#define LOG_ERROR(msg) cerr << "[ERROR] " << msg << "\n" #endif //SOLVITAIRE_LOG_HELPER_H -- cgit v1.2.1 From 6298ad1261c1f398b5f191600ec1efa5cb137a95 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Sun, 12 Nov 2017 09:53:08 +0000 Subject: Use new logging in command line helper --- src/main/input-output/command_line_helper.cpp | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) (limited to 'src/main/input-output') diff --git a/src/main/input-output/command_line_helper.cpp b/src/main/input-output/command_line_helper.cpp index b66cc39..e61c546 100644 --- a/src/main/input-output/command_line_helper.cpp +++ b/src/main/input-output/command_line_helper.cpp @@ -43,7 +43,7 @@ bool command_line_helper::parse(int argc, const char* argv[]) { store(po::command_line_parser(argc, argv).options(cmdline_options) .positional(p).run(), vm); } catch(po::error& e) { - LOG_ERROR << "Error: " << e.what() << "\n"; + LOG_ERROR ("Error: " << e.what()); return false; } @@ -108,35 +108,35 @@ bool command_line_helper::assess_sol_type() { return true; } else { - LOG_ERROR << "Error: Solitaire type is not valid: " << solitaire_type - << "\nValid solitaire types are: "; + LOG_ERROR ("Error: Solitaire type is not valid: " << solitaire_type + << "\nValid solitaire types are: "); copy(begin(sol_rules::valid_sol_strs), end(sol_rules::valid_sol_strs), ostream_iterator(cerr, ", ")); - LOG_ERROR << "\n"; + cerr << "\n"; print_help(); return false; } } void command_line_helper::print_help() { - LOG_ERROR << "Usage: solvitaire [options] input-file1 input-file2 ...\n" - << main_options << "\n"; + LOG_ERROR ("Usage: solvitaire [options] input-file1 input-file2 ...\n" + << main_options); } void command_line_helper::print_no_input_error() { - LOG_ERROR << "Error: User must supply input file(s) or the '--random' " - "option\n"; + LOG_ERROR ("Error: User must supply input file(s) or the '--random' " + "option"); print_help(); } void command_line_helper::print_no_sol_type_error() { - LOG_ERROR << "Error: User must supply a solitaire type\n"; + LOG_ERROR ("Error: User must supply a solitaire type"); print_help(); } void command_line_helper::print_rand_plus_input_err() { - LOG_ERROR << "Error: User must supply input file(s) or the '--random' option, " - "not both\n"; + LOG_ERROR ("Error: User must supply input file(s) or the '--random' option, " + "not both"); print_help(); } -- cgit v1.2.1 From dc2e84e3ca0ceda7753f1aaec1e648dbde24e9e3 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Sun, 12 Nov 2017 10:01:18 +0000 Subject: Tidy up headers for consistency --- src/main/input-output/command_line_helper.h | 1 + 1 file changed, 1 insertion(+) (limited to 'src/main/input-output') diff --git a/src/main/input-output/command_line_helper.h b/src/main/input-output/command_line_helper.h index 9cb772b..bb2f698 100644 --- a/src/main/input-output/command_line_helper.h +++ b/src/main/input-output/command_line_helper.h @@ -10,6 +10,7 @@ class command_line_helper { public: command_line_helper(); + bool parse(int argc, const char* argv[]); const std::vector get_input_files(); const std::string get_solitaire_type(); -- cgit v1.2.1