1 /*
2 Copyright (c) 2011, Yuya Unno
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7 
8     * Redistributions of source code must retain the above copyright
9       notice, this list of conditions and the following disclaimer.
10     * Redistributions in binary form must reproduce the above copyright
11       notice, this list of conditions and the following disclaimer in the
12       documentation and/or other materials provided with the distribution.
13     * Neither the name of the Yuya Unno nor the
14       names of its contributors may be used to endorse or promote products
15       derived from this software without specific prior written permission.
16 
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
21 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28 
29 #pragma once
30 
31 #include <errno.h>
32 #include <iconv.h>
33 
34 #include <stdexcept>
35 #include <string>
36 #include <vector>
37 
38 namespace iconvpp {
39 
40 class converter {
41  public:
converter(const std::string & out_encode,const std::string & in_encode,bool ignore_error=false,size_t buf_size=1024)42   converter(const std::string& out_encode,
43             const std::string& in_encode,
44             bool ignore_error = false,
45             size_t buf_size = 1024)
46       : ignore_error_(ignore_error),
47         buf_size_(buf_size) {
48     if (buf_size == 0) {
49       throw std::runtime_error("buffer size must be greater than zero");
50     }
51 
52     iconv_t conv = ::iconv_open(out_encode.c_str(), in_encode.c_str());
53     if (conv == (iconv_t)-1) {
54       if (errno == EINVAL)
55         throw std::runtime_error(
56             "not supported from " + in_encode + " to " + out_encode);
57       else
58         throw std::runtime_error("unknown error");
59     }
60     iconv_ = conv;
61   }
62 
~converter()63   ~converter() {
64     iconv_close(iconv_);
65   }
66 
convert(const std::string & input,std::string & output) const67   void convert(const std::string& input, std::string& output) const {
68     // copy the string to a buffer as iconv function requires a non-const char
69     // pointer.
70     std::vector<char> in_buf(input.begin(), input.end());
71     char* src_ptr = &in_buf[0];
72     size_t src_size = input.size();
73 
74     std::vector<char> buf(buf_size_);
75     std::string dst;
76     while (0 < src_size) {
77       char* dst_ptr = &buf[0];
78       size_t dst_size = buf.size();
79       size_t res = ::iconv(iconv_, &src_ptr, &src_size, &dst_ptr, &dst_size);
80       if (res == (size_t)-1) {
81         if (errno == E2BIG)  {
82           // ignore this error
83         } else if (ignore_error_) {
84           // skip character
85           ++src_ptr;
86           --src_size;
87         } else {
88           check_convert_error();
89         }
90       }
91       dst.append(&buf[0], buf.size() - dst_size);
92     }
93     dst.swap(output);
94   }
95 
96  private:
check_convert_error() const97   void check_convert_error() const {
98     switch (errno) {
99       case EILSEQ:
100         throw std::runtime_error("invalid multibyte chars");
101       case EINVAL:
102         throw std::runtime_error("invalid multibyte chars");
103       default:
104         throw std::runtime_error("unknown error");
105     }
106   }
107 
108   iconv_t iconv_;
109   bool ignore_error_;
110   const size_t buf_size_;
111 };
112 
113 }  // namespace iconvpp
114