summaryrefslogtreecommitdiffstats
path: root/maildrop/mio.h
blob: 6f5e250e1076aa1f82c05f5c3edecf183cabe3c8 (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
#ifndef	mio_h
#define	mio_h


#include	"config.h"
#include	<sys/types.h>
#include	<string.h>

// Fault-tolerant wrappers around I/O functions - they automatically retry
// when they get an EINTR (for kernels that do not restart the calls themselves)

int mopen(const char *fname, int flags, mode_t mode);
int mread(int fd, void *buf, size_t count);
int mwrite(int fd, const void *buf, size_t count);
int mclose(int fd);
off_t mseek(int, off_t, int);

/////////////////////////////////////////////////////////////////////////////
//
// Class Mio - reinventing the stdio wheel.
//
/////////////////////////////////////////////////////////////////////////////

class Mio {
	int fd_;
	unsigned char *readptr;
	unsigned char *writeptr;
	off_t readstartpos;
	int readsize;
	int readcnt;
	int writecnt;
	int err;

	unsigned char buf[8192];
public:
	struct rfc2045 *rfc2045p;

	Mio();
	virtual ~Mio();
	int Open(const char *, int, mode_t=0666);
	void Close();
	int peek() { return (readcnt ? *readptr:
			fill() < 0 ? -1:(++readcnt,*--readptr)); }
	int get() { return (readcnt ? (--readcnt,*readptr++):fill()); }
	int put(int c) { return (writecnt ?
				(--writecnt,*writeptr++=c):flush(c)); }
	int seek(off_t, int);
	int Rewind();
	off_t tell();
	virtual int write(const void *, int);
	int fd() { return (fd_); }
	void fd(int f) { Close();
			err=0;
			readcnt=0;
			writecnt=0;
			readptr=buf;
			writeptr=buf;
			fd_=f; }

	int errflag() { return (err); }
	void my_clearerr() { err=0; }
	int flush() { return (flush(-1)); }

	void write(const char *p) { write(p, strlen(p)); }

	Mio &operator<<(const char *p) { write(p); return (*this); }
	Mio &operator<<(const class Buffer &);

private:
	int fill();
	int flush(int);
} ;

class MioStdio : public Mio {
public:
	MioStdio(int);
	~MioStdio();
	int write(const void *, int);
	void write(const char *p) { write(p, strlen(p)); }
} ;

extern MioStdio mout, merr;

#endif