#include #include "BinaryLogReader.h" std::istream &operator >>(std::istream &stream, Record::Ref &record) { record = NULL; if (!stream) return stream; char envelope[8]; stream.read(envelope, 8); if (!stream.gcount()) // End of file (or other error) before we got started. Return that // error exactly as is. return stream; if (stream.gcount() != 8) { // Error in the middle of the record. Promote this to bad. If we failed // in the middle of the record, we have no (automatic) way to resync and // find the next record. stream.setstate(std::ios::badbit); return stream; } if (memcmp(envelope, "HERE:", 5)) { // The marker wasn't found. That means we were not positioned at the // beginning of a valid record. stream.setstate(std::ios::badbit); return stream; } int length = (unsigned char)envelope[5]; length <<= 8; length |= (unsigned char)envelope[6]; length <<= 8; length |= (unsigned char)envelope[7]; std::string encoded(length, '\0'); stream.read(&encoded[0], length); if (stream.gcount() != length) { // Error in the middle of the record. Promote this to bad. stream.setstate(std::ios::badbit); return stream; } record = Record::create(encoded); if (!record) // Invalid record. As far as we know the stream is okay, and you might // be able to read the next record. stream.setstate(std::ios::failbit); return stream; }