portable_strstream.h
Go to the documentation of this file.00001 #ifndef portable_strstream_included
00002 #define portable_strstream_included
00003
00008
00009
00010 #ifndef LINUX
00011 #include <strstream>
00012 #else
00013
00014 #include <iterator>
00015 #include <strstream>
00016
00017 #if !defined(__GNUC__) || __GNUC__ < 3
00018 namespace std
00019 {
00020 using namespace std;
00021
00022
00023 template<class charT>
00024 class istreambuf_iterator
00025 : public input_iterator<charT, int>
00027 {
00028
00029 public:
00030 typedef charT char_type;
00031 typedef int int_type;
00032
00033 typedef std::streambuf streambuf_type;
00034 typedef std::istream istream_type;
00035
00036 class proxy {
00037 char_type __keep;
00038 streambuf_type *__sbuf;
00039
00040 proxy(char_type c, streambuf_type *sbuf)
00041 : __keep(c), __sbuf(sbuf)
00042 { ; }
00043
00044 public:
00045
00046 char_type operator*()
00047 { return __keep; }
00048
00049 friend class istreambuf_iterator<charT>;
00050 };
00051
00052 public:
00053
00054 istreambuf_iterator()
00055 : __sbuf(0)
00056 { __failed_flag = true; }
00057 istreambuf_iterator(istream_type& s)
00058 : __sbuf(s.rdbuf())
00059 {
00060 if ( s.rdbuf() ) __failed_flag = false;
00061 else
00062 __failed_flag = true;
00063 }
00064 istreambuf_iterator(streambuf_type *s)
00065 : __sbuf(s)
00066 {
00067 if ( s ) __failed_flag = false;
00068 else
00069 __failed_flag = true;
00070 }
00071 istreambuf_iterator(const proxy& p)
00072 : __sbuf(p.__sbuf)
00073 { ; }
00074 inline char_type operator*();
00075 inline istreambuf_iterator<charT>& operator++();
00076 inline proxy operator++(int);
00077 inline bool equal(istreambuf_iterator<charT>& b);
00078
00079 bool failed( ) const
00080 { return __failed_flag; }
00081
00082 protected:
00083
00084 private:
00085 streambuf_type *__sbuf;
00086 bool __failed_flag;
00087 };
00088
00089 template<class charT>
00090 inline typename istreambuf_iterator<charT>::char_type
00091 istreambuf_iterator<charT>::operator*()
00092 {
00093 int_type c;
00094
00095 if ( __sbuf && !__failed_flag )
00096 {
00097 c= __sbuf->sgetc();
00098 if ( c == EOF )
00099 {
00100 __sbuf = 0;
00101 __failed_flag = true;
00102 }
00103 }
00104 else return EOF;
00105
00106 return c;
00107 }
00108
00109 template<class charT>
00110 inline istreambuf_iterator<charT>&
00111 istreambuf_iterator<charT>::operator++()
00112 {
00113 if (__sbuf && !__failed_flag )
00114 {
00115 __sbuf->sbumpc();
00116 if ( __sbuf->sgetc() == EOF )
00117 {
00118 __sbuf = 0;
00119 __failed_flag = true;
00120 }
00121 }
00122 return *this;
00123 }
00124
00125
00126 template<class charT>
00127 inline typename istreambuf_iterator<charT>::proxy
00128 istreambuf_iterator<charT>::operator++(int)
00129 {
00130
00131 if (__sbuf && !__failed_flag )
00132 {
00133 proxy prev(__sbuf->sgetc(), __sbuf);
00134 __sbuf->sbumpc();
00135 if ( __sbuf->sgetc() == EOF )
00136 {
00137 __sbuf = 0;
00138 __failed_flag = true;
00139 }
00140 return prev;
00141 }
00142
00143 charT c=EOF;
00144 return proxy(c, __sbuf);
00145 }
00146
00147
00148 template<class charT>
00149 inline bool
00150 istreambuf_iterator<charT>::
00151 equal(istreambuf_iterator<charT>& b)
00152 {
00153 if( ((__sbuf ==0) && (b.__sbuf==0)) || ((__sbuf !=0) && (b.__sbuf !=0)) )
00154 return true;
00155 else
00156 return false;
00157 }
00158
00159
00160 template<class charT>
00161 inline bool operator==(istreambuf_iterator<charT>& a,
00162 istreambuf_iterator<charT>& b)
00163 {
00164 return a.equal(b);
00165 }
00166
00167
00168 template<class charT>
00169 inline bool operator!=(istreambuf_iterator<charT>& a,
00170 istreambuf_iterator<charT>& b)
00171 {
00172 return !(a.equal(b));
00173 }
00174
00175 template< class charT >
00176 class ostreambuf_iterator
00177 : public output_iterator
00178 {
00179
00180 public:
00181
00182
00183
00184 typedef charT char_type;
00185 typedef std::streambuf streambuf_type;
00186 typedef std::ostream ostream_type;
00187
00188 ostreambuf_iterator(ostream_type& s)
00189 : __sbuf(s.rdbuf())
00190 {
00191 if ( s.rdbuf() ) __failed_flag = false;
00192 else
00193 __failed_flag = true;
00194 }
00195
00196 ostreambuf_iterator(streambuf_type *s)
00197 : __sbuf(s)
00198 {
00199 if ( s ) __failed_flag = false;
00200 else
00201 __failed_flag = true;
00202 }
00203
00204 ostreambuf_iterator< charT >& operator*()
00205 { return *this; }
00206 ostreambuf_iterator< charT >& operator++()
00207 { return *this; }
00208 ostreambuf_iterator< charT > operator++(int)
00209 { return *this; }
00210
00211 ostreambuf_iterator< charT >& operator=(charT c)
00212 {
00213 if ( !__failed_flag )
00214 {
00215 if ( __sbuf->sputc(c) == EOF )
00216 __failed_flag=true;
00217 }
00218 return *this;
00219 }
00220
00221 bool failed( ) const
00222 { return __failed_flag; }
00223
00224 protected:
00225
00226 private:
00227 streambuf_type *__sbuf;
00228 bool __failed_flag;
00229 };
00230
00231
00232 };
00233
00234 #endif
00235
00236
00237 #endif
00238 #endif