ximol/encoders/encoders.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 Facet access (implementation).
00024 
00025         \author Tournois Florent
00026         \version 1.0
00027 
00028     $Id: encoders.cpp,v 1.12 2004/02/22 10:27:34 tournois Exp $
00029     $Log: encoders.cpp,v $
00030     Revision 1.12  2004/02/22 10:27:34  tournois
00031     Add some doc.
00032 
00033     Revision 1.11  2004/02/22 09:54:20  tournois
00034     Change years on the copyright.
00035 
00036     Revision 1.10  2003/11/18 18:54:51  tournois
00037     Add str_cast and drop the transformation.hpp file.
00038 
00039     Revision 1.9  2003/11/16 13:25:25  tournois
00040     Change the error to use wide string.
00041     Change the translation to use wide string.
00042 
00043     Revision 1.8  2003/11/02 19:23:01  tournois
00044     Go to the boost coding standard.
00045     Change all name to lower case.
00046 
00047     Revision 1.7  2003/10/25 18:17:38  hfp
00048     *** empty log message ***
00049 
00050     Revision 1.6  2003/10/25 13:53:39  hfp
00051     review and separation
00052 
00053     Revision 1.5  2003/10/13 21:01:20  tournois
00054     no message
00055 
00056     Revision 1.4  2003/10/09 16:59:45  tournois
00057     add the stringstream initialisation.
00058     add some prolog test.
00059     Correct code for VC6.0.
00060 
00061     Revision 1.3  2003/10/03 17:29:41  tournois
00062     no message
00063 
00064     Revision 1.2  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/encoders/encoders.hpp>
00072 #include <ximol/encoders/error.hpp>
00073 #include <ximol/translation.hpp>
00074 #include <ximol/codecvt.hpp>
00075 
00076 
00077 XIMOL_ENCODERS_BEGIN_NAMESPACE
00078 
00079 //-----------------------------------------------------------------------------
00080 // Interface for the ximol_encoders.
00081 //-----------------------------------------------------------------------------
00082 ::std::codecvt<xchar_t, char, mbstate_t>* get_ximol_facet(::std::string const& encoding_name)
00083 {
00084         return new XIMOL_NAMESPACE_PATH::codecvt(encoding_name);
00085 }
00086 
00087 //-----------------------------------------------------------------------------
00088 // place with new placement.
00089 //-----------------------------------------------------------------------------
00090 void place_ximol_facet(::std::codecvt<xchar_t, char, mbstate_t>* placement, ::std::string const & encoding_name)
00091 {
00092         new (placement) XIMOL_NAMESPACE_PATH::codecvt(encoding_name);
00093 }
00094 
00095 //-----------------------------------------------------------------------------
00096 // Change encoding on a unique facet.
00097 //-----------------------------------------------------------------------------
00098 void change_ximol_facet_encoding(::std::codecvt<xchar_t, char, mbstate_t>* facet, ::std::string const & encoding_name)
00099 {
00100     if (facet==NULL) return;
00101         XIMOL_NAMESPACE_PATH::codecvt* the_facet=dynamic_cast<XIMOL_NAMESPACE_PATH::codecvt*>(facet);
00102 //      XIMOL_NAMESPACE_PATH::codecvt* the_facet=static_cast<XIMOL_NAMESPACE_PATH::codecvt*>(facet);
00103         if (the_facet==NULL) 
00104                 XIMOL_ENCODERS_THROW << _(L"XiMoL Encoder facet problem. ") 
00105                              << _(L"We can not retrive the XiMoL facet pointer. ")
00106                              << _(L"And then, the encoding can not be changed to '%o'. ", encoding_name )
00107                                      << XIMOL_ENCODERS_AS_ERROR;
00108         the_facet->delete_encoding();
00109         the_facet->create_encoding(encoding_name);
00110 }
00111 
00112 //-----------------------------------------------------------------------------
00113 // change the encoding.
00114 //-----------------------------------------------------------------------------
00115 void deep_change(::std::basic_ios<xchar_t>& ios, ::std::string const & encoding_name)
00116 {
00117         typedef ::std::codecvt<xchar_t,char,mbstate_t> FacetType;
00118 
00119         const FacetType* pfacet=&(XIMOL_USE_FACET(ios.getloc(),FacetType));
00120         
00121     // Warning there is a const cast.
00122         FacetType* the_facet=const_cast<FacetType*>(pfacet);
00123 
00124         change_ximol_facet_encoding(the_facet,encoding_name);
00125 }
00126 
00127 //-----------------------------------------------------------------------------
00128 // prepare ios.
00129 //-----------------------------------------------------------------------------
00130 void prepare_ios(::std::basic_ios<xchar_t>& ios, ::std::string const & encoding_name)
00131 {
00132     ios.imbue(XIMOL_ADD_FACET(ios.getloc(), get_ximol_facet(encoding_name)));
00133 }
00134 
00135 //-----------------------------------------------------------------------------
00136 // test ios.
00137 //-----------------------------------------------------------------------------
00138 bool test_ios(::std::basic_ios<xchar_t> & ios)
00139 {
00140         typedef ::std::codecvt<xchar_t,char,mbstate_t> FacetType;
00141 
00142         const FacetType* pfacet=&(XIMOL_USE_FACET(ios.getloc(),FacetType));
00143         return NULL != dynamic_cast<const XIMOL_NAMESPACE_PATH::codecvt*>(pfacet);
00144 }
00145 
00146 XIMOL_ENCODERS_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.