mirror of
https://github.com/jbeder/yaml-cpp.git
synced 2025-09-09 12:41:17 +00:00
Added convert<> specialization for Binary
This commit is contained in:
@@ -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<s;i++) {
|
||||||
|
if(*d1++ != *d2++)
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::vector<unsigned char> m_data;
|
std::vector<unsigned char> m_data;
|
||||||
const unsigned char *m_unownedData;
|
const unsigned char *m_unownedData;
|
||||||
|
@@ -6,6 +6,7 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
#include "yaml-cpp/binary.h"
|
||||||
#include "yaml-cpp/node/node.h"
|
#include "yaml-cpp/node/node.h"
|
||||||
#include "yaml-cpp/node/iterator.h"
|
#include "yaml-cpp/node/iterator.h"
|
||||||
#include "yaml-cpp/null.h"
|
#include "yaml-cpp/null.h"
|
||||||
@@ -183,6 +184,26 @@ namespace YAML
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// binary
|
||||||
|
template<>
|
||||||
|
struct convert<Binary> {
|
||||||
|
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<unsigned char> 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
|
#endif // NODE_CONVERT_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||||
|
@@ -316,6 +316,14 @@ namespace Test
|
|||||||
YAML_ASSERT(node[6].as<int>() == 13);
|
YAML_ASSERT(node[6].as<int>() == 13);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST Binary()
|
||||||
|
{
|
||||||
|
YAML::Node node = YAML::Load("[!!binary \"SGVsbG8sIFdvcmxkIQ==\", !!binary \"TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGludWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4K\"]");
|
||||||
|
YAML_ASSERT(node[0].as<YAML::Binary>() == YAML::Binary(reinterpret_cast<const unsigned char*>("Hello, World!"), 13));
|
||||||
|
YAML_ASSERT(node[1].as<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 true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void RunNodeTest(TEST (*test)(), const std::string& name, int& passed, int& total) {
|
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::Reassign, "reassign", passed, total);
|
||||||
RunNodeTest(&Node::FallbackValues, "fallback values", passed, total);
|
RunNodeTest(&Node::FallbackValues, "fallback values", passed, total);
|
||||||
RunNodeTest(&Node::NumericConversion, "numeric conversion", passed, total);
|
RunNodeTest(&Node::NumericConversion, "numeric conversion", passed, total);
|
||||||
|
RunNodeTest(&Node::Binary, "binary", passed, total);
|
||||||
|
|
||||||
std::cout << "Node tests: " << passed << "/" << total << " passed\n";
|
std::cout << "Node tests: " << passed << "/" << total << " passed\n";
|
||||||
return passed == total;
|
return passed == total;
|
||||||
|
Reference in New Issue
Block a user