]> git.lyx.org Git - lyx.git/blob - src/support/gzstream.cpp
Move debug.{cpp,h}, Messages.{cpp,h} and gettext.{cpp,h} to support/.
[lyx.git] / src / support / gzstream.cpp
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.4 $
22 // Revision_date : $Date: 2005/04/26 10:30:24 $
23 // Author(s)     : Deepak Bandyopadhyay, Lutz Kettner
24 //
25 // Standard streambuf implementation following Nicolai Josuttis, "The
26 // Standard C++ Library".
27 // ============================================================================
28
29 #include <config.h>
30
31 #include "support/gzstream.h"
32
33 #include <iostream>
34 #ifdef HAVE_STRING_H
35 # include <string.h> // for memcpy
36 #endif
37
38 #ifdef GZSTREAM_NAMESPACE
39 namespace GZSTREAM_NAMESPACE {
40 #endif
41
42 // ----------------------------------------------------------------------------
43 // Internal classes to implement gzstream. See header file for user classes.
44 // ----------------------------------------------------------------------------
45
46 // --------------------------------------
47 // class gzstreambuf:
48 // --------------------------------------
49
50 gzstreambuf* gzstreambuf::open( const char* name, int open_mode) {
51     if ( is_open())
52         return (gzstreambuf*)0;
53     mode = open_mode;
54     // no append nor read/write mode
55     if ((mode & std::ios::ate) || (mode & std::ios::app)
56         || ((mode & std::ios::in) && (mode & std::ios::out)))
57         return (gzstreambuf*)0;
58     char  fmode[10];
59     char* fmodeptr = fmode;
60     if ( mode & std::ios::in)
61         *fmodeptr++ = 'r';
62     else if ( mode & std::ios::out)
63         *fmodeptr++ = 'w';
64     *fmodeptr++ = 'b';
65     *fmodeptr = '\0';
66     file = gzopen( name, fmode);
67     if (file == 0)
68         return (gzstreambuf*)0;
69     opened = 1;
70     return this;
71 }
72
73 gzstreambuf * gzstreambuf::close() {
74     if ( is_open()) {
75         sync();
76         opened = 0;
77         if ( gzclose( file) == Z_OK)
78             return this;
79     }
80     return (gzstreambuf*)0;
81 }
82
83 int gzstreambuf::underflow() { // used for input buffer only
84     if ( gptr() && ( gptr() < egptr()))
85         return * reinterpret_cast<unsigned char *>( gptr());
86
87     if ( ! (mode & std::ios::in) || ! opened)
88         return EOF;
89     // Josuttis' implementation of inbuf
90     int n_putback = gptr() - eback();
91     if ( n_putback > 4)
92         n_putback = 4;
93     memcpy( buffer + (4 - n_putback), gptr() - n_putback, n_putback);
94
95     int num = gzread( file, buffer+4, bufferSize-4);
96     if (num <= 0) // ERROR or EOF
97         return EOF;
98
99     // reset buffer pointers
100     setg( buffer + (4 - n_putback),   // beginning of putback area
101           buffer + 4,                 // read position
102           buffer + 4 + num);          // end of buffer
103
104     // return next character
105     return * reinterpret_cast<unsigned char *>( gptr());
106 }
107
108 int gzstreambuf::flush_buffer() {
109     // Separate the writing of the buffer from overflow() and
110     // sync() operation.
111     int w = pptr() - pbase();
112     if ( gzwrite( file, pbase(), w) != w)
113         return EOF;
114     pbump( -w);
115     return w;
116 }
117
118 int gzstreambuf::overflow( int c) { // used for output buffer only
119     if ( ! ( mode & std::ios::out) || ! opened)
120         return EOF;
121     if (c != EOF) {
122         *pptr() = c;
123         pbump(1);
124     }
125     if ( flush_buffer() == EOF)
126         return EOF;
127     return c;
128 }
129
130 int gzstreambuf::sync() {
131     // Changed to use flush_buffer() instead of overflow( EOF)
132     // which caused improper behavior with std::endl and flush(),
133     // bug reported by Vincent Ricard.
134     if ( pptr() && pptr() > pbase()) {
135         if ( flush_buffer() == EOF)
136             return -1;
137     }
138     return 0;
139 }
140
141 // --------------------------------------
142 // class gzstreambase:
143 // --------------------------------------
144
145 gzstreambase::gzstreambase( const char* name, int mode) {
146     init( &buf);
147     open( name, mode);
148 }
149
150 gzstreambase::~gzstreambase() {
151     buf.close();
152 }
153
154 void gzstreambase::open( const char* name, int open_mode) {
155     if ( ! buf.open( name, open_mode))
156         clear( rdstate() | std::ios::badbit);
157 }
158
159 void gzstreambase::close() {
160     if ( buf.is_open())
161         if ( ! buf.close())
162             clear( rdstate() | std::ios::badbit);
163 }
164
165 #ifdef GZSTREAM_NAMESPACE
166 } // namespace GZSTREAM_NAMESPACE
167 #endif
168
169 // ============================================================================
170 // EOF //