source: trunk/MagicSoft/Mars/mbase/MZlib.cc@ 7447

Last change on this file since 7447 was 7438, checked in by tbretz, 19 years ago
*** empty log message ***
File size: 3.8 KB
Line 
1/* ======================================================================== *\
2!
3! *
4! * This file is part of MARS, the MAGIC Analysis and Reconstruction
5! * Software. It is distributed to you in the hope that it can be a useful
6! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
7! * It is distributed WITHOUT ANY WARRANTY.
8! *
9! * Permission to use, copy, modify and distribute this software and its
10! * documentation for any purpose is hereby granted without fee,
11! * provided that the above copyright notice appear in all copies and
12! * that both that copyright notice and this permission notice appear
13! * in supporting documentation. It is provided "as is" without express
14! * or implied warranty.
15! *
16!
17!
18! Author(s): Thomas Bretz, 12/2005 <mailto:tbretz@astro.uni-wuerzburg.de>
19!
20! Copyright: MAGIC Software Development, 2000-2005
21!
22!
23\* ======================================================================== */
24
25
26//////////////////////////////////////////////////////////////////////////////
27//
28// MZlib
29//
30// This is a C++ wrapper for zlib (taken from root)
31//
32// WARNING: - There might not be support for all features.
33// - seek calls might be rather slow
34//
35//////////////////////////////////////////////////////////////////////////////
36#include "MZlib.h"
37
38ClassImp(MZlib);
39
40using namespace std;
41
42// --------------------------------------------------------------------------
43//
44// Open a file by name. Test if it is open like for an ifstream
45// It doesn't matter whether the file is gzip compressed or not.
46//
47void MZlib::open(const char* name)
48{
49 if (is_open())
50 {
51 clear(rdstate()|ios::badbit);
52 return;
53 }
54
55 fFile = gzopen(name, "rb0");
56 if (fFile == 0)
57 {
58 clear(rdstate()|ios::badbit);
59 return;
60 }
61}
62
63// --------------------------------------------------------------------------
64//
65// Close an open file.
66//
67void MZlib::close()
68{
69 if (!is_open())
70 return;
71
72 sync();
73
74 if (gzclose(fFile) != Z_OK)
75 clear(rdstate()|ios::badbit);
76
77 fFile = 0;
78}
79
80// --------------------------------------------------------------------------
81//
82int MZlib::underflow()
83{
84 if (gptr() && gptr()<egptr())
85 return * reinterpret_cast<unsigned char *>(gptr());
86
87 if (!is_open())
88 return EOF;
89
90 // implementation of inbuf
91 const int putback = gptr()-eback()>4 ? 4 : gptr()-eback();
92
93 memcpy(fBuffer+(4-putback), gptr()-putback, putback);
94
95 const int num = gzread(fFile, fBuffer+4, fgBufferSize-4);
96 if (num <= 0) // ERROR or EOF
97 return EOF;
98
99 // reset buffer pointers
100 setg(fBuffer+(4-putback), fBuffer+4, fBuffer+4+num);
101
102 // return next character
103 return *reinterpret_cast<unsigned char *>(gptr());
104}
105
106// --------------------------------------------------------------------------
107//
108int MZlib::flush_buffer()
109{
110 // Separate the writing of the buffer from overflow() and sync() operation.
111 const int w = pptr() - pbase();
112
113 if (gzwrite(fFile, pbase(), w) != w)
114 return EOF;
115
116 pbump(-w);
117
118 return w;
119}
120
121/*
122int MZlib::overflow( int c)
123{
124 if ( ! ( mode & ios::out) || ! opened)
125 return EOF;
126
127 if (c != EOF)
128 {
129 *pptr() = c;
130 pbump(1);
131
132 }
133 if ( flush_buffer() == EOF)
134 return EOF;
135
136 return c;
137}
138*/
139
140// --------------------------------------------------------------------------
141//
142int MZlib::sync()
143{
144 // Use flush_buffer() instead of overflow(EOF) to
145 // cause proper behavior with std::endl and flush()
146 if (pptr() && pptr()>pbase())
147 {
148 if (flush_buffer() == EOF)
149 return -1;
150 }
151 return 0;
152}
153
154// --------------------------------------------------------------------------
155//
156streambuf::pos_type MZlib::seekoff(streambuf::off_type offset, ios_base::seekdir dir, ios_base::openmode)
157{
158 return gzseek(fFile, offset, dir);
159}
Note: See TracBrowser for help on using the repository browser.