Mercurial > hg > svcore
comparison data/fileio/BZipFileDevice.cpp @ 148:1a42221a1522
* Reorganising code base. This revision will not compile.
author | Chris Cannam |
---|---|
date | Mon, 31 Jul 2006 11:49:58 +0000 |
parents | |
children | 11e107062a2b |
comparison
equal
deleted
inserted
replaced
147:3a13b0d4934e | 148:1a42221a1522 |
---|---|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ | |
2 | |
3 /* | |
4 Sonic Visualiser | |
5 An audio file viewer and annotation editor. | |
6 Centre for Digital Music, Queen Mary, University of London. | |
7 This file copyright 2006 Chris Cannam. | |
8 | |
9 This program is free software; you can redistribute it and/or | |
10 modify it under the terms of the GNU General Public License as | |
11 published by the Free Software Foundation; either version 2 of the | |
12 License, or (at your option) any later version. See the file | |
13 COPYING included with this distribution for more information. | |
14 */ | |
15 | |
16 #include "BZipFileDevice.h" | |
17 | |
18 #include <bzlib.h> | |
19 | |
20 #include <iostream> | |
21 | |
22 BZipFileDevice::BZipFileDevice(QString fileName) : | |
23 m_fileName(fileName), | |
24 m_file(0), | |
25 m_bzFile(0), | |
26 m_atEnd(true) | |
27 { | |
28 } | |
29 | |
30 BZipFileDevice::~BZipFileDevice() | |
31 { | |
32 // std::cerr << "BZipFileDevice::~BZipFileDevice(" << m_fileName.toStdString() << ")" << std::endl; | |
33 if (m_bzFile) close(); | |
34 } | |
35 | |
36 bool | |
37 BZipFileDevice::open(OpenMode mode) | |
38 { | |
39 if (m_bzFile) { | |
40 setErrorString(tr("File is already open")); | |
41 return false; | |
42 } | |
43 | |
44 if (mode & Append) { | |
45 setErrorString(tr("Append mode not supported")); | |
46 return false; | |
47 } | |
48 | |
49 if ((mode & (ReadOnly | WriteOnly)) == 0) { | |
50 setErrorString(tr("File access mode not specified")); | |
51 return false; | |
52 } | |
53 | |
54 if ((mode & ReadOnly) && (mode & WriteOnly)) { | |
55 setErrorString(tr("Read and write modes both specified")); | |
56 return false; | |
57 } | |
58 | |
59 if (mode & WriteOnly) { | |
60 | |
61 m_file = fopen(m_fileName.toLocal8Bit().data(), "wb"); | |
62 if (!m_file) { | |
63 setErrorString(tr("Failed to open file for writing")); | |
64 return false; | |
65 } | |
66 | |
67 int bzError = BZ_OK; | |
68 m_bzFile = BZ2_bzWriteOpen(&bzError, m_file, 9, 0, 0); | |
69 | |
70 if (!m_bzFile) { | |
71 fclose(m_file); | |
72 m_file = 0; | |
73 setErrorString(tr("Failed to open bzip2 stream for writing")); | |
74 return false; | |
75 } | |
76 | |
77 // std::cerr << "BZipFileDevice: opened \"" << m_fileName.toStdString() << "\" for writing" << std::endl; | |
78 | |
79 setErrorString(QString()); | |
80 setOpenMode(mode); | |
81 return true; | |
82 } | |
83 | |
84 if (mode & ReadOnly) { | |
85 | |
86 m_file = fopen(m_fileName.toLocal8Bit().data(), "rb"); | |
87 if (!m_file) { | |
88 setErrorString(tr("Failed to open file for reading")); | |
89 return false; | |
90 } | |
91 | |
92 int bzError = BZ_OK; | |
93 m_bzFile = BZ2_bzReadOpen(&bzError, m_file, 0, 0, NULL, 0); | |
94 | |
95 if (!m_bzFile) { | |
96 fclose(m_file); | |
97 m_file = 0; | |
98 setErrorString(tr("Failed to open bzip2 stream for reading")); | |
99 return false; | |
100 } | |
101 | |
102 // std::cerr << "BZipFileDevice: opened \"" << m_fileName.toStdString() << "\" for reading" << std::endl; | |
103 | |
104 m_atEnd = false; | |
105 | |
106 setErrorString(QString()); | |
107 setOpenMode(mode); | |
108 return true; | |
109 } | |
110 | |
111 setErrorString(tr("Internal error (open for neither read nor write)")); | |
112 return false; | |
113 } | |
114 | |
115 void | |
116 BZipFileDevice::close() | |
117 { | |
118 if (!m_bzFile) { | |
119 setErrorString(tr("File not open")); | |
120 return; | |
121 } | |
122 | |
123 int bzError = BZ_OK; | |
124 | |
125 if (openMode() & WriteOnly) { | |
126 unsigned int in = 0, out = 0; | |
127 BZ2_bzWriteClose(&bzError, m_bzFile, 0, &in, &out); | |
128 // std::cerr << "Wrote bzip2 stream (in=" << in << ", out=" << out << ")" << std::endl; | |
129 if (bzError != BZ_OK) { | |
130 setErrorString(tr("bzip2 stream write close error")); | |
131 } | |
132 fclose(m_file); | |
133 m_bzFile = 0; | |
134 m_file = 0; | |
135 return; | |
136 } | |
137 | |
138 if (openMode() & ReadOnly) { | |
139 BZ2_bzReadClose(&bzError, m_bzFile); | |
140 if (bzError != BZ_OK) { | |
141 setErrorString(tr("bzip2 stream read close error")); | |
142 } | |
143 fclose(m_file); | |
144 m_bzFile = 0; | |
145 m_file = 0; | |
146 return; | |
147 } | |
148 | |
149 setErrorString(tr("Internal error (close for neither read nor write)")); | |
150 return; | |
151 } | |
152 | |
153 qint64 | |
154 BZipFileDevice::readData(char *data, qint64 maxSize) | |
155 { | |
156 if (m_atEnd) return 0; | |
157 | |
158 int bzError = BZ_OK; | |
159 int read = BZ2_bzRead(&bzError, m_bzFile, data, maxSize); | |
160 | |
161 // std::cerr << "BZipFileDevice::readData: requested " << maxSize << ", read " << read << std::endl; | |
162 | |
163 if (bzError != BZ_OK) { | |
164 if (bzError != BZ_STREAM_END) { | |
165 std::cerr << "BZipFileDevice::readData: error condition" << std::endl; | |
166 setErrorString(tr("bzip2 stream read error")); | |
167 return -1; | |
168 } else { | |
169 // std::cerr << "BZipFileDevice::readData: reached end of file" << std::endl; | |
170 m_atEnd = true; | |
171 } | |
172 } | |
173 | |
174 return read; | |
175 } | |
176 | |
177 qint64 | |
178 BZipFileDevice::writeData(const char *data, qint64 maxSize) | |
179 { | |
180 int bzError = BZ_OK; | |
181 BZ2_bzWrite(&bzError, m_bzFile, (void *)data, maxSize); | |
182 | |
183 // std::cerr << "BZipFileDevice::writeData: " << maxSize << " to write" << std::endl; | |
184 | |
185 if (bzError != BZ_OK) { | |
186 std::cerr << "BZipFileDevice::writeData: error condition" << std::endl; | |
187 setErrorString("bzip2 stream write error"); | |
188 return -1; | |
189 } | |
190 | |
191 // std::cerr << "BZipFileDevice::writeData: wrote " << maxSize << std::endl; | |
192 | |
193 return maxSize; | |
194 } | |
195 |