📄 coder_base.h
字号:
/*******************************************************************************++ LEDA 4.5 +++ coder_base.h+++ Copyright (c) 1995-2004+ by Algorithmic Solutions Software GmbH+ All rights reserved.+ *******************************************************************************/// $Revision: 1.20 $ $Date: 2004/02/23 17:12:02 $#ifndef _LEDA_CODER_BASE_H#define _LEDA_CODER_BASE_H#if !defined(LEDA_ROOT_INCL_ID)#define LEDA_ROOT_INCL_ID 450352#include <LEDA/PREAMBLE.h>#endif#include <LEDA/string.h>#if defined(_MSC_VER) && _MSC_VER < 1300#define LEDA_COMPRESSION_WA6#endif#if !defined(LEDA_STD_HEADERS) && (defined(__DECCXX) || defined (__HP_aCC))#define LEDA_COMPRESSION_WA8#define LEDA_COMPRESSION_WA9#endifLEDA_BEGIN_NAMESPACE/// coder_types /////////////////////////////////////////////////////////////////////////////#include <LEDA/std/limits.h>// determine 16 bits integers#undef LEDA_CODER_EXACT_SIZE_INT16#undef LEDA_UINT16#undef LEDA_SINT16#if (UINT_MAX == 0xFFFFUL)#define LEDA_UINT16 unsigned int#define LEDA_SINT16 signed int#define LEDA_CODER_EXACT_SIZE_INT16#elif (ULONG_MAX == 0xFFFFUL)#define LEDA_UINT16 unsigned long#define LEDA_SINT16 signed long#define LEDA_CODER_EXACT_SIZE_INT16#elif (USHRT_MAX == 0xFFFFUL)#define LEDA_UINT16 unsigned short#define LEDA_SINT16 signed short#define LEDA_CODER_EXACT_SIZE_INT16#elif (USHRT_MAX >= 0xFFFFUL)#define LEDA_UINT16 unsigned short#define LEDA_SINT16 signed short#elif (UINT_MAX >= 0xFFFFUL)#define LEDA_UINT16 unsigned int#define LEDA_SINT16 signed int#elif (ULONG_MAX >= 0xFFFFUL)#define LEDA_UINT16 unsigned long#define LEDA_SINT16 signed long#endif// determine 32 bits integers#undef LEDA_CODER_EXACT_SIZE_INT32#undef LEDA_UINT32#undef LEDA_SINT32#if (UINT_MAX == 0xFFFFFFFFUL)#define LEDA_UINT32 unsigned int#define LEDA_SINT32 signed int#define LEDA_CODER_EXACT_SIZE_INT32#elif (ULONG_MAX == 0xFFFFFFFFUL)#define LEDA_UINT32 unsigned long#define LEDA_SINT32 signed long#define LEDA_CODER_EXACT_SIZE_INT32#elif (USHRT_MAX == 0xFFFFFFFFUL)#define LEDA_UINT32 unsigned short#define LEDA_SINT32 signed short#define LEDA_CODER_EXACT_SIZE_INT32#elif (USHRT_MAX >= 0xFFFFFFFFUL)#define LEDA_UINT32 unsigned short#define LEDA_SINT32 signed short#elif (UINT_MAX >= 0xFFFFFFFFUL)#define LEDA_UINT32 unsigned int#define LEDA_SINT32 signed int#elif (ULONG_MAX >= 0xFFFFFFFFUL)#define LEDA_UINT32 unsigned long#define LEDA_SINT32 signed long#endif// LEDA_CODER_EXACT_SIZE_INT16 should be defined if sizeof(uint16)==sizeof(sint16)==2// LEDA_CODER_EXACT_SIZE_INT32 should be defined if sizeof(uint32)==sizeof(sint32)==4#if !defined(LEDA_UINT32)#error "no integer type with at least 32 bits on this platform"#endif#ifndef LEDA_CODER_TEST_64BITclass __exportC coder_types {public: typedef unsigned char byte; // exactly 8 bits (and unsigned) typedef LEDA_UINT16 uint16; // at least 16 bits (and unsigned) (faster if exactly 16) typedef LEDA_SINT16 sint16; // at least 16 bits (and signed) typedef LEDA_UINT32 uint32; // at least 32 bits (and unsigned) (faster if exactly 32) typedef LEDA_SINT32 sint32; // at least 32 bits (and signed) typedef uint32 id_type;// at least 32 bits (and unsigned)};#else// use types with non-exact size (for testing purposes)class __exportC coder_types {public: typedef unsigned char byte; typedef unsigned __int32 uint16; typedef signed __int32 sint16; typedef unsigned __int64 uint32; typedef signed __int64 sint32; typedef unsigned int id_type;};#undef LEDA_CODER_EXACT_SIZE_INT16#undef LEDA_CODER_EXACT_SIZE_INT32#endif#undef LEDA_UINT16#undef LEDA_SINT16#undef LEDA_UINT32#undef LEDA_SINT32/// coder_base //////////////////////////////////////////////////////////////////////////////#ifndef LEDA_COMPRESSION_WA8#define LC_READ_EOF(c) c#define LC_WRITE_EOF(c) c#else#define LC_READ_EOF(c) check_eof(c, get_src_stream())#define LC_WRITE_EOF(c) check_eof(c, get_tgt_stream())#endifclass __exportC coder_base : public coder_types {public: enum { StreamBlocked = -2 };#ifndef __DECCXX enum { binary = ios::binary };#else enum { binary = 0 };#endifpublic: coder_base(streambuf* src_stream = 0, streambuf* tgt_stream = 0, bool own_streams = false); coder_base(const char* src_file_name, const char* tgt_file_name); virtual ~coder_base(); void encode(); void decode(); streambuf* get_src_stream() const { return pSrcStream; } void set_src_stream(streambuf* src_stream, bool own_stream = false); void set_src_file(const char* file_name, ios::openmode mode = ios::openmode(ios::in | binary)); streambuf* get_tgt_stream() const { return pTgtStream; } void set_tgt_stream(streambuf* tgt_stream, bool own_Stream = false); void set_tgt_file(const char* file_name, ios::openmode mode = ios::openmode(ios::out | ios::trunc | binary)); virtual void reset(); // put coder in exactly the same state as the default ctor bool fail() const { return State == s_error; } bool finished() const { return State == s_finished; } virtual string get_description() const { return string(get_class_name()); } virtual bool is_single_pass_encoder() const { return true; } virtual bool is_standalone_coder() const { return true; }protected: // the following functions should be overriden in a derived class virtual const char* get_class_name() const = 0; virtual id_type get_id() const = 0; virtual void init_encoding(); virtual void encode_stream() { encoding_finished(); } virtual void encoding_finished(); virtual void init_decoding(); virtual void decode_stream() { decoding_finished(); } virtual void decoding_finished(); protected: // helpers for derived classes enum coder_state { s_init = 0, s_encoding = 1, s_decoding = 2, s_finished = 3, s_error = 4 }; coder_state get_state() const { return State; } void error(const char* err_msg = 0); void no_id_in_streams() { ProcessIdInStreams = false; } bool look_for_id_in_streams() const { return ProcessIdInStreams; } virtual void write_id_to_tgt_stream(); virtual void read_and_check_id_in_src_stream(); virtual void check_id(id_type id_to_check); int read_byte() { return LC_READ_EOF(pSrcStream->sbumpc()); } // read one character, returns the character or EOF (=failure) int read_bytes(byte* buffer, int buf_size) { return pSrcStream->sgetn((char*)buffer, buf_size); } // read maximum buf_size chars into buffer, returns number of chars read void read_uint16(uint16& val); // read uint16 (calls error() if it fails) void read_uint32(uint32& val); // read uint32 (calls error() if it fails) void read_id(id_type& val) { if (! read_id(val, pSrcStream)) error(); } // read id_type (calls error() if it fails)#ifndef LEDA_COMPRESSION_WA6 int write_byte(byte b) { return LC_WRITE_EOF(pTgtStream->sputc((char) b)); } // write one character to stream, returns the character or EOF (=failure)#else int write_byte(byte b) { return LC_WRITE_EOF(pTgtStream->sputc((int) b)); }#endif int write_bytes(byte* buffer, int buf_size) { return pTgtStream->sputn((char*)buffer, buf_size); } // write buf_size chars from buffer to stream, returns number of chars written void write_uint16(uint16 val); // write uint16 (calls error() if it fails) void write_uint32(uint32 val); // write uint32 (calls error() if it fails) void write_id(id_type val) { if (! write_id(val, pTgtStream)) error(); } // write id_type (calls error() if it fails) void rewind_src_stream() { if (pSrcStream->pubseekpos(0) != streampos(0)) error(); } void rewind_tgt_stream() { if (pTgtStream->pubseekpos(0) != streampos(0)) error(); } static bool read_id(id_type& val, streambuf* stream); static bool write_id(id_type val, streambuf* stream); static coder_base* construct_from_stream(streambuf* stream); virtual void finish_construction_from_stream(id_type id_in_stream, streambuf* stream); friend class __exportC coder_factory; friend class __exportC coder_pipe;#ifdef LEDA_COMPRESSION_WA8 static int check_eof(int c, streambuf* stream);#endifpublic: inline static void swap_bytes(uint16& i); inline static void swap_bytes(uint32& i);private: coder_base(const coder_base&); // forbidden const coder_base& operator=(const coder_base&); // forbiddenprivate: coder_state State; streambuf* pSrcStream; streambuf* pTgtStream; bool OwnSrcStream; bool OwnTgtStream; bool ProcessIdInStreams;};inline void coder_base::swap_bytes(uint16& i){ uint16 b1 = (i << 8) & 0xFF00; // take byte 0 and shift it into byte 1 uint16 b0 = (i >> 8) & 0x00FF; // take byte 1 and shift it into byte 0 i = b0 | b1;}inline void coder_base::swap_bytes(uint32& i){ uint32 b3 = (i << 24) & 0xFF000000; uint32 b2 = (i << 8) & 0x00FF0000; uint32 b1 = (i >> 8) & 0x0000FF00; uint32 b0 = (i >> 24) & 0x000000FF; i = b0 | b1 | b2 | b3;}/// coder_base_debug //////////////////////////////////////////////////////////////////////////#define LEDA_CODER_DEBUG#ifdef LEDA_CODER_DEBUGclass __exportC coder_base_debug : public coder_base {public: typedef coder_base base;public: coder_base_debug(streambuf* src_stream = 0, streambuf* tgt_stream = 0, bool own_streams = false); coder_base_debug(const char* src_file_name, const char* tgt_file_name); static coder_base_debug* construct_from_stream(streambuf* stream);protected: int read_byte(); int read_bytes(byte* buffer, int buf_size); int write_byte(byte b); int write_bytes(byte* buffer, int buf_size);private: int log_read(int v); int log_write(int v);private: uint32 ReadLogCount; int ReadLog[2048]; uint32 WriteLogCount; int WriteLog[2048];};#define coder_base coder_base_debug#endif // ifdef LEDA_CODER_DEBUG#if LEDA_ROOT_INCL_ID == 450352#undef LEDA_ROOT_INCL_ID#include <LEDA/POSTAMBLE.h>#endifLEDA_END_NAMESPACE#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -