ximol/codecvt.hpp

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 codecvt (declaration).
00024     @ingroup encoders
00025     We propose an libiconv wrapper for the C++ iostream. 
00026 
00027         \author Tournois Florent
00028         \version 1.0
00029 
00030     $Id: codecvt.hpp,v 1.8 2004/02/25 18:58:39 tournois Exp $
00031     $Log: codecvt.hpp,v $
00032     Revision 1.8  2004/02/25 18:58:39  tournois
00033     imporve the gcc compatibility.
00034 
00035     Revision 1.7  2004/02/22 10:27:32  tournois
00036     Add some doc.
00037 
00038     Revision 1.6  2004/02/22 09:54:19  tournois
00039     Change years on the copyright.
00040 
00041     Revision 1.5  2004/01/27 21:49:51  tournois
00042     Add docs and tutorial.
00043 
00044     Revision 1.4  2003/11/02 19:23:01  tournois
00045     Go to the boost coding standard.
00046     Change all name to lower case.
00047 
00048     Revision 1.3  2003/10/27 11:29:32  hfp
00049     *** empty log message ***
00050 
00051     Revision 1.2  2003/10/27 10:36:50  hfp
00052     *** empty log message ***
00053 
00054     Revision 1.1  2003/10/25 18:20:32  hfp
00055     XiMoL now provides a own codecvt (interface) based on standard.
00056     Hides use of iconv-library as implementation detail.
00057 
00058     Revision 1.6  2003/10/25 13:53:39  hfp
00059     review and separation
00060 
00061   */
00062 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00063 #ifndef XIMOL_CODECVT_HPP_
00064 #define XIMOL_CODECVT_HPP_
00065 
00066 #include <ximol/export_defs.hpp>
00067 #include <ximol/typedefs.hpp>
00068 #include <locale>
00069 
00070 XIMOL_BEGIN_NAMESPACE
00071 
00072 //=============================================================================
00073 /** Class codecvt.
00074  *  @ingroup encoders
00075  *  A small class to wrap the libiconv library.
00076  */
00077 //=============================================================================
00078 class XIMOL_EXPORT codecvt
00079         : public ::std::codecvt<xchar_t, char, mbstate_t>
00080 {
00081 public: // nested types
00082         typedef xchar_t    intern_type; //< internal char type.
00083         typedef char       extern_type; //< external char type.
00084         typedef mbstate_t  state_type;  //< state type.
00085 
00086 public: // constructor(s) & destructor
00087         /**
00088          * Contructor.
00089          * The constructor initializes its locale::facet base 
00090          *              object with locale::facet(refs).
00091          *  
00092          * If refs==0, the lifetime is managed by the locale that contains this. 
00093          * If refs==1, the memory must be explcitly managed. 
00094          * the behavior for refs>1 is not defined.
00095          */
00096         explicit codecvt(const ::std::string& encoding_name,
00097                                          size_t refs = 0);
00098 
00099         /// Free the libiconv memory
00100         void delete_encoding();
00101 
00102         /// Allocate the libiconv memory
00103         void create_encoding(const ::std::string& encoding_name);
00104 
00105         /// Destructor.
00106         virtual ~codecvt();
00107 
00108 protected:
00109         /**
00110          * Conversion internal layer to external layer.
00111          * The protected virtual member function endeavors to convert the source 
00112          * sequence at [from, from_end) to a destination sequence that it stores 
00113          * within [to, to_limit). It always stores in from_next a pointer to the first 
00114          * unconverted element in the source sequence, and it always stores in 
00115          * to_next a pointer to the first unaltered element in the destination sequence.
00116          *
00117          * state must represent the initial conversion state at the beginning of a 
00118          * new source sequence. The function alters its stored value, as needed, to 
00119          * reflect the current state of a successful conversion. Its stored value 
00120          * is otherwise unspecified.
00121          *
00122          * The template version always returns noconv.
00123          *
00124          * @param state: state transmission.
00125          * @param from: first internal char to convert.
00126          * @param from_end: limit internal char to convert.
00127          * @param from_next: next internal char to convert in return.
00128          * @param to: first external char destination.
00129          * @param to_limit: external char destination limit.
00130          * @param to_next: next external char destination.
00131          * @return The function returns: <ul>
00132          *    <li> codecvt_base::error if the source sequence is ill formed. 
00133          *    <li> codecvt_base::noconv if the function performs no conversion. 
00134          *    <li> codecvt_base::ok if the conversion succeeds. 
00135          *    <li> codecvt_base::partial if the source is insufficient, or if the 
00136          *                    destination is not large enough, for the conversion to succeed. 
00137          * </ul>
00138          */
00139         virtual result do_out(state_type& state,
00140                                                   const intern_type* from,
00141                                                   const intern_type* from_end,
00142                                                   const intern_type*& from_next,
00143                                                   extern_type* to,
00144                                                   extern_type* to_limit,
00145                                                   extern_type*& to_next) const;
00146 
00147         /**
00148          * Conversion external layer to internal layer.
00149          * The protected virtual member function endeavors to convert 
00150          * the source sequence at [from, from_end) to a destination sequence 
00151          * that it stores within [to, to_limit). It always stores in 
00152          * from_next a pointer to the first unconverted element in the source 
00153          * sequence, and it always stores in to_next a pointer to the first 
00154          * unaltered element in the destination sequence.
00155          *
00156          * state must represent the initial conversion state at the 
00157          * beginning of a new source sequence. The function alters its 
00158          * stored value, as needed, to reflect the current state of a 
00159          * successful conversion. Its stored value is otherwise unspecified.
00160          *
00161          * The template version always returns noconv.
00162          *
00163          * @param state: state transmission.
00164          * @param from: first external char to convert.
00165          * @param from_end: limit external char to convert.
00166          * @param from_next: next external char to convert in return.
00167          * @param to: first internal char destination.
00168          * @param to_limit: internal char destination limit.
00169          * @param to_next: next internal char destination.
00170          * @return The function returns: <ul>
00171          *              <li> codecvt_base::error if the source sequence is ill formed. 
00172          *          <li> codecvt_base::noconv if the function performs no conversion. 
00173          *          <li> codecvt_base::ok if the conversion succeeds. 
00174          *              <li> codecvt_base::partial if the source is insufficient, or if the 
00175          *                       destination is not large enough, for the conversion to succeed 
00176          *        </ul>
00177          */
00178         virtual result do_in(state_type& state,
00179                                                  const extern_type* from,
00180                                                  const extern_type* from_end,
00181                                                  const extern_type*& from_next,
00182                                                  intern_type* to,
00183                                                  intern_type* to_limit,
00184                                                  intern_type*& to_next) const;
00185 
00186         /**
00187          * Put unshift character.
00188          * The protected virtual member function endeavors to convert the source 
00189          * element intern_type(0) to a destination sequence that it stores 
00190          * within [to, to_limit), except for the terminating element extern_type(0). 
00191          * It always stores in to_next a pointer to the first unaltered element 
00192          * in the destination sequence.
00193          *
00194          * state must represent the initial conversion state at the beginning 
00195          * of a new source sequence. The function alters its stored value, as needed, 
00196          * to reflect the current state of a successful conversion. Typically, 
00197          * converting the source element intern_type(0) leaves the current state in the 
00198          * initial conversion state.
00199          *
00200          * The template version always returns noconv.
00201          *
00202          * @param state: state transmission.
00203          * @param to: first external char destination.
00204          * @param to_limit: external char destination limit.
00205          * @param to_next: next external char destination.
00206          * @return The function returns:<ul>
00207          *     <li> codecvt_base::error if state represents an invalid state 
00208          *     <li> codecvt_base::noconv if the function performs no conversion 
00209          *     <li> codecvt_base::ok if the conversion succeeds 
00210          *     <li> codecvt_base::partial if the destination is not large enough for the conversion to succeed 
00211          *  </ul>
00212          */
00213         virtual result do_unshift(state_type& state,
00214                                                           extern_type* to,
00215                                                           extern_type* to_limit,
00216                                                           extern_type*& to_next) const;
00217 
00218         /**
00219          * Encoding test.
00220          * @return The protected virtual member function returns: <ul>
00221          * <li> -1, if the encoding of sequences of type to_type is state dependent 
00222          * <li> 0, if the encoding involves sequences of varying lengths 
00223          * <li> n, if the encoding involves only sequences of length n 
00224          * </ul>
00225          */
00226         virtual int do_encoding() const throw();
00227 
00228         /**
00229          * The protected virtual member function returns true only. 
00230          * If every call to do_in or do_out returns noconv. 
00231          * The template version always returns true.
00232          */
00233         virtual bool do_always_noconv() const throw(); 
00234   
00235         /**
00236          * Number of external char needed to produce less than max internal char.
00237          * The protected virtual member function effectively calls 
00238          * do_in(mystate, from, end, next1, buf, buf + max, next2) for 
00239          * mystate a copy of state, some buffer buf, and pointers 
00240          * next1 and next2. It then returns next2 - buf. 
00241          * (Thus, it counts the maximum number of conversions, not 
00242          * greater than max, defined by the source sequence at [from, end).)
00243          *
00244          * The template version always returns the lesser of last1 - first1 and len2.
00245          */
00246         virtual int do_length(const state_type& state,
00247                                                   const extern_type* from,
00248                                                   const extern_type* end,
00249                                                   size_t max) const;
00250 
00251         /**
00252          * Return the max length encoding.
00253          * The protected virtual member function returns the largest permissible 
00254          * value that can be returned by do_length(first1, last1, 1), for 
00255          * arbitrary valid values of first1 and last1. 
00256          *
00257          * The template version always returns 1.
00258          */
00259         virtual int do_max_length() const throw();
00260 
00261 private:
00262         /// Private copy-contructor - restricted creation.
00263         codecvt(const codecvt&);
00264 
00265         /// Restricted assignment.
00266         codecvt& operator=(const codecvt&);  
00267 
00268 private:
00269         /**
00270          * State for a stream.
00271          * Type isn't exported - only forwarded.
00272          */
00273         struct codecvt_state *p_internal_state_;
00274 
00275 }; // end of class codecvt
00276 
00277 XIMOL_END_NAMESPACE
00278 
00279 #endif // #ifndef XIMOL_CODECVT_HPP_


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