mirror of
https://github.com/jbeder/yaml-cpp.git
synced 2025-09-09 12:41:17 +00:00
Implemented binary emitting without the binary tag
This commit is contained in:
@@ -724,5 +724,19 @@ namespace YAML
|
||||
PostAtomicWrite();
|
||||
return *this;
|
||||
}
|
||||
|
||||
Emitter& Emitter::Write(const _Binary& binary)
|
||||
{
|
||||
if(!good())
|
||||
return *this;
|
||||
|
||||
// TODO: write tag !!binary
|
||||
|
||||
PreAtomicWrite();
|
||||
EmitSeparationIfNecessary();
|
||||
Utils::WriteBinary(m_stream, binary.data, binary.size);
|
||||
PostAtomicWrite();
|
||||
return *this;
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -313,6 +313,43 @@ namespace YAML
|
||||
out << ">";
|
||||
return true;
|
||||
}
|
||||
|
||||
bool WriteBinary(ostream& out, const char *data, std::size_t size)
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -19,6 +19,7 @@ namespace YAML
|
||||
bool WriteAlias(ostream& out, const std::string& str);
|
||||
bool WriteAnchor(ostream& out, const std::string& str);
|
||||
bool WriteTag(ostream& out, const std::string& str, bool verbatim);
|
||||
bool WriteBinary(ostream& out, const char *data, std::size_t size);
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user