ximol/io.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 GetFrom and PutTo operator for common char and xchar_t (Implementation).
00024 
00025     \author Tournois Florent
00026         \version 1.0
00027 
00028     $Id: io.cpp,v 1.11 2004/02/22 10:27:32 tournois Exp $
00029     $Log: io.cpp,v $
00030     Revision 1.11  2004/02/22 10:27:32  tournois
00031     Add some doc.
00032 
00033     Revision 1.10  2004/02/22 09:54:19  tournois
00034     Change years on the copyright.
00035 
00036     Revision 1.9  2004/02/15 10:13:36  hfp
00037     an additional to_wide_char()-call is needed to compile
00038 
00039     Revision 1.8  2004/02/02 19:51:44  tournois
00040     Add a control_flow and finish the attribute tutorial.
00041 
00042     Revision 1.7  2003/11/27 15:31:55  hfp
00043     partially adapted to vc6.
00044 
00045     Revision 1.6  2003/11/19 20:52:54  tournois
00046     Add new manipulator for stag and etag.
00047     Correct bugs and add tests.
00048 
00049     Revision 1.5  2003/11/18 18:54:51  tournois
00050     Add str_cast and drop the transformation.hpp file.
00051 
00052     Revision 1.4  2003/11/02 19:23:01  tournois
00053     Go to the boost coding standard.
00054     Change all name to lower case.
00055 
00056     Revision 1.3  2003/10/25 13:53:39  hfp
00057     review and separation
00058 
00059     Revision 1.2  2003/10/09 16:59:45  tournois
00060     add the stringstream initialisation.
00061     add some prolog test.
00062     Correct code for VC6.0.
00063 
00064     Revision 1.1  2003/09/24 08:28:17  tournois
00065     Create the namespace Encoders, Parser, Xml
00066     Change the library organization.
00067     add VC7.1 project for this organization.
00068 
00069   */
00070 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00071 #include <ximol/io.hpp>
00072 #include <ximol/stream.hpp>
00073 #include <ximol/str_cast.hpp>
00074 #include <algorithm>
00075 #include <functional>
00076 #include <locale>
00077 
00078 XIMOL_BEGIN_PRIVATE_NAMESPACE
00079 
00080 wchar_t to_wide_char(char c)
00081 {
00082     XIMOL_USING_NAMESPACE;
00083     return converter<wchar_t,char>::get(c);
00084 };
00085 
00086 char to_narrow_char(wchar_t xc)
00087 {
00088     XIMOL_USING_NAMESPACE;
00089     return converter<char,wchar_t>::get(xc);
00090 };
00091 
00092 wchar_t to_wide_char(char c, ::std::basic_ios<wchar_t>& ios)
00093 {
00094     using namespace std;
00095     typedef ctype<wchar_t> ctype_facet;
00096     const ctype_facet & the_facet = XIMOL_USE_FACET(ios.getloc(), ctype_facet);
00097     return the_facet.widen(c);
00098 };
00099 
00100 char to_narrow_char(wchar_t xc, ::std::basic_ios<wchar_t>& ios)
00101 {
00102     using namespace std;
00103     typedef ctype<wchar_t> ctype_facet;
00104     const ctype_facet & the_facet = XIMOL_USE_FACET(ios.getloc(), ctype_facet);
00105     return the_facet.narrow(xc,' ');
00106 };
00107 
00108 XIMOL_END_PRIVATE_NAMESPACE
00109 
00110 namespace std {
00111     ostream& operator<<(ostream& ostream_, const wchar_t* pXChar_)
00112     {
00113                 if(pXChar_) while(*pXChar_ != '\0') operator<<(ostream_, to_narrow_char(*pXChar_++));
00114                 return ostream_;
00115     }
00116 
00117 /*    ostream& operator<<(ostream & os, const wchar_t xc)
00118     {
00119             return os << to_narrow_char(xc);
00120     };
00121 
00122 /*    ostream& operator<<(ostream & os, const wstring & wstr)
00123     {
00124         return os << wstr.c_str();
00125     };*/
00126 
00127     wostream& operator<<(wostream& wostream_, const char* pChar_)
00128     {
00129                 if(pChar_) while (*pChar_ != '\0') operator<<(wostream_, to_wide_char(*pChar_++));
00130         return wostream_;
00131     }
00132 
00133 /*    wostream& operator<<(wostream & wos, const char c)
00134     {
00135         return wos << to_wide_char(c,wos);
00136     };
00137 
00138 /*    wostream& operator<<(wostream & wos, const string & str)
00139     {
00140         return wos << str.c_str();
00141     };*/
00142 
00143 /*    istream& operator>>(istream & is, wchar_t & xc)
00144     {
00145         char c;
00146         is >> c;
00147         xc=to_wide_char(c);
00148         return is;
00149     };
00150 
00151 /*    istream& operator>>(istream & is, wstring & wstr)
00152     {
00153         string str;
00154         is >> str;
00155         wstr.resize(str.length());
00156         transform(str.begin(),str.end(),wstr.begin(),pointer_to_unary_function<char, wchar_t>(to_wide_char));
00157         return is;
00158     };*/
00159 
00160 /*    wistream& operator>>(wistream & wis, char & c)
00161     {
00162         wchar_t wc;
00163         wis >> wc;
00164         c=to_narrow_char(wc,wis);
00165         return wis;
00166     };
00167 
00168 /*    wistream& operator>>(wistream & wis, string & str)
00169     {
00170         wstring wstr;
00171         wis >> wstr;
00172         str.resize(wstr.length());
00173         transform(wstr.begin(),wstr.end(),str.begin(),pointer_to_unary_function<wchar_t, char>(to_narrow_char));
00174         return wis;
00175     };*/
00176 
00177 }; // end of namespace std
00178 
00179 XIMOL_BEGIN_NAMESPACE
00180 
00181 //-----------------------------------------------------------------------------
00182 // XML stream put to operator.
00183 //-----------------------------------------------------------------------------
00184 XIMOL_TEMPLATE_NULL
00185 xostream& operator<<(xostream& xos, const char c)
00186 {
00187         //typedef ctype<char> CTypeFacet;
00188         //const CTypeFacet& Facet = XIMOL_USE_FACET(locale::classic(), CTypeFacet);
00189         xos.rdbuf()->sputc((xchar_t)((unsigned char)c));
00190         return xos;
00191 }
00192 
00193 //-----------------------------------------------------------------------------
00194 // XML stream put to operator.
00195 //-----------------------------------------------------------------------------
00196 XIMOL_TEMPLATE_NULL
00197 xostream& operator<<(xostream& xos, const char * c)
00198 {
00199         //typedef ctype<char> CTypeFacet;
00200         //const CTypeFacet& Facet = XIMOL_USE_FACET(locale::classic(), CTypeFacet);
00201         while (*c != '\0') xos.rdbuf()->sputc((xchar_t)((unsigned char)*c++));
00202         return xos;
00203 }
00204 
00205 //-----------------------------------------------------------------------------
00206 // XML stream put to operator.
00207 //-----------------------------------------------------------------------------
00208 XIMOL_TEMPLATE_NULL
00209 xostream& operator<<(xostream& xostream_, const std::string& str_)
00210 {
00211         return xostream_ << str_.c_str();
00212 }
00213 
00214 //-----------------------------------------------------------------------------
00215 // XML stream put to operator.
00216 //-----------------------------------------------------------------------------
00217 //XIMOL_TEMPLATE_NULL
00218 //xostream& operator<<(xostream& xos, const string& c)
00219 //{
00220 //      return xos<<c.c_str();
00221 //}
00222 
00223 //-----------------------------------------------------------------------------
00224 // XML stream put to operator.
00225 //-----------------------------------------------------------------------------
00226 //XIMOL_TEMPLATE_NULL
00227 //xostream& operator<<(xostream& xos, xchar_t c)
00228 //{
00229 //      xos.rdbuf()->sputc((xchar_t)(c));
00230 //      return xos;
00231 //}
00232 
00233 //-----------------------------------------------------------------------------
00234 // XML stream put to operator.
00235 //-----------------------------------------------------------------------------
00236 XIMOL_TEMPLATE_NULL
00237 xostream& operator<<(xostream& xos, const xchar_t * c)
00238 {
00239         while (*c != '\0') xos.rdbuf()->sputc((xchar_t)(*c++));
00240         return xos;
00241 }
00242 
00243 //-----------------------------------------------------------------------------
00244 // XML stream put to operator.
00245 //-----------------------------------------------------------------------------
00246 XIMOL_TEMPLATE_NULL
00247 xostream& operator<<(xostream& xos, const xstring& c)
00248 {
00249         return xos<<c.c_str();
00250 }
00251 
00252 //-----------------------------------------------------------------------------
00253 // XML stream get from operator.
00254 //-----------------------------------------------------------------------------
00255 //XIMOL_TEMPLATE_NULL
00256 //xistream& operator>>(xistream& xis, char & c)
00257 //{
00258 //      xchar_t xc;
00259 //      xis >> xc;
00260 //      c=(char)(xc);
00261 //      return xis;
00262 //}
00263 
00264 //-----------------------------------------------------------------------------
00265 // XML stream get from operator.
00266 //-----------------------------------------------------------------------------
00267 //XIMOL_TEMPLATE_NULL
00268 //xistream& operator>>(xistream& xis, xchar_t & c)
00269 //{
00270 //      //((::std::basic_istream<xchar_t>&)xis)>>c;
00271 //      if (! xis.get(c))
00272 //              xis.setstate(std::ios::failbit | std::ios::eofbit); // with this line we could acces the xstream buffer for more than one putback.
00273 //      return xis;
00274 //}
00275 
00276 using namespace std;
00277 
00278 xistream& operator>>(xistream& xis, bool           & c){ (basic_istream<xchar_t>&)xis>>c; return xis;}
00279 xistream& operator>>(xistream& xis, short          & c){ (basic_istream<xchar_t>&)xis>>c; return xis;}
00280 xistream& operator>>(xistream& xis, long           & c){ (basic_istream<xchar_t>&)xis>>c; return xis;}
00281 xistream& operator>>(xistream& xis, unsigned long  & c){ (basic_istream<xchar_t>&)xis>>c; return xis;}
00282 xistream& operator>>(xistream& xis, int            & c){ (basic_istream<xchar_t>&)xis>>c; return xis;}
00283 xistream& operator>>(xistream& xis, unsigned int   & c){ (basic_istream<xchar_t>&)xis>>c; return xis;}
00284 xistream& operator>>(xistream& xis, float          & c){ (basic_istream<xchar_t>&)xis>>c; return xis;}
00285 xistream& operator>>(xistream& xis, double         & c){ (basic_istream<xchar_t>&)xis>>c; return xis;}
00286 xistream& operator>>(xistream& xis, long double    & c){ (basic_istream<xchar_t>&)xis>>c; return xis;}
00287 xistream& operator>>(xistream& xis, ::std::basic_istream<xchar_t>& (*fun)( ::std::basic_istream<xchar_t>&)){ fun(xis); return xis;}
00288 
00289 xostream& operator<<(xostream& xos, bool           c){ (basic_ostream<xchar_t>&)xos<<c; return xos;}
00290 xostream& operator<<(xostream& xos, short          c){ (basic_ostream<xchar_t>&)xos<<c; return xos;}
00291 xostream& operator<<(xostream& xos, long           c){ (basic_ostream<xchar_t>&)xos<<c; return xos;}
00292 xostream& operator<<(xostream& xos, unsigned long  c){ (basic_ostream<xchar_t>&)xos<<c; return xos;}
00293 xostream& operator<<(xostream& xos, int            c){ (basic_ostream<xchar_t>&)xos<<c; return xos;}
00294 xostream& operator<<(xostream& xos, unsigned int   c){ (basic_ostream<xchar_t>&)xos<<c; return xos;}
00295 xostream& operator<<(xostream& xos, float          c){ (basic_ostream<xchar_t>&)xos<<c; return xos;}
00296 xostream& operator<<(xostream& xos, double         c){ (basic_ostream<xchar_t>&)xos<<c; return xos;}
00297 xostream& operator<<(xostream& xos, long double    c){ (basic_ostream<xchar_t>&)xos<<c; return xos;}
00298 xostream& operator<<(xostream& xos, ::std::basic_ostream<xchar_t>& (*fun)( ::std::basic_ostream<xchar_t>&)){ fun(xos); return xos;}
00299 
00300 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.