LZW Compressor
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Pages
CodeReader Class Reference

Variable binary width code reader. More...

Public Member Functions

 CodeReader (std::istream &is)
 Default constructor. More...
 
std::size_t get_bits () const
 Getter for CodeReader::bits.
 
void reset_bits ()
 Resets internal binary width. More...
 
void increase_bits ()
 Increases internal binary width by one. More...
 
bool read (CodeType &k)
 Reads the code k with a binary width of CodeReader::bits. More...
 
bool corrupted () const
 Returns if EF is considered corrupted. More...
 

Private Attributes

std::istream & is
 Input Stream.
 
std::size_t bits
 Binary width of codes.
 
bool feofmc
 Found End-Of-File MetaCode.
 
ByteCache lo
 LeftOvers.
 

Detailed Description

Variable binary width code reader.

Definition at line 322 of file lzw_v6.cpp.

Constructor & Destructor Documentation

CodeReader::CodeReader ( std::istream &  is)
inlineexplicit

Default constructor.

Parameters
[in]isInput Stream to read codes from

Definition at line 329 of file lzw_v6.cpp.

329  : is(is), bits(CHAR_BIT + 1), feofmc(false)
330  {
331  }

Member Function Documentation

bool CodeReader::corrupted ( ) const
inline

Returns if EF is considered corrupted.

Return values
truedidn't find end-of-file metacode
falsefound end-of-file metacode

Definition at line 416 of file lzw_v6.cpp.

References feofmc.

417  {
418  return !feofmc;
419  }
void CodeReader::increase_bits ( )
inline

Increases internal binary width by one.

Exceptions
std::overflow_errorif internal binary width cannot be increased
Remarks
The exception should never be thrown, under normal circumstances.

Definition at line 355 of file lzw_v6.cpp.

References bits.

356  {
357 #ifdef TAKE_NO_RISKS
358  if (bits == SIZE_MAX)
359  throw std::overflow_error("CodeReader::increase_bits()");
360 #endif
361  ++bits;
362  }
bool CodeReader::read ( CodeType k)
inline

Reads the code k with a binary width of CodeReader::bits.

Parameters
[out]kcode to be read
Returns
Whether or not the stream can be used for input.
Return values
truethe input stream can still be used
falsethe input stream can no longer be used

Definition at line 371 of file lzw_v6.cpp.

References bits, ByteCache::data, feofmc, is, lo, and ByteCache::used.

372  {
373  // ready-made bit masks
374  static const std::array<unsigned long int, 9> masks {
375  {0x00, 0x01, 0x03, 0x07, 0x0F, 0x1F, 0x3F, 0x7F, 0xFF}
376  };
377 
378  std::size_t remaining_bits {bits};
379  std::size_t offset {lo.used};
380  unsigned char temp;
381 
382  k = lo.data;
383  remaining_bits -= lo.used;
384  lo.used = 0;
385  lo.data = 0x00;
386 
387  while (remaining_bits != 0 && is.get(reinterpret_cast<char &> (temp)))
388  if (remaining_bits >= CHAR_BIT)
389  {
390  k |= static_cast<CodeType> (temp) << offset;
391  offset += CHAR_BIT;
392  remaining_bits -= CHAR_BIT;
393  }
394  else
395  {
396  k |= static_cast<CodeType> (temp & masks[remaining_bits]) << offset;
397  lo.used = CHAR_BIT - remaining_bits;
398  lo.data = temp >> remaining_bits;
399  break;
400  }
401 
402  if (k == static_cast<CodeType> (MetaCode::Eof))
403  {
404  feofmc = true;
405  return false;
406  }
407 
408  return is;
409  }
void CodeReader::reset_bits ( )
inline

Resets internal binary width.

Note
Default value is CHAR_BIT + 1.

Definition at line 345 of file lzw_v6.cpp.

References bits.

346  {
347  bits = CHAR_BIT + 1;
348  }

The documentation for this class was generated from the following file: