source: trunk/MagicSoft/Mars/mbase/MBzlib2.cc@ 7929

Last change on this file since 7929 was 7819, checked in by tbretz, 18 years ago
*** empty log message ***
File size: 4.0 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// MBzlib2
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 "MBzlib2.h"
37
38ClassImp(MBzlib2);
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 MBzlib2::open(const char* name)
48{
49 if (is_open())
50 {
51 clear(rdstate()|ios::badbit);
52 return;
53 }
54
55 fFile = BZ2_bzopen(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 MBzlib2::close()
68{
69 if (!is_open())
70 return;
71
72 sync();
73
74 BZ2_bzclose(fFile);
75// if (BZ2_bzclose(fFile) != Z_OK)
76// clear(rdstate()|ios::badbit);
77
78 fFile = 0;
79}
80
81// --------------------------------------------------------------------------
82//
83int MBzlib2::underflow()
84{
85 if (gptr() && gptr()<egptr())
86 return * reinterpret_cast<unsigned char *>(gptr());
87
88 if (!is_open())
89 return EOF;
90
91 // implementation of inbuf
92 const int putback = gptr()-eback()>4 ? 4 : gptr()-eback();
93
94 memcpy(fBuffer+(4-putback), gptr()-putback, putback);
95
96 const int num = BZ2_bzread(fFile, fBuffer+4, fgBufferSize-4);
97 if (num <= 0) // ERROR or EOF
98 return EOF;
99
100 // reset buffer pointers
101 setg(fBuffer+(4-putback), fBuffer+4, fBuffer+4+num);
102
103 // return next character
104 return *reinterpret_cast<unsigned char *>(gptr());
105}
106
107// --------------------------------------------------------------------------
108//
109int MBzlib2::flush_buffer()
110{
111 // Separate the writing of the buffer from overflow() and sync() operation.
112 const int w = pptr() - pbase();
113
114 if (BZ2_bzwrite(fFile, pbase(), w) != w)
115 return EOF;
116
117 pbump(-w);
118
119 return w;
120}
121
122/*
123int MBzlib2::overflow( int c)
124{
125 if ( ! ( mode & ios::out) || ! opened)
126 return EOF;
127
128 if (c != EOF)
129 {
130 *pptr() = c;
131 pbump(1);
132
133 }
134 if ( flush_buffer() == EOF)
135 return EOF;
136
137 return c;
138}
139*/
140
141// --------------------------------------------------------------------------
142//
143int MBzlib2::sync()
144{
145 // Use flush_buffer() instead of overflow(EOF) to
146 // cause proper behavior with std::endl and flush()
147 if (pptr() && pptr()>pbase())
148 {
149 if (flush_buffer() == EOF)
150 return -1;
151 }
152 return 0;
153}
154
155// --------------------------------------------------------------------------
156//
157streambuf::pos_type MBzlib2::seekoff(streambuf::off_type offset, ios_base::seekdir dir, ios_base::openmode)
158{
159 char *c = new char[offset];
160 int rc = BZ2_bzread(fFile, c, offset);
161 delete c;
162 return rc;
163// return gzseek(fFile, offset, dir);
164}
Note: See TracBrowser for help on using the repository browser.