mirror of
https://github.com/jbeder/yaml-cpp.git
synced 2025-09-09 04:41:16 +00:00
Implemented sugar Parse() functions
This commit is contained in:
@@ -11,5 +11,6 @@
|
|||||||
#include "yaml-cpp/value/convert.h"
|
#include "yaml-cpp/value/convert.h"
|
||||||
#include "yaml-cpp/value/iterator.h"
|
#include "yaml-cpp/value/iterator.h"
|
||||||
#include "yaml-cpp/value/detail/impl.h"
|
#include "yaml-cpp/value/detail/impl.h"
|
||||||
|
#include "yaml-cpp/value/parse.h"
|
||||||
|
|
||||||
#endif // VALUE_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
#endif // VALUE_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||||
|
@@ -112,13 +112,13 @@ namespace YAML
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Value::AssignData(const Value& rhs)
|
inline void Value::AssignData(const Value& rhs)
|
||||||
{
|
{
|
||||||
m_pNode->set_data(*rhs.m_pNode);
|
m_pNode->set_data(*rhs.m_pNode);
|
||||||
m_pMemory->merge(*rhs.m_pMemory);
|
m_pMemory->merge(*rhs.m_pMemory);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Value::AssignNode(const Value& rhs)
|
inline void Value::AssignNode(const Value& rhs)
|
||||||
{
|
{
|
||||||
m_pNode->set_ref(*rhs.m_pNode);
|
m_pNode->set_ref(*rhs.m_pNode);
|
||||||
m_pMemory->merge(*rhs.m_pMemory);
|
m_pMemory->merge(*rhs.m_pMemory);
|
||||||
|
21
include/yaml-cpp/value/parse.h
Normal file
21
include/yaml-cpp/value/parse.h
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
#ifndef VALUE_PARSE_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||||
|
#define VALUE_PARSE_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||||
|
|
||||||
|
#if defined(_MSC_VER) || (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4
|
||||||
|
#pragma once
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <iosfwd>
|
||||||
|
|
||||||
|
namespace YAML
|
||||||
|
{
|
||||||
|
class Value;
|
||||||
|
|
||||||
|
Value Parse(const std::string& input);
|
||||||
|
Value Parse(const char *input);
|
||||||
|
Value Parse(std::istream& input);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // VALUE_PARSE_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||||
|
|
@@ -17,6 +17,7 @@ namespace YAML
|
|||||||
class Value
|
class Value
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
friend class ValueBuilder;
|
||||||
friend class detail::node_data;
|
friend class detail::node_data;
|
||||||
template<typename, typename, typename> friend class detail::iterator_base;
|
template<typename, typename, typename> friend class detail::iterator_base;
|
||||||
|
|
||||||
|
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&)
|
void ValueBuilder::OnDocumentStart(const Mark&)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
@@ -70,13 +78,13 @@ namespace YAML
|
|||||||
|
|
||||||
detail::node& ValueBuilder::Push(anchor_t anchor)
|
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();
|
detail::node& node = m_pMemory->create_node();
|
||||||
m_stack.push_back(&node);
|
m_stack.push_back(&node);
|
||||||
RegisterAnchor(anchor, node);
|
RegisterAnchor(anchor, node);
|
||||||
|
|
||||||
if(top.type() == ValueType::Map && m_keys.size() < m_mapDepth)
|
if(needsKey)
|
||||||
m_keys.push_back(&node);
|
m_keys.push_back(&node);
|
||||||
|
|
||||||
return node;
|
return node;
|
||||||
|
@@ -19,6 +19,8 @@ namespace YAML
|
|||||||
ValueBuilder();
|
ValueBuilder();
|
||||||
virtual ~ValueBuilder();
|
virtual ~ValueBuilder();
|
||||||
|
|
||||||
|
Value Root();
|
||||||
|
|
||||||
virtual void OnDocumentStart(const Mark& mark);
|
virtual void OnDocumentStart(const Mark& mark);
|
||||||
virtual void OnDocumentEnd();
|
virtual void OnDocumentEnd();
|
||||||
|
|
||||||
|
@@ -3,22 +3,7 @@
|
|||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
YAML::Value value;
|
YAML::Value value = YAML::Parse("foo: bar");
|
||||||
value["seq"] = YAML::Value(YAML::ValueType::Sequence);
|
|
||||||
for(int i=0;i<5;i++)
|
|
||||||
value["seq"].append(i);
|
|
||||||
value["map"]["one"] = "I";
|
|
||||||
value["map"]["two"] = "II";
|
|
||||||
value["map"]["three"] = "III";
|
|
||||||
value["map"]["four"] = "IV";
|
|
||||||
|
|
||||||
for(YAML::const_iterator it=value["seq"].begin();it!=value["seq"].end();++it) {
|
|
||||||
std::cout << it->as<int>() << "\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
for(YAML::const_iterator it=value["map"].begin();it!=value["map"].end();++it) {
|
|
||||||
std::cout << it->first.as<std::string>() << " -> " << it->second.as<std::string>() << "\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user