27 #include <strstream.h>
35 # define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
37 # define SET_BINARY_MODE(file)
44 size_t value()
const {
return val.word; }
46 struct Val {
unsigned char byte;
size_t word; } val;
55 izstream(FILE* fp) : m_fp(0) { open(fp); }
56 izstream(
const char* name) : m_fp(0) { open(name); }
65 void open(
const char* name) {
67 m_fp = ::gzopen(name,
"rb");
73 m_fp = ::gzdopen(fileno(fp),
"rb");
81 int r = ::gzclose(m_fp);
87 int read(
void* buf,
size_t len) {
88 return ::gzread(m_fp, buf, len);
97 const char* error(
int* errnum) {
98 return ::gzerror(m_fp, errnum);
101 gzFile fp() {
return m_fp; }
114 template <
class T,
class Items>
115 inline int read(
izstream& zs, T* x, Items items) {
116 return ::gzread(zs.fp(), x, items*
sizeof(T));
124 ::gzread(zs.fp(), &x,
sizeof(T));
129 inline zstringlen::zstringlen(
izstream& zs) {
131 if (val.byte == 255) zs > val.word;
132 else val.word = val.byte;
140 ::gzread(zs.fp(), x, len.value());
141 x[len.value()] =
'\0';
145 inline char* read_string(
izstream& zs) {
147 char* x =
new char[len.value()+1];
148 ::gzread(zs.fp(), x, len.value());
149 x[len.value()] =
'\0';
160 ozstream(FILE* fp,
int level = Z_DEFAULT_COMPRESSION)
164 ozstream(
const char* name,
int level = Z_DEFAULT_COMPRESSION)
177 void open(
const char* name,
int level = Z_DEFAULT_COMPRESSION) {
178 char mode[4] =
"wb\0";
179 if (level != Z_DEFAULT_COMPRESSION) mode[2] =
'0'+level;
181 m_fp = ::gzopen(name, mode);
186 void open(FILE* fp,
int level = Z_DEFAULT_COMPRESSION) {
188 char mode[4] =
"wb\0";
189 if (level != Z_DEFAULT_COMPRESSION) mode[2] =
'0'+level;
191 m_fp = ::gzdopen(fileno(fp), mode);
200 ::gzwrite(m_fp, m_os->str(), m_os->pcount());
201 delete[] m_os->str();
delete m_os; m_os = 0;
203 int r = ::gzclose(m_fp); m_fp = 0;
return r;
208 int write(
const void* buf,
size_t len) {
209 return ::gzwrite(m_fp, (voidp) buf, len);
219 int flush(
int _flush) {
221 return ::gzflush(m_fp, _flush);
230 const char* error(
int* errnum) {
231 return ::gzerror(m_fp, errnum);
234 gzFile fp() {
return m_fp; }
237 if (m_os == 0) m_os =
new ostrstream;
242 if (m_os && m_os->pcount()>0) {
243 ostrstream* oss =
new ostrstream;
244 oss->fill(m_os->fill());
245 oss->flags(m_os->flags());
246 oss->precision(m_os->precision());
247 oss->width(m_os->width());
248 ::gzwrite(m_fp, m_os->str(), m_os->pcount());
249 delete[] m_os->str();
delete m_os; m_os = oss;
263 template <
class T,
class Items>
264 inline int write(
ozstream& zs,
const T* x, Items items) {
265 return ::gzwrite(zs.fp(), (voidp) x, items*
sizeof(T));
273 ::gzwrite(zs.fp(), (voidp) &x,
sizeof(T));
277 inline zstringlen::zstringlen(
ozstream& zs,
const char* x) {
278 val.byte = 255; val.word = ::strlen(x);
279 if (val.word < 255) zs < (val.byte = val.word);
288 ::gzwrite(zs.fp(), (voidp) x, len.value());
294 return zs < (
const char*) x;
302 inline ostream& operator<<(
ozstream& zs,
const T& x) {
Definition: zstream.h:155