ximol/str_cast.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 cast to string implementation.
00024 
00025     \author Tournois Florent
00026         \version 1.0
00027 
00028     $Id: str_cast.cpp,v 1.4 2004/02/22 10:27:32 tournois Exp $
00029     $Log: str_cast.cpp,v $
00030     Revision 1.4  2004/02/22 10:27:32  tournois
00031     Add some doc.
00032 
00033     Revision 1.3  2004/02/22 09:54:19  tournois
00034     Change years on the copyright.
00035 
00036     Revision 1.2  2003/11/27 15:31:55  hfp
00037     partially adapted to vc6.
00038 
00039     Revision 1.1  2003/11/18 18:54:51  tournois
00040     Add str_cast and drop the transformation.hpp file.
00041 
00042 
00043   */
00044 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00045 #include <ximol/str_cast.hpp>
00046 #include <ximol/macros.hpp>
00047 #include <algorithm>
00048 #include <functional>
00049 #include <locale>
00050 
00051 XIMOL_BEGIN_PRIVATE_NAMESPACE
00052 
00053 wchar_t to_wide_char(char c)
00054 {
00055     using namespace std;
00056     typedef ctype<wchar_t> ctype_facet;
00057     static const ctype_facet & the_facet = XIMOL_USE_FACET(::std::locale::classic(), ctype_facet);
00058     return the_facet.widen(c);
00059 };
00060 
00061 char to_narrow_char(wchar_t xc)
00062 {
00063     using namespace std;
00064     typedef ctype<wchar_t> ctype_facet;
00065     static const ctype_facet & the_facet = XIMOL_USE_FACET(::std::locale::classic(), ctype_facet);
00066     return the_facet.narrow(xc,' ');
00067 };
00068 
00069 XIMOL_END_PRIVATE_NAMESPACE
00070 
00071 XIMOL_BEGIN_NAMESPACE
00072 
00073 //-----------------------------------------------------------------------------
00074 //
00075 //-----------------------------------------------------------------------------
00076 char converter<char, wchar_t>::get(wchar_t xc)
00077 {
00078     return to_narrow_char(xc);
00079 };
00080 
00081 //-----------------------------------------------------------------------------
00082 //
00083 //-----------------------------------------------------------------------------
00084 ::std::string converter<char, wchar_t>::get(const ::std::wstring & wstr)
00085 {
00086     using namespace std;
00087     string str;
00088     str.resize(wstr.length());
00089     transform(wstr.begin(),wstr.end(),str.begin(),ptr_fun(to_narrow_char));
00090     return str;
00091 };
00092 
00093 //-----------------------------------------------------------------------------
00094 //
00095 //-----------------------------------------------------------------------------
00096 char converter<char, char>::get(char c)
00097 {
00098     return c;
00099 };
00100 
00101 //-----------------------------------------------------------------------------
00102 //
00103 //-----------------------------------------------------------------------------
00104 ::std::string converter<char, char>::get(const ::std::string& str_)
00105 {
00106     return str_;
00107 };
00108 
00109 //-----------------------------------------------------------------------------
00110 //
00111 //-----------------------------------------------------------------------------
00112 wchar_t converter<wchar_t, char>::get(char c)
00113 {    
00114     return to_wide_char(c);
00115 
00116 };
00117 
00118 //-----------------------------------------------------------------------------
00119 //
00120 //-----------------------------------------------------------------------------
00121 ::std::wstring converter<wchar_t, char>::get(const ::std::string& str_)
00122 {
00123     using namespace std;
00124     wstring wstr;
00125     wstr.resize(str_.length());
00126     transform(str_.begin(), str_.end(), wstr.begin(), ptr_fun(to_wide_char));
00127     return wstr;
00128 }
00129 
00130 //-----------------------------------------------------------------------------
00131 //
00132 //-----------------------------------------------------------------------------
00133 wchar_t converter<wchar_t, wchar_t>::get(wchar_t xc)
00134 {
00135     return xc;
00136 };
00137 
00138 //-----------------------------------------------------------------------------
00139 //
00140 //-----------------------------------------------------------------------------
00141 ::std::wstring converter<wchar_t, wchar_t>::get(const ::std::wstring & wstr)
00142 {
00143     return wstr;
00144 };
00145 
00146 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.