Marine Library  1.0
C++ library for Linux Networking Development
charset_convert.hh
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016 Zhao DAI <daidodo@gmail.com>
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or any
7  * later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see accompanying file LICENSE.txt
16  * or <http://www.gnu.org/licenses/>.
17  */
18 
25 #ifndef DOZERG_CHARSET_CONVERT_H_20130513
26 #define DOZERG_CHARSET_CONVERT_H_20130513
27 
28 #include <iconv.h>
29 #include <cassert>
30 #include "to_string.hh"
31 
32 NS_SERVER_BEGIN
33 
41 {
42 public:
46  enum EMode{
50  kNormal = 0,
64  };
71  : cv_(iconv_t(-1))
72  , mode_(kNormal)
73  {}
78  if(valid())
79  ::iconv_close(cv_);
80  }
88  CCharsetConvert(const std::string & fromCode, const std::string & toCode, EMode mode = kNormal)
89  : cv_(iconv_t(-1))
90  , mode_(kNormal)
91  {
92  init(fromCode, toCode, mode);
93  }
103  bool init(const std::string & fromCode, const std::string & toCode, EMode mode = kNormal){
104  if(valid())
105  return false;
106  if(fromCode.empty() || toCode.empty())
107  return false;
108  std::string t = toCode;
109  switch(mode){
110  case kTranslit:t += "//TRANSLIT";break;
111  case kIgnore:t += "//IGNORE";break;
112  default:;
113  }
114  from_ = fromCode;
115  to_ = toCode;
116  mode_ = mode;
117  cv_ = ::iconv_open(t.c_str(), fromCode.c_str());
118  return valid();
119  }
124  bool valid() const{return (iconv_t(-1) != cv_);}
130  const std::string fromCode() const{return from_;}
136  const std::string toCode() const{return to_;}
142  EMode mode() const{return mode_;}
150  bool convert(const std::string & source, std::string & dest){
151  if(!valid())
152  return false;
153  if(source.empty())
154  return true;
155  ::iconv(cv_, NULL, NULL, NULL, NULL); //reset init state;
156  char * inbuf = const_cast<char *>(&source[0]); //iconv()'s bug
157  size_t inLen = source.size();
158  for(char buf[64];inLen > 0;){
159  const size_t inOld = inLen;
160  char * outbuf = buf;
161  size_t outLen = sizeof buf;
162  ::iconv(cv_, &inbuf, &inLen, &outbuf, &outLen);
163  if(inOld <= inLen) //cannot proceed
164  return false;
165  if(buf != outbuf){
166  assert(buf < outbuf);
167  dest.append(buf, outbuf - buf);
168  }
169  }
170  return true;
171  }
176  std::string toString() const{
177  CToString oss;
178  oss<<"{cv_="<<cv_
179  <<", from_="<<from_
180  <<", to_="<<to_
181  <<", mode_="<<mode_
182  <<'}';
183  return oss.str();
184  }
185 private:
186  CCharsetConvert(const CCharsetConvert &); //disable copy and assignment
187  CCharsetConvert & operator =(const CCharsetConvert &);
188  //members
189  std::string from_, to_;
190  iconv_t cv_;
191  EMode mode_;
192 };
193 
194 NS_SERVER_END
195 
196 #endif
197 
Definition: to_string.hh:43
Converting text between different character encodings.
Definition: charset_convert.hh:40
bool init(const std::string &fromCode, const std::string &toCode, EMode mode=kNormal)
Initialize a usable converter.
Definition: charset_convert.hh:103
Definition: charset_convert.hh:58
Definition: charset_convert.hh:63
bool valid() const
Test whether this object is initialized.
Definition: charset_convert.hh:124
bool convert(const std::string &source, std::string &dest)
Convert text from source encoding to destination encoding.
Definition: charset_convert.hh:150
Definition: charset_convert.hh:50
~CCharsetConvert()
Destructor for releasing resources.
Definition: charset_convert.hh:77
CCharsetConvert(const std::string &fromCode, const std::string &toCode, EMode mode=kNormal)
Construct a usable converter.
Definition: charset_convert.hh:88
const std::string fromCode() const
Get encoding name for source text.
Definition: charset_convert.hh:130
std::string toString() const
Get a readable description of this object.
Definition: charset_convert.hh:176
const std::string toCode() const
Get encoding name for destination text.
Definition: charset_convert.hh:136
EMode mode() const
Get conversion mode.
Definition: charset_convert.hh:142
EMode
Conversion mode.
Definition: charset_convert.hh:46
CCharsetConvert()
Construct a default object, with no ability for conversion.
Definition: charset_convert.hh:70