From 01eb370300aecb0ddfd414ad709e1b7b0b6db44b Mon Sep 17 00:00:00 2001 From: Jesse Beder Date: Sat, 21 Jan 2012 02:01:37 -0600 Subject: [PATCH] Added operator >> overload for Binary --- include/yaml-cpp/binary.h | 21 +++++++++++++++++++++ src/binary.cpp | 9 +++++++++ test/old-api/parsertests.cpp | 16 ++++++++++++++++ 3 files changed, 46 insertions(+) diff --git a/include/yaml-cpp/binary.h b/include/yaml-cpp/binary.h index 243a284..8504ebe 100644 --- a/include/yaml-cpp/binary.h +++ b/include/yaml-cpp/binary.h @@ -10,6 +10,8 @@ namespace YAML { + class Node; + std::string EncodeBase64(const unsigned char *data, std::size_t size); std::vector DecodeBase64(const std::string& input); @@ -35,11 +37,30 @@ 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; std::size_t m_unownedSize; }; + + void operator >> (const Node& node, Binary& binary); } #endif // BASE64_H_62B23520_7C8E_11DE_8A39_0800200C9A66 diff --git a/src/binary.cpp b/src/binary.cpp index 62a6032..589eb08 100644 --- a/src/binary.cpp +++ b/src/binary.cpp @@ -1,4 +1,5 @@ #include "yaml-cpp/binary.h" +#include "yaml-cpp/node.h" namespace YAML { @@ -90,4 +91,12 @@ namespace YAML ret.resize(out - &ret[0]); return ret; } + + void operator >> (const Node& node, Binary& binary) + { + std::string scalar; + node.GetScalar(scalar); + std::vector data = DecodeBase64(scalar); + binary.swap(data); + } } diff --git a/test/old-api/parsertests.cpp b/test/old-api/parsertests.cpp index 7145e15..de7f123 100644 --- a/test/old-api/parsertests.cpp +++ b/test/old-api/parsertests.cpp @@ -920,6 +920,21 @@ namespace Test return false; } + + bool Binary() + { + std::string input = "[!!binary \"SGVsbG8sIFdvcmxkIQ==\", !!binary \"TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGludWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4K\"]"; + std::stringstream stream(input); + YAML::Parser parser(stream); + YAML::Node doc; + parser.GetNextDocument(doc); + + if(doc[0].to() != YAML::Binary(reinterpret_cast("Hello, World!"), 13)) + return false; + if(doc[1].to() != 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 false; + return true; + } } namespace { @@ -1202,6 +1217,7 @@ namespace Test RunParserTest(&Parser::SingleChar, "single char", passed, total); RunParserTest(&Parser::QuotedNewline, "quoted newline", passed, total); RunParserTest(&Parser::DoubleAsInt, "double as int", passed, total); + RunParserTest(&Parser::Binary, "binary", passed, total); RunEncodingTest(&EncodeToUtf8, false, "UTF-8, no BOM", passed, total); RunEncodingTest(&EncodeToUtf8, true, "UTF-8 with BOM", passed, total);