modified: SpatialOmicsCoord.py
[GalaxyCodeBases.git] / BGI / SOAPsnp / gzstream.cpp
blobe3ad4a84cdec78dfc0fde8f1444f8a15be6a9926
2 // ============================================================================
3 // gzstream, C++ iostream classes wrapping the zlib compression library.
4 // Copyright (C) 2001 Deepak Bandyopadhyay, Lutz Kettner
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 // ============================================================================
21 // File : gzstream.C
22 // Revision : $Revision: 1.7 $
23 // Revision_date : $Date: 2003/01/08 14:41:27 $
24 // Author(s) : Deepak Bandyopadhyay, Lutz Kettner
25 //
26 // Standard streambuf implementation following Nicolai Josuttis, "The
27 // Standard C++ Library".
28 // ============================================================================
30 #include "gzstream.h"
31 #include <iostream>
32 #include <string.h> // for memcpy
34 #ifdef GZSTREAM_NAMESPACE
35 namespace GZSTREAM_NAMESPACE {
36 #endif
38 // ----------------------------------------------------------------------------
39 // Internal classes to implement gzstream. See header file for user classes.
40 // ----------------------------------------------------------------------------
42 // --------------------------------------
43 // class gzstreambuf:
44 // --------------------------------------
46 gzstreambuf* gzstreambuf::open( const char* name, int open_mode) {
47 if ( is_open())
48 return (gzstreambuf*)0;
49 mode = open_mode;
50 // no append nor read/write mode
51 if ((mode & std::ios::ate) || (mode & std::ios::app)
52 || ((mode & std::ios::in) && (mode & std::ios::out)))
53 return (gzstreambuf*)0;
54 char fmode[10];
55 char* fmodeptr = fmode;
56 if ( mode & std::ios::in)
57 *fmodeptr++ = 'r';
58 else if ( mode & std::ios::out)
59 *fmodeptr++ = 'w';
60 *fmodeptr++ = 'b';
61 *fmodeptr = '\0';
62 file = gzopen( name, fmode);
63 if (file == 0)
64 return (gzstreambuf*)0;
65 opened = 1;
66 return this;
69 gzstreambuf * gzstreambuf::close() {
70 if ( is_open()) {
71 sync();
72 opened = 0;
73 if ( gzclose( file) == Z_OK)
74 return this;
76 return (gzstreambuf*)0;
79 int gzstreambuf::underflow() { // used for input buffer only
80 if ( gptr() && ( gptr() < egptr()))
81 return * reinterpret_cast<unsigned char *>( gptr());
83 if ( ! (mode & std::ios::in) || ! opened)
84 return EOF;
85 // Josuttis' implementation of inbuf
86 int n_putback = gptr() - eback();
87 if ( n_putback > 4)
88 n_putback = 4;
89 memcpy( buffer + (4 - n_putback), gptr() - n_putback, n_putback);
91 int num = gzread( file, buffer+4, bufferSize-4);
92 if (num <= 0) // ERROR or EOF
93 return EOF;
95 // reset buffer pointers
96 setg( buffer + (4 - n_putback), // beginning of putback area
97 buffer + 4, // read position
98 buffer + 4 + num); // end of buffer
100 // return next character
101 return * reinterpret_cast<unsigned char *>( gptr());
104 int gzstreambuf::flush_buffer() {
105 // Separate the writing of the buffer from overflow() and
106 // sync() operation.
107 int w = pptr() - pbase();
108 if ( gzwrite( file, pbase(), w) != w)
109 return EOF;
110 pbump( -w);
111 return w;
114 int gzstreambuf::overflow( int c) { // used for output buffer only
115 if ( ! ( mode & std::ios::out) || ! opened)
116 return EOF;
117 if (c != EOF) {
118 *pptr() = c;
119 pbump(1);
121 if ( flush_buffer() == EOF)
122 return EOF;
123 return c;
126 int gzstreambuf::sync() {
127 // Changed to use flush_buffer() instead of overflow( EOF)
128 // which caused improper behavior with std::endl and flush(),
129 // bug reported by Vincent Ricard.
130 if ( pptr() && pptr() > pbase()) {
131 if ( flush_buffer() == EOF)
132 return -1;
134 return 0;
137 gzstreambuf& gzstreambuf::operator =(const gzstreambuf &gzs)
139 return *this;
142 // --------------------------------------
143 // class gzstreambase:
144 // --------------------------------------
146 gzstreambase::gzstreambase( const char* name, int mode) {
147 init( &buf);
148 open( name, mode);
151 gzstreambase::~gzstreambase() {
152 buf.close();
155 void gzstreambase::open( const char* name, int open_mode) {
156 if ( ! buf.open( name, open_mode))
157 clear( rdstate() | std::ios::badbit);
160 void gzstreambase::close() {
161 if ( buf.is_open())
162 if ( ! buf.close())
163 clear( rdstate() | std::ios::badbit);
165 gzstreambase& gzstreambase::operator =(const gzstreambase &gzst)
167 buf = gzst.buf;
168 return *this;
171 // --------------------------------------
172 // class ogzfstream
173 // --------------------------------------
177 //const ogfstream& ogzfstream::operator = (const ogfstream &ogzcon)
179 // consensus = ogzcon.consensus;
180 // return *this;
184 #ifdef GZSTREAM_NAMESPACE
185 } // namespace GZSTREAM_NAMESPACE
186 #endif
188 // ============================================================================
189 // EOF //