From e2a9c878c42fc5653f53cc98a9f43aa2488fe961 Mon Sep 17 00:00:00 2001 From: chatty Date: Tue, 10 May 1994 09:54:49 +0000 Subject: Renamed Delete into Clear replaced TRUE/FALSE by true/false Now derive from IOS --- comm/MsgBuffer.cc | 166 ++++++++++++++++++++++++++++++++++++------------------ comm/MsgBuffer.h | 64 ++++++++++----------- 2 files changed, 141 insertions(+), 89 deletions(-) (limited to 'comm') diff --git a/comm/MsgBuffer.cc b/comm/MsgBuffer.cc index 72d7668..b8a3be0 100644 --- a/comm/MsgBuffer.cc +++ b/comm/MsgBuffer.cc @@ -59,9 +59,10 @@ The buffer will be allocated with a default size whenever data is appended to it This default size is currently 128 bytes. ?*/ UchMsgBuffer :: UchMsgBuffer () +: UchIOS () { Begin = Start = Stop = End = 0; - GetErr = FALSE; + GetErr = false; GrowSize = MinSize = 128; MaxSize = 0; } @@ -70,11 +71,12 @@ UchMsgBuffer :: UchMsgBuffer () Construct a buffer with \var{sz} bytes. ?*/ UchMsgBuffer :: UchMsgBuffer (int sz) +: UchIOS () { MinSize = GrowSize = sz = pad (sz); Begin = Start = Stop = new byte [sz]; End = Begin + sz; - GetErr = FALSE; + GetErr = false; MaxSize = 0; } @@ -83,11 +85,12 @@ Construct a buffer with minimum \var{sz} bytes, growing by \var{grow} bytes, until a maximum size of \var{max} bytes. ?*/ UchMsgBuffer :: UchMsgBuffer (int sz, int grow, int max) +: UchIOS () { MinSize = sz = pad (sz); Begin = Start = Stop = new byte [sz]; End = Begin + sz; - GetErr = FALSE; + GetErr = false; GrowSize = grow; MaxSize = max; } @@ -104,6 +107,7 @@ the original buffer. The rule of thumb is to avoid modifying the original buffer while there is a fake buffer on it. ?*/ UchMsgBuffer :: UchMsgBuffer (const UchMsgBuffer& b, int l) +: UchIOS (b) { Begin = 0; Start = b.Start; @@ -111,7 +115,7 @@ UchMsgBuffer :: UchMsgBuffer (const UchMsgBuffer& b, int l) End = Stop = Start + l; else End = Stop = b.Stop; - GetErr = FALSE; + GetErr = false; GrowSize = b.GrowSize; MinSize = b.MinSize; MaxSize = b.MaxSize; @@ -120,7 +124,7 @@ UchMsgBuffer :: UchMsgBuffer (const UchMsgBuffer& b, int l) /*?nodoc?*/ UchMsgBuffer :: ~UchMsgBuffer () { - Delete (); + Clear (); } /*? @@ -129,7 +133,7 @@ The buffer can still be used after this function has been called (it will reallo The destructor calls this function. ?*/ void -UchMsgBuffer :: Delete () +UchMsgBuffer :: Clear () { if (Begin) delete Begin; @@ -193,7 +197,7 @@ UchMsgBuffer :: Flush (int n) Start = Stop = Begin; // free if too big if (MaxSize > 0 && End - Start >= MaxSize) - Delete (); + Clear (); } else Start += n; //printf ("flushed\n"); @@ -244,9 +248,9 @@ Append the \var{n} first bytes of \var{buf} to the buffer. All append functions call \fun{NeedSize} so that appending always succeeds. ?*/ void -UchMsgBuffer :: Append (const byte* buf, int n) +UchMsgBuffer :: WriteBuf (const byte* buf, int n) { - GetErr = FALSE; + GetErr = false; if (! Begin || End - Stop < n) NeedSize (n); memcpy (Stop, buf, n); @@ -255,39 +259,49 @@ UchMsgBuffer :: Append (const byte* buf, int n) /*?nextdoc?*/ void -UchMsgBuffer :: Append (byte b) +UchMsgBuffer :: WriteByte (byte b) { - GetErr = FALSE; + GetErr = false; if (! Begin || End - Stop < 1) NeedSize (1); *Stop++ = b; } -#ifdef DOC /*?nextdoc?*/ void -UchMsgBuffer :: Append (char c) -{ } +UchMsgBuffer :: WriteChar (char c) +{ + WriteByte ((byte) c); +} /*?nextdoc?*/ void -UchMsgBuffer :: Append (lword l) -{ } +UchMsgBuffer :: WriteLong (lword l) +{ + WriteBuf ((const byte*)&l, lwsize); +} /*? Append a short word, a long word, a byte or a character to a buffer. The buffer is extended as necessary. ?*/ void -UchMsgBuffer :: Append (sword s) -{ } +UchMsgBuffer :: WriteShort (sword s) +{ + WriteBuf ((const byte*)&s, swsize); +} -#endif /* DOC */ + +void +UchMsgBuffer :: WriteString (const char* s) +{ + Append (s); +} /*? Append a string to a buffer. -If the second argument is \var{TRUE}, the terminating null byte is appended, else it is not. +If the second argument is \var{true}, the terminating null byte is appended, else it is not. If the string is the null pointer, nothing is appended to the buffer. ?*/ void @@ -295,7 +309,7 @@ UchMsgBuffer :: Append (const char* s, bool nullbyte) { if (! s) return; - GetErr = FALSE; + GetErr = false; int l = strlen (s) + (nullbyte ? 1 : 0); if (! Begin || End - Stop < l) NeedSize (l); @@ -309,9 +323,9 @@ The virtual function \fun{WriteTo} of the buffer is called. The length of the message is automatically computed and prepended to the message itself. ?*/ void -UchMsgBuffer :: Append (UchMessage& msg) +UchMsgBuffer :: WriteMsg (UchMessage& msg) { - GetErr = FALSE; + GetErr = false; // reserve space for length if (! Begin || End - Stop < lwsize) @@ -333,30 +347,71 @@ UchMsgBuffer :: Append (UchMessage& msg) memcpy (p, &len, lwsize); } + +void +UchMsgBuffer :: ReadLong (lword& l) +{ + Get (l); +} + +void +UchMsgBuffer :: ReadShort (sword& s) +{ + Get (s); +} + +void +UchMsgBuffer :: ReadByte (byte& b) +{ + Get (b); +} + +void +UchMsgBuffer :: ReadChar (char& c) +{ + Get (c); +} + +void +UchMsgBuffer :: ReadString (char* s) +{ + Get (s); +} + +void +UchMsgBuffer :: ReadString (CcuString& s) +{ + Get (s); +} + +void +UchMsgBuffer :: ReadBuf (byte* b, int n) +{ + Get (b, n); +} + //----- get operations // /*? Extract exactly \var{n} bytes from the buffer and copy them into \var{b}. -Return TRUE if the bytes were extracted, else return FALSE and mark the buffer with the get error flag. -If \var{peek} is TRUE, the buffer is not flushed after extracting the data +Return true if the bytes were extracted, else return false and mark the buffer with the get error flag. +If \var{peek} is true, the buffer is not flushed after extracting the data (i.e. the extracted data is still in the buffer). ?*/ bool UchMsgBuffer :: Get (byte* b, int n, bool peek) { -//printf ("UchMsgBuffer %x : Get %d bytes\n", this, n); if (GetErr) - return FALSE; + return false; if (Stop - Start < n) { - GetErr = TRUE; -//printf ("Get failed : %d bytes\n", Stop - Start); - return FALSE; + GetErr = true; + return false; } memcpy (b, Start, n); if (! peek) Flush (n); - return TRUE; + return true; } @@ -375,8 +430,8 @@ UchMsgBuffer :: Get (sword& s, bool peek) /*? Extract a long word, a short word, a byte, or a character from the buffer into the argument. -Return TRUE if the data was actually extracted, else return FALSE and mark the buffer with the get error flag. -If \var{peek} is TRUE, the buffer is not flushed after extracting the data. +Return true if the data was actually extracted, else return false and mark the buffer with the get error flag. +If \var{peek} is true, the buffer is not flushed after extracting the data. ?*/ bool UchMsgBuffer :: Get (lword& l, bool peek) @@ -402,7 +457,7 @@ is imposed. If \var{delim} is non null, it is a delimiter or a string of delimiters: the buffer is transferred until a delimiter is encountered. A null character always terminates the transfer. -If \var{peek} is TRUE, the buffer is not flushed after extracting the data. +If \var{peek} is true, the buffer is not flushed after extracting the data. The number of characters actually transferred is returned. \var{str} is always terminated by a null character. ?*/ @@ -463,15 +518,15 @@ UchMsgBuffer :: Get (CcuString& s, bool peek) Get a message from the buffer. The message must be prefixed by its length, as done by \fun{Append(UchMessage*)}. The virtual function \fun{ReadFrom} of the message is called to convert the buffer into a message. -If there is not enough data in the buffer, FALSE is returned. +If there is not enough data in the buffer, false is returned. If \fun{ReadFrom} reads past the end of the message, -FALSE is returned and the get error flag is set. +false is returned and the get error flag is set. ?*/ bool -UchMsgBuffer :: Get (UchMessage& msg) +UchMsgBuffer :: ReadMsg (UchMessage& msg) { if (GetErr || (Stop - Start < lwsize)) - return FALSE; + return false; // get length lword l = Stop - Start; @@ -480,7 +535,7 @@ UchMsgBuffer :: Get (UchMessage& msg) // check that buffer is big enough if (l < len) - return FALSE; + return false; // read from buffer Start += lwsize; @@ -489,18 +544,17 @@ UchMsgBuffer :: Get (UchMessage& msg) // align to length lword rl = l - (Stop - Start); if (rl == len) { - return TRUE; + return true; } else if (rl < len) { Flush ((int) len - (int) rl); - return TRUE; + return true; } else { - GetErr = TRUE; - return FALSE; + GetErr = true; + return false; } } - //------ peek operations /*?nextdoc?*/ @@ -508,9 +562,9 @@ bool UchMsgBuffer :: Peek (byte* b, lword offset) { if (Stop - Start < offset + 1) - return FALSE; + return false; *b = *(Start + offset); - return TRUE; + return true; } /*?nextdoc?*/ @@ -518,9 +572,9 @@ bool UchMsgBuffer :: Peek (sword* sw, lword offset) { if (Stop - Start < offset + swsize) - return FALSE; + return false; memcpy (sw, Start + offset, swsize); - return TRUE; + return true; } /*?nextdoc?*/ @@ -528,23 +582,23 @@ bool UchMsgBuffer :: Peek (lword* lw, lword offset) { if (Stop - Start < offset + lwsize) - return FALSE; + return false; memcpy (lw, Start + offset, lwsize); - return TRUE; + return true; } /*? These functions extract a byte, a short word, a long word, a byte string at \var{offset} bytes from the start of the buffer. -They return FALSE if the data to peek falls outside the buffer, else TRUE. +They return false if the data to peek falls outside the buffer, else true. ?*/ bool UchMsgBuffer :: Peek (int sz, byte* b, lword offset) { if (Stop - Start < offset + sz) - return FALSE; + return false; memcpy (b, Start + offset, sz); - return TRUE; + return true; } #ifdef DOC @@ -595,7 +649,7 @@ if (nb > 0) void UchMsgBuffer :: More (int n) { - GetErr = FALSE; + GetErr = false; if (End) // Begin == 0 for fake buffers Stop += n; } @@ -615,7 +669,7 @@ UchMsgBuffer :: Discard (int n) Start = Stop = Begin; // free if too big if (MaxSize > 0 && End - Start >= MaxSize) - Delete (); + Clear (); } else Stop -= n; @@ -698,7 +752,7 @@ UchMsgBuffer :: Error () /*? Test the state of the buffer, as set by the get functions. -\fun{Error} returns TRUE if a get error has occurred. +\fun{Error} returns true if a get error has occurred. \fun{Ok} is the negation of \fun{Error}. ?*/ bool diff --git a/comm/MsgBuffer.h b/comm/MsgBuffer.h index 8f96358..8abee14 100644 --- a/comm/MsgBuffer.h +++ b/comm/MsgBuffer.h @@ -18,6 +18,7 @@ #include "cplus_bugs.h" #include "global.h" #include "ccu/SmartPointer.h" +#include "IOS.h" class CcuString; class UchMessage; @@ -26,7 +27,7 @@ class UchMessage; // UchMsgBuffer derives from CcuSmartData to be able to have smart pointers (pUchMsgBuffer) // a buffer grows and shrinks automatically // -class UchMsgBuffer : public CcuSmartData { +class UchMsgBuffer : public CcuSmartData, public UchIOS { protected: byte* Begin; byte* Start; @@ -42,30 +43,40 @@ public: UchMsgBuffer (const UchMsgBuffer&, int = 0); // construct a fake buffer ~UchMsgBuffer (); - void Delete (); + void Clear (); void NeedSize (int n); void Grow () { NeedSize (GrowSize); } void Flush (int n = -1); void Flush (byte*); void SetSizes (int, int, int); - void Append (const byte*, int); - void Append (byte); - void Append (char c) { Append ((byte) c); } - void Append (sword s) { Append ((const byte*) &s, swsize); } - void Append (lword l) { Append ((const byte*) &l, lwsize); } - void Append (const char*, bool = TRUE); - void Append (UchMessage&); + void WriteBuf (const byte*, int); + void WriteByte (byte); + void WriteChar (char); + void WriteShort (sword); + void WriteLong (lword); + void WriteString (const char*); - bool Get (byte*, int, bool = FALSE); - bool Get (byte& b, bool peek = FALSE) { return Get (&b, 1, peek); } - bool Get (char& c, bool peek = FALSE) { return Get ((byte*) &c, 1, peek); } - bool Get (lword& l, bool peek = FALSE) { return Get ((byte*) &l, lwsize, peek); } - bool Get (sword& s, bool peek = FALSE) { return Get ((byte*) &s, swsize, peek); } - int Get (char*, int, char, bool = FALSE); - int Get (char*, int = -1, const char* = 0, bool = FALSE); - int Get (CcuString&, bool = FALSE); - bool Get (UchMessage&); + void ReadBuf (byte*, int); + void ReadByte (byte&); + void ReadChar (char&); + void ReadShort (sword&); + void ReadLong (lword&); + void ReadString (char*); + void ReadString (CcuString&); + + void Append (const char*, bool = true); + void WriteMsg (UchMessage&); + + bool Get (byte*, int, bool = false); + bool Get (byte& b, bool peek = false) { return Get (&b, 1, peek); } + bool Get (char& c, bool peek = false) { return Get ((byte*) &c, 1, peek); } + bool Get (lword& l, bool peek = false) { return Get ((byte*) &l, lwsize, peek); } + bool Get (sword& s, bool peek = false) { return Get ((byte*) &s, swsize, peek); } + int Get (char*, int, char, bool = false); + int Get (char*, int = -1, const char* = 0, bool = false); + int Get (CcuString&, bool = false); + bool ReadMsg (UchMessage&); bool Peek (byte*, lword = 0); bool Peek (sword*, lword = 0); @@ -84,22 +95,9 @@ public: void Discard (int); bool Error () { return GetErr; } - bool Ok () { return GetErr ? FALSE : TRUE; } - void ResetError () { GetErr = FALSE; } + bool Ok () { return GetErr ? false : true; } + void ResetError () { GetErr = false; } - UchMsgBuffer& operator << (lword l) { Append (l); return *this; } - UchMsgBuffer& operator << (sword s) { Append (s); return *this; } - UchMsgBuffer& operator << (byte b) { Append (b); return *this; } - UchMsgBuffer& operator << (char c) { Append (c); return *this; } - UchMsgBuffer& operator << (const char* s) { Append (s, TRUE); return *this; } - - UchMsgBuffer& operator >> (lword& l) { Get (l); return *this; } - UchMsgBuffer& operator >> (sword& s) { Get (s); return *this; } - UchMsgBuffer& operator >> (byte& b) { Get (b); return *this; } - UchMsgBuffer& operator >> (char& c) { Get (c); return *this; } - UchMsgBuffer& operator >> (char* s) { Get (s); return *this; } - UchMsgBuffer& operator >> (CcuString& s) { Get (s); return *this; } - #ifdef DOC UchMsgBuffer& operator << (type data); UchMsgBuffer& operator >> (type& data); -- cgit v1.1