Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
#include "Alignment/Geners/interface/IOException.hh"
#include <cassert>
#include <climits>
#include <cstring>

#include "Alignment/Geners/interface/CharBuffer.hh"
#include "Alignment/Geners/interface/binaryIO.hh"

namespace gs {
  bool CharBuffer::write(std::ostream &os) const {
    unsigned long long tmp = 0ULL;
    const char *buf = buf_.getPutBuffer(&tmp);
    write_pod(os, tmp);
    os.write(buf, tmp);
    return !os.fail();
  }

  unsigned long CharBuffer::size() const {
    unsigned long long tmp = 0ULL;
    buf_.getPutBuffer(&tmp);
    if (tmp > ULONG_MAX)
      throw gs::IOLengthError("In CharBuffer::size: buffer is too large");
    return static_cast<unsigned long>(tmp);
  }

  void CharBuffer::restore(const ClassId &id, std::istream &in, CharBuffer *buf) {
    static const ClassId current(ClassId::makeId<CharBuffer>());

    assert(buf);
    current.ensureSameId(id);

    unsigned long long tmp = 0ULL;
    read_pod(in, &tmp);
    buf->clear();
    buf->seekp(0);

    const unsigned locLen = 4096;
    char local[locLen];
    std::streambuf *inbuf = in.rdbuf();
    std::streambuf *outbuf = buf->rdbuf();

    while (tmp > locLen) {
      inbuf->sgetn(local, locLen);
      outbuf->sputn(local, locLen);
      tmp -= locLen;
    }
    if (tmp) {
      inbuf->sgetn(local, tmp);
      outbuf->sputn(local, tmp);
    }
    if (in.fail())
      throw IOReadFailure("In gs::CharBuffer::restore: input stream failure");
    if (buf->fail())
      throw IOWriteFailure("In gs::CharBuffer::restore: buffer stream failure");
  }

  bool CharBuffer::operator==(const CharBuffer &r) const {
    unsigned long long tmp = 0ULL;
    const char *buf = buf_.getPutBuffer(&tmp);
    unsigned long long tmp2 = 0ULL;
    const char *buf2 = r.buf_.getPutBuffer(&tmp2);
    if (tmp != tmp2)
      return false;
    return memcmp(buf, buf2, tmp) == 0;
  }
}  // namespace gs