~ubuntu-branches/ubuntu/wily/cxxtools/wily-proposed

« back to all changes in this revision

Viewing changes to include/cxxtools/iconverter.h

  • Committer: Bazaar Package Importer
  • Author(s): Kari Pahula
  • Date: 2008-06-16 12:24:28 UTC
  • mfrom: (3.1.3 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080616122428-7bllgyt1358u779r
Tags: 1.4.8-2
Made libcxxtools-dev depend on libcxxtools6, not libcxxtools5.

Show diffs side-by-side

added added

removed removed

Lines of Context:
40
40
     cxxtools::IConverter conv("ISO8859-1", "UTF8");
41
41
     std::string utf8string = getUtf8String();
42
42
     std::string iso8895_1 = conv.convert(utf8string);
 
43
     // or functor-style:
 
44
     std::string iso8895_1 = conv(utf8string);
43
45
    \endcode
44
46
   */
45
47
  class IConverter
46
48
  {
47
 
          std::string tocode;
48
 
          std::string fromcode;
49
 
 
50
 
        public:
51
 
          IConverter()  { }
52
 
          IConverter(const std::string& tocode_, const std::string& fromcode_)
53
 
                : tocode(tocode_),
54
 
                  fromcode(fromcode_)
55
 
                  { }
56
 
 
57
 
          void setToCode(const std::string& tocode_)      { tocode = tocode_; }
58
 
          void setFromCode(const std::string& fromcode_)  { fromcode = fromcode_; }
59
 
          const std::string& getToCode() const            { return tocode; }
60
 
          const std::string& getFromCode() const          { return fromcode; }
61
 
 
62
 
          template <typename objT>
63
 
          std::string convert(objT s) const
64
 
          {
65
 
                std::ostringstream o;
66
 
 
67
 
                iconvstream conv(o, tocode.c_str(), fromcode.c_str());
68
 
                conv << s << std::flush;
69
 
 
70
 
                return o.str();
71
 
          }
72
 
 
73
 
          std::string convert(const char* data, unsigned size) const
74
 
          {
75
 
                std::ostringstream o;
76
 
 
77
 
                iconvstream conv(o, tocode.c_str(), fromcode.c_str());
78
 
                conv.write(data, size);
79
 
                conv.flush();
80
 
 
81
 
                return o.str();
82
 
          }
83
 
 
84
 
          template <typename iteratorT>
85
 
          std::string convert(iteratorT begin, iteratorT end) const
86
 
          {
87
 
                std::ostringstream o;
88
 
 
89
 
                iconvstream conv(o, tocode.c_str(), fromcode.c_str());
90
 
                for (iteratorT it = begin; it != end; ++it)
91
 
                  conv << *it;
92
 
 
93
 
                conv.flush();
94
 
 
95
 
                return o.str();
96
 
          }
 
49
      std::string tocode;
 
50
      std::string fromcode;
 
51
 
 
52
    public:
 
53
      IConverter()  { }
 
54
      IConverter(const std::string& tocode_, const std::string& fromcode_)
 
55
        : tocode(tocode_),
 
56
          fromcode(fromcode_)
 
57
          { }
 
58
 
 
59
      void setToCode(const std::string& tocode_)      { tocode = tocode_; }
 
60
      void setFromCode(const std::string& fromcode_)  { fromcode = fromcode_; }
 
61
      const std::string& getToCode() const            { return tocode; }
 
62
      const std::string& getFromCode() const          { return fromcode; }
 
63
 
 
64
      template <typename objT>
 
65
      std::string convert(objT s) const
 
66
      {
 
67
        std::ostringstream o;
 
68
 
 
69
        iconvstream conv;
 
70
        conv.exceptions(std::ios::failbit | std::ios::badbit);
 
71
        conv.open(o, tocode.c_str(), fromcode.c_str());
 
72
        conv << s << std::flush;
 
73
 
 
74
        return o.str();
 
75
      }
 
76
 
 
77
      std::string convert(const char* data, unsigned size) const
 
78
      {
 
79
        std::ostringstream o;
 
80
 
 
81
        iconvstream conv;
 
82
        conv.exceptions(std::ios::failbit | std::ios::badbit);
 
83
        conv.open(o, tocode.c_str(), fromcode.c_str());
 
84
        conv.write(data, size);
 
85
        conv.flush();
 
86
 
 
87
        return o.str();
 
88
      }
 
89
 
 
90
      template <typename iteratorT>
 
91
      std::string convertRange(iteratorT begin, iteratorT end) const
 
92
      {
 
93
        std::ostringstream o;
 
94
 
 
95
        iconvstream conv;
 
96
        conv.exceptions(std::ios::failbit | std::ios::badbit);
 
97
        conv.open(o, tocode.c_str(), fromcode.c_str());
 
98
        for (iteratorT it = begin; it != end; ++it)
 
99
          conv << *it;
 
100
 
 
101
        conv.flush();
 
102
 
 
103
        return o.str();
 
104
      }
 
105
 
 
106
      template <typename objT>
 
107
      std::string operator() (objT s) const
 
108
        { return convert(s); }
 
109
 
 
110
      std::string operator() (const char* data, unsigned size) const
 
111
        { return convert(data, size); }
97
112
  };
98
113
 
99
114
}