rvm  1.11
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
fdstream.cc
Go to the documentation of this file.
1 #include "config.h"
2 
3 #include <unistd.h>
4 
5 #include "error.h"
6 #include "fdstream.h"
7 
8 //-----------------------------------------------------------------------------
9 
11 {
12 }
13 
15 {
16  attach(a_fd);
17 }
18 
19 void fdoutbuf::attach(int a_fd)
20 {
21  m_fd = a_fd;
22 }
23 
24 int fdoutbuf::overflow(int a_c)
25 {
26  if (a_c != EOF) {
27  char z = a_c;
28  if (::write(m_fd, &z, 1) != 1)
29  return(EOF);
30  }
31  return(a_c);
32 }
33 
34 std::streamsize fdoutbuf::xsputn(const char* a_s, std::streamsize a_num)
35 {
36  return(::write(m_fd, a_s, a_num));
37 }
38 
39 //-----------------------------------------------------------------------------
40 
41 ofdstream::ofdstream() : std::ostream(&m_buf)
42 {
43 }
44 
45 ofdstream::ofdstream(int a_fd) : std::ostream(&m_buf)
46 {
47  m_fd = a_fd;
48  m_buf.attach(a_fd);
49 }
50 
51 void ofdstream::attach(int a_fd)
52 {
53  m_fd = a_fd;
54  m_buf.attach(a_fd);
55 }
56 
57 int ofdstream::fd(void)
58 {
59  return(m_fd);
60 }
61 
62 //-----------------------------------------------------------------------------
63 
65 {
66  setg(m_buffer+4, m_buffer+4, m_buffer+4);
67 }
68 
70 {
71  m_fd = a_fd;
72  setg(m_buffer+4, m_buffer+4, m_buffer+4);
73 }
74 
75 void fdinbuf::attach(int a_fd)
76 {
77  m_fd = a_fd;
78 }
79 
81 {
82  int num_put_back;
83  int num;
84 
85  if (gptr() < egptr()) {
86  return(*gptr());
87  }
88 
89  num_put_back = gptr() - eback();
90  if (num_put_back > 0)
91  num_put_back = 4;
92 
93  memcpy(m_buffer+(4-num_put_back), gptr()-num_put_back, num_put_back);
94 
95  num = ::read(m_fd, m_buffer+4, m_buffer_size-4);
96  if (num <= 0)
97  return(EOF);
98 
99  setg(m_buffer+(4-num_put_back), m_buffer+4, m_buffer+4+num);
100 
101  return(*gptr());
102 }
103 
104 //-----------------------------------------------------------------------------
105 
106 ifdstream::ifdstream() : std::istream(&m_buf)
107 {
108 }
109 
110 ifdstream::ifdstream(int a_fd) : std::istream(&m_buf)
111 {
112  m_fd = a_fd;
113  m_buf.attach(a_fd);
114 }
115 
116 void ifdstream::attach(int a_fd)
117 {
118  m_fd = a_fd;
119  m_buf.attach(a_fd);
120 }
121 
122 int ifdstream::fd(void)
123 {
124  return(m_fd);
125 }
126 
127 //-----------------------------------------------------------------------------
128 
int m_fd
Definition: fdstream.h:47
fdoutbuf m_buf
Definition: fdstream.h:34
int m_fd
Definition: fdstream.h:62
static const int m_buffer_size
Definition: fdstream.h:48
int fd(void)
Definition: fdstream.cc:57
void attach(int a_fd)
Definition: fdstream.cc:19
fdinbuf m_buf
Definition: fdstream.h:61
void attach(int a_fd)
Definition: fdstream.cc:116
int m_fd
Definition: fdstream.h:22
fdoutbuf()
Definition: fdstream.cc:10
char m_buffer[m_buffer_size]
Definition: fdstream.h:49
virtual int overflow(int a_c)
Definition: fdstream.cc:24
virtual int underflow(void)
Definition: fdstream.cc:80
void attach(int a_fd)
Definition: fdstream.cc:75
ofdstream()
Definition: fdstream.cc:41
fdinbuf()
Definition: fdstream.cc:64
void attach(int a_fd)
Definition: fdstream.cc:51
int m_fd
Definition: fdstream.h:35
int fd(void)
Definition: fdstream.cc:122
virtual std::streamsize xsputn(const char *a_s, std::streamsize a_num)
Definition: fdstream.cc:34