C++ (Cpp) tstream Example

Introduction

The c++ (cpp) tstream example is extracted from the most popular open source projects, you can refer to the following example for usage.

Programming language: C++ (Cpp)

Class/type: tstream

Example#1
File: gzip.cppProject: 0x0all/nanocv

        bool io_uncompress_gzip(tstream& istream, size_t num_bytes, io::data_t& data)
        {
                // zlib decompression buffers
                static const std::streamsize chunk_size = 64 * 1024;

                z_stream strm;
                unsigned char in[chunk_size];
                unsigned char out[chunk_size];

                strm.zalloc = Z_NULL;
                strm.zfree = Z_NULL;
                strm.opaque = Z_NULL;
                strm.avail_in = 0;
                strm.next_in = Z_NULL;
                if (inflateInit(&strm) != Z_OK)
                {
                        return false;
                }

                // decompress the data chunk
                while (num_bytes > 0 && istream)
                {
                        const std::streamsize to_read = (num_bytes >= chunk_size) ? chunk_size : num_bytes;
                        num_bytes -= to_read;

                        if (!istream.read(reinterpret_cast<char*>(in), to_read))
                        {
                                inflateEnd(&strm);
                                return false;
                        }

                        strm.avail_in = static_cast<uInt>(istream.gcount());
                        strm.next_in = in;

                        do
                        {
                                strm.avail_out = static_cast<uInt>(chunk_size);
                                strm.next_out = out;

                                const int ret = inflate(&strm, Z_NO_FLUSH);
                                if (ret != Z_OK && ret != Z_STREAM_END)
                                {
                                        inflateEnd(&strm);
                                        return false;
                                }

                                const std::streamsize have = chunk_size - strm.avail_out;
                                data.insert(data.end(), out, out + have);
                        }
                        while (strm.avail_out == 0);
                }

                inflateEnd(&strm);

                // OK
                return (num_bytes == std::string::npos) ? true : (num_bytes == 0);
        }

Example#2
File: fulcrum.cppProject: M-HT/fulcrum

static void loadxm(tstream &s, const char *name, int pt) {
  int len;
  void *xmdata;

  s.openfile(name);
  len = s.getint();
  xmdata = getmem(len);
  s.read(xmdata,len);
  rxmplay(xmdata,len,pt);
}