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
00036 #include "OW_config.h"
00037 #include "OW_BaseStreamBuffer.hpp"
00038 #include "OW_Exception.hpp"
00039 #include "OW_String.hpp"
00040 #include "OW_Assertion.hpp"
00041 #include <iostream>
00042 #include <cstring>
00043
00044 namespace OW_NAMESPACE
00045 {
00046
00047 BaseStreamBuffer::BaseStreamBuffer(size_t bufSize,
00048 const char* direction_)
00049 : m_bufSize(bufSize), m_inputBuffer(NULL), m_outputBuffer(NULL)
00050 {
00051 String direction(direction_);
00052 if (direction.equals("in") || direction.equals("io"))
00053 {
00054 m_inputBuffer = new char[m_bufSize];
00055 initGetBuffer();
00056 }
00057 if (direction.equals("out") || direction.equals("io"))
00058 {
00059 m_outputBuffer = new char[m_bufSize];
00060 initPutBuffer();
00061 }
00062 }
00064 void
00065 BaseStreamBuffer::initBuffers()
00066 {
00067 initPutBuffer();
00068 initGetBuffer();
00069 }
00071 void
00072 BaseStreamBuffer::initPutBuffer()
00073 {
00074 setp(m_outputBuffer, m_outputBuffer + m_bufSize);
00075 }
00077 void
00078 BaseStreamBuffer::initGetBuffer()
00079 {
00080 setg(m_inputBuffer, m_inputBuffer, m_inputBuffer);
00081 }
00083 BaseStreamBuffer::~BaseStreamBuffer()
00084 {
00085 delete [] m_inputBuffer;
00086 delete [] m_outputBuffer;
00087 }
00089 int
00090 BaseStreamBuffer::sync()
00091 {
00092 return buffer_out();
00093 }
00095 int
00096 BaseStreamBuffer::buffer_out()
00097 {
00098
00099
00100 #if __GNUC__ == 2 && __GNUC_MINOR__ <= 96
00101 try
00102 {
00103 #endif
00104 int cnt = pptr() - pbase();
00105 int retval = buffer_to_device(m_outputBuffer, cnt);
00106 pbump(-cnt);
00107 return retval;
00108 #if __GNUC__ == 2 && __GNUC_MINOR__ <= 96
00109 }
00110 catch (const Exception& e)
00111 {
00112 std::cerr << "Caught Exception in BaseStreamBuffer::buffer_out(): " << e << std::endl;
00113 return EOF;
00114 }
00115 catch (const std::exception& e)
00116 {
00117 std::cerr << "Caught std::exception in BaseStreamBuffer::buffer_out(): " << e.what() << std::endl;
00118 return EOF;
00119 }
00120 catch (...)
00121 {
00122 std::cerr << "Caught unknown exception in BaseStreamBuffer::buffer_out()" << std::endl;
00123 return EOF;
00124 }
00125 #endif
00126 }
00128 int
00129 BaseStreamBuffer::overflow(int c)
00130 {
00131 if (buffer_out() < 0)
00132 {
00133 return EOF;
00134 }
00135 else
00136 {
00137 if (c != EOF)
00138 {
00139 return sputc(c);
00140 }
00141 else
00142 {
00143 return c;
00144 }
00145 }
00146 }
00148 std::streamsize
00149 BaseStreamBuffer::xsputn(const char* s, std::streamsize n)
00150 {
00151 if (n < epptr() - pptr())
00152 {
00153 memcpy(pptr(), s, n * sizeof(char));
00154 pbump(n);
00155 return n;
00156 }
00157 else
00158 {
00159 for (std::streamsize i = 0; i < n; i++)
00160 {
00161 if (sputc(s[i]) == EOF)
00162 {
00163 return i;
00164 }
00165 }
00166 return n;
00167 }
00168 }
00170 int
00171 BaseStreamBuffer::underflow()
00172 {
00173
00174
00175 #if __GNUC__ == 2 && __GNUC_MINOR__ <= 96
00176 try
00177 {
00178 #endif
00179 if (gptr() < egptr())
00180 {
00181 return static_cast<unsigned char>(*gptr());
00182 }
00183 if (buffer_in() < 0)
00184 {
00185 return EOF;
00186 }
00187 else
00188 {
00189 return static_cast<unsigned char>(*gptr());
00190 }
00191 #if __GNUC__ == 2 && __GNUC_MINOR__ <= 96
00192 }
00193 catch (const Exception& e)
00194 {
00195 std::cerr << "Caught Exception in BaseStreamBuffer::underflow(): " << e << std::endl;
00196 return EOF;
00197 }
00198 catch (const std::exception& e)
00199 {
00200 std::cerr << "Caught std::exception in BaseStreamBuffer::underflow(): " << e.what() << std::endl;
00201 return EOF;
00202 }
00203 catch (...)
00204 {
00205 std::cerr << "Caught unknown exception in BaseStreamBuffer::underflow()" << std::endl;
00206 return EOF;
00207 }
00208 #endif
00209 }
00211 int
00212 BaseStreamBuffer::buffer_in()
00213 {
00214 int retval = buffer_from_device(m_inputBuffer,
00215 m_bufSize);
00216 if (retval <= 0)
00217 {
00218 setg(0,0,0);
00219 return -1;
00220 }
00221 else
00222 {
00223 setg(m_inputBuffer, m_inputBuffer, m_inputBuffer + retval);
00224 return retval;
00225 }
00226 }
00228 int
00229 BaseStreamBuffer::buffer_to_device(const char* c, int n)
00230 {
00231 OW_ASSERT("Not implemented, should overwrite" == 0);
00232 return -1;
00233 }
00235 int
00236 BaseStreamBuffer::buffer_from_device(char* c, int n)
00237 {
00238 OW_ASSERT("Not implemented, should overwrite" == 0);
00239 return -1;
00240 }
00241
00242 }
00243