ximol/stream.cpp

Go to the documentation of this file.
00001 /*****************************************************************************\
00002  *                                                                           *
00003  * library XiMoL                                                             *
00004  * Copyright (C) 2002, 2003, 2004 Florent Tournois                           *
00005  *                                                                           *
00006  * This library is free software; you can redistribute it and/or             *
00007  * modify it under the terms of the GNU Lesser General Public                *
00008  * License as published by the Free Software Foundation; either              *
00009  * version 2.1 of the License, or (at your option) any later version.        *
00010  *                                                                           *
00011  * This library is distributed in the hope that it will be useful,           *
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of            *
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         *
00014  * Lesser General Public License for more details.                           *
00015  *                                                                           *
00016  * You should have received a copy of the GNU Lesser General Public          *
00017  * License along with this library; if not, write to the Free Software       *
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA   *
00019  *                                                                           *
00020 \*****************************************************************************/
00021 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00022 /** \file 
00023         \brief XML streams implementations.
00024         
00025         \author Tournois Florent
00026         \version 1.0
00027 
00028     $Id: stream.cpp,v 1.10 2004/02/22 10:27:32 tournois Exp $
00029     $Log: stream.cpp,v $
00030     Revision 1.10  2004/02/22 10:27:32  tournois
00031     Add some doc.
00032 
00033     Revision 1.9  2004/02/22 09:54:19  tournois
00034     Change years on the copyright.
00035 
00036     Revision 1.8  2003/11/18 18:54:51  tournois
00037     Add str_cast and drop the transformation.hpp file.
00038 
00039     Revision 1.7  2003/11/02 19:23:01  tournois
00040     Go to the boost coding standard.
00041     Change all name to lower case.
00042 
00043     Revision 1.6  2003/10/25 18:17:38  hfp
00044     *** empty log message ***
00045 
00046     Revision 1.5  2003/10/25 13:53:39  hfp
00047     review and separation
00048 
00049     Revision 1.4  2003/10/09 16:59:45  tournois
00050     add the stringstream initialisation.
00051     add some prolog test.
00052     Correct code for VC6.0.
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 // Change encoding.
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 // Change encoding.
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         //((::std::basic_istream<xchar_t>&)xis)>>c;
00136         if (! get(c))
00137                 setstate(std::ios::failbit | std::ios::eofbit); // with this line we could acces the xstream buffer for more than one putback.
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


Donate to the XiMoL project SourceForge.net Logo If you have any questions about XiMoL, you could write to tournois@users.sourceforge.net.