parse.cpp 1.4 KB

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