Implemented binary emitting without the binary tag

This commit is contained in:
Jesse Beder
2010-10-28 21:53:54 +00:00
parent f1697dea15
commit d508203ed8
6 changed files with 85 additions and 0 deletions

View File

@@ -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;
}
}

View File

@@ -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;
}
}
}

View File

@@ -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);
}
}