-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsongraph_parse.cpp
More file actions
70 lines (56 loc) · 2.16 KB
/
Copy pathjsongraph_parse.cpp
File metadata and controls
70 lines (56 loc) · 2.16 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
#include "jsongraph_parse.hpp"
#include <cstdio>
#include <rapidjson/document.h>
#include <string>
namespace jsongraph {
auto parse_node = [](const char* name, const auto& val) -> graph_node {
std::vector<graph_node> value;
for (const auto& member : val) {
graph_node inner;
inner.key = member.name.GetString();
switch (member.value.GetType())
{
case rapidjson::Type::kNullType: break;
case rapidjson::Type::kFalseType: inner.value = false; break;
case rapidjson::Type::kTrueType: inner.value = true; break;
case rapidjson::Type::kObjectType: break;
case rapidjson::Type::kArrayType: break;
case rapidjson::Type::kStringType: inner.value = member.value.GetString(); break;
case rapidjson::Type::kNumberType: inner.value = member.value.GetDouble(); break; // TODO int handling
default: break; // throw here?
}
value.emplace_back(inner);
}
return {name, {}, IM_COL32_WHITE, value};
};
auto parse_edge = [](const auto& val) -> graph_edge {
graph_edge e;
e.output_name = val[0].GetString();
e.output_key = val[1].GetString();
e.input_name = val[2].GetString();
e.input_key = val[3].GetString();
return e;
};
bool parse_file(const char* path, std::vector<graph_node>& nodes, std::vector<graph_edge>& edges) {
// todo buffer this
FILE* fp = std::fopen(path, "rb");
if (!fp) return false;
int c; std::string contents;
while ((c = std::fgetc(fp)) != EOF)
contents += c;
if (std::ferror(fp))
return false; // is not closing here catastrophic?
std::fclose(fp);
rapidjson::Document doc;
doc.Parse(contents.c_str());
for (const auto& m : doc.GetObject()) {
const char* name = m.name.GetString();
if (name[0] != '$') // This throws out all special nodes like $edges or $comment
nodes.emplace_back(parse_node(name, m.value.GetObject()));
else if (stricmp(name, "$edges") == 0)
for (const auto& edge_val : m.value.GetArray())
edges.emplace_back(parse_edge(edge_val.GetArray()));
}
return true;
}
}