source: trunk/Mars/mbase/MBzlib2.cc@ 17127

Last change on this file since 17127 was 14154, checked in by tbretz, 12 years ago
included string
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
38#include <string.h>
39
40ClassImp(MBzlib2);
41
42using namespace std;
43
44// --------------------------------------------------------------------------
45//
46// Open a file by name. Test if it is open like for an ifstream
47// It doesn't matter whether the file is gzip compressed or not.
48//
49void MBzlib2::open(const char* name)
50{
51 if (is_open())
52 {
53 clear(rdstate()|ios::badbit);
54 return;
55 }
56
57 fFile = BZ2_bzopen(name, "rb0");
58 if (fFile == 0)
59 {
60 clear(rdstate()|ios::badbit);
61 return;
62 }
63}
64
65// --------------------------------------------------------------------------
66//
67// Close an open file.
68//
69void MBzlib2::close()
70{
71 if (!is_open())
72 return;
73
74 sync();
75
76 BZ2_bzclose(fFile);
77// if (BZ2_bzclose(fFile) != Z_OK)
78// clear(rdstate()|ios::badbit);
79
80 fFile = 0;
81}
82
83// --------------------------------------------------------------------------
84//
85int MBzlib2::underflow()
86{
87 if (gptr() && gptr()<egptr())
88 return * reinterpret_cast<unsigned char *>(gptr());
89
90 if (!is_open())
91 return EOF;
92
93 // implementation of inbuf
94 const int putback = gptr()-eback()>4 ? 4 : gptr()-eback();
95
96 memcpy(fBuffer+(4-putback), gptr()-putback, putback);
97
98 const int num = BZ2_bzread(fFile, fBuffer+4, fgBufferSize-4);
99 if (num <= 0) // ERROR or EOF
100 return EOF;
101
102 // reset buffer pointers
103 setg(fBuffer+(4-putback), fBuffer+4, fBuffer+4+num);
104
105 // return next character
106 return *reinterpret_cast<unsigned char *>(gptr());
107}
108
109// --------------------------------------------------------------------------
110//
111int MBzlib2::flush_buffer()
112{
113 // Separate the writing of the buffer from overflow() and sync() operation.
114 const int w = pptr() - pbase();
115
116 if (BZ2_bzwrite(fFile, pbase(), w) != w)
117 return EOF;
118
119 pbump(-w);
120
121 return w;
122}
123
124/*
125int MBzlib2::overflow( int c)
126{
127 if ( ! ( mode & ios::out) || ! opened)
128 return EOF;
129
130 if (c != EOF)
131 {
132 *pptr() = c;
133 pbump(1);
134
135 }
136 if ( flush_buffer() == EOF)
137 return EOF;
138
139 return c;
140}
141*/
142
143// --------------------------------------------------------------------------
144//
145int MBzlib2::sync()
146{
147 // Use flush_buffer() instead of overflow(EOF) to
148 // cause proper behavior with std::endl and flush()
149 if (pptr() && pptr()>pbase())
150 {
151 if (flush_buffer() == EOF)
152 return -1;
153 }
154 return 0;
155}
156
157// --------------------------------------------------------------------------
158//
159streambuf::pos_type MBzlib2::seekoff(streambuf::off_type offset, ios_base::seekdir dir, ios_base::openmode)
160{
161 char *c = new char[offset];
162 int rc = BZ2_bzread(fFile, c, offset);
163 delete c;
164 return rc;
165// return gzseek(fFile, offset, dir);
166}
Note: See TracBrowser for help on using the repository browser.