Commit d63ec48c authored by Jesse Beder's avatar Jesse Beder
Browse files

Run clang-format

parent 3355bbb3
#ifndef YAML_H_62B23520_7C8E_11DE_8A39_0800200C9A66
#define YAML_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
#if defined(_MSC_VER) || \
(defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || \
(__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4
#pragma once
#endif
......
......@@ -2,53 +2,48 @@
#include "yaml-cpp/node/impl.h"
#include <algorithm>
namespace
{
// we're not gonna mess with the mess that is all the isupper/etc. functions
bool IsLower(char ch) { return 'a' <= ch && ch <= 'z'; }
bool IsUpper(char ch) { return 'A' <= ch && ch <= 'Z'; }
char ToLower(char ch) { return IsUpper(ch) ? ch + 'a' - 'A' : ch; }
namespace {
// we're not gonna mess with the mess that is all the isupper/etc. functions
bool IsLower(char ch) { return 'a' <= ch && ch <= 'z'; }
bool IsUpper(char ch) { return 'A' <= ch && ch <= 'Z'; }
char ToLower(char ch) { return IsUpper(ch) ? ch + 'a' - 'A' : ch; }
std::string tolower(const std::string& str)
{
std::string tolower(const std::string& str) {
std::string s(str);
std::transform(s.begin(), s.end(), s.begin(), ToLower);
return s;
}
}
template <typename T>
bool IsEntirely(const std::string& str, T func)
{
for(std::size_t i=0;i<str.size();i++)
if(!func(str[i]))
template <typename T>
bool IsEntirely(const std::string& str, T func) {
for (std::size_t i = 0; i < str.size(); i++)
if (!func(str[i]))
return false;
return true;
}
}
// IsFlexibleCase
// . Returns true if 'str' is:
// . UPPERCASE
// . lowercase
// . Capitalized
bool IsFlexibleCase(const std::string& str)
{
if(str.empty())
// IsFlexibleCase
// . Returns true if 'str' is:
// . UPPERCASE
// . lowercase
// . Capitalized
bool IsFlexibleCase(const std::string& str) {
if (str.empty())
return true;
if(IsEntirely(str, IsLower))
if (IsEntirely(str, IsLower))
return true;
bool firstcaps = IsUpper(str[0]);
std::string rest = str.substr(1);
return firstcaps && (IsEntirely(rest, IsLower) || IsEntirely(rest, IsUpper));
}
}
}
namespace YAML
{
bool convert<bool>::decode(const Node& node, bool& rhs) {
if(!node.IsScalar())
namespace YAML {
bool convert<bool>::decode(const Node& node, bool& rhs) {
if (!node.IsScalar())
return false;
// we can't use iostream bool extraction operators as they don't
......@@ -56,28 +51,23 @@ namespace YAML
// http://yaml.org/type/bool.html)
static const struct {
std::string truename, falsename;
} names[] = {
{ "y", "n" },
{ "yes", "no" },
{ "true", "false" },
{ "on", "off" },
};
} names[] = {{"y", "n"}, {"yes", "no"}, {"true", "false"}, {"on", "off"}, };
if(!IsFlexibleCase(node.Scalar()))
if (!IsFlexibleCase(node.Scalar()))
return false;
for(unsigned i=0;i<sizeof(names)/sizeof(names[0]);i++) {
if(names[i].truename == tolower(node.Scalar())) {
for (unsigned i = 0; i < sizeof(names) / sizeof(names[0]); i++) {
if (names[i].truename == tolower(node.Scalar())) {
rhs = true;
return true;
}
if(names[i].falsename == tolower(node.Scalar())) {
if (names[i].falsename == tolower(node.Scalar())) {
rhs = false;
return true;
}
}
return false;
}
}
}
......@@ -3,27 +3,23 @@
#include "yaml-cpp/emitter.h"
#include "nodeevents.h"
namespace YAML
{
Emitter& operator << (Emitter& out, const Node& node)
{
namespace YAML {
Emitter& operator<<(Emitter& out, const Node& node) {
EmitFromEvents emitFromEvents(out);
NodeEvents events(node);
events.Emit(emitFromEvents);
return out;
}
}
std::ostream& operator << (std::ostream& out, const Node& node)
{
std::ostream& operator<<(std::ostream& out, const Node& node) {
Emitter emitter(out);
emitter << node;
return out;
}
}
std::string Dump(const Node& node)
{
std::string Dump(const Node& node) {
Emitter emitter;
emitter << node;
return emitter.c_str();
}
}
}
#include "yaml-cpp/node/detail/memory.h"
#include "yaml-cpp/node/detail/node.h"
namespace YAML
{
namespace detail
{
void memory_holder::merge(memory_holder& rhs)
{
if(m_pMemory == rhs.m_pMemory)
namespace YAML {
namespace detail {
void memory_holder::merge(memory_holder& rhs) {
if (m_pMemory == rhs.m_pMemory)
return;
m_pMemory->merge(*rhs.m_pMemory);
rhs.m_pMemory = m_pMemory;
}
}
node& memory::create_node()
{
node& memory::create_node() {
shared_node pNode(new node);
m_nodes.insert(pNode);
return *pNode;
}
}
void memory::merge(const memory& rhs)
{
void memory::merge(const memory& rhs) {
m_nodes.insert(rhs.m_nodes.begin(), rhs.m_nodes.end());
}
}
}
}
}
......@@ -2,13 +2,11 @@
#include "nodebuilder.h"
#include "nodeevents.h"
namespace YAML
{
Node Clone(const Node& node)
{
namespace YAML {
Node Clone(const Node& node) {
NodeEvents events(node);
NodeBuilder builder;
events.Emit(builder);
return builder.Root();
}
}
}
......@@ -4,39 +4,33 @@
#include "yaml-cpp/exceptions.h"
#include <sstream>
namespace YAML
{
namespace detail
{
std::string node_data::empty_scalar;
node_data::node_data(): m_isDefined(false), m_type(NodeType::Null), m_seqSize(0)
{
}
namespace YAML {
namespace detail {
std::string node_data::empty_scalar;
node_data::node_data()
: m_isDefined(false), m_type(NodeType::Null), m_seqSize(0) {}
void node_data::mark_defined()
{
if(m_type == NodeType::Undefined)
void node_data::mark_defined() {
if (m_type == NodeType::Undefined)
m_type = NodeType::Null;
m_isDefined = true;
}
}
void node_data::set_type(NodeType::value type)
{
if(type == NodeType::Undefined) {
void node_data::set_type(NodeType::value type) {
if (type == NodeType::Undefined) {
m_type = type;
m_isDefined = false;
return;
}
m_isDefined = true;
if(type == m_type)
if (type == m_type)
return;
m_type = type;
switch(m_type) {
switch (m_type) {
case NodeType::Null:
break;
case NodeType::Scalar:
......@@ -52,123 +46,125 @@ namespace YAML
assert(false);
break;
}
}
}
void node_data::set_tag(const std::string& tag)
{
m_tag = tag;
}
void node_data::set_tag(const std::string& tag) { m_tag = tag; }
void node_data::set_null()
{
void node_data::set_null() {
m_isDefined = true;
m_type = NodeType::Null;
}
}
void node_data::set_scalar(const std::string& scalar)
{
void node_data::set_scalar(const std::string& scalar) {
m_isDefined = true;
m_type = NodeType::Scalar;
m_scalar = scalar;
}
}
// size/iterator
std::size_t node_data::size() const
{
if(!m_isDefined)
// size/iterator
std::size_t node_data::size() const {
if (!m_isDefined)
return 0;
switch(m_type) {
case NodeType::Sequence: compute_seq_size(); return m_seqSize;
case NodeType::Map: compute_map_size(); return m_map.size() - m_undefinedPairs.size();
switch (m_type) {
case NodeType::Sequence:
compute_seq_size();
return m_seqSize;
case NodeType::Map:
compute_map_size();
return m_map.size() - m_undefinedPairs.size();
default:
return 0;
}
return 0;
}
}
void node_data::compute_seq_size() const
{
while(m_seqSize < m_sequence.size() && m_sequence[m_seqSize]->is_defined())
void node_data::compute_seq_size() const {
while (m_seqSize < m_sequence.size() && m_sequence[m_seqSize]->is_defined())
m_seqSize++;
}
}
void node_data::compute_map_size() const
{
void node_data::compute_map_size() const {
kv_pairs::iterator it = m_undefinedPairs.begin();
while(it != m_undefinedPairs.end()) {
while (it != m_undefinedPairs.end()) {
kv_pairs::iterator jt = boost::next(it);
if(it->first->is_defined() && it->second->is_defined())
if (it->first->is_defined() && it->second->is_defined())
m_undefinedPairs.erase(it);
it = jt;
}
}
}
const_node_iterator node_data::begin() const
{
if(!m_isDefined)
const_node_iterator node_data::begin() const {
if (!m_isDefined)
return const_node_iterator();
switch(m_type) {
case NodeType::Sequence: return const_node_iterator(m_sequence.begin());
case NodeType::Map: return const_node_iterator(m_map.begin(), m_map.end());
default: return const_node_iterator();
}
switch (m_type) {
case NodeType::Sequence:
return const_node_iterator(m_sequence.begin());
case NodeType::Map:
return const_node_iterator(m_map.begin(), m_map.end());
default:
return const_node_iterator();
}
}
node_iterator node_data::begin()
{
if(!m_isDefined)
node_iterator node_data::begin() {
if (!m_isDefined)
return node_iterator();
switch(m_type) {
case NodeType::Sequence: return node_iterator(m_sequence.begin());
case NodeType::Map: return node_iterator(m_map.begin(), m_map.end());
default: return node_iterator();
}
switch (m_type) {
case NodeType::Sequence:
return node_iterator(m_sequence.begin());
case NodeType::Map:
return node_iterator(m_map.begin(), m_map.end());
default:
return node_iterator();
}
}
const_node_iterator node_data::end() const
{
if(!m_isDefined)
const_node_iterator node_data::end() const {
if (!m_isDefined)
return const_node_iterator();
switch(m_type) {
case NodeType::Sequence: return const_node_iterator(m_sequence.end());
case NodeType::Map: return const_node_iterator(m_map.end(), m_map.end());
default: return const_node_iterator();
}
switch (m_type) {
case NodeType::Sequence:
return const_node_iterator(m_sequence.end());
case NodeType::Map:
return const_node_iterator(m_map.end(), m_map.end());
default:
return const_node_iterator();
}
}
node_iterator node_data::end()
{
if(!m_isDefined)
node_iterator node_data::end() {
if (!m_isDefined)
return node_iterator();
switch(m_type) {
case NodeType::Sequence: return node_iterator(m_sequence.end());
case NodeType::Map: return node_iterator(m_map.end(), m_map.end());
default: return node_iterator();
}
switch (m_type) {
case NodeType::Sequence:
return node_iterator(m_sequence.end());
case NodeType::Map:
return node_iterator(m_map.end(), m_map.end());
default:
return node_iterator();
}
}
// sequence
void node_data::push_back(node& node, shared_memory_holder /* pMemory */)
{
if(m_type == NodeType::Undefined || m_type == NodeType::Null) {
// sequence
void node_data::push_back(node& node, shared_memory_holder /* pMemory */) {
if (m_type == NodeType::Undefined || m_type == NodeType::Null) {
m_type = NodeType::Sequence;
reset_sequence();
}
if(m_type != NodeType::Sequence)
if (m_type != NodeType::Sequence)
throw BadPushback();
m_sequence.push_back(&node);
}
}
void node_data::insert(node& key, node& value, shared_memory_holder pMemory)
{
switch(m_type) {
void node_data::insert(node& key, node& value, shared_memory_holder pMemory) {
switch (m_type) {
case NodeType::Map:
break;
case NodeType::Undefined:
......@@ -181,25 +177,23 @@ namespace YAML
}
insert_map_pair(key, value);
}
}
// indexing
node& node_data::get(node& key, shared_memory_holder pMemory) const
{
if(m_type != NodeType::Map)
// indexing
node& node_data::get(node& key, shared_memory_holder pMemory) const {
if (m_type != NodeType::Map)
return pMemory->create_node();
for(node_map::const_iterator it=m_map.begin();it!=m_map.end();++it) {
if(it->first->is(key))
for (node_map::const_iterator it = m_map.begin(); it != m_map.end(); ++it) {
if (it->first->is(key))
return *it->second;
}
return pMemory->create_node();
}
}
node& node_data::get(node& key, shared_memory_holder pMemory)
{
switch(m_type) {
node& node_data::get(node& key, shared_memory_holder pMemory) {
switch (m_type) {
case NodeType::Map:
break;
case NodeType::Undefined:
......@@ -211,53 +205,48 @@ namespace YAML
throw BadSubscript();
}
for(node_map::const_iterator it=m_map.begin();it!=m_map.end();++it) {
if(it->first->is(key))
for (node_map::const_iterator it = m_map.begin(); it != m_map.end(); ++it) {
if (it->first->is(key))
return *it->second;
}
node& value = pMemory->create_node();
insert_map_pair(key, value);
return value;
}
}
bool node_data::remove(node& key, shared_memory_holder /* pMemory */)
{
if(m_type != NodeType::Map)
bool node_data::remove(node& key, shared_memory_holder /* pMemory */) {
if (m_type != NodeType::Map)
return false;
for(node_map::iterator it=m_map.begin();it!=m_map.end();++it) {
if(it->first->is(key)) {
for (node_map::iterator it = m_map.begin(); it != m_map.end(); ++it) {
if (it->first->is(key)) {
m_map.erase(it);
return true;
}
}
return false;
}
}
void node_data::reset_sequence()
{
void node_data::reset_sequence() {
m_sequence.clear();
m_seqSize = 0;
}
}
void node_data::reset_map()
{
void node_data::reset_map() {
m_map.clear();
m_undefinedPairs.clear();
}
}
void node_data::insert_map_pair(node& key, node& value)
{
void node_data::insert_map_pair(node& key, node& value) {
m_map[&key] = &value;
if(!key.is_defined() || !value.is_defined())
if (!key.is_defined() || !value.is_defined())
m_undefinedPairs.push_back(kv_pair(&key, &value));
}
}
void node_data::convert_to_map(shared_memory_holder pMemory)
{
switch(m_type) {
void node_data::convert_to_map(shared_memory_holder pMemory) {
switch (m_type) {
case NodeType::Undefined:
case NodeType::Null:
reset_map();
......@@ -272,14 +261,13 @@ namespace YAML
assert(false);
break;
}
}
}
void node_data::convert_sequence_to_map(shared_memory_holder pMemory)
{
void node_data::convert_sequence_to_map(shared_memory_holder pMemory) {
assert(m_type == NodeType::Sequence);
reset_map();
for(std::size_t i=0;i<m_sequence.size();i++) {
for (std::size_t i = 0; i < m_sequence.size(); i++) {
std::stringstream stream;
stream << i;
......@@ -290,6 +278,6 @@ namespace YAML
reset_sequence();
m_type = NodeType::Map;
}
}
}
}
}
......@@ -4,103 +4,88 @@
#include "yaml-cpp/node/impl.h"
#include <cassert>
namespace YAML
{
NodeBuilder::NodeBuilder(): m_pMemory(new detail::memory_holder), m_pRoot(0), m_mapDepth(0)
{
namespace YAML {
NodeBuilder::NodeBuilder()
: m_pMemory(new detail::memory_holder), m_pRoot(0), m_mapDepth(0) {
m_anchors.push_back(0); // since the anchors start at 1
}
}
NodeBuilder::~NodeBuilder()
{
}
NodeBuilder::~NodeBuilder() {}
Node NodeBuilder::Root()
{
if(!m_pRoot)
Node NodeBuilder::Root() {
if (!m_pRoot)
return Node();
return Node(*m_pRoot, m_pMemory);
}
}
void NodeBuilder::OnDocumentStart(const Mark&)
{
}
void NodeBuilder::OnDocumentStart(const Mark&) {}
void NodeBuilder::OnDocumentEnd()
{
}
void NodeBuilder::OnDocumentEnd() {}
void NodeBuilder::OnNull(const Mark& /* mark */, anchor_t anchor)
{
void NodeBuilder::OnNull(const Mark& /* mark */, anchor_t anchor) {
detail::node& node = Push(anchor);
node.set_null();
Pop();
}
}
void NodeBuilder::OnAlias(const Mark& /* mark */, anchor_t anchor)
{
void NodeBuilder::OnAlias(const Mark& /* mark */, anchor_t anchor) {
detail::node& node = *m_anchors[anchor];
Push(node);
Pop();
}
}
void NodeBuilder::OnScalar(const Mark& /* mark */, const std::string& tag, anchor_t anchor, const std::string& value)
{
void NodeBuilder::OnScalar(const Mark& /* mark */, const std::string& tag,
anchor_t anchor, const std::string& value) {
detail::node& node = Push(anchor);
node.set_scalar(value);
node.set_tag(tag);
Pop();
}
}
void NodeBuilder::OnSequenceStart(const Mark& /* mark */, const std::string& tag, anchor_t anchor)
{
void NodeBuilder::OnSequenceStart(const Mark& /* mark */,
const std::string& tag, anchor_t anchor) {
detail::node& node = Push(anchor);
node.set_tag(tag);
node.set_type(NodeType::Sequence);
}
}
void NodeBuilder::OnSequenceEnd()
{
Pop();
}
void NodeBuilder::OnSequenceEnd() { Pop(); }
void NodeBuilder::OnMapStart(const Mark& /* mark */, const std::string& tag, anchor_t anchor)
{
void NodeBuilder::OnMapStart(const Mark& /* mark */, const std::string& tag,
anchor_t anchor) {
detail::node& node = Push(anchor);
node.set_type(NodeType::Map);
node.set_tag(tag);
m_mapDepth++;
}
}
void NodeBuilder::OnMapEnd()
{
void NodeBuilder::OnMapEnd() {
assert(m_mapDepth > 0);
m_mapDepth--;
Pop();
}
}
detail::node& NodeBuilder::Push(anchor_t anchor)
{
detail::node& NodeBuilder::Push(anchor_t anchor) {
detail::node& node = m_pMemory->create_node();
RegisterAnchor(anchor, node);
Push(node);
return node;
}
}
void NodeBuilder::Push(detail::node& node)
{
const bool needsKey = (!m_stack.empty() && m_stack.back()->type() == NodeType::Map && m_keys.size() < m_mapDepth);
void NodeBuilder::Push(detail::node& node) {
const bool needsKey =
(!m_stack.empty() && m_stack.back()->type() == NodeType::Map &&
m_keys.size() < m_mapDepth);
m_stack.push_back(&node);
if(needsKey)
if (needsKey)
m_keys.push_back(PushedKey(&node, false));
}
}
void NodeBuilder::Pop()
{
void NodeBuilder::Pop() {
assert(!m_stack.empty());
if(m_stack.size() == 1) {
if (m_stack.size() == 1) {
m_pRoot = m_stack[0];
m_stack.pop_back();
return;
......@@ -111,12 +96,12 @@ namespace YAML
detail::node& collection = *m_stack.back();
if(collection.type() == NodeType::Sequence) {
if (collection.type() == NodeType::Sequence) {
collection.push_back(node, m_pMemory);
} else if(collection.type() == NodeType::Map) {
} else if (collection.type() == NodeType::Map) {
assert(!m_keys.empty());
PushedKey& key = m_keys.back();
if(key.second) {
if (key.second) {
collection.insert(*key.first, node, m_pMemory);
m_keys.pop_back();
} else {
......@@ -126,13 +111,12 @@ namespace YAML
assert(false);
m_stack.clear();
}
}
}
void NodeBuilder::RegisterAnchor(anchor_t anchor, detail::node& node)
{
if(anchor) {
void NodeBuilder::RegisterAnchor(anchor_t anchor, detail::node& node) {
if (anchor) {
assert(anchor == m_anchors.size());
m_anchors.push_back(&node);
}
}
}
}
#ifndef NODE_NODEBUILDER_H_62B23520_7C8E_11DE_8A39_0800200C9A66
#define NODE_NODEBUILDER_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
#if defined(_MSC_VER) || \
(defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || \
(__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4
#pragma once
#endif
......@@ -9,12 +11,10 @@
#include "yaml-cpp/node/ptr.h"
#include <vector>
namespace YAML
{
class Node;
namespace YAML {
class Node;
class NodeBuilder: public EventHandler
{
class NodeBuilder : public EventHandler {
public:
NodeBuilder();
virtual ~NodeBuilder();
......@@ -26,12 +26,15 @@ namespace YAML
virtual void OnNull(const Mark& mark, anchor_t anchor);
virtual void OnAlias(const Mark& mark, anchor_t anchor);
virtual void OnScalar(const Mark& mark, const std::string& tag, anchor_t anchor, const std::string& value);
virtual void OnScalar(const Mark& mark, const std::string& tag,
anchor_t anchor, const std::string& value);
virtual void OnSequenceStart(const Mark& mark, const std::string& tag, anchor_t anchor);
virtual void OnSequenceStart(const Mark& mark, const std::string& tag,
anchor_t anchor);
virtual void OnSequenceEnd();
virtual void OnMapStart(const Mark& mark, const std::string& tag, anchor_t anchor);
virtual void OnMapStart(const Mark& mark, const std::string& tag,
anchor_t anchor);
virtual void OnMapEnd();
private:
......@@ -42,17 +45,16 @@ namespace YAML
private:
detail::shared_memory_holder m_pMemory;
detail::node *m_pRoot;
detail::node* m_pRoot;
typedef std::vector<detail::node *> Nodes;
typedef std::vector<detail::node*> Nodes;
Nodes m_stack;
Nodes m_anchors;
typedef std::pair<detail::node *, bool> PushedKey;
typedef std::pair<detail::node*, bool> PushedKey;
std::vector<PushedKey> m_keys;
std::size_t m_mapDepth;
};
};
}
#endif // NODE_NODEBUILDER_H_62B23520_7C8E_11DE_8A39_0800200C9A66
......@@ -4,61 +4,58 @@
#include "yaml-cpp/eventhandler.h"
#include "yaml-cpp/mark.h"
namespace YAML
{
void NodeEvents::AliasManager::RegisterReference(const detail::node& node)
{
namespace YAML {
void NodeEvents::AliasManager::RegisterReference(const detail::node& node) {
m_anchorByIdentity.insert(std::make_pair(node.ref(), _CreateNewAnchor()));
}
}
anchor_t NodeEvents::AliasManager::LookupAnchor(const detail::node& node) const
{
anchor_t NodeEvents::AliasManager::LookupAnchor(const detail::node& node)
const {
AnchorByIdentity::const_iterator it = m_anchorByIdentity.find(node.ref());
if(it == m_anchorByIdentity.end())
if (it == m_anchorByIdentity.end())
return 0;
return it->second;
}
}
NodeEvents::NodeEvents(const Node& node): m_pMemory(node.m_pMemory), m_root(node.m_pNode)
{
if(m_root)
NodeEvents::NodeEvents(const Node& node)
: m_pMemory(node.m_pMemory), m_root(node.m_pNode) {
if (m_root)
Setup(*m_root);
}
}
void NodeEvents::Setup(const detail::node& node)
{
void NodeEvents::Setup(const detail::node& node) {
int& refCount = m_refCount[node.ref()];
refCount++;
if(refCount > 1)
if (refCount > 1)
return;
if(node.type() == NodeType::Sequence) {
for(detail::const_node_iterator it=node.begin();it!=node.end();++it)
if (node.type() == NodeType::Sequence) {
for (detail::const_node_iterator it = node.begin(); it != node.end(); ++it)
Setup(**it);
} else if(node.type() == NodeType::Map) {
for(detail::const_node_iterator it=node.begin();it!=node.end();++it) {
} else if (node.type() == NodeType::Map) {
for (detail::const_node_iterator it = node.begin(); it != node.end();
++it) {
Setup(*it->first);
Setup(*it->second);
}
}
}
}
void NodeEvents::Emit(EventHandler& handler)
{
void NodeEvents::Emit(EventHandler& handler) {
AliasManager am;
handler.OnDocumentStart(Mark());
if(m_root)
if (m_root)
Emit(*m_root, handler, am);
handler.OnDocumentEnd();
}
}
void NodeEvents::Emit(const detail::node& node, EventHandler& handler, AliasManager& am) const
{
void NodeEvents::Emit(const detail::node& node, EventHandler& handler,
AliasManager& am) const {
anchor_t anchor = NullAnchor;
if(IsAliased(node)) {
if (IsAliased(node)) {
anchor = am.LookupAnchor(node);
if(anchor) {
if (anchor) {
handler.OnAlias(Mark(), anchor);
return;
}
......@@ -67,7 +64,7 @@ namespace YAML
anchor = am.LookupAnchor(node);
}
switch(node.type()) {
switch (node.type()) {
case NodeType::Undefined:
break;
case NodeType::Null:
......@@ -78,24 +75,25 @@ namespace YAML
break;
case NodeType::Sequence:
handler.OnSequenceStart(Mark(), node.tag(), anchor);
for(detail::const_node_iterator it=node.begin();it!=node.end();++it)
for (detail::const_node_iterator it = node.begin(); it != node.end();
++it)
Emit(**it, handler, am);
handler.OnSequenceEnd();
break;
case NodeType::Map:
handler.OnMapStart(Mark(), node.tag(), anchor);
for(detail::const_node_iterator it=node.begin();it!=node.end();++it) {
for (detail::const_node_iterator it = node.begin(); it != node.end();
++it) {
Emit(*it->first, handler, am);
Emit(*it->second, handler, am);
}
handler.OnMapEnd();
break;
}
}
}
bool NodeEvents::IsAliased(const detail::node& node) const
{
bool NodeEvents::IsAliased(const detail::node& node) const {
RefCount::const_iterator it = m_refCount.find(node.ref());
return it != m_refCount.end() && it->second > 1;
}
}
}
#ifndef NODE_NODEEVENTS_H_62B23520_7C8E_11DE_8A39_0800200C9A66
#define NODE_NODEEVENTS_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
#if defined(_MSC_VER) || \
(defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || \
(__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4
#pragma once
#endif
......@@ -10,13 +12,11 @@
#include <map>
#include <vector>
namespace YAML
{
class EventHandler;
class Node;
namespace YAML {
class EventHandler;
class Node;
class NodeEvents
{
class NodeEvents {
public:
explicit NodeEvents(const Node& node);
......@@ -25,7 +25,7 @@ namespace YAML
private:
class AliasManager {
public:
AliasManager(): m_curAnchor(0) {}
AliasManager() : m_curAnchor(0) {}
void RegisterReference(const detail::node& node);
anchor_t LookupAnchor(const detail::node& node) const;
......@@ -41,17 +41,17 @@ namespace YAML
};
void Setup(const detail::node& node);
void Emit(const detail::node& node, EventHandler& handler, AliasManager& am) const;
void Emit(const detail::node& node, EventHandler& handler,
AliasManager& am) const;
bool IsAliased(const detail::node& node) const;
private:
detail::shared_memory_holder m_pMemory;
detail::node* m_root;
typedef std::map<const detail::node_ref *, int> RefCount;
typedef std::map<const detail::node_ref*, int> RefCount;
RefCount m_refCount;
};
};
}
#endif // NODE_NODEEVENTS_H_62B23520_7C8E_11DE_8A39_0800200C9A66
......@@ -2,55 +2,52 @@
#include <cstring>
#include <iostream>
namespace YAML
{
ostream_wrapper::ostream_wrapper(): m_buffer(1), m_pStream(0), m_pos(0), m_row(0), m_col(0), m_comment(false)
{
}
ostream_wrapper::ostream_wrapper(std::ostream& stream): m_pStream(&stream), m_pos(0), m_row(0), m_col(0), m_comment(false)
{
}
ostream_wrapper::~ostream_wrapper()
{
}
void ostream_wrapper::write(const std::string& str)
{
if(m_pStream) {
namespace YAML {
ostream_wrapper::ostream_wrapper()
: m_buffer(1),
m_pStream(0),
m_pos(0),
m_row(0),
m_col(0),
m_comment(false) {}
ostream_wrapper::ostream_wrapper(std::ostream& stream)
: m_pStream(&stream), m_pos(0), m_row(0), m_col(0), m_comment(false) {}
ostream_wrapper::~ostream_wrapper() {}
void ostream_wrapper::write(const std::string& str) {
if (m_pStream) {
m_pStream->write(str.c_str(), str.size());
} else {
m_buffer.resize(std::max(m_buffer.size(), m_pos + str.size() + 1));
std::copy(str.begin(), str.end(), &m_buffer[m_pos]);
}
for(std::size_t i=0;i<str.size();i++)
for (std::size_t i = 0; i < str.size(); i++)
update_pos(str[i]);
}
}
void ostream_wrapper::write(const char *str, std::size_t size)
{
if(m_pStream) {
void ostream_wrapper::write(const char* str, std::size_t size) {
if (m_pStream) {
m_pStream->write(str, size);
} else {
m_buffer.resize(std::max(m_buffer.size(), m_pos + size + 1));
std::copy(str, str + size, &m_buffer[m_pos]);
}
for(std::size_t i=0;i<size;i++)
for (std::size_t i = 0; i < size; i++)
update_pos(str[i]);
}
}
void ostream_wrapper::update_pos(char ch)
{
void ostream_wrapper::update_pos(char ch) {
m_pos++;
m_col++;
if(ch == '\n') {
if (ch == '\n') {
m_row++;
m_col = 0;
m_comment = false;
}
}
}
}
......@@ -7,62 +7,61 @@
#include <fstream>
#include <sstream>
namespace YAML
{
Node Load(const std::string& input) {
namespace YAML {
Node Load(const std::string& input) {
std::stringstream stream(input);
return Load(stream);
}
}
Node Load(const char *input) {
Node Load(const char* input) {
std::stringstream stream(input);
return Load(stream);
}
}
Node Load(std::istream& input) {
Node Load(std::istream& input) {
Parser parser(input);
NodeBuilder builder;
if(!parser.HandleNextDocument(builder))
if (!parser.HandleNextDocument(builder))
return Node();
return builder.Root();
}
}
Node LoadFile(const std::string& filename) {
Node LoadFile(const std::string& filename) {
std::ifstream fin(filename.c_str());
if(!fin)
if (!fin)
throw BadFile();
return Load(fin);
}
}
std::vector<Node> LoadAll(const std::string& input) {
std::vector<Node> LoadAll(const std::string& input) {
std::stringstream stream(input);
return LoadAll(stream);
}
}
std::vector<Node> LoadAll(const char *input) {
std::vector<Node> LoadAll(const char* input) {
std::stringstream stream(input);
return LoadAll(stream);
}
}
std::vector<Node> LoadAll(std::istream& input) {
std::vector<Node> LoadAll(std::istream& input) {
std::vector<Node> docs;
Parser parser(input);
while(1) {
while (1) {
NodeBuilder builder;
if(!parser.HandleNextDocument(builder))
if (!parser.HandleNextDocument(builder))
break;
docs.push_back(builder.Root());
}
return docs;
}
}
std::vector<Node> LoadAllFromFile(const std::string& filename) {
std::vector<Node> LoadAllFromFile(const std::string& filename) {
std::ifstream fin(filename.c_str());
if(!fin)
if (!fin)
throw BadFile();
return LoadAll(fin);
}
}
}
This diff is collapsed.
This diff is collapsed.
#include "parsertests.h"
namespace Test {
bool RunParserTests()
{
return true;
}
bool RunParserTests() { return true; }
}
This diff is collapsed.
#ifndef NODETESTS_H_62B23520_7C8E_11DE_8A39_0800200C9A66
#define NODETESTS_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
#if defined(_MSC_VER) || \
(defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || \
(__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4
#pragma once
#endif
namespace Test {
bool RunNodeTests();
bool RunNodeTests();
}
#endif // NODETESTS_H_62B23520_7C8E_11DE_8A39_0800200C9A6666
......@@ -9,25 +9,22 @@
#include <vector>
#include <iostream>
namespace Test
{
void RunAll()
{
namespace Test {
void RunAll() {
bool passed = true;
if(!RunParserTests())
if (!RunParserTests())
passed = false;
if(!RunEmitterTests())
if (!RunEmitterTests())
passed = false;
if(!RunSpecTests())
if (!RunSpecTests())
passed = false;
if(!RunNodeTests())
if (!RunNodeTests())
passed = false;
if(passed)
if (passed)
std::cout << "All tests passed!\n";
}
}
}
......@@ -9,7 +9,7 @@ struct Params {
std::string fileName;
};
Params ParseArgs(int argc, char **argv) {
Params ParseArgs(int argc, char** argv) {
Params p;
std::vector<std::string> args(argv + 1, argv + argc);
......@@ -17,38 +17,39 @@ Params ParseArgs(int argc, char **argv) {
return p;
}
class NullEventHandler: public YAML::EventHandler
{
public:
class NullEventHandler : public YAML::EventHandler {
public:
virtual void OnDocumentStart(const YAML::Mark&) {}
virtual void OnDocumentEnd() {}
virtual void OnNull(const YAML::Mark&, YAML::anchor_t) {}
virtual void OnAlias(const YAML::Mark&, YAML::anchor_t) {}
virtual void OnScalar(const YAML::Mark&, const std::string&, YAML::anchor_t, const std::string&) {}
virtual void OnScalar(const YAML::Mark&, const std::string&, YAML::anchor_t,
const std::string&) {}
virtual void OnSequenceStart(const YAML::Mark&, const std::string&, YAML::anchor_t) {}
virtual void OnSequenceStart(const YAML::Mark&, const std::string&,
YAML::anchor_t) {}
virtual void OnSequenceEnd() {}
virtual void OnMapStart(const YAML::Mark&, const std::string&, YAML::anchor_t) {}
virtual void OnMapStart(const YAML::Mark&, const std::string&,
YAML::anchor_t) {}
virtual void OnMapEnd() {}
};
void parse(std::istream& input)
{
void parse(std::istream& input) {
try {
YAML::Node doc = YAML::Load(input);
std::cout << doc << "\n";
} catch(const YAML::Exception& e) {
}
catch (const YAML::Exception& e) {
std::cerr << e.what() << "\n";
}
}
int main(int argc, char **argv)
{
int main(int argc, char** argv) {
Params p = ParseArgs(argc, argv);
if(argc > 1) {
if (argc > 1) {
std::ifstream fin;
fin.open(argv[1]);
parse(fin);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment