Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:44:55

0001 #include "Alignment/Geners/interface/IOException.hh"
0002 #include <cassert>
0003 #include <climits>
0004 #include <cstring>
0005 
0006 #include "Alignment/Geners/interface/CharBuffer.hh"
0007 #include "Alignment/Geners/interface/binaryIO.hh"
0008 
0009 namespace gs {
0010   bool CharBuffer::write(std::ostream &os) const {
0011     unsigned long long tmp = 0ULL;
0012     const char *buf = buf_.getPutBuffer(&tmp);
0013     write_pod(os, tmp);
0014     os.write(buf, tmp);
0015     return !os.fail();
0016   }
0017 
0018   unsigned long CharBuffer::size() const {
0019     unsigned long long tmp = 0ULL;
0020     buf_.getPutBuffer(&tmp);
0021     if (tmp > ULONG_MAX)
0022       throw gs::IOLengthError("In CharBuffer::size: buffer is too large");
0023     return static_cast<unsigned long>(tmp);
0024   }
0025 
0026   void CharBuffer::restore(const ClassId &id, std::istream &in, CharBuffer *buf) {
0027     static const ClassId current(ClassId::makeId<CharBuffer>());
0028 
0029     assert(buf);
0030     current.ensureSameId(id);
0031 
0032     unsigned long long tmp = 0ULL;
0033     read_pod(in, &tmp);
0034     buf->clear();
0035     buf->seekp(0);
0036 
0037     const unsigned locLen = 4096;
0038     char local[locLen];
0039     std::streambuf *inbuf = in.rdbuf();
0040     std::streambuf *outbuf = buf->rdbuf();
0041 
0042     while (tmp > locLen) {
0043       inbuf->sgetn(local, locLen);
0044       outbuf->sputn(local, locLen);
0045       tmp -= locLen;
0046     }
0047     if (tmp) {
0048       inbuf->sgetn(local, tmp);
0049       outbuf->sputn(local, tmp);
0050     }
0051     if (in.fail())
0052       throw IOReadFailure("In gs::CharBuffer::restore: input stream failure");
0053     if (buf->fail())
0054       throw IOWriteFailure("In gs::CharBuffer::restore: buffer stream failure");
0055   }
0056 
0057   bool CharBuffer::operator==(const CharBuffer &r) const {
0058     unsigned long long tmp = 0ULL;
0059     const char *buf = buf_.getPutBuffer(&tmp);
0060     unsigned long long tmp2 = 0ULL;
0061     const char *buf2 = r.buf_.getPutBuffer(&tmp2);
0062     if (tmp != tmp2)
0063       return false;
0064     return memcmp(buf, buf2, tmp) == 0;
0065   }
0066 }  // namespace gs