-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_loader.cpp
More file actions
132 lines (115 loc) · 4.34 KB
/
data_loader.cpp
File metadata and controls
132 lines (115 loc) · 4.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include "data_loader.hpp"
#include <iostream>
#include <string>
#include <boost/filesystem.hpp>
#include <rapidjson/document.h>
#include <rapidjson/error/en.h>
#include <valijson/utils/file_utils.hpp>
#include <valijson/schema_parser.hpp>
#include <valijson/adapters/rapidjson_adapter.hpp>
#include <valijson/validator.hpp>
#include "invalid_json.hpp"
#include "jeopardy_exception.hpp"
using namespace std;
using namespace boost::filesystem;
using namespace rapidjson;
using namespace valijson;
using namespace valijson::adapters;
static Schema initialize_schema(path schema_file)
{
if (!is_regular_file(schema_file))
throw jeopardy_exception("'" + schema_file.string() + "' is not a regular file");
Document schema_doc;
string json;
valijson::utils::loadFile(schema_file.string(), json);
schema_doc.Parse(json.c_str());
if (schema_doc.HasParseError())
throw invalid_json(valijson::ValidationResults::Error({schema_file.string()}, GetParseError_En(schema_doc.GetParseError())));
Schema schema;
SchemaParser parser;
RapidJsonAdapter schemaAdapter(schema_doc);
parser.populateSchema(schemaAdapter, schema);
return schema;
}
static jeopardy_round load_round(const path &round_directory, Validator &validator)
{
if (!is_directory(round_directory))
throw jeopardy_exception("'" + round_directory.string() + "' is not a directory");
Document d = data_loader::load_validated_document(round_directory / "round.json", validator);
return jeopardy_round(jeopardy_round(round_directory.filename().string(), d, round_directory));
}
Document data_loader::load_validated_document(const path &json_file, Validator &validator)
{
if (!is_regular_file(json_file))
throw jeopardy_exception("'" + json_file.string() + "' is not a file");
Document d;
string json;
valijson::utils::loadFile(json_file.string(), json);
d.Parse(json.c_str());
if (d.HasParseError())
throw invalid_json("Error while parsing '" + json_file.string() + "': " + GetParseError_En(d.GetParseError()));
adapters::RapidJsonAdapter targetAdapter(d);
ValidationResults results;
if (!validator.validate(targetAdapter, &results))
throw invalid_json(results);
return d;
}
list<jeopardy_round> data_loader::load_rounds()
{
path rounds_dir = "rounds";
if (!is_directory(rounds_dir))
throw jeopardy_exception("'" + rounds_dir.string() + "' is not a directory");
Validator validator(initialize_schema(path("json-schema") / "files" / "round.json"));
list<jeopardy_round> rounds;
for (directory_iterator it(rounds_dir), end;it != end;it++)
{
path current_directory = *it;
if (!is_directory(current_directory))
{
cerr << current_directory << " is not a directory" << endl;
continue;
}
try
{
rounds.push_back(::load_round(current_directory, validator));
}
catch (jeopardy_exception &e)
{
cerr << e.what() << endl;
}
}
return rounds;
}
jeopardy_round data_loader::load_round(const std::string &name)
{
path rounds_dir = "rounds";
if (!is_directory(rounds_dir))
throw jeopardy_exception("'" + rounds_dir.string() + "' is not a directory");
Validator validator(initialize_schema(path("json-schema") / "files" / "round.json"));
path round_directory = rounds_dir / name;
return ::load_round(round_directory, validator);
}
list<pair<string, device_type>> data_loader::load_default_devices()
{
list<pair<string, device_type>> devices;
path device_file = "default_devices.json";
if (!is_regular_file(device_file))
return devices;
Validator validator(initialize_schema(path("json-schema") / "files" / "default_devices.json"));
Document d = load_validated_document(device_file, validator);
for (unsigned int i = 0;i < d.Capacity();i++)
{
auto &device = d[i];
string path = device["device"].GetString();
string type_str = device["type"].GetString();
device_type type;
if (type_str == "serial")
type = device_type::SERIAL;
else if (type_str == "keyboard")
type = device_type::KEYBOARD;
else
throw invalid_json("Invalid type '" + type_str + "' in default devices");
devices.emplace_back(path, type);
}
return devices;
}