summaryrefslogtreecommitdiff
path: root/support/extractpdfmark/src/iconv_wrapper.hh
blob: 6517a4e1d372a749fa8a05ecb528f118c517bde7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
//
// One header file iconv wrapper for C++11 2016-10-19.22 GPL
// https://github.com/trueroad/iconv_wrapper/
//
// Copyright (C) 2016 Masamichi Hosoda. All rights reserved.
//
// One header file iconv wrapper for C++11 is free software:
// you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// One header file iconv wrapper for C++11 is distributed
// in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with One header file Commandline Parse for C++11.
// If not, see <http://www.gnu.org/licenses/>.
//

#ifndef INCLUDE_GUARD_ICONV_WRAPPER_H
#define INCLUDE_GUARD_ICONV_WRAPPER_H

#include <iconv.h>
#include <string>
#include <system_error>

namespace iconv_wrapper
{
  class iconv
  {
  public:
    // Open
    void open (const std::string &fromcode, const std::string &tocode);

    // Close
    void close () noexcept;

    // Convert
    std::string convert (const std::string &in);

    // Convert (with getting the incomplete input / output
    // when an exception occurs)
    std::string &convert (const std::string &in,
                          std::string::size_type *pinpos,
                          std::string *pout);

    // Get initial sequence
    std::string get_initial_sequence (void);

    // Get initial sequence
    std::string &get_initial_sequence (std::string *pout);

    // Reset conversion state
    void reset (void) const noexcept;

    // Constructor / destructor
    iconv () = default;

    iconv (const std::string &fromcode, const std::string &tocode)
    {
      open (fromcode, tocode);
    }

    ~iconv () noexcept
    {
      close ();
    }

    // Enable move
    iconv (iconv &&) = default;
    iconv& operator = (iconv &&) = default;

  private:
    // Internal class
    // There is two different implements for the second argument of iconv ().
    //   `const char**' and `char **'
    // This class is for automatic type cast switching.
    class iconv_const_cast
    {
    public:
      iconv_const_cast (const char **in) noexcept:
        t (in)
      {
      }
      operator char** () const noexcept
      {
        return const_cast<char**>(t);
      }
      operator const char ** () const noexcept
      {
        return t;
      }
    private:
      const char ** t;
    };

    // Internal function
    void do_iconv (std::string *pout, const char *inbuf, size_t *pinleft,
                   std::string::size_type *pinpos = nullptr);

    // Disable copy
    // Since iconv_t cannot duplicate.
    iconv (iconv const &) = delete;
    iconv& operator = (iconv const &) = delete;

    // Const
    // Here is C cast instead of C++ cast.
    // C++ reinterpret_cast (pointer) and static_cast (integer) cannot be used.
    // Since it depends on the implementation of iconv_t.
    const iconv_t invalid_cd = (iconv_t)-1;

    // Conversion descriptor
    iconv_t convdesc = invalid_cd;
  };

  inline void iconv::open (const std::string &fromcode,
                           const std::string &tocode)
  {
    if (convdesc == invalid_cd)
      {
        close ();
      }
    convdesc = iconv_open (tocode.c_str (), fromcode.c_str ());
    if (convdesc == invalid_cd)
      {
        throw std::system_error (errno, std::system_category ());
      }
  }

  inline void iconv::close () noexcept
  {
    if (convdesc != invalid_cd)
      {
        iconv_close (convdesc);
        convdesc = invalid_cd;
      }
  }

  inline std::string iconv::convert (const std::string &in)
  {
    std::string out (in.size (), '\0');
    convert (in, nullptr, &out);
    return out;
  }

  inline std::string &iconv::convert (const std::string &in,
                                      std::string::size_type *pinpos,
                                      std::string *pout)
  {
    size_t inleft {in.size ()};
    do_iconv (pout, &in.at (0), &inleft, pinpos);
    return *pout;
  }

  inline std::string iconv::get_initial_sequence (void)
  {
    std::string out (1, '\0');
    get_initial_sequence (&out);
    return out;
  }

  inline std::string &iconv::get_initial_sequence (std::string *pout)
  {
    do_iconv (pout, nullptr, nullptr);
    return *pout;
  }

  inline void iconv::reset (void) const noexcept
  {
    ::iconv (convdesc, nullptr, nullptr, nullptr, nullptr);
  }

  inline void iconv::do_iconv (std::string *pout,
                               const char *inbuf, size_t *pinleft,
                               std::string::size_type *pinpos)
  {
    if (pout->empty ())
      {
        pout->resize (1);
      }
    const char *inbuf_tmp {inbuf};
    char *outbuf {&pout->at (0)};
    size_t outleft {pout->size ()};
    size_t s;

    while ((s = ::iconv (convdesc,
                         iconv_const_cast(&inbuf_tmp), pinleft,
                         &outbuf, &outleft)) == static_cast<size_t>(-1))
      {
        if (errno != E2BIG)
          {
            if (pinpos && inbuf)
              {
                *pinpos = (inbuf_tmp - inbuf);
              }
            pout->resize (outbuf - &pout->at (0));
            throw std::system_error (errno, std::system_category ());
          }
        std::string::size_type pos = (outbuf - &pout->at (0));
        pout->resize (pout->size () * 2);
        outbuf = &pout->at (pos);
        outleft = pout->size () - pos;
      }
    pout->resize (outbuf - &pout->at (0));
  }
}

#endif  // INCLUDE_GUARD_ICONV_WRAPPER_H