00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
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