parse_stdin.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #include "cpptoml.h"
  2. #include <iostream>
  3. #include <limits>
  4. /**
  5. * A visitor for toml objects that writes to an output stream in the JSON
  6. * format that the toml-test suite expects.
  7. */
  8. class toml_test_writer
  9. {
  10. public:
  11. toml_test_writer(std::ostream& s) : stream_(s)
  12. {
  13. // nothing
  14. }
  15. void visit(const cpptoml::value<std::string>& v)
  16. {
  17. stream_ << "{\"type\":\"string\",\"value\":\""
  18. << cpptoml::toml_writer::escape_string(v.get()) << "\"}";
  19. }
  20. void visit(const cpptoml::value<int64_t>& v)
  21. {
  22. stream_ << "{\"type\":\"integer\",\"value\":\"" << v.get() << "\"}";
  23. }
  24. void visit(const cpptoml::value<double>& v)
  25. {
  26. stream_ << "{\"type\":\"float\",\"value\":\"" << v.get() << "\"}";
  27. }
  28. void visit(const cpptoml::value<cpptoml::local_date>& v)
  29. {
  30. stream_ << "{\"type\":\"local_date\",\"value\":\"" << v.get() << "\"}";
  31. }
  32. void visit(const cpptoml::value<cpptoml::local_time>& v)
  33. {
  34. stream_ << "{\"type\":\"local_time\",\"value\":\"" << v.get() << "\"}";
  35. }
  36. void visit(const cpptoml::value<cpptoml::local_datetime>& v)
  37. {
  38. stream_ << "{\"type\":\"local_datetime\",\"value\":\"" << v.get()
  39. << "\"}";
  40. }
  41. void visit(const cpptoml::value<cpptoml::offset_datetime>& v)
  42. {
  43. stream_ << "{\"type\":\"datetime\",\"value\":\"" << v.get() << "\"}";
  44. }
  45. void visit(const cpptoml::value<bool>& v)
  46. {
  47. stream_ << "{\"type\":\"bool\",\"value\":\"" << v << "\"}";
  48. }
  49. void visit(const cpptoml::array& arr)
  50. {
  51. stream_ << "{\"type\":\"array\",\"value\":[";
  52. auto it = arr.get().begin();
  53. while (it != arr.get().end())
  54. {
  55. (*it)->accept(*this);
  56. if (++it != arr.get().end())
  57. stream_ << ", ";
  58. }
  59. stream_ << "]}";
  60. }
  61. void visit(const cpptoml::table_array& tarr)
  62. {
  63. stream_ << "[";
  64. auto arr = tarr.get();
  65. auto ait = arr.begin();
  66. while (ait != arr.end())
  67. {
  68. (*ait)->accept(*this);
  69. if (++ait != arr.end())
  70. stream_ << ", ";
  71. }
  72. stream_ << "]";
  73. }
  74. void visit(const cpptoml::table& t)
  75. {
  76. stream_ << "{";
  77. auto it = t.begin();
  78. while (it != t.end())
  79. {
  80. stream_ << '"' << cpptoml::toml_writer::escape_string(it->first)
  81. << "\":";
  82. it->second->accept(*this);
  83. if (++it != t.end())
  84. stream_ << ", ";
  85. }
  86. stream_ << "}";
  87. }
  88. private:
  89. std::ostream& stream_;
  90. };
  91. int main()
  92. {
  93. std::cout.precision(std::numeric_limits<double>::max_digits10);
  94. cpptoml::parser p{std::cin};
  95. try
  96. {
  97. std::shared_ptr<cpptoml::table> g = p.parse();
  98. toml_test_writer writer{std::cout};
  99. g->accept(writer);
  100. std::cout << std::endl;
  101. }
  102. catch (const cpptoml::parse_exception& ex)
  103. {
  104. std::cerr << "Parsing failed: " << ex.what() << std::endl;
  105. return 1;
  106. }
  107. catch (...)
  108. {
  109. std::cerr << "Something horrible happened!" << std::endl;
  110. // return as if there was success so that toml-test will complain
  111. return 0;
  112. }
  113. return 0;
  114. }