From 2731f862a5d5ca0251056d10eac2ba58803a3460 Mon Sep 17 00:00:00 2001 From: beder Date: Sat, 21 Jan 2012 01:54:54 -0600 Subject: [PATCH] Added convert<> specialization for Binary --- include/yaml-cpp/binary.h | 13 +++++++++++++ include/yaml-cpp/node/convert.h | 21 +++++++++++++++++++++ test/new-api/nodetests.cpp | 9 +++++++++ 3 files changed, 43 insertions(+) diff --git a/include/yaml-cpp/binary.h b/include/yaml-cpp/binary.h index 243a284..28a2bb1 100644 --- a/include/yaml-cpp/binary.h +++ b/include/yaml-cpp/binary.h @@ -35,6 +35,19 @@ namespace YAML } } + bool operator == (const Binary& rhs) const { + const std::size_t s = size(); + if(s != rhs.size()) + return false; + const unsigned char *d1 = data(); + const unsigned char *d2 = rhs.data(); + for(std::size_t i=0;i m_data; const unsigned char *m_unownedData; diff --git a/include/yaml-cpp/node/convert.h b/include/yaml-cpp/node/convert.h index 420a935..8ab9736 100644 --- a/include/yaml-cpp/node/convert.h +++ b/include/yaml-cpp/node/convert.h @@ -6,6 +6,7 @@ #endif +#include "yaml-cpp/binary.h" #include "yaml-cpp/node/node.h" #include "yaml-cpp/node/iterator.h" #include "yaml-cpp/null.h" @@ -183,6 +184,26 @@ namespace YAML return true; } }; + + // binary + template<> + struct convert { + static Node encode(const Binary& rhs) { + return Node(EncodeBase64(rhs.data(), rhs.size())); + } + + static bool decode(const Node& node, Binary& rhs) { + if(!node.IsScalar()) + return false; + + std::vector data = DecodeBase64(node.Scalar()); + if(data.empty() && !node.Scalar().empty()) + return false; + + rhs.swap(data); + return true; + } + }; } #endif // NODE_CONVERT_H_62B23520_7C8E_11DE_8A39_0800200C9A66 diff --git a/test/new-api/nodetests.cpp b/test/new-api/nodetests.cpp index 7039482..c4781b3 100644 --- a/test/new-api/nodetests.cpp +++ b/test/new-api/nodetests.cpp @@ -316,6 +316,14 @@ namespace Test YAML_ASSERT(node[6].as() == 13); return true; } + + TEST Binary() + { + YAML::Node node = YAML::Load("[!!binary \"SGVsbG8sIFdvcmxkIQ==\", !!binary \"TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGludWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4K\"]"); + YAML_ASSERT(node[0].as() == YAML::Binary(reinterpret_cast("Hello, World!"), 13)); + YAML_ASSERT(node[1].as() == YAML::Binary(reinterpret_cast("Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure.\n"), 270)); + return true; + } } void RunNodeTest(TEST (*test)(), const std::string& name, int& passed, int& total) { @@ -364,6 +372,7 @@ namespace Test RunNodeTest(&Node::Reassign, "reassign", passed, total); RunNodeTest(&Node::FallbackValues, "fallback values", passed, total); RunNodeTest(&Node::NumericConversion, "numeric conversion", passed, total); + RunNodeTest(&Node::Binary, "binary", passed, total); std::cout << "Node tests: " << passed << "/" << total << " passed\n"; return passed == total;