3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "support/docstream.h"
14 #include "support/unicode.h"
23 using lyx::ucs4_codeset;
27 // We use C IO throughout this file, because the facets might be used with
28 // lyxerr in the future.
31 /// codecvt facet for conversion of UCS4 (internal representation) to UTF8
32 /// (external representation) or vice versa
33 class iconv_codecvt_facet : public codecvt<lyx::char_type, char, mbstate_t>
35 typedef codecvt<lyx::char_type, char, mbstate_t> base;
37 /// Constructor. You have to specify with \p inout whether you want
38 /// to use this facet only for input, only for output or for both.
39 explicit iconv_codecvt_facet(string const & encoding = "UTF-8",
40 ios_base::openmode inout = ios_base::in | ios_base::out,
42 : base(refs), encoding_(encoding)
44 if (inout & ios_base::in) {
45 in_cd_ = iconv_open(ucs4_codeset, encoding.c_str());
46 if (in_cd_ == (iconv_t)(-1)) {
47 fprintf(stderr, "Error %d returned from iconv_open(in_cd_): %s\n",
48 errno, strerror(errno));
50 throw lyx::iconv_codecvt_facet_exception();
53 in_cd_ = (iconv_t)(-1);
54 if (inout & ios_base::out) {
55 out_cd_ = iconv_open(encoding.c_str(), ucs4_codeset);
56 if (out_cd_ == (iconv_t)(-1)) {
57 fprintf(stderr, "Error %d returned from iconv_open(out_cd_): %s\n",
58 errno, strerror(errno));
60 throw lyx::iconv_codecvt_facet_exception();
63 out_cd_ = (iconv_t)(-1);
66 virtual ~iconv_codecvt_facet()
68 if (in_cd_ != (iconv_t)(-1))
69 if (iconv_close(in_cd_) == -1) {
70 fprintf(stderr, "Error %d returned from iconv_close(in_cd_): %s\n",
71 errno, strerror(errno));
74 if (out_cd_ != (iconv_t)(-1))
75 if (iconv_close(out_cd_) == -1) {
76 fprintf(stderr, "Error %d returned from iconv_close(out_cd_): %s\n",
77 errno, strerror(errno));
81 virtual result do_out(state_type &, intern_type const * from,
82 intern_type const * from_end, intern_type const *& from_next,
83 extern_type * to, extern_type * to_end,
84 extern_type *& to_next) const
86 size_t inbytesleft = (from_end - from) * sizeof(intern_type);
87 size_t outbytesleft = (to_end - to) * sizeof(extern_type);
90 result const retval = do_iconv(out_cd_,
91 reinterpret_cast<char const **>(&from_next),
92 &inbytesleft, &to_next, &outbytesleft);
93 if (retval == base::error) {
95 "Error %d returned from iconv when converting from %s to %s: %s\n",
96 errno, ucs4_codeset, encoding_.c_str(),
98 fputs("Converted input:", stderr);
99 for (intern_type const * i = from; i < from_next; ++i) {
100 unsigned int const c = *i;
101 fprintf(stderr, " 0x%04x", c);
103 unsigned int const c = *from_next;
104 fprintf(stderr, "\nStopped at: 0x%04x\n", c);
105 fputs("Unconverted input:", stderr);
106 for (intern_type const * i = from_next + 1; i < from_end; ++i) {
107 unsigned int const c = *i;
108 fprintf(stderr, " 0x%04x", c);
110 fputs("\nConverted output:", stderr);
111 for (extern_type const * i = to; i < to_next; ++i) {
112 // extern_type may be signed, avoid output of
113 // something like 0xffffffc2
114 unsigned int const c =
115 *reinterpret_cast<unsigned char const *>(i);
116 fprintf(stderr, " 0x%02x", c);
123 virtual result do_unshift(state_type &, extern_type * to,
124 extern_type *, extern_type *& to_next) const
126 // utf8 does not use shifting
130 virtual result do_in(state_type &,
131 extern_type const * from, extern_type const * from_end,
132 extern_type const *& from_next,
133 intern_type * to, intern_type * to_end,
134 intern_type *& to_next) const
136 size_t inbytesleft = (from_end - from) * sizeof(extern_type);
137 size_t outbytesleft = (to_end - to) * sizeof(intern_type);
140 result const retval = do_iconv(in_cd_, &from_next, &inbytesleft,
141 reinterpret_cast<char **>(&to_next),
143 if (retval == base::error) {
145 "Error %d returned from iconv when converting from %s to %s: %s\n",
146 errno, encoding_.c_str(), ucs4_codeset,
148 fputs("Converted input:", stderr);
149 for (extern_type const * i = from; i < from_next; ++i) {
150 // extern_type may be signed, avoid output of
151 // something like 0xffffffc2
152 unsigned int const c =
153 *reinterpret_cast<unsigned char const *>(i);
154 fprintf(stderr, " 0x%02x", c);
156 unsigned int const c =
157 *reinterpret_cast<unsigned char const *>(from_next);
158 fprintf(stderr, "\nStopped at: 0x%02x\n", c);
159 fputs("Unconverted input:", stderr);
160 for (extern_type const * i = from_next + 1; i < from_end; ++i) {
161 unsigned int const c =
162 *reinterpret_cast<unsigned char const *>(i);
163 fprintf(stderr, " 0x%02x", c);
165 fputs("\nConverted output:", stderr);
166 for (intern_type const * i = to; i < to_next; ++i) {
167 unsigned int const c = *i;
168 fprintf(stderr, " 0x%02x", c);
175 virtual int do_encoding() const throw()
179 virtual bool do_always_noconv() const throw()
183 virtual int do_length(state_type & /*state*/, extern_type const * from,
184 extern_type const * end, size_t max) const
186 // The docs are a bit unclear about this method.
187 // It seems that we should calculate the actual length of the
188 // converted sequence, but that would not make sense, since
189 // once could just do the conversion directly.
190 // Therefore we just return the number of unconverted
191 // characters, since that is the best guess we can do.
193 intern_type * to = new intern_type[max];
194 intern_type * to_end = to + max;
195 intern_type * to_next = to;
196 extern_type const * from_next = from;
197 do_in(state, from, end, from_next, to, to_end, to_next);
201 size_t const length = end - from;
202 return min(length, max);
205 virtual int do_max_length() const throw()
207 // FIXME: this information should be transferred to lib/encodings
208 // UTF8 uses at most 4 bytes to represent one UCS4 code point
209 // (see RFC 3629). RFC 2279 specifies 6 bytes, but that
210 // information is outdated, and RFC 2279 has been superseded by
212 // The CJK encodings use (different) multibyte representation as well.
213 // All other encodings encode one UCS4 code point in one byte
214 // (and can therefore only encode a subset of UCS4)
215 // Note that BIG5 and SJIS do not work with LaTeX (see lib/encodings).
216 // Furthermore, all encodings that use shifting (like SJIS) do not work with
217 // iconv_codecvt_facet.
218 if (encoding_ == "UTF-8" ||
220 encoding_ == "EUC-TW")
222 else if (encoding_ == "EUC-JP")
224 else if (encoding_ == "BIG5" ||
225 encoding_ == "EUC-KR" ||
226 encoding_ == "EUC-CN" ||
227 encoding_ == "SJIS" ||
228 encoding_ == "GBK" ||
235 /// Do the actual conversion. The interface is equivalent to that of
236 /// iconv() (but const correct).
237 inline base::result do_iconv(iconv_t cd, char const ** from,
238 size_t * inbytesleft, char ** to, size_t * outbytesleft) const
240 char const * const to_start = *to;
241 size_t converted = iconv(cd, const_cast<char ICONV_CONST **>(from),
242 inbytesleft, to, outbytesleft);
243 if (converted == (size_t)(-1)) {
247 return base::partial;
259 /// The narrow encoding
269 void setEncoding(Ios & ios, string const & encoding, ios_base::openmode mode)
271 // We must imbue the stream before openening the file
273 locale locale(global, new iconv_codecvt_facet(encoding, mode));
278 const char * iconv_codecvt_facet_exception::what() const throw()
280 return "iconv problem in iconv_codecvt_facet initialization";
284 idocfstream::idocfstream(string const & encoding) : base()
286 setEncoding(*this, encoding, in);
290 idocfstream::idocfstream(const char* s, ios_base::openmode mode,
291 string const & encoding)
294 setEncoding(*this, encoding, in);
299 odocfstream::odocfstream(): base()
304 odocfstream::odocfstream(string const & encoding) : base()
306 setEncoding(*this, encoding, out);
310 odocfstream::odocfstream(const char* s, ios_base::openmode mode,
311 string const & encoding)
314 setEncoding(*this, encoding, out);
319 void odocfstream::reset(string const & encoding)
321 setEncoding(*this, encoding, out);
326 SetEnc setEncoding(string const & encoding)
328 return SetEnc(encoding);
332 odocstream & operator<<(odocstream & os, SetEnc e)
334 if (has_facet<iconv_codecvt_facet>(os.rdbuf()->getloc())) {
335 // This stream must be a file stream, since we never imbue
336 // any other stream with a locale having a iconv_codecvt_facet.
337 // Flush the stream so that all pending output is written
338 // with the old encoding.
340 locale locale(os.rdbuf()->getloc(),
341 new iconv_codecvt_facet(e.encoding, ios_base::out));
342 // FIXME Does changing the codecvt facet of an open file
343 // stream always work? It does with gcc 4.1, but I have read
344 // somewhere that it does not with MSVC.
345 // What does the standard say?
352 #if ! defined(USE_WCHAR_T)
353 odocstream & operator<<(odocstream & os, char c)
362 #if ! defined(USE_WCHAR_T) && defined(__GNUC__)
363 // We get undefined references to these virtual methods. This looks like
364 // a bug in gcc. The implementation here does not do anything useful, since
365 // it is overriden in iconv_codecvt_facet.
368 template<> codecvt<lyx::char_type, char, mbstate_t>::result
369 codecvt<lyx::char_type, char, mbstate_t>::do_out(
370 mbstate_t &, const lyx::char_type *, const lyx::char_type *,
371 const lyx::char_type *&, char *, char *, char *&) const
377 template<> codecvt<lyx::char_type, char, mbstate_t>::result
378 codecvt<lyx::char_type, char, mbstate_t>::do_unshift(
379 mbstate_t &, char *, char *, char *&) const
385 template<> codecvt<lyx::char_type, char, mbstate_t>::result
386 codecvt<lyx::char_type, char, mbstate_t>::do_in(
387 mbstate_t &, const char *, const char *, const char *&,
388 lyx::char_type *, lyx::char_type *, lyx::char_type *&) const
395 int codecvt<lyx::char_type, char, mbstate_t>::do_encoding() const throw()
402 bool codecvt<lyx::char_type, char, mbstate_t>::do_always_noconv() const throw()
407 #if __GNUC__ == 3 && __GNUC_MINOR__ < 4
410 int codecvt<lyx::char_type, char, mbstate_t>::do_length(
411 mbstate_t const &, const char *, const char *, size_t) const
419 int codecvt<lyx::char_type, char, mbstate_t>::do_length(
420 mbstate_t &, const char *, const char *, size_t) const
428 int codecvt<lyx::char_type, char, mbstate_t>::do_max_length() const throw()