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

/home/tommi/cxxtools/include/cxxtools/iconverter.h

00001 /*
00002  * Copyright (C) 2006 Tommi Maekitalo
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 
00029 #ifndef CXXTOOLS_ICONVERTER_H
00030 #define CXXTOOLS_ICONVERTER_H
00031 
00032 #include <cxxtools/iconvstream.h>
00033 #include <string>
00034 #include <sstream>
00035 
00036 namespace cxxtools
00037 {
00054   class IConverter
00055   {
00056       std::string tocode;
00057       std::string fromcode;
00058 
00059     public:
00060       IConverter()  { }
00061       IConverter(const std::string& tocode_, const std::string& fromcode_)
00062         : tocode(tocode_),
00063           fromcode(fromcode_)
00064           { }
00065 
00066       void setToCode(const std::string& tocode_)      { tocode = tocode_; }
00067       void setFromCode(const std::string& fromcode_)  { fromcode = fromcode_; }
00068       const std::string& getToCode() const            { return tocode; }
00069       const std::string& getFromCode() const          { return fromcode; }
00070 
00071       template <typename objT>
00072       std::string convert(objT s) const
00073       {
00074         std::ostringstream o;
00075 
00076         iconvstream conv;
00077         conv.exceptions(std::ios::failbit | std::ios::badbit);
00078         conv.open(o, tocode.c_str(), fromcode.c_str());
00079         conv << s << std::flush;
00080 
00081         return o.str();
00082       }
00083 
00084       std::string convert(const char* data, unsigned size) const
00085       {
00086         std::ostringstream o;
00087 
00088         iconvstream conv;
00089         conv.exceptions(std::ios::failbit | std::ios::badbit);
00090         conv.open(o, tocode.c_str(), fromcode.c_str());
00091         conv.write(data, size);
00092         conv.flush();
00093 
00094         return o.str();
00095       }
00096 
00097       template <typename iteratorT>
00098       std::string convertRange(iteratorT begin, iteratorT end) const
00099       {
00100         std::ostringstream o;
00101 
00102         iconvstream conv;
00103         conv.exceptions(std::ios::failbit | std::ios::badbit);
00104         conv.open(o, tocode.c_str(), fromcode.c_str());
00105         for (iteratorT it = begin; it != end; ++it)
00106           conv << *it;
00107 
00108         conv.flush();
00109 
00110         return o.str();
00111       }
00112 
00113       template <typename objT>
00114       std::string operator() (objT s) const
00115         { return convert(s); }
00116 
00117       std::string operator() (const char* data, unsigned size) const
00118         { return convert(data, size); }
00119   };
00120 
00121 }
00122 
00123 #endif // CXXTOOLS_ICONVERTER_H
00124 
Copyright © 2008 The Tntnet Development Team
Tntnet 1.6