123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- #include <core/basics/gzstream.h>
- #ifdef NICE_USELIB_ZLIB
- #include <iostream>
- #include <string.h> // for memcpy
- namespace NICE {
- gzstreambuf* gzstreambuf::open( const char* name, int open_mode) {
- if ( is_open())
- return (gzstreambuf*)0;
- mode = open_mode;
-
- if ((mode & std::ios::ate) || (mode & std::ios::app)
- || ((mode & std::ios::in) && (mode & std::ios::out)))
- return (gzstreambuf*)0;
- char fmode[10];
- char* fmodeptr = fmode;
- if ( mode & std::ios::in)
- *fmodeptr++ = 'r';
- else if ( mode & std::ios::out)
- *fmodeptr++ = 'w';
- *fmodeptr++ = 'b';
- *fmodeptr = '\0';
- file = 0;
- file = gzopen( name, fmode);
- if (file == 0)
- return (gzstreambuf*)0;
- opened = 1;
- return this;
- }
- gzstreambuf * gzstreambuf::close() {
- if ( is_open()) {
- sync();
- opened = 0;
- if ( gzclose( file) == Z_OK)
- return this;
- }
- return (gzstreambuf*)0;
- }
- int gzstreambuf::underflow() {
- if ( gptr() && ( gptr() < egptr()))
- return * reinterpret_cast<unsigned char *>( gptr());
- if ( ! (mode & std::ios::in) || ! opened)
- return EOF;
-
- int n_putback = gptr() - eback();
- if ( n_putback > 4)
- n_putback = 4;
- memcpy( buffer + (4 - n_putback), gptr() - n_putback, n_putback);
- int num = 0;
- num = gzread( file, buffer+4, bufferSize-4);
- if (num <= 0)
- return EOF;
-
- setg( buffer + (4 - n_putback),
- buffer + 4,
- buffer + 4 + num);
-
- return * reinterpret_cast<unsigned char *>( gptr());
- }
- int gzstreambuf::flush_buffer() {
-
-
- int w = pptr() - pbase();
- if ( gzwrite( file, pbase(), w) != w)
- return EOF;
- pbump( -w);
- return w;
- }
- int gzstreambuf::overflow( int c) {
- if ( ! ( mode & std::ios::out) || ! opened)
- return EOF;
- if (c != EOF) {
- *pptr() = c;
- pbump(1);
- }
- if ( flush_buffer() == EOF)
- return EOF;
- return c;
- }
- int gzstreambuf::sync() {
-
-
-
- if ( pptr() && pptr() > pbase()) {
- if ( flush_buffer() == EOF)
- return -1;
- }
- return 0;
- }
- gzstreambase::gzstreambase( const char* name, int mode) {
- init( &buf);
- open( name, mode);
- }
- gzstreambase::~gzstreambase() {
- buf.close();
- }
- void gzstreambase::open( const char* name, int open_mode) {
- if ( ! buf.open( name, open_mode))
- clear( rdstate() | std::ios::badbit);
- }
- void gzstreambase::close() {
- if ( buf.is_open())
- if ( ! buf.close())
- clear( rdstate() | std::ios::badbit);
- }
- }
- #else
- #include "CrossplatformDefines.h"
- #pragma message NICE_WARNING("gzstream will not be compiled.")
- #endif
|