/* binio.h */ /* Binary Input / Output */ #ifndef __PROG_BINIO_H__ #define __PROG_BINIO_H__ #include "std.h" #include "msglevel.h" // type MessageLevel #include #include OPEN_NAMESPACE #define INT64 #define BIN_INPUT_OUTPUT /* types */ #ifdef BIN_INPUT_OUTPUT // !? typedef signed char int8; typedef short int16; typedef int int32; typedef unsigned char uint8; typedef unsigned short uint16; typedef unsigned int uint32; #ifdef INT64 typedef long long int64; typedef unsigned long long uint64; #endif #endif typedef unsigned char byte; // !? /* TInput */ const size_t BufSize = 4 * 1024; class TInput { protected: istream * InpStream; size_t BufPos; size_t BufLen; byte Buf [BufSize]; string FileName; istream * release_stream; protected: virtual void Init (); virtual void ReadBuf (); void ReadData (void * adr, size_t size); virtual void Message (MessageLevel level, const string msg); public: void Open (istream & stream); void Open (string name); string GetFileName () { return FileName; } virtual void SetFileName (const string p_filename) { FileName = p_filename; } void Info (const string msg); void Note (const string msg); void Warning (const string msg); void Error (const string msg); void Abort (const string msg); TInput (); virtual ~ TInput (); }; /* TOutput */ class TOutput { protected: ostream * OutStream; size_t BufLen; byte Buf [BufSize]; string FileName; ostream * release_stream; protected: virtual void Init (); virtual void WriteBuf (); void WriteData (const void * adr, size_t size); void WriteOneByte (byte value); void WriteSameBytes (byte value, size_t size); virtual void Message (MessageLevel level, const string msg); public: void Open (ostream & stream); void Open (string name); void Flush (); string GetFileName () { return FileName; } void SetFileName (const string p_filename) { FileName = p_filename; } void Info (const string msg); void Note (const string msg); void Warning (const string msg); void Error (const string msg); void Abort (const string msg); TOutput (); virtual ~ TOutput (); }; /* ---------------------------------------------------------------------- */ /* TBinInput */ #ifdef BIN_INPUT_OUTPUT class TBinInput : public TInput { private: size_t ReadCode (); // uint16 ReadUtf8 (); public: void CheckEof (); bool ReadBool (); char ReadChar (); wchar_t ReadWideChar (); int8 ReadInt8 (); int16 ReadInt16 (); int32 ReadInt32 (); uint8 ReadUInt8 (); uint16 ReadUInt16 (); uint32 ReadUInt32 (); #ifdef INT64 int64 ReadInt64 (); uint64 ReadUInt64 (); #endif float ReadFloat (); double ReadDouble (); string ReadString (); void Read (bool & value) { value = ReadBool (); } void Read (char & value) { value = ReadChar (); } void Read (wchar_t & value) { value = ReadWideChar (); } void Read (int8 & value) { value = ReadInt8 (); } void Read (int16 & value) { value = ReadInt16 (); } void Read (int32 & value) { value = ReadInt32 (); } void Read (uint32 & value) { value = ReadUInt32 (); } void Read (uint8 & value) { value = ReadUInt8 (); } void Read (uint16 & value) { value = ReadUInt16 (); } #ifdef INT64 void Read (int64 & value) { value = ReadInt64 (); } void Read (uint64 & value) { value = ReadUInt64 (); } #endif void Read (float & value) { value = ReadFloat (); } void Read (double & value) { value = ReadDouble (); } void Read (string & value) { value = ReadString (); } }; #endif // BIN_INPUT_OUTPUT /* TBinOutput */ #ifdef BIN_INPUT_OUTPUT class TBinOutput : public TOutput { private: void WriteCode (size_t code); // void WriteUtf8 (uint16 code); public: void WriteBool (bool value); void WriteChar (char value); void WriteWideChar (wchar_t value); void WriteInt8 (int8 value); void WriteInt16 (int16 value); void WriteInt32 (int32 value); void WriteUInt8 (uint8 value); void WriteUInt16 (uint16 value); void WriteUInt32 (uint32 value); #ifdef INT64 void WriteInt64 (int64 value); void WriteUInt64 (uint64 value); #endif void WriteFloat (float value); void WriteDouble (double value); void WriteString (const string s); void Write (bool value) { WriteBool (value); } void Write (char value) { WriteChar (value); } void Write (wchar_t value) { WriteWideChar (value); } void Write (int8 value) { WriteInt8 (value); } void Write (int16 value) { WriteInt16 (value); } void Write (int32 value) { WriteInt32 (value); } void Write (uint8 value) { WriteUInt8 (value); } void Write (uint16 value) { WriteUInt16 (value); } void Write (uint32 value) { WriteUInt32 (value); } #ifdef INT64 void Write (int64 value) { WriteInt64 (value); } void Write (uint64 value) { WriteUInt64 (value); } #endif void Write (float value) { WriteFloat (value); } void Write (double value) { WriteDouble (value); } void Write (const string value) { WriteString (value); } }; #endif // BIN_INPUT_OUTPUT CLOSE_NAMESPACE #endif /* __PROG_BINIO_H__ */