summaryrefslogtreecommitdiff
path: root/comm/MsgBuffer.h
blob: 8f96358815aeb3f45b08a4c6d8235724610e2279 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
 *	The Unix Channel
 *
 *	by Michel Beaudouin-Lafon
 *
 *	Copyright 1990-1993
 *	Laboratoire de Recherche en Informatique (LRI)
 *
 *	Messages, buffers
 *
 *	$Id$
 *	$CurLog$
 */

#ifndef Buffer_H_
#define Buffer_H_

#include "cplus_bugs.h"
#include "global.h"
#include "ccu/SmartPointer.h"
class CcuString;
class UchMessage;

//	Buffer is the buffer itself, End points after its last byte
//	Start and Stop limit its content
//	UchMsgBuffer derives from CcuSmartData to be able to have smart pointers (pUchMsgBuffer)
//	a buffer grows and shrinks automatically
//
class UchMsgBuffer : public CcuSmartData {
protected:
	byte*	Begin;
	byte*	Start;
	byte*	Stop;
	byte*	End;
	bool	GetErr;
	int	GrowSize, MaxSize, MinSize;

public:
		UchMsgBuffer ();
		UchMsgBuffer (int);
		UchMsgBuffer (int, int, int);
		UchMsgBuffer (const UchMsgBuffer&, int = 0);	// construct a fake buffer
		~UchMsgBuffer ();
	
	void	Delete ();
	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&);

	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&);
	
	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); }
	
	byte*	Buffer ()		{ return Start; }
	int	BufLength ()	{ return Stop - Start; }
	byte*	Free ()		{ return Stop; }
	int	FreeLength ()	{ return End - Stop; }
	void	More (int);
	void	Discard (int);
	
	bool	Error ()	{ return GetErr; }
	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);
	UchMsgBuffer&	operator >> (type* data);
#endif

friend	UchMsgBuffer&	DbgPrint (UchMsgBuffer&, int);
};

PointerClass (pUchMsgBuffer, UchMsgBuffer)

#endif /* Buffer_H_ */