File: buffer.h

package info (click to toggle)
maildrop 0.54a-2
  • links: PTS
  • area: main
  • in suites: slink
  • size: 872 kB
  • ctags: 883
  • sloc: cpp: 8,469; ansic: 658; sh: 327; makefile: 82
file content (76 lines) | stat: -rw-r--r-- 2,859 bytes parent folder | download
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
/* DSTART                                                                    */
/*                                                                           */
/*           maildrop - mail delivery agent with filtering abilities         */
/*                                                                           */
/*  Copyright 1998, Double Precision Inc.                                    */
/*                                                                           */
/*  This program is distributed under the terms of the GNU General Public    */
/*  License. See COPYING for additional information.                         */
/* DEND                                                                      */
#ifndef	buffer_h
#define	buffer_h

static const char buffer_h_rcsid[]="$Id: buffer.h 1.2 1998/06/21 17:49:09 mrsam Exp $";

#include	<string.h>

///////////////////////////////////////////////////////////////////////////
//
// Generic text/data buffer.  Not null-terminated by default.
//
// This class is used to store arbitrary text strings.  It is used by
// the lexical scanner to build up text that's recognized as a token,
// and the rest of the code to store strings.
//
///////////////////////////////////////////////////////////////////////////

class Buffer {

	unsigned char *buf;
	int	bufsize;
	int	buflength;
public:
	Buffer() : buf(0), bufsize(0), buflength(0)	{}
	~Buffer()	{ if (buf)	delete[] buf; }
	const unsigned char *Ptr() const { return (buf); }
	operator const unsigned char *() const { return (buf); }
	operator const char *() const { return ((const char *)buf); }
	int	Int(const char * =0) const;
	int	Length() const { return (buflength); }
	void	Length(int l) { if (l < buflength) buflength=l; }

	Buffer(const Buffer &);		// UNDEFINED
	Buffer &operator=(const Buffer &);

	void	push(int c) { if (buflength < bufsize)
				{
					buf[buflength]=c;
					++buflength;
				}
				else	append(c); }
	int	pop() { return (buflength ? buf[--buflength]:0); }
	int	peek() { return (buflength ? buf[buflength-1]:0); }
	void	reset() { buflength=0; }

private:
	void	append(int);
public:
	void	append(const void *, int);
	void	set(const char *);
	void	append(const char *p) { append(p, strlen(p)); }
	void	set(unsigned long n) { buflength=0; append(n); }
	void	append(unsigned long n);
	void	append(double);
	Buffer	&operator=(const char *p) { set(p); return (*this); }
	Buffer	&operator += (const Buffer &p) { append(p.buf, p.buflength); return (*this); }
	Buffer	&operator += (const char *p) { append(p, strlen(p)); return (*this); }
	Buffer	&operator += (char c) { push(c); return (*this); }
	int	operator-(const Buffer &) const;
	int	operator-(const char *) const;
	int	operator==(const Buffer &b) const
		{ return ( operator-(b) == 0); }
	int	operator==(const char *b) const
		{ return ( operator-(b) == 0); }
} ;

#endif