summaryrefslogtreecommitdiff
path: root/comm/MsgBuffer.h
blob: 869933b8573b5fec9bc908a5986175b9bf59b9cd (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
/*
 *	The Unix Channel
 *
 *	by Michel Beaudouin-Lafon
 *
 *	Copyright 1990-1995
 *	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"
#include "IOS.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, public UchIOS {
protected:
	byte*	Begin;
	byte*	Start;
	byte*	Stop;
	byte*	End;
	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);
		UchMsgBuffer (int, int, int);
		UchMsgBuffer (const UchMsgBuffer&, int = 0);	// construct a fake buffer
		~UchMsgBuffer ();
	
	void	Clear ();
	void	NeedSize (int n);
	void	Grow ()			{ NeedSize (GrowSize); }
	void	Flush (int n = -1);
	void	Flush (byte*);
	void	SetSizes (int, int, int);
	
	void	WriteBuf (const byte*, int);
	void	WriteByte (byte);
	void	WriteChar (char);
	void	WriteShort (sword);
	void	WriteLong (lword);
	void	WriteString (const char*);

	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	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; }
	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; }
	
#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_ */