Debian Codegraph @

yo/concrete/streambuf/fdstreambuf/inok/seek.1.h

raw
1 #include <unistd.h>
2 #include <streambuf>
3 #include <ios>
4
5 #include <sys/types.h>
6 #include <unistd.h>
7
8 #include <iostream>
9 using namespace std;
10
11 class fdstreambuf: public std::streambuf
12 {
13 public:
14 fdstreambuf(int fd, std::ios::openmode mode =
15 std::ios::in | std::ios::out)
16 :
17 fd(fd)
18 {
19 if (mode & std::ios::in)
20 setg(buffer, buffer + 1, buffer + 1);
21 }
22
23 pos_type
24 seekoff(off_type offset, ios::seekdir dir,
25 ios_base::openmode)
26 {
27 pos_type
28 pos =
29 lseek
30 (
31 fd, offset,
32 (dir == ios::beg) ? SEEK_SET :
33 (dir == ios::cur) ? SEEK_CUR :
34 SEEK_END
35 );
36
37 if (pos < 0)
38 return -1;
39
40 setg(buffer, buffer + 1, buffer + 1);
41 return pos;
42 }
43
44 pos_type // used by seekg()
45 seekpos(pos_type offset, ios_base::openmode mode)
46 {
47 return seekoff(offset, ios::beg, mode);
48 }
49
50 int underflow() // inspects the character
51 {
52 if (gptr() < egptr())
53 return static_cast<unsigned char>(*gptr());
54 // return next char waiting in the buffer
55
56 if (read(fd, buffer, 1) <= 0) // none read ->
57 return EOF;
58
59 setg(buffer, buffer, buffer + 1);
60 return static_cast<unsigned char>(*gptr());
61 }
62
63 private:
64 char
65 buffer[1];
66 int
67 fd;
68 };