00001 #include "config.h"
00002
00003 #include <unistd.h>
00004
00005 #include "error.h"
00006 #include "fdstream.h"
00007
00008
00009
00010 fdoutbuf::fdoutbuf()
00011 {
00012 }
00013
00014 fdoutbuf::fdoutbuf(int a_fd)
00015 {
00016 attach(a_fd);
00017 }
00018
00019 void fdoutbuf::attach(int a_fd)
00020 {
00021 m_fd = a_fd;
00022 }
00023
00024 int fdoutbuf::overflow(int a_c)
00025 {
00026 if (a_c != EOF) {
00027 char z = a_c;
00028 if (::write(m_fd, &z, 1) != 1)
00029 return(EOF);
00030 }
00031 return(a_c);
00032 }
00033
00034 std::streamsize fdoutbuf::xsputn(const char* a_s, std::streamsize a_num)
00035 {
00036 return(::write(m_fd, a_s, a_num));
00037 }
00038
00039
00040
00041 ofdstream::ofdstream() : std::ostream(&m_buf)
00042 {
00043 }
00044
00045 ofdstream::ofdstream(int a_fd) : std::ostream(&m_buf)
00046 {
00047 m_fd = a_fd;
00048 m_buf.attach(a_fd);
00049 }
00050
00051 void ofdstream::attach(int a_fd)
00052 {
00053 m_fd = a_fd;
00054 m_buf.attach(a_fd);
00055 }
00056
00057 int ofdstream::fd(void)
00058 {
00059 return(m_fd);
00060 }
00061
00062
00063
00064 fdinbuf::fdinbuf()
00065 {
00066 setg(m_buffer+4, m_buffer+4, m_buffer+4);
00067 }
00068
00069 fdinbuf::fdinbuf(int a_fd)
00070 {
00071 m_fd = a_fd;
00072 setg(m_buffer+4, m_buffer+4, m_buffer+4);
00073 }
00074
00075 void fdinbuf::attach(int a_fd)
00076 {
00077 m_fd = a_fd;
00078 }
00079
00080 int fdinbuf::underflow(void)
00081 {
00082 int num_put_back;
00083 int num;
00084
00085 if (gptr() < egptr()) {
00086 return(*gptr());
00087 }
00088
00089 num_put_back = gptr() - eback();
00090 if (num_put_back > 0)
00091 num_put_back = 4;
00092
00093 std::memcpy(m_buffer+(4-num_put_back), gptr()-num_put_back, num_put_back);
00094
00095 num = ::read(m_fd, m_buffer+4, m_buffer_size-4);
00096 if (num <= 0)
00097 return(EOF);
00098
00099 setg(m_buffer+(4-num_put_back), m_buffer+4, m_buffer+4+num);
00100
00101 return(*gptr());
00102 }
00103
00104
00105
00106 ifdstream::ifdstream() : std::istream(&m_buf)
00107 {
00108 }
00109
00110 ifdstream::ifdstream(int a_fd) : std::istream(&m_buf)
00111 {
00112 m_fd = a_fd;
00113 m_buf.attach(a_fd);
00114 }
00115
00116 void ifdstream::attach(int a_fd)
00117 {
00118 m_fd = a_fd;
00119 m_buf.attach(a_fd);
00120 }
00121
00122 int ifdstream::fd(void)
00123 {
00124 return(m_fd);
00125 }
00126
00127
00128