00001 #pragma ident "$Id: StreamBuf.hpp 3149 2012-06-20 16:22:19Z prestonherrmann $"
00002
00008 #ifndef GPSTK_STREAMBUF_HPP
00009 #define GPSTK_STREAMBUF_HPP
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #include <streambuf>
00034 #include <iosfwd>
00035 #include <ios>
00036
00037 namespace gpstk
00038 {
00040 template <typename ch, typename tr>
00041 class BasicStreamBuf: public std::basic_streambuf<ch, tr>
00042 {
00043 protected:
00044 typedef std::basic_streambuf<ch, tr> Base;
00045 typedef std::basic_ios<ch, tr> IOS;
00046 typedef ch char_type;
00047 typedef tr char_traits;
00048 typedef typename Base::int_type int_type;
00049 typedef typename Base::pos_type pos_type;
00050 typedef typename Base::off_type off_type;
00051 typedef typename IOS::openmode openmode;
00052
00053 public:
00054 BasicStreamBuf() : _pb(char_traits::eof()), _ispb(false)
00055 {
00056 this->setg(0, 0, 0);
00057 this->setp(0, 0);
00058 }
00059
00060 ~BasicStreamBuf()
00061 {
00062 }
00063
00064 virtual int_type overflow(int_type c)
00065 {
00066 if (c != char_traits::eof())
00067 return writeToDevice(char_traits::to_char_type(c));
00068 else
00069 return c;
00070 }
00071
00072 virtual int_type underflow()
00073 {
00074 if (_ispb)
00075 {
00076 return _pb;
00077 }
00078 else
00079 {
00080 int_type c = readFromDevice();
00081 if (c != char_traits::eof())
00082 {
00083 _ispb = true;
00084 _pb = c;
00085 }
00086 return c;
00087 }
00088 }
00089
00090 virtual int_type uflow()
00091 {
00092 if (_ispb)
00093 {
00094 _ispb = false;
00095 return _pb;
00096 }
00097 else
00098 {
00099 int_type c = readFromDevice();
00100 if (c != char_traits::eof())
00101 {
00102 _pb = c;
00103 }
00104 return c;
00105 }
00106 }
00107
00108 virtual int_type pbackfail(int_type c)
00109 {
00110 if (_ispb)
00111 {
00112 return char_traits::eof();
00113 }
00114 else
00115 {
00116 _ispb = true;
00117 _pb = c;
00118 return c;
00119 }
00120 }
00121
00122 virtual std::streamsize xsgetn(char_type* p, std::streamsize count)
00123 {
00124 std::streamsize copied = 0;
00125 while (count > 0)
00126 {
00127 int_type c = uflow();
00128 if (c == char_traits::eof()) break;
00129 *p++ = char_traits::to_char_type(c);
00130 ++copied;
00131 --count;
00132 }
00133 return copied;
00134 }
00135
00136 protected:
00137 static int_type charToInt(char_type c)
00138 {
00139 return char_traits::to_int_type(c);
00140 }
00141
00142 private:
00143 virtual int_type readFromDevice()
00144 {
00145 return char_traits::eof();
00146 }
00147
00148 virtual int_type writeToDevice(char_type)
00149 {
00150 return char_traits::eof();
00151 }
00152
00153 int_type _pb;
00154 bool _ispb;
00155
00156 BasicStreamBuf(const BasicStreamBuf&);
00157 BasicStreamBuf& operator = (const BasicStreamBuf&);
00158
00159 };
00160
00161 typedef BasicStreamBuf<char, std::char_traits<char> > StreamBuf;
00162
00163 }
00164
00165
00166
00167 #endif //GPSTK_BASICSTREAMBUF_HPP
00168