File: decompress.cpp

package info (click to toggle)
libclaw 1.7.4-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,080 kB
  • sloc: cpp: 13,287; sh: 227; makefile: 8
file content (98 lines) | stat: -rw-r--r-- 2,046 bytes parent folder | download | duplicates (8)
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
#include <claw/lzw_decoder.hpp>
#include <claw/bit_istream.hpp>
#include <claw/buffered_istream.hpp>
#include <claw/buffered_ostream.hpp>
#include <iostream>
#include <limits>
#include <cassert>

/*----------------------------------------------------------------------------*/
class input_buffer
{
public:
  input_buffer( std::istream& is )
    : val(0), buffer(is), stream(buffer), m_code_size(9),
      m_code_limit(1 << m_code_size)
  {

  }

  bool end_of_data() const { return !stream || (val == 256); }

  unsigned int symbols_count() const
  {
    return (unsigned int)std::numeric_limits<unsigned char>::max() + 2;
  }

  unsigned int get_next()
  {
    val = 0;
    stream.read( reinterpret_cast<char*>(&val), m_code_size );
    return val;
  }

  void reset()
  {
    val = 0;
    m_code_size = 9;
    m_code_limit = 1 << m_code_size;
  }

  void new_code( unsigned int code )
  {
    if ( (code == m_code_limit) && (m_code_size != 12) )
      {
        ++m_code_size;
        m_code_limit = 1 << m_code_size;
      }
  }

private:
  unsigned int val;
  claw::buffered_istream<std::istream> buffer;
  claw::bit_istream< claw::buffered_istream<std::istream> > stream;

  unsigned int m_code_size;
  unsigned int m_code_limit;
};

/*----------------------------------------------------------------------------*/
class output_buffer
{
public:
  output_buffer( std::ostream& os, unsigned int max )
    : stream(os), m_max_code(max)
  { }

  unsigned int max_code() const
  {
    return m_max_code;
  }

  void write( unsigned int code )
  {
    assert(code < 256);
    const char v(code);
    stream.write(v);
  }

private:
  claw::buffered_ostream<std::ostream> stream;
  const unsigned int m_max_code;
};

/*----------------------------------------------------------------------------*/
int main()
{
  claw::lzw_decoder<input_buffer, output_buffer> decoder;
  input_buffer input(std::cin);
  output_buffer output(std::cout, 4096);

  while ( std::cin )
    {
      input.reset();
      decoder.decode(input, output);
    }

  return 0;
}