1 #include "yaml-cpp/node/parse.h"
2 
3 #include <fstream>
4 #include <sstream>
5 
6 #include "yaml-cpp/node/node.h"
7 #include "yaml-cpp/node/impl.h"
8 #include "yaml-cpp/parser.h"
9 #include "nodebuilder.h"
10 
11 namespace YAML {
Load(const std::string & input)12 Node Load(const std::string& input) {
13   std::stringstream stream(input);
14   return Load(stream);
15 }
16 
Load(const char * input)17 Node Load(const char* input) {
18   std::stringstream stream(input);
19   return Load(stream);
20 }
21 
Load(std::istream & input)22 Node Load(std::istream& input) {
23   Parser parser(input);
24   NodeBuilder builder;
25   if (!parser.HandleNextDocument(builder)) {
26     return Node();
27   }
28 
29   return builder.Root();
30 }
31 
LoadFile(const std::string & filename)32 Node LoadFile(const std::string& filename) {
33   std::ifstream fin(filename.c_str());
34   if (!fin) {
35     throw BadFile();
36   }
37   return Load(fin);
38 }
39 
LoadAll(const std::string & input)40 std::vector<Node> LoadAll(const std::string& input) {
41   std::stringstream stream(input);
42   return LoadAll(stream);
43 }
44 
LoadAll(const char * input)45 std::vector<Node> LoadAll(const char* input) {
46   std::stringstream stream(input);
47   return LoadAll(stream);
48 }
49 
LoadAll(std::istream & input)50 std::vector<Node> LoadAll(std::istream& input) {
51   std::vector<Node> docs;
52 
53   Parser parser(input);
54   while (1) {
55     NodeBuilder builder;
56     if (!parser.HandleNextDocument(builder)) {
57       break;
58     }
59     docs.push_back(builder.Root());
60   }
61 
62   return docs;
63 }
64 
LoadAllFromFile(const std::string & filename)65 std::vector<Node> LoadAllFromFile(const std::string& filename) {
66   std::ifstream fin(filename.c_str());
67   if (!fin) {
68     throw BadFile();
69   }
70   return LoadAll(fin);
71 }
72 }  // namespace YAML
73