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