17#include <c4/yml/std/map.hpp>
18#include <c4/yml/std/string.hpp>
19#include <c4/yml/common.hpp>
24size_t count_nlines(c4::csubstr src)
27 size_t n = (src.len > 0);
29 n += (src.begins_with(
'\n') || src.begins_with(
'\r'));
35c4::yml::Tree makeTree(std::istream &is)
38 throw std::runtime_error(
"invalid input!");
39 std::string s(std::istreambuf_iterator<char>(is), {});
41 auto src = c4::to_csubstr(s.c_str());
42 size_t nlines = count_nlines(src);
43 c4::yml::Tree
tree(nlines, s.size());
45 np.parse({},
tree.copy_to_arena(src), &
tree);
60 inline ryml::Tree &
get()
65 inline const ryml::Tree &
get()
const
86 inline c4::yml::NodeRef &
get()
91 inline const c4::yml::NodeRef &
get()
const
103 os << c4::yml::as_json(
node->get());
115 node->get() |= c4::yml::MAP;
121 node->get() |= c4::yml::SEQ;
189 return Impl::mkNode(
tree, node->get()[c4::to_csubstr(
tree->incache(k))]);
195 return Impl::mkNode(
tree, node->get()[pos]);
201 return Impl::mkNode(
tree, node->get()[c4::to_csubstr(
tree->incache(k))]);
207 return Impl::mkNode(
tree, node->get()[pos]);
213 return node->get().is_container();
219 return node->get().is_map();
225 return node->get().is_seq();
231 std::stringstream ss;
232 ss << node->get().key();
240 std::stringstream ss;
241 ss << node->get().val();
248 return Impl::mkNode(
tree, node->get().append_child());
254 return node->get().has_key();
260 return node->get().has_val();
266 return node->get().has_child(c4::to_csubstr(s.c_str()));
272 return node->get().num_children();
278 return Impl::mkNode(
tree, node->get().child(pos));
284 return Impl::mkNode(
tree, node->get().child(pos));
290void error_cb(
const char *msg,
size_t msg_len, c4::yml::Location ,
void * )
293 if (msg && msg_len > 0) {
294 throw std::runtime_error(msg);
296 throw std::runtime_error(
"error handler invoked without error message");
303 c4::yml::set_callbacks(c4::yml::Callbacks(c4::yml::get_callbacks().m_user_data, c4::yml::get_callbacks().m_allocate,
304 c4::yml::get_callbacks().m_free, &::error_cb));
307bool ok = setcallbacks();
const ryml::Tree & get() const
const c4::yml::NodeRef & get() const
static TRYMLTree::Node & mkNode(TRYMLTree *t, c4::yml::NodeRef node)
Impl(const c4::yml::NodeRef &n)
virtual bool is_map() const override
virtual bool has_key() const override
virtual Node & child(size_t pos) override
virtual const Node & operator>>(std::string &v) const override
virtual void writeYML(std::ostream &) const override
virtual bool is_container() const override
virtual std::string key() const override
virtual Node & operator<<(std::string const &s) override
std::unique_ptr< Impl > node
virtual std::string val() const override
virtual Node & operator[](std::string const &k) override
virtual Node & append_child() override
virtual void set_seq() override
virtual bool is_seq() const override
Node(TRYMLTree *t, const Impl &other)
virtual bool has_val() const override
virtual bool has_child(std::string const &) const override
virtual void writeJSON(std::ostream &os) const override
virtual size_t num_children() const override
virtual void set_map() override
std::list< std::string > _strcache
Node & incache(const Node &n)
std::list< Node > _nodecache