comparison data/fileio/BZipFileDevice.cpp @ 207:8ee6cf529c4e

* Further fix for restoring layer visibility from session file * Better handling of error state in bzip file device to avoid spurious errors * Fix #1495001 deleted layers prevail in saved session file
author Chris Cannam
date Fri, 05 Jan 2007 15:49:10 +0000
parents a75e678f5d37
children b4a8d8221eaf
comparison
equal deleted inserted replaced
206:a75e678f5d37 207:8ee6cf529c4e
21 21
22 BZipFileDevice::BZipFileDevice(QString fileName) : 22 BZipFileDevice::BZipFileDevice(QString fileName) :
23 m_fileName(fileName), 23 m_fileName(fileName),
24 m_file(0), 24 m_file(0),
25 m_bzFile(0), 25 m_bzFile(0),
26 m_atEnd(true) 26 m_atEnd(true),
27 m_ok(true)
27 { 28 {
28 } 29 }
29 30
30 BZipFileDevice::~BZipFileDevice() 31 BZipFileDevice::~BZipFileDevice()
31 { 32 {
32 // std::cerr << "BZipFileDevice::~BZipFileDevice(" << m_fileName.toStdString() << ")" << std::endl; 33 // std::cerr << "BZipFileDevice::~BZipFileDevice(" << m_fileName.toStdString() << ")" << std::endl;
33 if (m_bzFile) close(); 34 if (m_bzFile) close();
34 } 35 }
35 36
36 bool 37 bool
38 BZipFileDevice::isOK() const
39 {
40 return m_ok;
41 }
42
43 bool
37 BZipFileDevice::open(OpenMode mode) 44 BZipFileDevice::open(OpenMode mode)
38 { 45 {
39 setErrorString(""); 46 setErrorString("");
40 47
41 if (m_bzFile) { 48 if (m_bzFile) {
43 return false; 50 return false;
44 } 51 }
45 52
46 if (mode & Append) { 53 if (mode & Append) {
47 setErrorString(tr("Append mode not supported")); 54 setErrorString(tr("Append mode not supported"));
55 m_ok = false;
48 return false; 56 return false;
49 } 57 }
50 58
51 if ((mode & (ReadOnly | WriteOnly)) == 0) { 59 if ((mode & (ReadOnly | WriteOnly)) == 0) {
52 setErrorString(tr("File access mode not specified")); 60 setErrorString(tr("File access mode not specified"));
61 m_ok = false;
53 return false; 62 return false;
54 } 63 }
55 64
56 if ((mode & ReadOnly) && (mode & WriteOnly)) { 65 if ((mode & ReadOnly) && (mode & WriteOnly)) {
57 setErrorString(tr("Read and write modes both specified")); 66 setErrorString(tr("Read and write modes both specified"));
67 m_ok = false;
58 return false; 68 return false;
59 } 69 }
60 70
61 if (mode & WriteOnly) { 71 if (mode & WriteOnly) {
62 72
63 m_file = fopen(m_fileName.toLocal8Bit().data(), "wb"); 73 m_file = fopen(m_fileName.toLocal8Bit().data(), "wb");
64 if (!m_file) { 74 if (!m_file) {
65 setErrorString(tr("Failed to open file for writing")); 75 setErrorString(tr("Failed to open file for writing"));
76 m_ok = false;
66 return false; 77 return false;
67 } 78 }
68 79
69 int bzError = BZ_OK; 80 int bzError = BZ_OK;
70 m_bzFile = BZ2_bzWriteOpen(&bzError, m_file, 9, 0, 0); 81 m_bzFile = BZ2_bzWriteOpen(&bzError, m_file, 9, 0, 0);
71 82
72 if (!m_bzFile) { 83 if (!m_bzFile) {
73 fclose(m_file); 84 fclose(m_file);
74 m_file = 0; 85 m_file = 0;
75 setErrorString(tr("Failed to open bzip2 stream for writing")); 86 setErrorString(tr("Failed to open bzip2 stream for writing"));
87 m_ok = false;
76 return false; 88 return false;
77 } 89 }
78 90
79 // std::cerr << "BZipFileDevice: opened \"" << m_fileName.toStdString() << "\" for writing" << std::endl; 91 // std::cerr << "BZipFileDevice: opened \"" << m_fileName.toStdString() << "\" for writing" << std::endl;
80 92
86 if (mode & ReadOnly) { 98 if (mode & ReadOnly) {
87 99
88 m_file = fopen(m_fileName.toLocal8Bit().data(), "rb"); 100 m_file = fopen(m_fileName.toLocal8Bit().data(), "rb");
89 if (!m_file) { 101 if (!m_file) {
90 setErrorString(tr("Failed to open file for reading")); 102 setErrorString(tr("Failed to open file for reading"));
103 m_ok = false;
91 return false; 104 return false;
92 } 105 }
93 106
94 int bzError = BZ_OK; 107 int bzError = BZ_OK;
95 m_bzFile = BZ2_bzReadOpen(&bzError, m_file, 0, 0, NULL, 0); 108 m_bzFile = BZ2_bzReadOpen(&bzError, m_file, 0, 0, NULL, 0);
96 109
97 if (!m_bzFile) { 110 if (!m_bzFile) {
98 fclose(m_file); 111 fclose(m_file);
99 m_file = 0; 112 m_file = 0;
100 setErrorString(tr("Failed to open bzip2 stream for reading")); 113 setErrorString(tr("Failed to open bzip2 stream for reading"));
114 m_ok = false;
101 return false; 115 return false;
102 } 116 }
103 117
104 // std::cerr << "BZipFileDevice: opened \"" << m_fileName.toStdString() << "\" for reading" << std::endl; 118 // std::cerr << "BZipFileDevice: opened \"" << m_fileName.toStdString() << "\" for reading" << std::endl;
105 119
109 setOpenMode(mode); 123 setOpenMode(mode);
110 return true; 124 return true;
111 } 125 }
112 126
113 setErrorString(tr("Internal error (open for neither read nor write)")); 127 setErrorString(tr("Internal error (open for neither read nor write)"));
128 m_ok = false;
114 return false; 129 return false;
115 } 130 }
116 131
117 void 132 void
118 BZipFileDevice::close() 133 BZipFileDevice::close()
119 { 134 {
120 if (!m_bzFile) { 135 if (!m_bzFile) {
121 setErrorString(tr("File not open")); 136 setErrorString(tr("File not open"));
137 m_ok = false;
122 return; 138 return;
123 } 139 }
124 140
125 int bzError = BZ_OK; 141 int bzError = BZ_OK;
126 142
132 setErrorString(tr("bzip2 stream write close error")); 148 setErrorString(tr("bzip2 stream write close error"));
133 } 149 }
134 fclose(m_file); 150 fclose(m_file);
135 m_bzFile = 0; 151 m_bzFile = 0;
136 m_file = 0; 152 m_file = 0;
153 m_ok = false;
137 return; 154 return;
138 } 155 }
139 156
140 if (openMode() & ReadOnly) { 157 if (openMode() & ReadOnly) {
141 BZ2_bzReadClose(&bzError, m_bzFile); 158 BZ2_bzReadClose(&bzError, m_bzFile);
143 setErrorString(tr("bzip2 stream read close error")); 160 setErrorString(tr("bzip2 stream read close error"));
144 } 161 }
145 fclose(m_file); 162 fclose(m_file);
146 m_bzFile = 0; 163 m_bzFile = 0;
147 m_file = 0; 164 m_file = 0;
165 m_ok = false;
148 return; 166 return;
149 } 167 }
150 168
151 setErrorString(tr("Internal error (close for neither read nor write)")); 169 setErrorString(tr("Internal error (close for neither read nor write)"));
152 return; 170 return;
164 182
165 if (bzError != BZ_OK) { 183 if (bzError != BZ_OK) {
166 if (bzError != BZ_STREAM_END) { 184 if (bzError != BZ_STREAM_END) {
167 std::cerr << "BZipFileDevice::readData: error condition" << std::endl; 185 std::cerr << "BZipFileDevice::readData: error condition" << std::endl;
168 setErrorString(tr("bzip2 stream read error")); 186 setErrorString(tr("bzip2 stream read error"));
187 m_ok = false;
169 return -1; 188 return -1;
170 } else { 189 } else {
171 // std::cerr << "BZipFileDevice::readData: reached end of file" << std::endl; 190 // std::cerr << "BZipFileDevice::readData: reached end of file" << std::endl;
172 m_atEnd = true; 191 m_atEnd = true;
173 } 192 }
174 } 193 }
175 194
176 setErrorString("");
177 return read; 195 return read;
178 } 196 }
179 197
180 qint64 198 qint64
181 BZipFileDevice::writeData(const char *data, qint64 maxSize) 199 BZipFileDevice::writeData(const char *data, qint64 maxSize)
186 // std::cerr << "BZipFileDevice::writeData: " << maxSize << " to write" << std::endl; 204 // std::cerr << "BZipFileDevice::writeData: " << maxSize << " to write" << std::endl;
187 205
188 if (bzError != BZ_OK) { 206 if (bzError != BZ_OK) {
189 std::cerr << "BZipFileDevice::writeData: error condition" << std::endl; 207 std::cerr << "BZipFileDevice::writeData: error condition" << std::endl;
190 setErrorString("bzip2 stream write error"); 208 setErrorString("bzip2 stream write error");
209 m_ok = false;
191 return -1; 210 return -1;
192 } 211 }
193 212
194 // std::cerr << "BZipFileDevice::writeData: wrote " << maxSize << std::endl; 213 // std::cerr << "BZipFileDevice::writeData: wrote " << maxSize << std::endl;
195 214
196 setErrorString("");
197 return maxSize; 215 return maxSize;
198 } 216 }
199 217