Refactored the base64 binary to its own space with a unified class that (will) be used for parsing (in addition to emitting)

This commit is contained in:
Jesse Beder
2012-01-21 01:18:37 -06:00
parent e145488547
commit 6105d4cfeb
7 changed files with 148 additions and 49 deletions

View File

@@ -1,6 +1,7 @@
#include "emitterutils.h"
#include "exp.h"
#include "indentation.h"
#include "yaml-cpp/binary.h"
#include "yaml-cpp/exceptions.h"
#include "stringsource.h"
#include <sstream>
@@ -367,41 +368,10 @@ namespace YAML
return true;
}
bool WriteBinary(ostream& out, const unsigned char *data, std::size_t size)
bool WriteBinary(ostream& out, const Binary& binary)
{
static const char encoding[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
const char PAD = '=';
out << "\"";
std::size_t chunks = size / 3;
std::size_t remainder = size % 3;
for(std::size_t i=0;i<chunks;i++, data += 3) {
out << encoding[data[0] >> 2];
out << encoding[((data[0] & 0x3) << 4) | (data[1] >> 4)];
out << encoding[((data[1] & 0xf) << 2) | (data[2] >> 6)];
out << encoding[data[2] & 0x3f];
}
switch(remainder) {
case 0:
break;
case 1:
out << encoding[data[0] >> 2];
out << encoding[((data[0] & 0x3) << 4)];
out << PAD;
out << PAD;
break;
case 2:
out << encoding[data[0] >> 2];
out << encoding[((data[0] & 0x3) << 4) | (data[1] >> 4)];
out << encoding[((data[1] & 0xf) << 2)];
out << PAD;
break;
}
out << "\"";
return true;
WriteBase64(out, binary.data(), binary.size());
return true;
}
}
}