/* * The Unix Channel * * by Michel Beaudouin-Lafon * * Copyright 1990-1993 * Laboratoire de Recherche en Informatique (LRI) * * Text streams * * $Id$ * $CurLog$ */ #ifndef TextStream_H_ #define TextStream_H_ #include "cplus_bugs.h" #include "Stream.h" #include "MsgBuffer.h" #include "MsgStream.h" #include "ccu/String.h" class UchTextLine; class UchTextStream : public UchBufStream { public: enum cmd_res { isCmdSyntax, // syntax error when parsing command isCmdUnknown, // unknown command isCmdOk, // request executed isCmdError, // problem while executing request isCmdClose, // close connection isCmdQuit, // quit server isCmdTerminate, // terminate multiplexer isCmdAbort, // abort multiplexer isCmdExit // call exit }; protected: void Closing (bool); void HandleRead (); void Process (UchMsgBuffer&); void Flush (); void ProcessCmdResult (cmd_res, const UchTextLine&); cmd_res TryPredefined (const UchTextLine&); virtual cmd_res Execute (const UchTextLine&) = 0; void WriteLong (lword); void WriteShort (sword); void WriteByte (byte); void WriteChar (char); void WriteString (const char*); bool ReadLong (lword&); bool ReadShort (sword&); bool ReadByte (byte&); bool ReadChar (char&); int ReadString (char*, int); int ReadString (CcuString&); public: UchTextStream (UchAddress* = 0, UchAddress* = 0); ~UchTextStream (); // UchTextStream (const UchTextStream&); // UchChannel* Copy () const; virtual void Close (); // 'close' request virtual void Quit (); // 'quit' request void Send (const char*, bool = false); void Send (const UchTextLine&, bool = false); }; class UchTextWord { protected: const char* Sval; int Ival; public: inline UchTextWord () : Sval (0), Ival (0) {} inline UchTextWord (const char* s) { SetVal (s); } inline UchTextWord (int i) { SetVal (i); } void SetVal (const char*); inline void SetVal (int i) { Sval = 0; Ival = i; } inline bool IsInt () const { return (Sval == 0) ? true : false; } inline bool IsString () const { return (Sval != 0) ? true : false; } const char* GetQuotes () const; inline operator int () const { return Ival; } inline operator const char* () const { return Sval; } }; class UchTextLine { protected: int Num; int Max; UchTextWord* Words; void NewWord (const char* s, int i); public: UchTextLine (); ~UchTextLine (); inline UchTextWord& operator [] (int i) const { return Words [i]; } inline int NumWords () const { return Num; } inline void AddWord (const char* s) { NewWord (s, 0); } inline void AddWord (int i) { NewWord (0, i); } void AddTrailer (const UchTextLine&, int); inline UchTextLine& operator << (int i) { AddWord (i); return *this; } inline UchTextLine& operator << (const char* s) { AddWord (s); return *this; } inline UchTextLine& operator << (const UchTextLine& l) { AddTrailer (l, 0); return *this; } bool Parse (char*); char* Unparse (char* dest, int len) const; char* Unparse (UchMsgBuffer*) const; bool Match (const char*, const char* = 0) const; int Index (const char*) const; int Index (int) const; inline bool Contains (const char* w) const { return (Index (w) == -1) ? false : true; } inline bool Contains (int i) const { return (Index (i) == -1) ? false : true; } inline void Reset () { Num = 0; } }; #endif /* TextStream_H_ */