Debian Codegraph @

yo/concrete/streambuf/fdstreambuf/fdstreambuf.h0

raw
1 #include <unistd.h>
2 #include <streambuf>
3 #include <ios>
4
5 #include <iostream>
6
7 using std::cerr;
8
9 class fdstreambuf: public std::streambuf
10 {
11 public:
12 fdstreambuf(int fd, std::ios::openmode mode =
13 std::ios::in | std::ios::out)
14 :
15 fd(fd),
16 input_char(EOF),
17 mode(mode)
18 {
19 std::streambuf::_M_mode = mode;
20 // char
21 // dummy;
22 // if (mode & std::ios::in)
23 // setg(&dummy, &dummy, &dummy);
24
25
26
27 // if (mode & std::ios::out)
28 // setp(&dummy, &dummy);
29 }
30
31
32 int uflow() // consumes the character
33 {
34 std::cerr << "uflow\n";
35 if (input_char != EOF)
36 {
37 int
38 next = input_char;
39 input_char = EOF;
40 return next;
41 }
42
43 return underflow();
44 }
45
46 int underflow() // inspects the character
47 {
48 std::cerr << "underflow, in_avail() = " << in_avail() << "\n";
49 char
50 c;
51
52 if (read(fd, &c, 1))
53 {
54 // std::cerr << "Got " << c;
55 input_char = c;
56 std::cerr << ", input_char is " << input_char <<
57 " (" << (char)input_char << ")\n";
58 return input_char;
59 }
60
61 //std::cerr << "EOF underflow\n";
62 return input_char = EOF;
63 }
64
65 int overflow(int c)
66 {
67 if (c != EOF)
68 {
69 if (write(fd, &c, 1) != 1)
70 return EOF;
71 }
72 return c;
73 }
74
75 private:
76 std::ios::openmode
77 mode;
78 int
79 fd,
80 input_char;
81 };