summaryrefslogtreecommitdiff
path: root/comm/MsgBuffer.cc
diff options
context:
space:
mode:
Diffstat (limited to 'comm/MsgBuffer.cc')
-rw-r--r--comm/MsgBuffer.cc282
1 files changed, 135 insertions, 147 deletions
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
//