File: bz2stream.h

package info (click to toggle)
vtk6 6.1.0+dfsg2-6
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 165,164 kB
  • ctags: 226,428
  • sloc: cpp: 1,354,490; ansic: 730,748; python: 227,134; tcl: 48,285; xml: 8,290; yacc: 4,832; java: 3,827; perl: 3,108; lex: 1,809; sh: 1,437; asm: 471; makefile: 229
file content (682 lines) | stat: -rw-r--r-- 15,113 bytes parent folder | download | duplicates (21)
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
/* Created (x) 2003, by Sandor Fazekas. For public domain. */
/* Last modified: $Date: 2009-08-31 14:46:54 $ $Revision: 1.1 $ */

#ifndef BZ2STREAM_H
#define BZ2STREAM_H

/* C++ stream classes for the bzip2 compression library. This is a */
/* lightweight C++ class library built on top of the excelent bzip2 */
/* compression library. Please note that the files handled by this */
/* library are opened in binary mode, which means that what you write */
/* is what you get, and there are no character substitutions (see e.g. */
/* Win32's two-character new-line). */

/* This code was inspired by Aaron Isotton's implementation of */
/* C++ Stream Classes for the bzip2 compression library. */

/* Modifications: 2009, Dominik Szczerba <dominik@itis.ethz.ch> */
/*                added multiple instances of std:: prefixes */

/* INCLUDE */

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <exception>
#include <string>

/* INCLUDE */

#include <bzlib.h>

/* DEFINE */

/* These define some default values. */

#define BZ2S_BLOCK_SIZE_100_K       9       /* 1..9 */
#define BZ2S_WORK_FACTOR            0       /* 0..255 */
#define BZ2S_SMALL                  0       /* TRUE or FALSE */
#define BZ2S_IN_BUF_LEN             1024    /* > 0 */
#define BZ2S_OUT_BUF_LEN            1024    /* > 0 */
#define BZ2S_OUT_BACK_LEN           256     /* >= 0 */

/* USE NAMESPACE */

//using namespace std;

/* CLASS DECLARATION */

class obz2buf : public std::streambuf
{
private:
  int bzBlockSize100K;
  int bzWorkFactor; 

private:
  int bzInBufLen;
  int bzOutBufLen;

private:
  FILE* file;

private:
  bz_stream bzStrm;

private:
  char* bzInBuf;
  char* bzOutBuf;

public:
  inline obz2buf( 
      int blockSize100K = BZ2S_BLOCK_SIZE_100_K, 
      int workFactor = BZ2S_WORK_FACTOR,
      int inBufLen = BZ2S_IN_BUF_LEN, 
      int outBufLen = BZ2S_OUT_BUF_LEN 
    ) :
      bzBlockSize100K ( blockSize100K ),
      bzWorkFactor ( workFactor ),
      bzInBufLen ( inBufLen ), 
      bzOutBufLen ( outBufLen )
  { 
    reset(); /* initialize class */
  }

public:
  virtual ~obz2buf() 
  {
    close(); /* ignore error */
  }

public:
  inline bool is_open() const { return file != NULL; }

public:
  obz2buf* open( const char* fileName )
  {
    if( ! isParmOk() || is_open() ) return NULL;
    if( ! fileOpen( fileName ) ) return NULL; /* open file */
    if( ! bzInit() ) return NULL; /* initialize the BZ stream */
    return this;
  }

  obz2buf* open( const std::string& fileName )
  {
    if( ! isParmOk() || is_open() ) return NULL;
    if( ! fileOpen( fileName.c_str() ) ) return NULL; /* open file */
    if( ! bzInit() ) return NULL; /* initialize the BZ stream */
    return this;
  }

  obz2buf* open( int fileDesc )
  {
    if( ! isParmOk() || is_open() ) return NULL; 
    if( ! fileOpen( fileDesc ) ) return NULL; /* open file */
    if( ! bzInit() ) return NULL; /* initialize the BZ stream */
    return this;
  }

public:
  obz2buf* close()
  {
    if( ! is_open() ) return NULL;
    bool done = bzProcess() && bzFinish();
    bzEnd(); /* close BZ stream */
    bool closed = fileClose(); /* close file */
    reset(); /* reinitialize class */
    return ( done && closed ) ? this : NULL;
  }

protected:
  virtual int overflow( int c ) 
  {
    if( c != EOF ) 
    {
      /* we allocated place for one more element! */
      *pptr() = c; pbump( 1 ); /* add one more element */
    }
    /* process buffered elements */
    return bzProcess() ? ( c != EOF ? c : !EOF ) : EOF;
  }

protected:
  virtual int sync() 
  {
    /* process buffered elements and flush the BZ stream */
    return ( bzProcess() && bzFlush() ) ? 0 : -1;
  }

private:
  inline bool bzInit()
  {
    bzInBuf = ( char* ) malloc( bzInBufLen );
    if( bzInBuf == NULL ) return false;

    bzOutBuf = ( char* ) malloc( bzOutBufLen );
    if( bzOutBuf == NULL ) return false;

    memset( bzInBuf, 0, bzInBufLen );
    memset( bzOutBuf, 0, bzOutBufLen );

    {
      int retVal = BZ2_bzCompressInit( &bzStrm, 
        bzBlockSize100K, 0, bzWorkFactor );
      if( retVal != BZ_OK ) return false;
    }

    /* position streambuf's put positions */
    setp( bzInBuf, bzInBuf + bzInBufLen - 1 );

    return true;
  }

private:
  inline void bzEnd()
  {
    BZ2_bzCompressEnd( &bzStrm );
    if( bzInBuf != NULL ) free( bzInBuf );
    if( bzOutBuf != NULL ) free( bzOutBuf );
  }

private:
  inline bool bzProcess()
  {
    int inLen = pptr() - pbase();

    bzStrm.next_in = pbase();
    bzStrm.avail_in = inLen;
        
    while( bzStrm.avail_in > 0 ) 
    {
      bzStrm.next_out = bzOutBuf;
      bzStrm.avail_out = bzOutBufLen;
      
      BZ2_bzCompress( &bzStrm, BZ_RUN );

      if( ! fileWrite() ) return false;
    }

    pbump( - inLen );

    return true;
  }

private:
  inline bool bzFlush() 
  {
    bool flushed = false;

    bzStrm.next_in = NULL;
    bzStrm.avail_in = 0;
    
    while( ! flushed )
    {
      bzStrm.next_out = bzOutBuf;
      bzStrm.avail_out = bzOutBufLen;

      {
        int retVal = BZ2_bzCompress( &bzStrm, BZ_FLUSH );
        flushed = ( retVal == BZ_RUN_OK );
      }

      if( ! fileWrite() || ! fileFlush() ) return false;
    } 

    return true;
  }

private:
  inline bool bzFinish() 
  {
    bool finished = false;

    bzStrm.next_in = NULL;
    bzStrm.avail_in = 0;
    
    while( ! finished )
    {
      bzStrm.next_out = bzOutBuf;
      bzStrm.avail_out = bzOutBufLen;

      {
        int retVal = BZ2_bzCompress( &bzStrm, BZ_FINISH );
        finished = ( retVal == BZ_STREAM_END );
      }

      if( ! fileWrite() ) return false;
    }

    return true;
  }

private:
  inline bool fileOpen( const char* fileName )
  {
    file = fopen( fileName, "wb" );
    return file != NULL;
  }

  inline bool fileOpen( int fileDesc )
  {
    file = fdopen( fileDesc, "wb" );
    return file != NULL;
  }

private:
  inline bool fileClose()
  {
    return fclose( file ) == 0;
  }

private:
  inline bool fileWrite()
  {
    int outLen = bzOutBufLen - bzStrm.avail_out;
    int retVal = fwrite( bzOutBuf, 1, outLen, file );
    return retVal == outLen;
  }

private:
  inline bool fileFlush()
  {
    return fflush( file ) == 0;
  }

private:
  inline bool isParmOk() const
  {
    if( bzBlockSize100K < 1 || bzBlockSize100K > 9 ) return false;
    if( bzWorkFactor < 0 || bzWorkFactor > 250 ) return false;
    if( bzInBufLen <= 0 ) return false;
    if( bzOutBufLen <= 0 ) return false;
    return true;
  }

private:
  inline void reset()
  {
    file = NULL; 
    memset( &bzStrm, 0, sizeof( bz_stream ) );
    bzInBuf = bzOutBuf = NULL;
    setp( NULL, NULL );
  }
};

/* CLASS DECLARATION */

class ibz2buf : public std::streambuf
{
private:
  int bzSmall;

private:
  int bzInBufLen;
  int bzOutBufLen;

private:
  int bzOutBackLen;

private:
  FILE* file;
  bool isBadType;

private:
  bz_stream bzStrm;

private:
  char* bzInBuf;
  char* bzOutBuf;

private:
  char* bzInBegin;
  char* bzInEnd;

private:
  char* bzOutBasePos;
  int bzOutBaseLen;

public:
  inline ibz2buf( 
      int small = BZ2S_SMALL,
      int inBufLen = BZ2S_IN_BUF_LEN, 
      int outBufLen = BZ2S_OUT_BUF_LEN + BZ2S_OUT_BACK_LEN, 
      int outBufBackLen = BZ2S_OUT_BACK_LEN 
    ) :
      bzSmall ( small ),
      bzInBufLen ( inBufLen ), 
      bzOutBufLen ( outBufLen ),
      bzOutBackLen ( outBufBackLen )
  { 
    reset(); /* initialize class */
  }

public:
  virtual ~ibz2buf() 
  {
    close(); /* ignore error */
  }

public:
  inline bool is_open() const { return file != NULL; }
  inline bool is_bad_type() const { return isBadType; }

public:
  ibz2buf* open( const char* fileName )
  {
    if( ! isParmOk() || is_open() ) return NULL;
    if( ! fileOpen( fileName ) ) return NULL; /* open file */
    if( ! bzInit() ) return NULL; /* initialize the BZ stream */
    return this;
  }

  ibz2buf* open( const std::string& fileName )
  {
    if( ! isParmOk() || is_open() ) return NULL;
    if( ! fileOpen( fileName.c_str() ) ) return NULL; /* open file */
    if( ! bzInit() ) return NULL; /* initialize the BZ stream */
    return this;
  }

  ibz2buf* open( int fileDesc )
  {
    if( ! isParmOk() || is_open() ) return NULL; 
    if( ! fileOpen( fileDesc ) ) return NULL; /* open file */
    if( ! bzInit() ) return NULL; /* initialize the BZ stream */
    return this;
  }

public:
  ibz2buf* close()
  {
    if( ! is_open() ) return NULL;
    bzEnd(); /* close BZ stream */
    bool closed = fileClose(); /* close file */
    reset(); /* reinitialize class */
    return closed ? this : NULL;
  }

protected:
  virtual int underflow() 
  {
    bool empty = true;

    int outLen = gptr() - eback(); /* calc size of putback area */
    int backLen = ( outLen < bzOutBackLen ) ? outLen : bzOutBackLen;

    if( backLen > 0 ) /* prepare putback area */
      memcpy( bzOutBasePos - backLen, gptr() - backLen, backLen );

    while( empty ) 
    {
      if( ( bzInBegin == bzInEnd ) && ! fileRead() ) return EOF;
      
      bzStrm.next_in = bzInBegin;
      bzStrm.avail_in = bzInEnd - bzInBegin;

      bzStrm.next_out = bzOutBasePos;
      bzStrm.avail_out = bzOutBaseLen;

      {
        int retVal = BZ2_bzDecompress( &bzStrm );

        empty = ( bzStrm.avail_out == ( unsigned ) bzOutBaseLen );

        isBadType = 
          ( retVal == BZ_DATA_ERROR_MAGIC ) ||
          ( retVal == BZ_DATA_ERROR );

        if( retVal == BZ_STREAM_END )
        {
          if( empty ) return EOF;
        }
        else if( retVal != BZ_OK ) 
        {
          return EOF;
        }
      }
      
      bzInBegin = bzInEnd - bzStrm.avail_in;
    } 

    setg( bzOutBasePos - backLen, bzOutBasePos,
         bzOutBasePos + bzOutBaseLen - bzStrm.avail_out );

    return *( ( unsigned char* ) gptr() );
  }

private:
  inline bool bzInit()
  {
    bzInBuf = ( char* ) malloc( bzInBufLen );
    if( bzInBuf == NULL ) return false;

    bzOutBuf = ( char* ) malloc( bzOutBufLen );
    if( bzOutBuf == NULL ) return false;

    memset( bzInBuf, 0, bzInBufLen );
    memset( bzOutBuf, 0, bzOutBufLen );

    {
      int retVal = BZ2_bzDecompressInit( &bzStrm, 0, bzSmall );
      if( retVal != BZ_OK ) return false;
    }

    bzInBegin = bzInEnd = bzInBuf;

    bzOutBasePos = bzOutBuf + bzOutBackLen;
    bzOutBaseLen = bzOutBufLen - bzOutBackLen;

    setg( bzOutBasePos, bzOutBasePos, bzOutBasePos );

    return true;
  }

private:
  inline void bzEnd()
  {
    BZ2_bzDecompressEnd( &bzStrm );
    if( bzInBuf != NULL ) free( bzInBuf );
    if( bzOutBuf != NULL ) free( bzOutBuf );
  }

private:
  inline bool fileOpen( const char* fileName )
  {
    file = fopen( fileName, "rb" );
    return file != NULL;
  }

  inline bool fileOpen( int fileDesc )
  {
    file = fdopen( fileDesc, "rb" );
    return file != NULL;
  }

private:
  inline bool fileClose()
  {
    return fclose( file ) == 0;
  }

private:
  inline bool fileRead()
  {
    int inLen = fread( bzInBuf, 1, bzInBufLen, file );
    bzInBegin = bzInBuf; bzInEnd = bzInBegin + inLen;
    return inLen > 0;
  }

private:
  inline bool isParmOk() const
  {
    if( bzInBufLen <= 0 ) return false;
    if( bzOutBufLen <= 0 ) return false;
    if( bzOutBackLen < 0 ) return false;
    if( bzOutBackLen >= bzOutBufLen ) return false;
    return true;
  }

private:
  inline void reset()
  {
    file = NULL; 
    isBadType = false;
    memset( &bzStrm, 0, sizeof( bz_stream ) );
    bzInBuf = bzOutBuf = NULL;
    bzInBegin = bzInEnd = NULL;
    bzOutBasePos = NULL;
    bzOutBaseLen = 0;
    setg( NULL, NULL, NULL );
  }
};

/* CLASS DECLARATION */

class obz2stream : public std::ostream
{
private:
  obz2buf buf;

public:
  inline explicit obz2stream(
      int blockSize100K = BZ2S_BLOCK_SIZE_100_K, 
      int workFactor = BZ2S_WORK_FACTOR,
      int inBufLen = BZ2S_IN_BUF_LEN, 
      int outBufLen = BZ2S_OUT_BUF_LEN 
    ) : std::ostream( &buf ), 
        buf( blockSize100K, workFactor, inBufLen, outBufLen )
  {}

  inline explicit obz2stream(
      const char* fileName,
      int blockSize100K = BZ2S_BLOCK_SIZE_100_K, 
      int workFactor = BZ2S_WORK_FACTOR,
      int inBufLen = BZ2S_IN_BUF_LEN, 
      int outBufLen = BZ2S_OUT_BUF_LEN 
    ) : std::ostream( &buf ), 
        buf( blockSize100K, workFactor, inBufLen, outBufLen )
  { 
    open( fileName ); 
  }

  inline explicit obz2stream(
      const std::string& fileName,
      int blockSize100K = BZ2S_BLOCK_SIZE_100_K, 
      int workFactor = BZ2S_WORK_FACTOR,
      int inBufLen = BZ2S_IN_BUF_LEN, 
      int outBufLen = BZ2S_OUT_BUF_LEN 
    ) : std::ostream( &buf ), 
        buf( blockSize100K, workFactor, inBufLen, outBufLen )
  { 
    open( fileName ); 
  }

public:
  virtual ~obz2stream() {}

public:
  inline obz2buf* rdbuf() const { return ( obz2buf* ) &buf; }

public:
  inline bool is_open() const { return buf.is_open(); }

public:
  inline void open( const char* fileName )
  {
    if( buf.open( fileName ) == NULL ) setstate( failbit );
  }

  inline void open( const std::string& fileName )
  {
    if( buf.open( fileName ) == NULL ) setstate( failbit );
  }

  inline void open( int fileDesc )
  {
    if( buf.open( fileDesc ) == NULL ) setstate( failbit );
  }

public:
  inline void close() 
  { 
    if( buf.close() == NULL ) setstate( failbit ); 
  }
};

/* CLASS DECLARATION */

class ibz2stream : public std::istream
{
private:
  ibz2buf buf;

public:
  inline explicit ibz2stream(
      int small = BZ2S_SMALL,
      int inBufLen = BZ2S_IN_BUF_LEN, 
      int outBufLen = BZ2S_OUT_BUF_LEN + BZ2S_OUT_BACK_LEN, 
      int outBufBackLen = BZ2S_OUT_BACK_LEN 
    ) : std::istream( &buf ), 
        buf( small, inBufLen, outBufLen, outBufBackLen )
  {}

  inline explicit ibz2stream(
      const char* fileName,
      int small = BZ2S_SMALL,
      int inBufLen = BZ2S_IN_BUF_LEN, 
      int outBufLen = BZ2S_OUT_BUF_LEN + BZ2S_OUT_BACK_LEN, 
      int outBufBackLen = BZ2S_OUT_BACK_LEN 
    ) : std::istream( &buf ), 
        buf( small, inBufLen, outBufLen, outBufBackLen )
  { 
    open( fileName ); 
  }

  inline explicit ibz2stream(
      const std::string& fileName,
      int small = BZ2S_SMALL,
      int inBufLen = BZ2S_IN_BUF_LEN, 
      int outBufLen = BZ2S_OUT_BUF_LEN + BZ2S_OUT_BACK_LEN, 
      int outBufBackLen = BZ2S_OUT_BACK_LEN 
    ) : std::istream( &buf ), 
        buf( small, inBufLen, outBufLen, outBufBackLen )
  { 
    open( fileName ); 
  }

public:
  virtual ~ibz2stream() {}

public:
  inline ibz2buf* rdbuf() const { return ( ibz2buf* ) &buf; }

public:
  inline bool is_open() const { return buf.is_open(); }
  inline bool is_bad_type() const { return buf.is_bad_type(); }

public:
  inline void open( const char* fileName )
  {
    if( buf.open( fileName ) == NULL ) setstate( failbit );
  }

  inline void open( const std::string& fileName )
  {
    if( buf.open( fileName ) == NULL ) setstate( failbit );
  }

  inline void open( int fileDesc )
  {
    if( buf.open( fileDesc ) == NULL ) setstate( failbit );
  }

public:
  inline void close() 
  { 
    if( buf.close() == NULL ) setstate( failbit ); 
  }
};

#endif /* BZ2STREAM_H */

/* End of File */