source: branches/work_320/gzstream.cpp @ 679

Revision 329, 5.2 KB checked in by jls17, 6 years ago (diff)
  • finished ResourceFileLocatorAndExtractor::ExtractTo? implementation
  • added some more error handling to UntarStream? class
  • now zlib is always needed to compile ARInside
  • added ExtractResource? method to CARInside class
  • the resources tar.gz archive is created by cmake
Line 
1// ============================================================================
2// gzstream, C++ iostream classes wrapping the zlib compression library.
3// Copyright (C) 2001  Deepak Bandyopadhyay, Lutz Kettner
4//
5// This library is free software; you can redistribute it and/or
6// modify it under the terms of the GNU Lesser General Public
7// License as published by the Free Software Foundation; either
8// version 2.1 of the License, or (at your option) any later version.
9//
10// This library is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13// Lesser General Public License for more details.
14//
15// You should have received a copy of the GNU Lesser General Public
16// License along with this library; if not, write to the Free Software
17// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18// ============================================================================
19//
20// File          : gzstream.C
21// Revision      : $Revision: 1.7 $
22// Revision_date : $Date: 2003/01/08 14:41:27 $
23// Author(s)     : Deepak Bandyopadhyay, Lutz Kettner
24//
25// Standard streambuf implementation following Nicolai Josuttis, "The
26// Standard C++ Library".
27// ============================================================================
28
29#include "stdafx.h"
30
31#include <gzstream.h>
32#include <iostream>
33#include <string.h>  // for memcpy
34
35#ifdef GZSTREAM_NAMESPACE
36namespace GZSTREAM_NAMESPACE {
37#endif
38
39// ----------------------------------------------------------------------------
40// Internal classes to implement gzstream. See header file for user classes.
41// ----------------------------------------------------------------------------
42
43// --------------------------------------
44// class gzstreambuf:
45// --------------------------------------
46
47gzstreambuf* gzstreambuf::open( const char* name, int open_mode) {
48    if ( is_open())
49        return (gzstreambuf*)0;
50    mode = open_mode;
51    // no append nor read/write mode
52    if ((mode & std::ios::ate) || (mode & std::ios::app)
53        || ((mode & std::ios::in) && (mode & std::ios::out)))
54        return (gzstreambuf*)0;
55    char  fmode[10];
56    char* fmodeptr = fmode;
57    if ( mode & std::ios::in)
58        *fmodeptr++ = 'r';
59    else if ( mode & std::ios::out)
60        *fmodeptr++ = 'w';
61    *fmodeptr++ = 'b';
62    *fmodeptr = '\0';
63    file = gzopen( name, fmode);
64    if (file == 0)
65        return (gzstreambuf*)0;
66    opened = 1;
67    return this;
68}
69
70gzstreambuf * gzstreambuf::close() {
71    if ( is_open()) {
72        sync();
73        opened = 0;
74        if ( gzclose( file) == Z_OK)
75            return this;
76    }
77    return (gzstreambuf*)0;
78}
79
80int gzstreambuf::underflow() { // used for input buffer only
81    if ( gptr() && ( gptr() < egptr()))
82        return * reinterpret_cast<unsigned char *>( gptr());
83
84    if ( ! (mode & std::ios::in) || ! opened)
85        return EOF;
86    // Josuttis' implementation of inbuf
87    int n_putback = static_cast<int>(gptr() - eback());
88    if ( n_putback > 4)
89        n_putback = 4;
90    memcpy( buffer + (4 - n_putback), gptr() - n_putback, n_putback);
91
92    int num = gzread( file, buffer+4, bufferSize-4);
93    if (num <= 0) // ERROR or EOF
94        return EOF;
95
96    // reset buffer pointers
97    setg( buffer + (4 - n_putback),   // beginning of putback area
98          buffer + 4,                 // read position
99          buffer + 4 + num);          // end of buffer
100
101    // return next character
102    return * reinterpret_cast<unsigned char *>( gptr());   
103}
104
105int gzstreambuf::flush_buffer() {
106    // Separate the writing of the buffer from overflow() and
107    // sync() operation.
108    int w = static_cast<int>(pptr() - pbase());
109    if ( gzwrite( file, pbase(), w) != w)
110        return EOF;
111    pbump( -w);
112    return w;
113}
114
115int gzstreambuf::overflow( int c) { // used for output buffer only
116    if ( ! ( mode & std::ios::out) || ! opened)
117        return EOF;
118    if (c != EOF) {
119        *pptr() = c;
120        pbump(1);
121    }
122    if ( flush_buffer() == EOF)
123        return EOF;
124    return c;
125}
126
127int gzstreambuf::sync() {
128    // Changed to use flush_buffer() instead of overflow( EOF)
129    // which caused improper behavior with std::endl and flush(),
130    // bug reported by Vincent Ricard.
131    if ( pptr() && pptr() > pbase()) {
132        if ( flush_buffer() == EOF)
133            return -1;
134    }
135    return 0;
136}
137
138// --------------------------------------
139// class gzstreambase:
140// --------------------------------------
141
142gzstreambase::gzstreambase( const char* name, int mode) {
143    init( &buf);
144    open( name, mode);
145}
146
147gzstreambase::~gzstreambase() {
148    buf.close();
149}
150
151void gzstreambase::open( const char* name, int open_mode) {
152    if ( ! buf.open( name, open_mode))
153        clear( rdstate() | std::ios::badbit);
154}
155
156void gzstreambase::close() {
157    if ( buf.is_open())
158        if ( ! buf.close())
159            clear( rdstate() | std::ios::badbit);
160}
161
162#ifdef GZSTREAM_NAMESPACE
163} // namespace GZSTREAM_NAMESPACE
164#endif
165
166// ============================================================================
167// EOF //
168
Note: See TracBrowser for help on using the repository browser.