Webmaster  |  Imprint 
C++ Server Pages
Main  |  License  |  Documentation  |  Download 

/home/tommi/cxxtools/include/cxxtools/textcodec.h

00001 /*
00002  * Copyright (C) 2004-2009 Marc Boris Duerner
00003  * 
00004  * This library is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU Lesser General Public
00006  * License as published by the Free Software Foundation; either
00007  * version 2.1 of the License, or (at your option) any later version.
00008  * 
00009  * As a special exception, you may use this file as part of a free
00010  * software library without restriction. Specifically, if other files
00011  * instantiate templates or use macros or inline functions from this
00012  * file, or you compile this file and link it with other files to
00013  * produce an executable, this file does not by itself cause the
00014  * resulting executable to be covered by the GNU General Public
00015  * License. This exception does not however invalidate any other
00016  * reasons why the executable file might be covered by the GNU Library
00017  * General Public License.
00018  * 
00019  * This library is distributed in the hope that it will be useful,
00020  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00021  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00022  * Lesser General Public License for more details.
00023  * 
00024  * You should have received a copy of the GNU Lesser General Public
00025  * License along with this library; if not, write to the Free Software
00026  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00027  */
00028 #ifndef cxxtools_TextCodec_h
00029 #define cxxtools_TextCodec_h
00030 
00031 #include <cxxtools/api.h>
00032 #include <cxxtools/char.h>
00033 
00034 #ifdef CXXTOOLS_WITH_STD_LOCALE
00035 
00036 namespace std {
00037 
00038 template<>
00039 class CXXTOOLS_API codecvt<cxxtools::Char, char, cxxtools::MBState> : public codecvt_base, public locale::facet
00040 {
00041     public:
00042         static locale::id id;
00043         virtual locale::id& __get_id (void) const { return id; }
00044 
00045     public:
00046         explicit codecvt(size_t ref = 0);
00047 
00048         virtual ~codecvt();
00049 
00050         codecvt_base::result out(cxxtools::MBState& state,
00051                                  const cxxtools::Char* from,
00052                                  const cxxtools::Char* from_end,
00053                                  const cxxtools::Char*& from_next,
00054                                  char* to, 
00055                                  char* to_end, 
00056                                  char*& to_next) const
00057         { return this->do_out(state, from, from_end, from_next, to, to_end, to_next); }
00058 
00059         codecvt_base::result unshift(cxxtools::MBState& state,
00060                                      char* to, 
00061                                      char* to_end,
00062                                      char*& to_next) const
00063         { return this->do_unshift(state, to, to_end, to_next); }
00064 
00065         codecvt_base::result in(cxxtools::MBState& state,
00066                                 const char* from,
00067                                 const char* from_end, 
00068                                 const char*& from_next,
00069                                 cxxtools::Char* to,
00070                                 cxxtools::Char* to_end,
00071                                 cxxtools::Char*& to_next) const
00072         { return this->do_in(state, from, from_end, from_next, to, to_end, to_next); }
00073 
00074         int encoding() const
00075         { return this->do_encoding(); }
00076 
00077         bool always_noconv() const
00078         { return this->do_always_noconv(); }
00079 
00080         int length(cxxtools::MBState& state, const char* from,
00081                    const char* end, size_t max) const
00082         { return this->do_length(state, from, end, max); }
00083 
00084         int max_length() const
00085         { return this->do_max_length(); }
00086 
00087     protected:
00088         virtual codecvt_base::result do_out(cxxtools::MBState& state,
00089                                             const cxxtools::Char* from,
00090                                             const cxxtools::Char* from_end,
00091                                             const cxxtools::Char*& from_next,
00092                                             char* to, 
00093                                             char* to_end, 
00094                                             char*& to_next) const = 0;
00095 
00096         virtual codecvt_base::result do_unshift(cxxtools::MBState& state,
00097                                                 char* to,
00098                                                 char* to_end, 
00099                                                 char*& to_next) const = 0;
00100 
00101         virtual codecvt_base::result do_in(cxxtools::MBState& state,
00102                                            const char* from, 
00103                                            const char* from_end,
00104                                            const char*& from_next, 
00105                                            cxxtools::Char* to,
00106                                            cxxtools::Char* to_end,
00107                                            cxxtools::Char*& to_next) const = 0;
00108 
00109         virtual int do_encoding() const throw() = 0;
00110 
00111         virtual bool do_always_noconv() const throw() = 0;
00112 
00113         virtual int do_length(cxxtools::MBState&,
00114                               const char* from,
00115                               const char* end, 
00116                               size_t max) const = 0;
00117 
00118         virtual int do_max_length() const throw() = 0;
00119 };
00120 
00121 
00122 template<>
00123 class CXXTOOLS_API codecvt<char, char, cxxtools::MBState> : public codecvt_base, public locale::facet
00124 {
00125     public:
00126         static locale::id id;
00127         virtual locale::id& __get_id (void) const { return id; }
00128 
00129     public:
00130         explicit codecvt(size_t ref = 0);
00131 
00132         virtual ~codecvt();
00133 
00134         codecvt_base::result out(cxxtools::MBState& state,
00135                                  const char* from,
00136                                  const char* from_end, 
00137                                  const char*& from_next,
00138                                  char* to, 
00139                                  char* to_end, 
00140                                  char*& to_next) const
00141         { return this->do_out(state, from, from_end, from_next, to, to_end, to_next); }
00142 
00143         codecvt_base::result unshift(cxxtools::MBState& state,
00144                                      char* to, 
00145                                      char* to_end,
00146                                      char*& to_next) const
00147         { return this->do_unshift(state, to, to_end, to_next); }
00148 
00149         codecvt_base::result in(cxxtools::MBState& state,
00150                                 const char* from,
00151                                 const char* from_end, 
00152                                 const char*& from_next,
00153                                 char* to, char* to_end, 
00154                                 char*& to_next) const
00155         { return this->do_in(state, from, from_end, from_next, to, to_end, to_next); }
00156 
00157         int encoding() const
00158         { return this->do_encoding(); }
00159 
00160         bool always_noconv() const
00161         { return this->do_always_noconv(); }
00162 
00163         int length(cxxtools::MBState& state, const char* from,
00164                    const char* end, size_t max) const
00165         { return this->do_length(state, from, end, max); }
00166 
00167         int max_length() const
00168         { return this->do_max_length(); }
00169 
00170     protected:
00171         virtual codecvt_base::result do_out(cxxtools::MBState& state,
00172                                             const char* from,
00173                                             const char* from_end, 
00174                                             const char*& from_next,
00175                                             char* to, 
00176                                             char* to_end, 
00177                                             char*& to_next) const = 0;
00178 
00179         virtual codecvt_base::result do_unshift(cxxtools::MBState& state,
00180                                                 char* to,
00181                                                 char* to_end, 
00182                                                 char*& to_next) const = 0;
00183 
00184         virtual codecvt_base::result do_in(cxxtools::MBState& state,
00185                                            const char* from, 
00186                                            const char* from_end,
00187                                            const char*& from_next, 
00188                                            char* to, 
00189                                            char* to_end,
00190                                            char*& to_next) const = 0;
00191 
00192         virtual int do_encoding() const throw() = 0;
00193 
00194         virtual bool do_always_noconv() const throw() = 0;
00195 
00196         virtual int do_length(cxxtools::MBState&,
00197                               const char* from,
00198                               const char* end, 
00199                               size_t max) const = 0;
00200 
00201         virtual int do_max_length() const throw() = 0;
00202 }; 
00203 
00204 }
00205 
00206 #else // no CXXTOOLS_WITH_STD_LOCALE
00207 
00208 namespace std {
00209 
00210 class codecvt_base
00211 {
00212     public:
00213         enum { ok, partial, error, noconv };
00214         typedef int result;
00215 
00216         virtual ~codecvt_base()
00217         { }
00218 };
00219 
00220 template <typename I, typename E, typename S>
00221 class codecvt : public std::codecvt_base
00222 {
00223     public:
00224         typedef I InternT;
00225         typedef E ExternT;
00226         typedef S StateT; 
00227 
00228     public: 
00229         explicit codecvt(size_t ref = 0)
00230         {}
00231 
00232         virtual ~codecvt()
00233         { }
00234 
00235         codecvt_base::result out(StateT& state, 
00236                                  const InternT* from,
00237                                  const InternT* from_end, 
00238                                  const InternT*& from_next,
00239                                  ExternT* to, 
00240                                  ExternT* to_end, 
00241                                  ExternT*& to_next) const
00242         { return this->do_out(state, from, from_end, from_next, to, to_end, to_next); }
00243 
00244         codecvt_base::result unshift(StateT& state, 
00245                                      ExternT* to, 
00246                                      ExternT* to_end,
00247                                      ExternT*& to_next) const
00248         { return this->do_unshift(state, to, to_end, to_next); }
00249 
00250         codecvt_base::result in(StateT& state, 
00251                                 const ExternT* from,
00252                                 const ExternT* from_end, 
00253                                 const ExternT*& from_next,
00254                                 InternT* to, 
00255                                 InternT* to_end, 
00256                                 InternT*& to_next) const
00257         { return this->do_in(state, from, from_end, from_next, to, to_end, to_next); }
00258 
00259         int encoding() const
00260         { return this->do_encoding(); }
00261 
00262         bool always_noconv() const
00263         { return this->do_always_noconv(); }
00264 
00265         int length(StateT& state, const ExternT* from,
00266                    const ExternT* end, size_t max) const
00267         { return this->do_length(state, from, end, max); }
00268 
00269         int max_length() const
00270         { return this->do_max_length(); }
00271 
00272     protected:
00273         virtual result do_in(StateT& s, const ExternT* fromBegin,
00274                              const ExternT* fromEnd, const ExternT*& fromNext,
00275                              InternT* toBegin, InternT* toEnd, InternT*& toNext) const = 0;
00276 
00277         virtual result do_out(StateT& s, const InternT* fromBegin,
00278                               const InternT* fromEnd, const InternT*& fromNext,
00279                               ExternT* toBegin, ExternT* toEnd, ExternT*& toNext) const = 0;
00280 
00281         virtual bool do_always_noconv() const = 0;
00282 
00283         virtual int do_length(StateT& s, const ExternT* fromBegin, 
00284                               const ExternT* fromEnd, size_t max) const = 0;
00285 
00286         virtual int do_max_length() const = 0;
00287 
00288         virtual std::codecvt_base::result do_unshift(StateT&, 
00289                                                      ExternT*, 
00290                                                      ExternT*, 
00291                                                      ExternT*&) const = 0;
00292 
00293         virtual int do_encoding() const = 0;
00294 };
00295 
00296 }
00297 
00298 #endif // CXXTOOLS_WITH_STD_LOCALE
00299 
00300 namespace cxxtools {
00301 
00324 template <typename I, typename E>
00325 class TextCodec : public std::codecvt<I, E, cxxtools::MBState>
00326 {
00327     public:
00328         typedef I InternT;
00329         typedef E ExternT;
00330     
00331     public:
00340         explicit TextCodec(size_t ref = 0)
00341         : std::codecvt<InternT, ExternT, MBState>(ref)
00342         , _refs(ref)
00343         {}
00344 
00345     public:
00347         virtual ~TextCodec()
00348         {}
00349 
00350         size_t refs() const
00351         { return _refs; }
00352 
00353     private:
00354         size_t _refs;
00355 };
00356 
00357 }
00358 
00359 #endif
Copyright © 2008 The Tntnet Development Team
Tntnet 1.6