template <typename T> std::string rdwr_readString(T fd, const char *file, int line) { int len; static char *buf = 0; static int bufLen = 0; rdwr_tryRead(fd, &len, sizeof(int), file, line); if (len + 1 > bufLen) { delete buf; buf = new char[len + 1]; bufLen = len + 1; } rdwr_tryRead(fd, buf, len, file, line); buf[len] = '\0'; return std::string(buf); }
template <typename T> int rdwr_readInt(T fd, const char *file, int line) { int i = 0; rdwr_tryRead(fd, &i, sizeof(int), file, line); return i; }
template <typename T> float rdwr_readFloat(T fd, const char *file, int line) { float f = 0; rdwr_tryRead(fd, &f, sizeof(float), file, line); return f; }
template <typename T> std::vector<char> rdwr_readRaw(T fd, const char *file, int line) { int complen, len; static char *rawbuf = 0; static int bufLen = 0; rdwr_tryRead(fd, &complen, sizeof(int), file, line); rdwr_tryRead(fd, &len, sizeof(int), file, line); if (complen > bufLen) { delete rawbuf; rawbuf = new char[complen]; bufLen = complen; } rdwr_tryRead(fd, rawbuf, complen, file, line); char *uncompressed = new char [len]; if(!uncompressed) { fprintf(stderr, "Failed to allocate %d bytes of memory at %s:%d\n", len, file, line); throw RemotePluginClosedException(); } unsigned long destlen = len; if(uncompress((Bytef *)uncompressed, &destlen, (Bytef *)rawbuf, complen) != Z_OK) { delete uncompressed; fprintf(stderr, "Failed to uncompress source buffer at %s:%d\n", file, line); throw RemotePluginClosedException(); } fprintf(stderr, "uncompressed source buffer. size=%lu bytes, complen=%d\n", destlen, complen); std::vector<char> rawout; for(unsigned long i = 0; i < destlen; i++) { rawout.push_back(uncompressed [i]); } delete uncompressed; return rawout; }