summaryrefslogtreecommitdiff
path: root/comm/MsgBuffer.h
blob: 24d201a2836c17378aac88039ee60452360dc1b2 (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-1997
 *	Laboratoire de Recherche en Informatique (LRI)
 *
 *	Buffers for messages
 *
 *	$Id$
 *	$CurLog$
 *	Removed smart pointers
 *	Removed global.h
 */

#ifndef Buffer_H_
#define Buffer_H_

#include "cplus_bugs.h"
#include "ivl/bool.h"
#include "ivl/word.h"
#include "IOS.h"
class IvlString;
class IvlMessage;

//	Buffer is the buffer itself, End points after its last byte
//	Start and Stop limit its content
//	a buffer grows and shrinks automatically
//
class IvlMsgBuffer : public IvlIOS {
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:
		IvlMsgBuffer ();
		IvlMsgBuffer (int);
		IvlMsgBuffer (int, int, int);
		IvlMsgBuffer (const IvlMsgBuffer&, int = 0);	// construct a fake buffer
		~IvlMsgBuffer ();
	
	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 (IvlString&);
	bool	ReadMsg (IvlMessage&);

	void	Append (const char*, bool = true);
	void	WriteMsg (IvlMessage&);

	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 (IvlString&, 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
	IvlMsgBuffer&	operator << (type data);
	IvlMsgBuffer&	operator >> (type& data);
	IvlMsgBuffer&	operator >> (type* data);
#endif

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

#endif /* Buffer_H_ */