diff options
Diffstat (limited to 'src/libutil/serialise.cc')
-rw-r--r-- | src/libutil/serialise.cc | 71 |
1 files changed, 15 insertions, 56 deletions
diff --git a/src/libutil/serialise.cc b/src/libutil/serialise.cc index 789227102864..f8e9d00c12b8 100644 --- a/src/libutil/serialise.cc +++ b/src/libutil/serialise.cc @@ -16,11 +16,11 @@ BufferedSink::~BufferedSink() delete[] buffer; } - + void BufferedSink::operator () (const unsigned char * data, size_t len) { if (!buffer) buffer = new unsigned char[bufSize]; - + while (len) { /* Optimisation: bypass the buffer if the data exceeds the buffer size. */ @@ -96,7 +96,7 @@ size_t BufferedSource::read(unsigned char * data, size_t len) if (!buffer) buffer = new unsigned char[bufSize]; if (!bufPosIn) bufPosIn = readUnbuffered(buffer, bufSize); - + /* Copy out the data in the buffer. */ size_t n = len > bufPosIn - bufPosOut ? bufPosIn - bufPosOut : len; memcpy(data, buffer + bufPosOut, n); @@ -144,79 +144,38 @@ void writePadding(size_t len, Sink & sink) } -void writeInt(unsigned int n, Sink & sink) -{ - unsigned char buf[8]; - memset(buf, 0, sizeof(buf)); - buf[0] = n & 0xff; - buf[1] = (n >> 8) & 0xff; - buf[2] = (n >> 16) & 0xff; - buf[3] = (n >> 24) & 0xff; - sink(buf, sizeof(buf)); -} - -Sink & operator << (Sink & out, unsigned int n) -{ - writeInt(n, out); - return out; -} - - -void writeLongLong(unsigned long long n, Sink & sink) -{ - unsigned char buf[8]; - buf[0] = n & 0xff; - buf[1] = (n >> 8) & 0xff; - buf[2] = (n >> 16) & 0xff; - buf[3] = (n >> 24) & 0xff; - buf[4] = (n >> 32) & 0xff; - buf[5] = (n >> 40) & 0xff; - buf[6] = (n >> 48) & 0xff; - buf[7] = (n >> 56) & 0xff; - sink(buf, sizeof(buf)); -} - - void writeString(const unsigned char * buf, size_t len, Sink & sink) { - writeInt(len, sink); + sink << len; sink(buf, len); writePadding(len, sink); } -void writeString(const string & s, Sink & sink) +Sink & operator << (Sink & sink, const string & s) { writeString((const unsigned char *) s.data(), s.size(), sink); -} - -Sink & operator << (Sink & out, const string & s) -{ - writeString(s, out); - return out; + return sink; } template<class T> void writeStrings(const T & ss, Sink & sink) { - writeInt(ss.size(), sink); + sink << ss.size(); for (auto & i : ss) - writeString(i, sink); + sink << i; } -template void writeStrings(const Paths & ss, Sink & sink); -template void writeStrings(const PathSet & ss, Sink & sink); - -Sink & operator << (Sink & out, const Strings & s) +Sink & operator << (Sink & sink, const Strings & s) { - writeStrings(s, out); - return out; + writeStrings(s, sink); + return sink; } -Sink & operator << (Sink & out, const StringSet & s) +Sink & operator << (Sink & sink, const StringSet & s) { - writeStrings(s, out); - return out; + writeStrings(s, sink); + return sink; } @@ -271,7 +230,7 @@ size_t readString(unsigned char * buf, size_t max, Source & source) return len; } - + string readString(Source & source) { size_t len = readInt(source); |