File indexing completed on 2025-08-03 08:19:50
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029 #include <gzstream.h>
0030 #include <iostream>
0031 #include <string.h> // for memcpy
0032
0033 #ifdef GZSTREAM_NAMESPACE
0034 namespace GZSTREAM_NAMESPACE {
0035 #endif
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045 gzstreambuf* gzstreambuf::open( const char* name, int open_mode) {
0046 if ( is_open())
0047 return (gzstreambuf*)0;
0048 mode = open_mode;
0049
0050 if ((mode & std::ios::ate) || (mode & std::ios::app)
0051 || ((mode & std::ios::in) && (mode & std::ios::out)))
0052 return (gzstreambuf*)0;
0053 char fmode[10];
0054 char* fmodeptr = fmode;
0055 if ( mode & std::ios::in)
0056 *fmodeptr++ = 'r';
0057 else if ( mode & std::ios::out)
0058 *fmodeptr++ = 'w';
0059 *fmodeptr++ = 'b';
0060 *fmodeptr = '\0';
0061 file = gzopen( name, fmode);
0062 if (file == 0)
0063 return (gzstreambuf*)0;
0064 opened = 1;
0065 return this;
0066 }
0067
0068 gzstreambuf * gzstreambuf::close() {
0069 if ( is_open()) {
0070 sync();
0071 opened = 0;
0072 if ( gzclose( file) == Z_OK)
0073 return this;
0074 }
0075 return (gzstreambuf*)0;
0076 }
0077
0078 int gzstreambuf::underflow() {
0079 if ( gptr() && ( gptr() < egptr()))
0080 return * reinterpret_cast<unsigned char *>( gptr());
0081
0082 if ( ! (mode & std::ios::in) || ! opened)
0083 return EOF;
0084
0085 int n_putback = gptr() - eback();
0086 if ( n_putback > 4)
0087 n_putback = 4;
0088 memcpy( buffer + (4 - n_putback), gptr() - n_putback, n_putback);
0089
0090 int num = gzread( file, buffer+4, bufferSize-4);
0091 if (num <= 0)
0092 return EOF;
0093
0094
0095 setg( buffer + (4 - n_putback),
0096 buffer + 4,
0097 buffer + 4 + num);
0098
0099
0100 return * reinterpret_cast<unsigned char *>( gptr());
0101 }
0102
0103 int gzstreambuf::flush_buffer() {
0104
0105
0106 int w = pptr() - pbase();
0107 if ( gzwrite( file, pbase(), w) != w)
0108 return EOF;
0109 pbump( -w);
0110 return w;
0111 }
0112
0113 int gzstreambuf::overflow( int c) {
0114 if ( ! ( mode & std::ios::out) || ! opened)
0115 return EOF;
0116 if (c != EOF) {
0117 *pptr() = c;
0118 pbump(1);
0119 }
0120 if ( flush_buffer() == EOF)
0121 return EOF;
0122 return c;
0123 }
0124
0125 int gzstreambuf::sync() {
0126
0127
0128
0129 if ( pptr() && pptr() > pbase()) {
0130 if ( flush_buffer() == EOF)
0131 return -1;
0132 }
0133 return 0;
0134 }
0135
0136
0137
0138
0139
0140 gzstreambase::gzstreambase( const char* name, int mode) {
0141 init( &buf);
0142 open( name, mode);
0143 }
0144
0145 gzstreambase::~gzstreambase() {
0146 buf.close();
0147 }
0148
0149 void gzstreambase::open( const char* name, int open_mode) {
0150 if ( ! buf.open( name, open_mode))
0151 clear( rdstate() | std::ios::badbit);
0152 }
0153
0154 void gzstreambase::close() {
0155 if ( buf.is_open())
0156 if ( ! buf.close())
0157 clear( rdstate() | std::ios::badbit);
0158 }
0159
0160 #ifdef GZSTREAM_NAMESPACE
0161 }
0162 #endif
0163
0164
0165