/* txtio.h */ /* Text Input / Output */ #ifndef __PROG_TXTIO_H__ #define __PROG_TXTIO_H__ #include "binio.h" OPEN_NAMESPACE #define TEXT_INPUT_OUTPUT /* ---------------------------------------------------------------------- */ enum TToken { StartToken, IdentToken, // identifier NumberToken, // number CharToken, // character in single quotes StringToken, // string in double quotes SeparatorToken, // single character separator EofToken // end of file }; const int DuplMax = 255; /* ---------------------------------------------------------------------- */ class TTxtInput : public TInput { private: int LinePos; // line counter int ColPos; // column counter int CharPos; // character counter int TokenLinePos; int TokenColPos; int TokenCharPos; char InpCh; // current inpput character char PrevCh; // previous character (for end of line detection) bool PrevEol; // end of line detected (but LinePos will be incremented with next character) bool InpEof; // end of source text bool PositionInitialized; int DuplLen; char DuplBuf [DuplMax + 1]; // copy of current token int ValueLen; char ValueBuf [DuplMax + 1]; // string value public: TToken TokenKind; string TokenTxt; // original token string TokenVal; // value of string bool SemicolonComments; bool HashComments; private: virtual void Init (); void GetCh (); void SetTokenLine (int p_line); void SetTokenCol (int p_col); virtual void Message (MessageLevel level, const string msg); public: string GetFileName (); virtual void SetFileName (const string p_filename); int GetTokenLine () { return TokenLinePos; } int GetTokenCol() { return TokenColPos; } void Debug (const string msg); void Info (const string msg); void Note (const string msg); void Warning (const string msg); void Flaw (const string msg); void Error (const string msg); void Abort (const string msg); private: void NextCh (); void StoreCharacter (char c); char Character (); void GetIdent (); void GetNumber (); void GetChar (); void GetString (); void StoreSeparator (char ch); virtual void GetSeparator (); void SkipLine (); void SkipWhiteSpace (); public: virtual void GetToken (); bool IsIdent () { return TokenKind == IdentToken; } bool IsNumber () { return TokenKind == NumberToken; } bool IsChar () { return TokenKind == CharToken; } bool IsString () { return TokenKind == StringToken; } bool IsSeparator () { return TokenKind == SeparatorToken; } bool IsEof () { return TokenKind == EofToken; } bool IsIdent (const string par); bool IsSeparator (char par); void CheckIdent (const string par); void CheckSeparator (char par); void CheckEof (); string ReadIdent (); char ReadChar (); string ReadNumber (); string ReadSignedNumber (); // including minus sign string ReadString (); #ifdef TEXT_INPUT_OUTPUT bool ReadBool (); int ReadInt (); int8 ReadInt8 (); int16 ReadInt16 (); int32 ReadInt32 (); uint8 ReadUInt8 (); uint16 ReadUInt16 (); uint32 ReadUInt32 (); #ifdef INT64 int64 ReadInt64 (); uint64 ReadUInt64 (); #endif float ReadFloat (); double ReadDouble (); void Read (bool & value) { value = ReadBool (); } void Read (char & value) { value = ReadChar (); } void Read (int8 & value) { value = ReadInt8 (); } void Read (int16 & value) { value = ReadInt16 (); } void Read (int32 & value) { value = ReadInt32 (); } void Read (uint8 & value) { value = ReadUInt8 (); } void Read (uint16 & value) { value = ReadUInt16 (); } void Read (uint32 & value) { value = ReadUInt32 (); } #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 // TEXT_INPUT_OUTPUT TTxtInput (); virtual ~ TTxtInput (); }; /* ---------------------------------------------------------------------- */ class TTxtOutput : public TOutput { private: bool ShowMsg; bool StartLine; int Indent; public: int LinePos; int ColPos; protected: virtual void Init (); public: string GetFileName () { return FileName; } void SetFileName (const string p_filename) { FileName = p_filename; } void ReadFrom (const string file_name); protected: virtual void Message (MessageLevel level, const string msg); public: void Debug (const string msg); void Info (const string msg); void Note (const string msg); void Warning (const string msg); void Error (const string msg); void Flaw (const string msg); void Abort (const string msg); protected: void PutPlainChr (char c); void PutPlainSpaces (int n); void PutPlainStr (const string s); void PutPlainData (void * adr, size_t len); void PutPlainEol (); virtual void OpenLine (); virtual void CloseLine (); public: void Put (const string s); void PutChr (char c); void PutSpaces (int n); void PutEol (); void PutCondEol (); void PutLn (const string s); void PutBlock (const string s); void SetIndent (int i); void IncIndent (); void DecIndent (); #ifdef TEXT_INPUT_OUTPUT void WriteBool (bool value); void WriteChar (char value); void WriteInt (int 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 value); void Write (bool value) { WriteBool (value); } void Write (char value) { WriteChar (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 // TEXT_INPUT_OUTPUT public: TTxtOutput (); virtual ~ TTxtOutput (); }; /* ---------------------------------------------------------------------- */ CLOSE_NAMESPACE #endif /* __PROG_TXTIO_H__ */