00001
00002
00003
00004
00005
00006
00007
00008
00009 static const char rcsid[] __attribute__((used)) = "$Id: Binary.cpp 1197 2014-10-14 22:26:11Z sella $";
00010
00011 #include "Binary.h"
00012
00013 using namespace sella::variant;
00014
00015 const boost::regex Binary::Pattern("^0x([0-9A-Fa-f]{2})*$");
00016
00017 Binary::Binary() throw () : Nullable() { }
00018
00019 Binary::Binary(const std::string &value) throw (TypeCastException) : Nullable() {
00020 if (boost::regex_match(value, Binary::Pattern)) {
00021 short unsigned int v;
00022 char buf[3] = { 0, 0, 0 };
00023 this->value = std::make_shared<std::vector<uint8_t>>();
00024
00025 for (auto it = value.begin() + 2; it != value.end(); ++it, ++it) {
00026 buf[0] = *(it + 0);
00027 buf[1] = *(it + 1);
00028
00029 sscanf(buf, "%hx", &v);
00030
00031 (*std::static_pointer_cast<std::vector<uint8_t>>(this->value)).push_back((uint8_t) v);
00032 }
00033 } else {
00034 THROW(TypeCastException, "unable to cast '%s' to a binary value", value.c_str());
00035 }
00036 }
00037
00038 Binary::Binary(const std::vector<uint8_t> &value) throw () : Nullable(std::make_shared<std::vector<uint8_t>>(value)) { }
00039
00040 Binary::Binary(const Binary &other) throw () : Nullable(other) {
00041 if (!other.isNull()) {
00042 this->value = std::make_shared<std::vector<uint8_t>>(*other.getBinary());
00043 }
00044 }
00045
00046 bool Binary::operator==(const Binary &other) const throw () {
00047 return (*std::static_pointer_cast<std::vector<uint8_t>>(this->value) == *std::static_pointer_cast<std::vector<uint8_t>>(other.value));
00048 }
00049
00050 bool Binary::operator!=(const Binary &other) const throw () {
00051 return !(*this == other);
00052 }
00053
00054 Binary& Binary::operator=(const Binary &other) throw () {
00055 if (this != &other) {
00056 Nullable::operator=(other);
00057 if (!other.isNull()) {
00058 this->value = std::make_shared<std::vector<uint8_t>>(*other.getBinary());
00059 }
00060 }
00061
00062 return *this;
00063 }
00064
00065 Binary::~Binary() throw () { }
00066
00067 const std::vector<uint8_t>* Binary::getBinary() const throw (UnsupportedOperatorException) {
00068 if (isNull()) {
00069 THROW(UnsupportedOperatorException, "unable to take reference of null value");
00070 }
00071
00072 return &(*std::static_pointer_cast<std::vector<uint8_t>>(this->value));
00073 }
00074
00075 size_t Binary::size() const throw () {
00076 if (isNull()) {
00077 return -1;
00078 }
00079
00080 return getBinary()->size();
00081 }
00082
00083 bool Binary::empty() const throw () {
00084 if (isNull()) {
00085 return true;
00086 }
00087
00088 return getBinary()->empty();
00089 }
00090
00091 const std::string& Binary::str() const throw () {
00092 if (cache.empty()) {
00093 if (this->isNull()) {
00094 cache = "null";
00095 } else {
00096 char buffer[32];
00097
00098 cache = "0x";
00099 const std::vector<uint8_t> *data = this->getBinary();
00100
00101 for (auto it = data->begin(); it != data->end(); ++it) {
00102 snprintf(buffer, sizeof(buffer), "%02x", *it);
00103 cache.append(buffer);
00104 }
00105 }
00106 }
00107
00108 return cache;
00109 }
00110
00111
00112
00113