From fc323560826ac24b7a66b5cd0a5621963b9d1677 Mon Sep 17 00:00:00 2001 From: chatty Date: Wed, 8 Feb 1995 14:44:33 +0000 Subject: Additional cleanup of Read functions --- comm/IOS.h | 16 ++-- comm/MsgBuffer.cc | 282 ++++++++++++++++++++++++++---------------------------- comm/MsgBuffer.h | 47 ++++----- 3 files changed, 164 insertions(+), 181 deletions(-) (limited to 'comm') diff --git a/comm/IOS.h b/comm/IOS.h index 02ee3f3..10fb08c 100644 --- a/comm/IOS.h +++ b/comm/IOS.h @@ -3,7 +3,7 @@ * * by Michel Beaudouin-Lafon * - * Copyright 1990-1994 + * Copyright 1990-1995 * Laboratoire de Recherche en Informatique (LRI) * * Basic I/O @@ -36,13 +36,13 @@ virtual void WriteChar (char) = 0; virtual void WriteString (const char*) = 0; virtual void WriteBuf (const byte*, int) = 0; -virtual void ReadLong (lword&) = 0; -virtual void ReadShort (sword&) = 0; -virtual void ReadByte (byte&) = 0; -virtual void ReadChar (char&) = 0; -virtual void ReadString (char*) = 0; -virtual void ReadString (CcuString&) = 0; -virtual void ReadBuf (byte*, int) = 0; +virtual bool ReadLong (lword&) = 0; +virtual bool ReadShort (sword&) = 0; +virtual bool ReadByte (byte&) = 0; +virtual bool ReadChar (char&) = 0; +virtual bool ReadBuf (byte*, int) = 0; +virtual int ReadString (char*, int = -1) = 0; +virtual int ReadString (CcuString&) = 0; inline UchIOS& operator << (lword l) { WriteLong (l); return *this; } inline UchIOS& operator << (sword s) { WriteShort (s); return *this; } diff --git a/comm/MsgBuffer.cc b/comm/MsgBuffer.cc index b8a3be0..2746c8e 100644 --- a/comm/MsgBuffer.cc +++ b/comm/MsgBuffer.cc @@ -3,7 +3,7 @@ * * by Michel Beaudouin-Lafon * - * Copyright 1990-1993 + * Copyright 1990-1995 * Laboratoire de Recherche en Informatique (LRI) * * Messages, buffers @@ -348,132 +348,43 @@ UchMsgBuffer :: WriteMsg (UchMessage& msg) } -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 -(i.e. the extracted data is still in the buffer). -?*/ bool -UchMsgBuffer :: Get (byte* b, int n, bool peek) +UchMsgBuffer :: GetBuf (byte* b, int n, bool peek, int offset) { if (GetErr) return false; - if (Stop - Start < n) { + if (!peek || offset < 0) + offset = 0; + if (Stop - Start < offset + n) { GetErr = true; return false; } - memcpy (b, Start, n); + memcpy (b, Start+offset, n); if (! peek) Flush (n); return true; } -#ifdef DOC - -/*?nextdoc?*/ -bool -UchMsgBuffer :: Get (char& c, bool peek) -{ -} - -/*?nextdoc?*/ -bool -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. -?*/ -bool -UchMsgBuffer :: Get (lword& l, bool peek) -{ -} - -#endif /* DOC */ - -/*?nextdoc?*/ -int -UchMsgBuffer :: Get (char* str, int n, char delim, bool peek) -{ - char ds [2]; - ds [0] = delim; - ds [1] = 0; - return Get (str, n, ds, peek); -} -/*? -Get at most \var{n} characters from the buffer into \var{str}. -If \var{n} is negative, no limit other than the size of the buffer -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. -The number of characters actually transferred is returned. -\var{str} is always terminated by a null character. -?*/ int -UchMsgBuffer :: Get (char* str, int n, const char* delim, bool peek) +UchMsgBuffer :: GetString (char* str, int n, const char* delim, bool peek, int offset) { if (GetErr) return 0; - if (n < 0 || Stop - Start < n) - n = Stop - Start; - if (! n) + if (!peek || offset < 0) + offset = 0; + if (n < 0 || Stop - Start < offset + n) + n = Stop - Start - offset; + if (n < 0) return 0; int i = 0; - char*q = str; - for (char* p = (char*) Start; *p && n; n--, i++) { + char* q = str; + for (char* p = (char*) (Start+offset); *p && n; n--, i++) { if (delim) { for (const char *s = delim; *s; s++) if (*s == *p) @@ -491,12 +402,45 @@ UchMsgBuffer :: Get (char* str, int n, const char* delim, bool peek) return i; } + +/*?nextdoc?*/ +bool +UchMsgBuffer :: ReadLong (lword& l) +{ + return GetBuf ((byte*) &l, lwsize, false); +} + +/*?nextdoc?*/ +bool +UchMsgBuffer :: ReadShort (sword& s) +{ + return GetBuf ((byte*) &s, swsize, false); +} + +/*?nextdoc?*/ +bool +UchMsgBuffer :: ReadByte (byte& b) +{ + return GetBuf (&b, 1, false); +} + +/*? +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. +?*/ +bool +UchMsgBuffer :: ReadChar (char& c) +{ + return GetBuf ((byte*) &c, 1, false); +} + /*? Extract a string from a buffer and copy it to a \typ{CcuString}. This assumes that the string in the buffer is null-terminated. ?*/ int -UchMsgBuffer :: Get (CcuString& s, bool peek) +UchMsgBuffer :: ReadString (CcuString& s) { if (GetErr) return 0; @@ -509,12 +453,54 @@ UchMsgBuffer :: Get (CcuString& s, bool peek) int l = p - Start; s.Assign ((const char*) Start, l); - if (! peek) - Flush (*p ? l : l + 1); + Flush (*p ? l : l + 1); return l; } /*? +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 +(i.e. the extracted data is still in the buffer). +?*/ + +bool +UchMsgBuffer :: ReadBuf (byte* b, int n) +{ + return GetBuf (b, n, false); +} + +/*?nextdoc?*/ +int +UchMsgBuffer :: ReadString (char* str, char delim, int n) +{ + char ds [2]; + ds [0] = delim; + ds [1] = 0; + return GetString (str, n, ds, false); +} + +/*?nextdoc?*/ +int +UchMsgBuffer :: ReadString (char* str, int n) +{ + return GetString (str, n, 0, false); +} + +/*? +Get at most \var{n} characters from the buffer into \var{str}. +\var{delim} (resp. \var{ds} if not null) is a delimiter: +the buffer is transferred until a null character or the delimiter is encountered. +The number of characters actually transferred is returned. +\var{str} is always terminated by a null character. +?*/ +int +UchMsgBuffer :: ReadString (char* str, const char* ds, int n) +{ + return GetString (str, n, ds, false); +} + +/*? 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. @@ -555,80 +541,82 @@ UchMsgBuffer :: ReadMsg (UchMessage& msg) } } + + //------ peek operations /*?nextdoc?*/ bool -UchMsgBuffer :: Peek (byte* b, lword offset) +UchMsgBuffer :: PeekByte (byte& b, int offset) { - if (Stop - Start < offset + 1) - return false; - *b = *(Start + offset); - return true; + return GetBuf (&b, 1, true, offset); } /*?nextdoc?*/ bool -UchMsgBuffer :: Peek (sword* sw, lword offset) +UchMsgBuffer :: PeekShort (sword& sw, int offset) { - if (Stop - Start < offset + swsize) - return false; - memcpy (sw, Start + offset, swsize); - return true; + return GetBuf ((byte*) &sw, swsize, true, offset); } /*?nextdoc?*/ bool -UchMsgBuffer :: Peek (lword* lw, lword offset) +UchMsgBuffer :: PeekLong (lword& lw, int offset) { - if (Stop - Start < offset + lwsize) - return false; - memcpy (lw, Start + offset, lwsize); - return true; + return GetBuf ((byte*) &lw, lwsize, true, offset); } /*? -These functions extract a byte, a short word, a long word, a byte string +These functions extract a byte, a short word, a long word, a byte array at \var{offset} bytes from the start of the buffer. They return false if the data to peek falls outside the buffer, else true. ?*/ bool -UchMsgBuffer :: Peek (int sz, byte* b, lword offset) +UchMsgBuffer :: PeekBuf (byte* b, int n, int offset) { - if (Stop - Start < offset + sz) - return false; - memcpy (b, Start + offset, sz); - return true; + return GetBuf (b, n, true, offset); } -#ifdef DOC +int +UchMsgBuffer :: PeekString (CcuString& s, int offset) +{ + if (GetErr) + return 0; -/*?nextdoc?*/ -bool -UchMsgBuffer :: MsgPeek (byte* b, lword offset) -{ } + int n = Stop - Start - offset; + byte* p = Start + offset; + while (n-- && *p) + ++p; -/*?nextdoc?*/ -bool -UchMsgBuffer :: MsgPeek (sword* sw, lword offset) -{ } + int l = p - Start; + + s.Assign ((const char*) (Start + offset), l); + return l; +} /*?nextdoc?*/ -bool -UchMsgBuffer :: MsgPeek (lword* lw, lword offset) -{ } +int +UchMsgBuffer :: PeekString (char* str, int n, char delim, int offset) +{ + char ds [2]; + ds [0] = delim; + ds [1] = 0; + return GetString (str, n, ds, true, offset); +} /*? -These function are similar to the \fun{Peek} functions; -they must be used when the buffer contains a message: -a message is always prefixed by its length, thus changing the origin of the offset. -Note that the length itself can be extracted with \fun{Peek(lword*, 0)}. +Get at most \var{n} characters from the buffer into \var{str}. +\var{delim} (resp. \var{ds} if not null) is a delimiter: +the buffer is transferred until a null character or the delimiter is encountered. +The number of characters actually transferred is returned. +\var{str} is always terminated by a null character. ?*/ -bool -UchMsgBuffer :: MsgPeek (int sz, byte* b, lword offset) -{ } +int +UchMsgBuffer :: PeekString (char* str, int n, const char* delim, int offset) +{ + return GetString (str, n, delim, true, offset); +} -#endif /* DOC */ //----- miscellaneous // diff --git a/comm/MsgBuffer.h b/comm/MsgBuffer.h index 8abee14..869933b 100644 --- a/comm/MsgBuffer.h +++ b/comm/MsgBuffer.h @@ -3,7 +3,7 @@ * * by Michel Beaudouin-Lafon * - * Copyright 1990-1993 + * Copyright 1990-1995 * Laboratoire de Recherche en Informatique (LRI) * * Messages, buffers @@ -36,6 +36,9 @@ protected: bool GetErr; int GrowSize, MaxSize, MinSize; + bool GetBuf (byte*, int, bool, int = 0); + int GetString (char*, int, const char*, bool, int = 0); + public: UchMsgBuffer (); UchMsgBuffer (int); @@ -57,35 +60,27 @@ public: void WriteLong (lword); void WriteString (const char*); - void ReadBuf (byte*, int); - void ReadByte (byte&); - void ReadChar (char&); - void ReadShort (sword&); - void ReadLong (lword&); - void ReadString (char*); - void ReadString (CcuString&); + bool ReadByte (byte&); + bool ReadChar (char&); + bool ReadShort (sword&); + bool ReadLong (lword&); + bool ReadBuf (byte*, int); + int ReadString (char*, const char*, int = -1); + int ReadString (char*, char, int = -1); + int ReadString (char*, int = -1); + int ReadString (CcuString&); + bool ReadMsg (UchMessage&); 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); - bool Peek (lword*, lword = 0); - bool Peek (int, byte*, lword = 0); - bool MsgPeek (byte* b, lword o = 0) { return Peek (b, o+lwsize); } - bool MsgPeek (sword* sw, lword o = 0) { return Peek (sw, o+lwsize); } - bool MsgPeek (lword* lw, lword o = 0) { return Peek (lw, o+lwsize); } - bool MsgPeek (int sz, byte* b, lword o = 0) { return Peek (sz, b, o+lwsize); } + bool PeekByte (byte&, int = 0); + bool PeekShort (sword&, int = 0); + bool PeekLong (lword&, int = 0); + bool PeekBuf (byte*, int, int = 0); + int PeekString (char*, int=-1, const char* = 0, int = 0); + int PeekString (char*, int, char, int = 0); + int PeekString (CcuString&, int = 0); byte* Buffer () { return Start; } int BufLength () { return Stop - Start; } -- cgit v1.1