forked from jbeder/yaml-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.cpp
61 lines (48 loc) · 1.43 KB
/
parse.cpp
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
#include <fstream>
#include <iostream>
#include <vector>
#include "yaml-cpp/eventhandler.h"
#include "yaml-cpp/yaml.h" // IWYU pragma: keep
struct Params {
bool hasFile;
std::string fileName;
};
Params ParseArgs(int argc, char** argv) {
Params p;
std::vector<std::string> args(argv + 1, argv + argc);
return p;
}
class NullEventHandler : public YAML::EventHandler {
public:
void OnDocumentStart(const YAML::Mark&) override {}
void OnDocumentEnd() override {}
void OnNull(const YAML::Mark&, YAML::anchor_t) override {}
void OnAlias(const YAML::Mark&, YAML::anchor_t) override {}
void OnScalar(const YAML::Mark&, const std::string&, YAML::anchor_t,
const std::string&) override {}
void OnSequenceStart(const YAML::Mark&, const std::string&, YAML::anchor_t,
YAML::EmitterStyle::value) override {}
void OnSequenceEnd() override {}
void OnMapStart(const YAML::Mark&, const std::string&, YAML::anchor_t,
YAML::EmitterStyle::value) override {}
void OnMapEnd() override {}
};
void parse(std::istream& input) {
try {
YAML::Node doc = YAML::Load(input);
std::cout << doc << "\n";
} catch (const YAML::Exception& e) {
std::cerr << e.what() << "\n";
}
}
int main(int argc, char** argv) {
Params p = ParseArgs(argc, argv);
if (argc > 1) {
std::ifstream fin;
fin.open(argv[1]);
parse(fin);
} else {
parse(std::cin);
}
return 0;
}