00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00039 #include "blocxx/BLOCXX_config.h"
00040 #include "blocxx/IOIFCStreamBuffer.hpp"
00041 #include <cstring>
00042
00043 namespace BLOCXX_NAMESPACE
00044 {
00045
00046 namespace
00047 {
00048 IOIFCStreamBuffer::EDirectionFlag directionToEnum(const char* direction)
00049 {
00050 if (strcmp(direction, "in") == 0)
00051 {
00052 return IOIFCStreamBuffer::E_IN;
00053 }
00054 if (strcmp(direction, "out") == 0)
00055 {
00056 return IOIFCStreamBuffer::E_OUT;
00057 }
00058 return IOIFCStreamBuffer::E_IN_OUT;
00059 }
00060 }
00061
00063 IOIFCStreamBuffer::IOIFCStreamBuffer(IOIFC* dev, int bufSize,
00064 const char* direction)
00065 : BaseStreamBuffer(directionToEnum(direction), bufSize)
00066 , m_dev(dev)
00067 , m_tied_buf(0)
00068 , m_error_action(IOIFC::E_RETURN_ON_ERROR)
00069 {
00070 }
00072 IOIFCStreamBuffer::IOIFCStreamBuffer(IOIFC* dev, EDirectionFlag direction, int bufSize)
00073 : BaseStreamBuffer(direction, bufSize)
00074 , m_dev(dev)
00075 , m_tied_buf(0)
00076 , m_error_action(IOIFC::E_RETURN_ON_ERROR)
00077 {
00078 }
00080 IOIFCStreamBuffer::~IOIFCStreamBuffer()
00081 {
00082 try
00083 {
00084 sync();
00085 }
00086 catch (...)
00087 {
00088 }
00089 }
00091 std::streambuf * IOIFCStreamBuffer::tie(std::streambuf * tied_buf)
00092 {
00093 std::streambuf * retval = m_tied_buf;
00094 m_tied_buf = tied_buf;
00095 return retval;
00096 }
00098 void IOIFCStreamBuffer::setErrorAction(IOIFC::ErrorAction error_action)
00099 {
00100 m_error_action = error_action;
00101 }
00102
00104 int
00105 IOIFCStreamBuffer::buffer_from_device(char* c, int n)
00106 {
00107 if (m_tied_buf)
00108 {
00109 m_tied_buf->pubsync();
00110 }
00111 return m_dev->read(c, n, m_error_action);
00112 }
00114 int
00115 IOIFCStreamBuffer::buffer_to_device(const char* c, int n)
00116 {
00117 while (n > 0)
00118 {
00119 int cnt = m_dev->write(c, n, m_error_action);
00120 if (cnt == -1)
00121 {
00122 return -1;
00123 }
00124 c += cnt;
00125 n -= cnt;
00126 }
00127 return 0;
00128 }
00129
00131 void
00132 IOIFCStreamBuffer::reset()
00133 {
00134 initBuffers();
00135 }
00136
00137 }
00138