mirror of
https://github.com/jbeder/yaml-cpp.git
synced 2025-09-09 04:41:16 +00:00
Added operator >> overload for Binary
This commit is contained in:
@@ -10,6 +10,8 @@
|
|||||||
|
|
||||||
namespace YAML
|
namespace YAML
|
||||||
{
|
{
|
||||||
|
class Node;
|
||||||
|
|
||||||
std::string EncodeBase64(const unsigned char *data, std::size_t size);
|
std::string EncodeBase64(const unsigned char *data, std::size_t size);
|
||||||
std::vector<unsigned char> DecodeBase64(const std::string& input);
|
std::vector<unsigned char> 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<s;i++) {
|
||||||
|
if(*d1++ != *d2++)
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool operator != (const Binary& rhs) const {
|
||||||
|
return !(*this == rhs);
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::vector<unsigned char> m_data;
|
std::vector<unsigned char> m_data;
|
||||||
const unsigned char *m_unownedData;
|
const unsigned char *m_unownedData;
|
||||||
std::size_t m_unownedSize;
|
std::size_t m_unownedSize;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
void operator >> (const Node& node, Binary& binary);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // BASE64_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
#endif // BASE64_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||||
|
@@ -1,4 +1,5 @@
|
|||||||
#include "yaml-cpp/binary.h"
|
#include "yaml-cpp/binary.h"
|
||||||
|
#include "yaml-cpp/node.h"
|
||||||
|
|
||||||
namespace YAML
|
namespace YAML
|
||||||
{
|
{
|
||||||
@@ -90,4 +91,12 @@ namespace YAML
|
|||||||
ret.resize(out - &ret[0]);
|
ret.resize(out - &ret[0]);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void operator >> (const Node& node, Binary& binary)
|
||||||
|
{
|
||||||
|
std::string scalar;
|
||||||
|
node.GetScalar(scalar);
|
||||||
|
std::vector<unsigned char> data = DecodeBase64(scalar);
|
||||||
|
binary.swap(data);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -920,6 +920,21 @@ namespace Test
|
|||||||
|
|
||||||
return false;
|
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>() != YAML::Binary(reinterpret_cast<const unsigned char*>("Hello, World!"), 13))
|
||||||
|
return false;
|
||||||
|
if(doc[1].to<YAML::Binary>() != YAML::Binary(reinterpret_cast<const unsigned char*>("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 {
|
namespace {
|
||||||
@@ -1202,6 +1217,7 @@ namespace Test
|
|||||||
RunParserTest(&Parser::SingleChar, "single char", passed, total);
|
RunParserTest(&Parser::SingleChar, "single char", passed, total);
|
||||||
RunParserTest(&Parser::QuotedNewline, "quoted newline", passed, total);
|
RunParserTest(&Parser::QuotedNewline, "quoted newline", passed, total);
|
||||||
RunParserTest(&Parser::DoubleAsInt, "double as int", 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, false, "UTF-8, no BOM", passed, total);
|
||||||
RunEncodingTest(&EncodeToUtf8, true, "UTF-8 with BOM", passed, total);
|
RunEncodingTest(&EncodeToUtf8, true, "UTF-8 with BOM", passed, total);
|
||||||
|
Reference in New Issue
Block a user