yaml-cpp/src/parse.cpp

73 lines
1.4 KiB
C++
Raw Normal View History

#include "yaml-cpp/node/parse.h"
2014-03-23 08:14:48 +04:00
#include <fstream>
#include <sstream>
#include "yaml-cpp/node/node.h"
#include "yaml-cpp/node/impl.h"
#include "yaml-cpp/parser.h"
#include "nodebuilder.h"
2014-03-22 22:05:03 +04:00
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;
2016-05-13 07:20:03 +03:00
if (!parser.HandleNextDocument(builder)) {
2014-03-22 22:05:03 +04:00
return Node();
2016-05-13 07:20:03 +03:00
}
2014-03-22 22:05:03 +04:00
return builder.Root();
}
Node LoadFile(const std::string& filename) {
std::ifstream fin(filename.c_str());
2016-05-13 07:20:03 +03:00
if (!fin) {
2014-03-22 22:05:03 +04:00
throw BadFile();
2016-05-13 07:20:03 +03:00
}
2014-03-22 22:05:03 +04:00
return Load(fin);
}
2014-03-22 22:05:03 +04:00
std::vector<Node> LoadAll(const std::string& input) {
std::stringstream stream(input);
return LoadAll(stream);
}
2014-03-22 22:05:03 +04:00
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;
2016-05-13 07:20:03 +03:00
if (!parser.HandleNextDocument(builder)) {
2014-03-22 22:05:03 +04:00
break;
2016-05-13 07:20:03 +03:00
}
2014-03-22 22:05:03 +04:00
docs.push_back(builder.Root());
}
return docs;
}
std::vector<Node> LoadAllFromFile(const std::string& filename) {
std::ifstream fin(filename.c_str());
2016-05-13 07:20:03 +03:00
if (!fin) {
2014-03-22 22:05:03 +04:00
throw BadFile();
2016-05-13 07:20:03 +03:00
}
2014-03-22 22:05:03 +04:00
return LoadAll(fin);
}
2016-05-13 07:20:03 +03:00
} // namespace YAML