mirror of
https://github.com/jbeder/yaml-cpp.git
synced 2025-09-09 20:51:16 +00:00
Implemented sugar Parse() functions
This commit is contained in:
29
src/value/parse.cpp
Normal file
29
src/value/parse.cpp
Normal file
@@ -0,0 +1,29 @@
|
||||
#include "yaml-cpp/value/parse.h"
|
||||
#include "yaml-cpp/value/value.h"
|
||||
#include "yaml-cpp/value/impl.h"
|
||||
#include "yaml-cpp/parser.h"
|
||||
#include "valuebuilder.h"
|
||||
|
||||
#include <sstream>
|
||||
|
||||
namespace YAML
|
||||
{
|
||||
Value Parse(const std::string& input) {
|
||||
std::stringstream stream(input);
|
||||
return Parse(stream);
|
||||
}
|
||||
|
||||
Value Parse(const char *input) {
|
||||
std::stringstream stream(input);
|
||||
return Parse(stream);
|
||||
}
|
||||
|
||||
Value Parse(std::istream& input) {
|
||||
Parser parser(input);
|
||||
ValueBuilder builder;
|
||||
if(!parser.HandleNextDocument(builder))
|
||||
return Value();
|
||||
|
||||
return builder.Root();
|
||||
}
|
||||
}
|
@@ -14,6 +14,14 @@ namespace YAML
|
||||
{
|
||||
}
|
||||
|
||||
Value ValueBuilder::Root()
|
||||
{
|
||||
if(!m_pRoot)
|
||||
return Value();
|
||||
|
||||
return Value(*m_pRoot, m_pMemory);
|
||||
}
|
||||
|
||||
void ValueBuilder::OnDocumentStart(const Mark&)
|
||||
{
|
||||
}
|
||||
@@ -70,13 +78,13 @@ namespace YAML
|
||||
|
||||
detail::node& ValueBuilder::Push(anchor_t anchor)
|
||||
{
|
||||
detail::node& top = *m_stack.back();
|
||||
const bool needsKey = (!m_stack.empty() && m_stack.back()->type() == ValueType::Map && m_keys.size() < m_mapDepth);
|
||||
|
||||
detail::node& node = m_pMemory->create_node();
|
||||
m_stack.push_back(&node);
|
||||
RegisterAnchor(anchor, node);
|
||||
|
||||
if(top.type() == ValueType::Map && m_keys.size() < m_mapDepth)
|
||||
if(needsKey)
|
||||
m_keys.push_back(&node);
|
||||
|
||||
return node;
|
||||
|
@@ -19,6 +19,8 @@ namespace YAML
|
||||
ValueBuilder();
|
||||
virtual ~ValueBuilder();
|
||||
|
||||
Value Root();
|
||||
|
||||
virtual void OnDocumentStart(const Mark& mark);
|
||||
virtual void OnDocumentEnd();
|
||||
|
||||
|
Reference in New Issue
Block a user