1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | |
15 | |
16 | |
17 | |
18 | |
19 | |
20 | |
21 | |
22 | |
23 | |
24 | |
25 | |
26 | |
27 | |
28 | |
29 | |
30 | |
31 | |
32 | |
33 | |
34 | #ifdef HAVE_CONFIG_H1 |
35 | #include <config.h> |
36 | #endif |
37 | |
38 | #include <iostream> |
39 | |
40 | #include "zfstream.h" |
41 | |
42 | #ifdef HAVE_ZLIB1 |
43 | |
44 | #include <cstring> // for strcpy, strcat, strlen (mode strings) |
45 | #include <cstdio> // for BUFSIZ |
46 | |
47 | |
48 | #define STASHED_CHARACTERS16 16 |
49 | #define BIGBUFSIZE(256 * 1024 + 16) (256 * 1024 + STASHED_CHARACTERS16) |
50 | #define SMALLBUFSIZE1 1 |
51 | |
52 | |
53 | |
54 | |
55 | gzfilebuf::gzfilebuf () |
56 | : file(0), io_mode(std::ios_base::openmode(0)), own_fd(false), |
57 | buffer(0), buffer_size(BIGBUFSIZE(256 * 1024 + 16)), own_buffer(true) |
58 | { |
59 | |
60 | this->disable_buffer (); |
61 | } |
62 | |
63 | |
64 | gzfilebuf::~gzfilebuf () |
65 | { |
66 | |
67 | |
68 | this->sync (); |
69 | if (own_fd) |
70 | this->close (); |
71 | |
72 | this->disable_buffer (); |
73 | } |
74 | |
75 | |
76 | int |
77 | gzfilebuf::setcompression (int comp_level, int comp_strategy) |
78 | { |
79 | return gzsetparams (file, comp_level, comp_strategy); |
80 | } |
81 | |
82 | |
83 | gzfilebuf* |
84 | gzfilebuf::open (const char *name, std::ios_base::openmode mode) |
85 | { |
86 | |
87 | if (this->is_open ()) |
88 | return 0; |
89 | |
90 | if ((mode & std::ios_base::in) && (mode & std::ios_base::out)) |
91 | return 0; |
92 | |
93 | |
94 | char char_mode[6] = "\0\0\0\0\0"; |
95 | if (! this->open_mode (mode, char_mode)) |
96 | return 0; |
97 | |
98 | |
99 | if ((file = gzopen (name, char_mode)) == 0) |
100 | return 0; |
101 | |
102 | |
103 | this->enable_buffer (); |
104 | io_mode = mode; |
105 | own_fd = true; |
106 | return this; |
107 | } |
108 | |
109 | |
110 | gzfilebuf* |
111 | gzfilebuf::attach (int fd, std::ios_base::openmode mode) |
112 | { |
113 | |
114 | if (this->is_open ()) |
| |
115 | return 0; |
116 | |
117 | if ((mode & std::ios_base::in) && (mode & std::ios_base::out)) |
118 | return 0; |
119 | |
120 | |
121 | char char_mode[6] = "\0\0\0\0\0"; |
122 | if (! this->open_mode (mode, char_mode)) |
| 3 | | Calling 'gzfilebuf::open_mode' | |
|
123 | return 0; |
124 | |
125 | |
126 | if ((file = gzdopen (fd, char_mode)) == 0) |
127 | return 0; |
128 | |
129 | |
130 | this->enable_buffer (); |
131 | io_mode = mode; |
132 | own_fd = false; |
133 | return this; |
134 | } |
135 | |
136 | |
137 | gzfilebuf* |
138 | gzfilebuf::close () |
139 | { |
140 | |
141 | if (! this->is_open ()) |
142 | return 0; |
143 | |
144 | gzfilebuf* retval = this; |
145 | |
146 | if (this->sync () == -1) |
147 | retval = 0; |
148 | if (gzclose (file) < 0) |
149 | retval = 0; |
150 | |
151 | file = 0; |
152 | own_fd = false; |
153 | |
154 | this->disable_buffer (); |
155 | return retval; |
156 | } |
157 | |
158 | |
159 | |
160 | |
161 | bool |
162 | gzfilebuf::open_mode (std::ios_base::openmode mode, char* c_mode) const |
163 | { |
164 | |
165 | |
166 | bool testi = mode & std::ios_base::in; |
167 | bool testo = mode & std::ios_base::out; |
168 | bool testt = mode & std::ios_base::trunc; |
169 | bool testa = mode & std::ios_base::app; |
170 | |
171 | |
172 | |
173 | |
174 | |
175 | |
176 | if (!testi && testo && !testt && !testa) |
177 | strcpy (c_mode, "w"); |
178 | if (!testi && testo && !testt && testa) |
179 | strcpy (c_mode, "a"); |
180 | if (!testi && testo && testt && !testa) |
181 | strcpy (c_mode, "w"); |
182 | if (testi && !testo && !testt && !testa) |
183 | strcpy (c_mode, "r"); |
184 | |
185 | |
186 | |
187 | |
188 | |
189 | |
190 | |
191 | if (strlen (c_mode) == 0) |
| |
192 | return false; |
193 | |
194 | strcat (c_mode, "b"); |
| 5 | | String copy function overflows destination buffer |
|
195 | |
196 | return true; |
197 | } |
198 | |
199 | |
200 | std::streamsize |
201 | gzfilebuf::showmanyc () |
202 | { |
203 | |
204 | if (! this->is_open () || !(io_mode & std::ios_base::in)) |
205 | return -1; |
206 | |
207 | if (this->gptr () && (this->gptr () < this->egptr ())) |
208 | return std::streamsize (this->egptr () - this->gptr ()); |
209 | else |
210 | return 0; |
211 | } |
212 | |
213 | |
214 | |
215 | |
216 | |
217 | gzfilebuf::int_type |
218 | gzfilebuf::pbackfail (gzfilebuf::int_type c) |
219 | { |
220 | if (this->is_open ()) |
221 | { |
222 | if (gzseek (file, this->gptr () - this->egptr () - 1, SEEK_CUR1) < 0) |
223 | return traits_type::eof (); |
224 | |
225 | |
226 | enable_buffer (); |
227 | |
228 | |
229 | |
230 | int bytes_read = gzread (file, buffer, buffer_size); |
231 | |
232 | if (bytes_read <= 0) |
233 | { |
234 | |
235 | this->setg (buffer, buffer, buffer); |
236 | return traits_type::eof (); |
237 | } |
238 | |
239 | |
240 | this->setg (buffer, buffer, buffer + bytes_read); |
241 | |
242 | |
243 | |
244 | gzfilebuf::int_type ret = traits_type::to_int_type (*(this->gptr ())); |
245 | if (ret != c) |
246 | return traits_type::eof (); |
247 | else |
248 | return ret; |
249 | } |
250 | else |
251 | return traits_type::eof (); |
252 | } |
253 | |
254 | |
255 | gzfilebuf::int_type |
256 | gzfilebuf::underflow () |
257 | { |
258 | |
259 | |
260 | |
261 | if (this->gptr () && (this->gptr () < this->egptr ())) |
262 | return traits_type::to_int_type (*(this->gptr ())); |
263 | |
264 | |
265 | if (! this->is_open () || !(io_mode & std::ios_base::in)) |
266 | return traits_type::eof (); |
267 | |
268 | |
269 | int stash = 0; |
270 | if (this->eback () && buffer && buffer_size > STASHED_CHARACTERS16) |
271 | { |
272 | char_type *ptr1 = buffer; |
273 | char_type *ptr2 = this->egptr () - STASHED_CHARACTERS16 + 1; |
274 | if (ptr2 > this->eback ()) |
275 | while (stash++ <= STASHED_CHARACTERS16) |
276 | *ptr1++ = *ptr2++; |
277 | } |
278 | |
279 | |
280 | |
281 | int bytes_read = gzread (file, buffer + stash, buffer_size - stash); |
282 | |
283 | |
284 | if (bytes_read <= 0) |
285 | { |
286 | |
287 | this->setg (buffer, buffer, buffer); |
288 | return traits_type::eof (); |
289 | } |
290 | |
291 | this->setg (buffer, buffer + stash, buffer + bytes_read + stash); |
292 | |
293 | |
294 | return traits_type::to_int_type (*(this->gptr ())); |
295 | } |
296 | |
297 | |
298 | gzfilebuf::int_type |
299 | gzfilebuf::overflow (int_type c) |
300 | { |
301 | |
302 | if (this->pbase ()) |
303 | { |
304 | |
305 | if (this->pptr () > this->epptr () || this->pptr () < this->pbase ()) |
306 | return traits_type::eof (); |
307 | |
308 | if (! traits_type::eq_int_type (c, traits_type::eof ())) |
309 | { |
310 | *(this->pptr ()) = traits_type::to_char_type (c); |
311 | this->pbump (1); |
312 | } |
313 | |
314 | int bytes_to_write = this->pptr () - this->pbase (); |
315 | |
316 | if (bytes_to_write > 0) |
317 | { |
318 | |
319 | if (! this->is_open () || !(io_mode & std::ios_base::out)) |
320 | return traits_type::eof (); |
321 | |
322 | if (gzwrite (file, this->pbase (), bytes_to_write) != bytes_to_write) |
323 | return traits_type::eof (); |
324 | |
325 | this->pbump (-bytes_to_write); |
326 | } |
327 | } |
328 | |
329 | else if (! traits_type::eq_int_type (c, traits_type::eof ())) |
330 | { |
331 | |
332 | if (! this->is_open () || !(io_mode & std::ios_base::out)) |
333 | return traits_type::eof (); |
334 | |
335 | char_type last_char = traits_type::to_char_type (c); |
336 | |
337 | if (gzwrite (file, &last_char, 1) != 1) |
338 | return traits_type::eof (); |
339 | } |
340 | |
341 | |
342 | |
343 | if (traits_type::eq_int_type (c, traits_type::eof ())) |
344 | return traits_type::not_eof (c); |
345 | else |
346 | return c; |
347 | } |
348 | |
349 | |
350 | std::streambuf* |
351 | gzfilebuf::setbuf (char_type* p, std::streamsize n) |
352 | { |
353 | |
354 | if (this->sync () == -1) |
355 | return 0; |
356 | |
357 | |
358 | |
359 | |
360 | |
361 | if (!p || !n) |
362 | { |
363 | |
364 | this->disable_buffer (); |
365 | buffer = 0; |
366 | buffer_size = 0; |
367 | own_buffer = true; |
368 | this->enable_buffer (); |
369 | } |
370 | else |
371 | { |
372 | |
373 | this->disable_buffer (); |
374 | buffer = p; |
375 | buffer_size = n; |
376 | own_buffer = false; |
377 | this->enable_buffer (); |
378 | } |
379 | return this; |
380 | } |
381 | |
382 | |
383 | int |
384 | gzfilebuf::sync () |
385 | { |
386 | return traits_type::eq_int_type (this->overflow (), |
387 | traits_type::eof ()) ? -1 : 0; |
388 | } |
389 | |
390 | |
391 | |
392 | |
393 | void |
394 | gzfilebuf::enable_buffer () |
395 | { |
396 | |
397 | if (own_buffer && !buffer) |
398 | { |
399 | |
400 | if (buffer_size > 0) |
401 | { |
402 | |
403 | buffer = new char_type [buffer_size]; |
404 | |
405 | this->setg (buffer, buffer, buffer); |
406 | |
407 | |
408 | |
409 | |
410 | |
411 | this->setp (buffer, buffer + buffer_size - 1); |
412 | } |
413 | else |
414 | { |
415 | |
416 | buffer_size = SMALLBUFSIZE1; |
417 | buffer = new char_type [buffer_size]; |
418 | this->setg (buffer, buffer, buffer); |
419 | |
420 | this->setp (0, 0); |
421 | } |
422 | } |
423 | else |
424 | { |
425 | |
426 | |
427 | this->setg (buffer, buffer, buffer); |
428 | this->setp (buffer, buffer + buffer_size - 1); |
429 | } |
430 | } |
431 | |
432 | |
433 | void |
434 | gzfilebuf::disable_buffer () |
435 | { |
436 | |
437 | if (own_buffer && buffer) |
438 | { |
439 | |
440 | if (! this->pbase ()) |
441 | buffer_size = 0; |
442 | delete[] buffer; |
443 | buffer = 0; |
444 | this->setg (0, 0, 0); |
445 | this->setp (0, 0); |
446 | } |
447 | else |
448 | { |
449 | |
450 | this->setg (buffer, buffer, buffer); |
451 | if (buffer) |
452 | this->setp (buffer, buffer + buffer_size - 1); |
453 | else |
454 | this->setp (0, 0); |
455 | } |
456 | } |
457 | |
458 | |
459 | |
460 | |
461 | gzfilebuf::pos_type |
462 | gzfilebuf::seekoff (off_type off, std::ios_base::seekdir way, |
463 | std::ios_base::openmode) |
464 | { |
465 | pos_type ret = pos_type (off_type (-1)); |
466 | |
467 | if (this->is_open ()) |
468 | { |
469 | off_type computed_off = off; |
470 | |
471 | if ((io_mode & std::ios_base::in) && way == std::ios_base::cur) |
472 | computed_off += this->gptr () - this->egptr (); |
473 | |
474 | |
475 | |
476 | if (off == 0 && way == std::ios_base::cur) |
477 | return pos_type (gztell (file) + computed_off); |
478 | |
479 | if (way == std::ios_base::beg) |
480 | ret = pos_type (gzseek (file, computed_off, SEEK_SET0)); |
481 | else if (way == std::ios_base::cur) |
482 | ret = pos_type (gzseek (file, computed_off, SEEK_CUR1)); |
483 | else |
484 | |
485 | ret = pos_type (gzseek (file, computed_off, SEEK_END2)); |
486 | |
487 | if (io_mode & std::ios_base::in) |
488 | |
489 | enable_buffer (); |
490 | else |
491 | |
492 | overflow (); |
493 | } |
494 | |
495 | return ret; |
496 | } |
497 | |
498 | gzfilebuf::pos_type |
499 | gzfilebuf::seekpos (pos_type sp, std::ios_base::openmode) |
500 | { |
501 | pos_type ret = pos_type (off_type (-1)); |
502 | |
503 | if (this->is_open ()) |
504 | { |
505 | ret = pos_type (gzseek (file, sp, SEEK_SET0)); |
506 | |
507 | if (io_mode & std::ios_base::in) |
508 | |
509 | enable_buffer (); |
510 | else |
511 | |
512 | overflow (); |
513 | } |
514 | |
515 | return ret; |
516 | } |
517 | |
518 | |
519 | |
520 | |
521 | gzifstream::gzifstream () |
522 | : std::istream (0), sb () |
523 | { this->init (&sb); } |
524 | |
525 | |
526 | gzifstream::gzifstream (const char* name, std::ios_base::openmode mode) |
527 | : std::istream (0), sb () |
528 | { |
529 | this->init (&sb); |
530 | this->open (name, mode); |
531 | } |
532 | |
533 | |
534 | gzifstream::gzifstream (int fd, std::ios_base::openmode mode) |
535 | : std::istream (0), sb () |
536 | { |
537 | this->init (&sb); |
538 | this->attach (fd, mode); |
539 | } |
540 | |
541 | |
542 | void |
543 | gzifstream::open (const char* name, std::ios_base::openmode mode) |
544 | { |
545 | if (! sb.open (name, mode | std::ios_base::in)) |
546 | this->setstate (std::ios_base::failbit); |
547 | else |
548 | this->clear (); |
549 | } |
550 | |
551 | |
552 | void |
553 | gzifstream::attach (int fd, std::ios_base::openmode mode) |
554 | { |
555 | if (! sb.attach (fd, mode | std::ios_base::in)) |
556 | this->setstate (std::ios_base::failbit); |
557 | else |
558 | this->clear (); |
559 | } |
560 | |
561 | |
562 | void |
563 | gzifstream::close () |
564 | { |
565 | if (! sb.close ()) |
566 | this->setstate (std::ios_base::failbit); |
567 | } |
568 | |
569 | |
570 | |
571 | |
572 | gzofstream::gzofstream () |
573 | : std::ostream (0), sb () |
574 | { this->init (&sb); } |
575 | |
576 | |
577 | gzofstream::gzofstream (const char* name, std::ios_base::openmode mode) |
578 | : std::ostream (0), sb () |
579 | { |
580 | this->init (&sb); |
581 | this->open (name, mode); |
582 | } |
583 | |
584 | |
585 | gzofstream::gzofstream (int fd, std::ios_base::openmode mode) |
586 | : std::ostream (0), sb () |
587 | { |
588 | this->init (&sb); |
589 | this->attach (fd, mode); |
590 | } |
591 | |
592 | |
593 | void |
594 | gzofstream::open (const char* name, std::ios_base::openmode mode) |
595 | { |
596 | if (! sb.open (name, mode | std::ios_base::out)) |
597 | this->setstate (std::ios_base::failbit); |
598 | else |
599 | this->clear (); |
600 | } |
601 | |
602 | |
603 | void |
604 | gzofstream::attach (int fd, std::ios_base::openmode mode) |
605 | { |
606 | if (! sb.attach (fd, mode | std::ios_base::out)) |
| 1 | Calling 'gzfilebuf::attach' | |
|
607 | this->setstate (std::ios_base::failbit); |
608 | else |
609 | this->clear (); |
610 | } |
611 | |
612 | |
613 | void |
614 | gzofstream::close () |
615 | { |
616 | if (! sb.close ()) |
617 | this->setstate (std::ios_base::failbit); |
618 | } |
619 | |
620 | #endif // HAVE_ZLIB |