parse.cpp 1.33 KB
Newer Older
1
#include "yaml-cpp/node/parse.h"
Jesse Beder's avatar
Jesse Beder committed
2
3
4
5

#include <fstream>
#include <sstream>

6
7
8
9
10
#include "yaml-cpp/node/node.h"
#include "yaml-cpp/node/impl.h"
#include "yaml-cpp/parser.h"
#include "nodebuilder.h"

Jesse Beder's avatar
Jesse Beder committed
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
namespace YAML {
Node Load(const std::string& input) {
  std::stringstream stream(input);
  return Load(stream);
}

Node Load(const char* input) {
  std::stringstream stream(input);
  return Load(stream);
}

Node Load(std::istream& input) {
  Parser parser(input);
  NodeBuilder builder;
  if (!parser.HandleNextDocument(builder))
    return Node();
27

Jesse Beder's avatar
Jesse Beder committed
28
29
30
31
32
33
34
35
36
  return builder.Root();
}

Node LoadFile(const std::string& filename) {
  std::ifstream fin(filename.c_str());
  if (!fin)
    throw BadFile();
  return Load(fin);
}
37

Jesse Beder's avatar
Jesse Beder committed
38
39
40
41
std::vector<Node> LoadAll(const std::string& input) {
  std::stringstream stream(input);
  return LoadAll(stream);
}
42

Jesse Beder's avatar
Jesse Beder committed
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
std::vector<Node> LoadAll(const char* input) {
  std::stringstream stream(input);
  return LoadAll(stream);
}

std::vector<Node> LoadAll(std::istream& input) {
  std::vector<Node> docs;

  Parser parser(input);
  while (1) {
    NodeBuilder builder;
    if (!parser.HandleNextDocument(builder))
      break;
    docs.push_back(builder.Root());
  }

  return docs;
}

std::vector<Node> LoadAllFromFile(const std::string& filename) {
  std::ifstream fin(filename.c_str());
  if (!fin)
    throw BadFile();
  return LoadAll(fin);
}
68
}