xref: /trafficserver/lib/yamlcpp/util/parse.cpp (revision 96592e57)
1 #include <fstream>
2 #include <iostream>
3 #include <vector>
4 
5 #include "yaml-cpp/eventhandler.h"
6 #include "yaml-cpp/yaml.h"  // IWYU pragma: keep
7 
8 struct Params {
9   bool hasFile;
10   std::string fileName;
11 };
12 
ParseArgs(int argc,char ** argv)13 Params ParseArgs(int argc, char** argv) {
14   Params p;
15 
16   std::vector<std::string> args(argv + 1, argv + argc);
17 
18   return p;
19 }
20 
21 class NullEventHandler : public YAML::EventHandler {
22  public:
OnDocumentStart(const YAML::Mark &)23   void OnDocumentStart(const YAML::Mark&) override {}
OnDocumentEnd()24   void OnDocumentEnd() override {}
25 
OnNull(const YAML::Mark &,YAML::anchor_t)26   void OnNull(const YAML::Mark&, YAML::anchor_t) override {}
OnAlias(const YAML::Mark &,YAML::anchor_t)27   void OnAlias(const YAML::Mark&, YAML::anchor_t) override {}
OnScalar(const YAML::Mark &,const std::string &,YAML::anchor_t,const std::string &)28   void OnScalar(const YAML::Mark&, const std::string&, YAML::anchor_t,
29                 const std::string&) override {}
30 
OnSequenceStart(const YAML::Mark &,const std::string &,YAML::anchor_t,YAML::EmitterStyle::value)31   void OnSequenceStart(const YAML::Mark&, const std::string&, YAML::anchor_t,
32                        YAML::EmitterStyle::value) override {}
OnSequenceEnd()33   void OnSequenceEnd() override {}
34 
OnMapStart(const YAML::Mark &,const std::string &,YAML::anchor_t,YAML::EmitterStyle::value)35   void OnMapStart(const YAML::Mark&, const std::string&, YAML::anchor_t,
36                   YAML::EmitterStyle::value) override {}
OnMapEnd()37   void OnMapEnd() override {}
38 };
39 
parse(std::istream & input)40 void parse(std::istream& input) {
41   try {
42     YAML::Node doc = YAML::Load(input);
43     std::cout << doc << "\n";
44   } catch (const YAML::Exception& e) {
45     std::cerr << e.what() << "\n";
46   }
47 }
48 
main(int argc,char ** argv)49 int main(int argc, char** argv) {
50   Params p = ParseArgs(argc, argv);
51 
52   if (argc > 1) {
53     std::ifstream fin;
54     fin.open(argv[1]);
55     parse(fin);
56   } else {
57     parse(std::cin);
58   }
59 
60   return 0;
61 }
62