|
2
|
1 |
/* $Id: fdobuf.h 616 2005-08-19 20:11:01Z bruce $ */
|
|
0
|
2 |
// Copyright (C) 1999,2000 Bruce Guenter <bruceg@em.ca>
|
|
|
3 |
//
|
|
|
4 |
// This program is free software; you can redistribute it and/or modify
|
|
|
5 |
// it under the terms of the GNU General Public License as published by
|
|
|
6 |
// the Free Software Foundation; either version 2 of the License, or
|
|
|
7 |
// (at your option) any later version.
|
|
|
8 |
//
|
|
|
9 |
// This program is distributed in the hope that it will be useful,
|
|
|
10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
12 |
// GNU General Public License for more details.
|
|
|
13 |
//
|
|
|
14 |
// You should have received a copy of the GNU General Public License
|
|
|
15 |
// along with this program; if not, write to the Free Software
|
|
|
16 |
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
17 |
|
|
|
18 |
#ifndef FDBUF__FDOBUF__H__
|
|
|
19 |
#define FDBUF__FDOBUF__H__
|
|
|
20 |
|
|
|
21 |
class fdobuf : protected fdbuf
|
|
|
22 |
{
|
|
|
23 |
public:
|
|
|
24 |
enum openflags { create=O_CREAT,
|
|
|
25 |
excl=O_EXCL,
|
|
|
26 |
trunc=O_TRUNC,
|
|
|
27 |
append=O_APPEND };
|
|
|
28 |
|
|
|
29 |
fdobuf(const char* filename, int, int mode = 0666,
|
|
|
30 |
unsigned bufsz = FDBUF_SIZE);
|
|
|
31 |
fdobuf(int fdesc, bool dc=false, unsigned bufsz = FDBUF_SIZE);
|
|
|
32 |
virtual ~fdobuf();
|
|
|
33 |
bool close();
|
|
|
34 |
bool operator!() const;
|
|
|
35 |
operator bool() const
|
|
|
36 |
{
|
|
|
37 |
return !operator!();
|
|
|
38 |
}
|
|
|
39 |
bool flush();
|
|
|
40 |
bool sync();
|
|
|
41 |
virtual bool write(char);
|
|
|
42 |
bool write(unsigned char c) { return write((char)c); }
|
|
|
43 |
bool write(signed char c) { return write((char)c); }
|
|
|
44 |
virtual bool write(const char*, unsigned);
|
|
|
45 |
bool write(const unsigned char* b, unsigned l) { return write((char*)b, l); }
|
|
|
46 |
bool write(const signed char* b, unsigned l) { return write((char*)b, l); }
|
|
|
47 |
virtual bool write_large(const char*, unsigned);
|
|
|
48 |
unsigned last_count() { return count; }
|
|
|
49 |
bool seek(unsigned o);
|
|
|
50 |
bool rewind() { return seek(0); }
|
|
|
51 |
unsigned tell() const { return offset + bufpos; }
|
|
|
52 |
|
|
|
53 |
bool chown(uid_t, gid_t) const;
|
|
|
54 |
bool chmod(mode_t) const;
|
|
|
55 |
|
|
|
56 |
fdobuf& operator<<(const char* str)
|
|
|
57 |
{
|
|
|
58 |
write(str, strlen(str));
|
|
|
59 |
return *this;
|
|
|
60 |
}
|
|
|
61 |
fdobuf& operator<<(char ch)
|
|
|
62 |
{
|
|
|
63 |
write(ch);
|
|
|
64 |
return *this;
|
|
|
65 |
}
|
|
|
66 |
fdobuf& operator<<(fdobuf& (*manip)(fdobuf&))
|
|
|
67 |
{
|
|
|
68 |
return manip(*this);
|
|
|
69 |
}
|
|
|
70 |
fdobuf& operator<<(unsigned long);
|
|
|
71 |
fdobuf& operator<<(signed long);
|
|
|
72 |
fdobuf& operator<<(unsigned i) { return operator<<((unsigned long)i); }
|
|
|
73 |
fdobuf& operator<<(signed i) { return operator<<((signed long)i); }
|
|
|
74 |
fdobuf& operator<<(unsigned short i) { return operator<<((unsigned long)i); }
|
|
|
75 |
fdobuf& operator<<(signed short i) { return operator<<((signed long)i); }
|
|
|
76 |
|
|
|
77 |
int error_number() const { return errnum; }
|
|
|
78 |
protected:
|
|
|
79 |
virtual bool nflush(bool withsync);
|
|
|
80 |
|
|
|
81 |
unsigned bufpos; // Current write position in the buffer
|
|
|
82 |
unsigned count; // Number of bytes written by last operation
|
|
|
83 |
};
|
|
|
84 |
|
|
|
85 |
fdobuf& endl(fdobuf& fd);
|
|
|
86 |
|
|
|
87 |
extern fdobuf fout;
|
|
|
88 |
extern fdobuf ferr;
|
|
|
89 |
|
|
|
90 |
#endif // FDBUF__FDOBUF__H__
|