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 
00034 
00035 
00036 
00037 
00038 
00039 
00040 
00041 
00042 
00043 
00044 
00045 
00046 
00047 
00048 
00049 
00050 
00051 
00052 
00053 
00054 
00055 
00056 #include <ximol/stream.hpp>
00057 #include <ximol/encoders/encoders.hpp>
00058 #include <ximol/str_cast.hpp>
00059 
00060 
00061 XIMOL_BEGIN_NAMESPACE
00062 
00063 
00064 
00065 
00066 xostream& use_XML(xostream& xos, const ::std::string& encoding_name)
00067 {
00068         xos.encoding(encoding_name.c_str());
00069         return xos;
00070 }
00071 
00072 
00073 
00074 
00075 xistream& use_XML(xistream& xis, const ::std::string& encoding_name)
00076 {
00077         xis.encoding(encoding_name.c_str());
00078         return xis;
00079 }
00080 
00081 
00082 
00083 
00084 xiostream::xiostream(const char* encoding_name)
00085         : xistream(encoding_name)
00086         , xostream(encoding_name)
00087 {
00088 }
00089 
00090 
00091 
00092 
00093 xostream::xostream(const char* encoding_name)
00094         :std::basic_ostream<xchar_t>(0)
00095 {
00096         XIMOL_ENCODERS_USING_NAMESPACE;
00097 
00098         prepare_ios(*this, encoding_name);
00099         encoding(encoding_name);
00100 }
00101 
00102 
00103 
00104 
00105 xostream& xostream::operator<<(xchar_t c)
00106 {
00107         rdbuf()->sputc((xchar_t)(c));
00108         return *this;
00109 }
00110 
00111 xostream& xostream::operator<<(char c)
00112 {
00113         rdbuf()->sputc((xchar_t)((unsigned char)c));
00114         return *this;
00115 }
00116 
00117 
00118 
00119 
00120 xistream::xistream(const char* encoding_name)
00121         : std::basic_istream<xchar_t>(0)
00122         , buffer_()
00123 {
00124         XIMOL_ENCODERS_USING_NAMESPACE;
00125 
00126         prepare_ios(*this, encoding_name);
00127         encoding(encoding_name);
00128 }
00129 
00130 
00131 
00132 
00133 xistream& xistream::operator>>(xchar_t& c)
00134 {
00135         
00136         if (! get(c))
00137                 setstate(std::ios::failbit | std::ios::eofbit); 
00138         return *this;
00139 }
00140 
00141 
00142 
00143 
00144 xistream& xistream::operator>>(char& c)
00145 {
00146         xchar_t xc(0);
00147         *this >> xc;
00148         c=(char)(xc);
00149         return *this;
00150 }
00151 
00152 
00153 
00154 
00155 
00156 bool xistream::get(xchar_t & xc)
00157 {
00158         if (buffer_.empty()) 
00159         {
00160                 if (rdbuf()->sgetn(&xc, 1) != 1) return false;
00161         } else {
00162                 xc = buffer_.front();
00163                 pop_front();
00164         }
00165         return true;
00166 }
00167 
00168 
00169 
00170 
00171 xchar_t xistream::front() const
00172 {
00173         if (buffer_.empty()) 
00174         {
00175                 xchar_t xc; 
00176                 if (rdbuf()->sgetn(&xc, 1) == 1) rdbuf()->sputbackc(xc);
00177                 return xc;
00178         }
00179         return buffer_.front();
00180 }
00181 
00182 
00183 
00184 
00185 void xistream::pop_front()
00186 {
00187         if (buffer_.empty()) 
00188         {
00189                 xchar_t xc; 
00190                 rdbuf()->sgetn(&xc, 1);
00191         } else {
00192                 buffer_.pop_front();
00193         }
00194 }
00195 
00196 
00197 
00198 
00199 void xistream::putback(xchar_t xc)
00200 {
00201         buffer_.push_front(xc);
00202 }
00203 
00204 
00205 
00206 
00207 void xistream::putback(const xstring & xstr)
00208 {
00209         if (xstr.empty()) return;
00210         xstring::const_reverse_iterator i(xstr.rbegin()), i_end(xstr.rend());
00211         for(;i!=i_end;++i) putback(*i);
00212 }
00213 
00214 
00215 
00216 
00217 ::std::string xostream::encoding() const
00218 {
00219         return str< ::std::string>::cast(context.get_encoding_decl());
00220 }
00221 
00222 
00223 
00224 
00225 void xostream::encoding(const char* encoding_name)
00226 {
00227         XIMOL_ENCODERS_USING_NAMESPACE;
00228 
00229         deep_change(*this, encoding_name);
00230         context.set_encoding_decl(str< ::std::wstring>::cast(encoding_name));
00231 }
00232 
00233 
00234 
00235 
00236 ::std::string xistream::encoding() const
00237 {
00238         return str< ::std::string>::cast(context.get_encoding_decl());
00239 }
00240 
00241 
00242 
00243 
00244 void xistream::encoding(const char* encoding_name)
00245 {
00246         XIMOL_ENCODERS_USING_NAMESPACE;
00247 
00248         deep_change(*this, encoding_name);
00249         context.set_encoding_decl(str< ::std::wstring>::cast(encoding_name));
00250 }
00251 
00252 
00253 XIMOL_END_NAMESPACE