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/lstrings.h"
15 #include "support/unicode.h"
26 using lyx::ucs4_codeset;
29 #if defined(_MSC_VER) && (_MSC_VER >= 1600)
30 std::locale::id numpunct<lyx::char_type>::id;
33 // Implementation of numpunct<lyx::char_type> defined in numpunct_lyx_char_type.h
34 typedef basic_string<lyx::char_type> string_type;
36 string_type numpunct<lyx::char_type>::truename() const
38 return lyx::from_ascii(numpunct<char>::truename());
41 string_type numpunct<lyx::char_type>::falsename() const
43 return lyx::from_ascii(numpunct<char>::falsename());
48 #endif // _MSC_VER >= 1600
53 // We use C IO throughout this file, because the facets might be used with
54 // lyxerr in the future.
57 /// codecvt facet for conversion of UCS4 (internal representation) to UTF8
58 /// (external representation) or vice versa
59 class iconv_codecvt_facet : public codecvt<lyx::char_type, char, mbstate_t>
61 typedef codecvt<lyx::char_type, char, mbstate_t> base;
63 /// Constructor. You have to specify with \p inout whether you want
64 /// to use this facet only for input, only for output or for both.
65 explicit iconv_codecvt_facet(string const & encoding = "UTF-8",
66 ios_base::openmode inout = ios_base::in | ios_base::out,
68 : base(refs), encoding_(encoding)
70 if (inout & ios_base::in) {
71 in_cd_ = iconv_open(ucs4_codeset, encoding.c_str());
72 if (in_cd_ == (iconv_t)(-1)) {
73 fprintf(stderr, "Error %d returned from iconv_open(in_cd_): %s\n",
74 errno, strerror(errno));
76 throw lyx::iconv_codecvt_facet_exception();
79 in_cd_ = (iconv_t)(-1);
80 if (inout & ios_base::out) {
81 out_cd_ = iconv_open(encoding.c_str(), ucs4_codeset);
82 if (out_cd_ == (iconv_t)(-1)) {
83 fprintf(stderr, "Error %d returned from iconv_open(out_cd_): %s\n",
84 errno, strerror(errno));
86 throw lyx::iconv_codecvt_facet_exception();
89 out_cd_ = (iconv_t)(-1);
91 string const & encoding() const { return encoding_; }
93 virtual ~iconv_codecvt_facet()
95 if (in_cd_ != (iconv_t)(-1))
96 if (iconv_close(in_cd_) == -1) {
97 fprintf(stderr, "Error %d returned from iconv_close(in_cd_): %s\n",
98 errno, strerror(errno));
101 if (out_cd_ != (iconv_t)(-1))
102 if (iconv_close(out_cd_) == -1) {
103 fprintf(stderr, "Error %d returned from iconv_close(out_cd_): %s\n",
104 errno, strerror(errno));
108 result do_out(state_type &, intern_type const * from,
109 intern_type const * from_end, intern_type const *& from_next,
110 extern_type * to, extern_type * to_end,
111 extern_type *& to_next) const override
113 #define WORKAROUND_ICONV_BUG 1
114 #if WORKAROUND_ICONV_BUG
115 // Due to a bug in some iconv versions, when the last char
116 // in the buffer is a wide char and the output encoding is
117 // ISO-2022-JP and we are going to switch to another encoding,
118 // the appropriate escape sequence for changing the character
119 // set is not output (see bugs 5216, 5280, and also 5489).
120 // As a workaround, we append a nul char in order to force
121 // a switch to ASCII, and then remove it from output after
123 intern_type * from_new = nullptr;
124 intern_type const * from_old = from;
126 if (*(from_end - 1) >= 0x80 && encoding_ == "ISO-2022-JP") {
127 size_t len = from_end - from;
128 from_new = new intern_type[len + 1];
129 memcpy(from_new, from, len * sizeof(intern_type));
131 from_end = from_new + len + 1;
136 size_t inbytesleft = (from_end - from) * sizeof(intern_type);
137 size_t outbytesleft = (to_end - to) * sizeof(extern_type);
138 #if WORKAROUND_ICONV_BUG
139 outbytesleft += extra * sizeof(extern_type);
143 result const retval = do_iconv(out_cd_,
144 reinterpret_cast<char const **>(&from_next),
145 &inbytesleft, &to_next, &outbytesleft);
146 #if WORKAROUND_ICONV_BUG
147 // Remove from output the nul char that we inserted at the end
148 // of the input buffer in order to circumvent an iconv bug.
152 from_next = from_old + (from_next - from);
157 if (retval == base::error) {
159 "Error %d returned from iconv when converting from %s to %s: %s\n",
160 errno, ucs4_codeset, encoding_.c_str(),
162 fputs("Converted input:", stderr);
163 for (intern_type const * i = from; i < from_next; ++i) {
164 unsigned int const c = *i;
165 fprintf(stderr, " 0x%04x", c);
167 unsigned int const c = *from_next;
168 fprintf(stderr, "\nStopped at: 0x%04x\n", c);
169 fputs("Unconverted input:", stderr);
170 for (intern_type const * i = from_next + 1; i < from_end; ++i) {
171 unsigned int const cc = *i;
172 fprintf(stderr, " 0x%04x", cc);
174 fputs("\nConverted output:", stderr);
175 for (extern_type const * i = to; i < to_next; ++i) {
176 // extern_type may be signed, avoid output of
177 // something like 0xffffffc2
178 unsigned int const cc =
179 *reinterpret_cast<unsigned char const *>(i);
180 fprintf(stderr, " 0x%02x", cc);
187 result do_unshift(state_type &, extern_type * to,
188 extern_type *, extern_type *& to_next) const override
190 // utf8 does not use shifting
194 result do_in(state_type &,
195 extern_type const * from, extern_type const * from_end,
196 extern_type const *& from_next,
197 intern_type * to, intern_type * to_end,
198 intern_type *& to_next) const override
200 size_t inbytesleft = (from_end - from) * sizeof(extern_type);
201 size_t outbytesleft = (to_end - to) * sizeof(intern_type);
204 result const retval = do_iconv(in_cd_, &from_next, &inbytesleft,
205 reinterpret_cast<char **>(&to_next),
207 if (retval == base::error) {
209 "Error %d returned from iconv when converting from %s to %s: %s\n",
210 errno, encoding_.c_str(), ucs4_codeset,
212 fputs("Converted input:", stderr);
213 for (extern_type const * i = from; i < from_next; ++i) {
214 // extern_type may be signed, avoid output of
215 // something like 0xffffffc2
216 unsigned int const c =
217 *reinterpret_cast<unsigned char const *>(i);
218 fprintf(stderr, " 0x%02x", c);
220 unsigned int const c =
221 *reinterpret_cast<unsigned char const *>(from_next);
222 fprintf(stderr, "\nStopped at: 0x%02x\n", c);
223 fputs("Unconverted input:", stderr);
224 for (extern_type const * i = from_next + 1; i < from_end; ++i) {
225 unsigned int const cc =
226 *reinterpret_cast<unsigned char const *>(i);
227 fprintf(stderr, " 0x%02x", cc);
229 fputs("\nConverted output:", stderr);
230 for (intern_type const * i = to; i < to_next; ++i) {
231 unsigned int const cc = *i;
232 fprintf(stderr, " 0x%02x", cc);
239 int do_encoding() const noexcept override
243 bool do_always_noconv() const noexcept override
247 int do_length(state_type & /*state*/, extern_type const * from,
248 extern_type const * end, size_t max) const override
250 // The docs are a bit unclear about this method.
251 // It seems that we should calculate the actual length of the
252 // converted sequence, but that would not make sense, since
253 // once could just do the conversion directly.
254 // Therefore we just return the number of unconverted
255 // characters, since that is the best guess we can do.
257 intern_type * to = new intern_type[max];
258 intern_type * to_end = to + max;
259 intern_type * to_next = to;
260 extern_type const * from_next = from;
261 do_in(state, from, end, from_next, to, to_end, to_next);
265 size_t const length = end - from;
266 return min(length, max);
269 int do_max_length() const noexcept override
271 return lyx::max_encoded_bytes(encoding_);
274 /// Do the actual conversion. The interface is equivalent to that of
275 /// iconv() (but const correct).
276 inline base::result do_iconv(iconv_t cd, char const ** from,
277 size_t * inbytesleft, char ** to, size_t * outbytesleft) const
279 char const * const to_start = *to;
280 size_t converted = iconv(cd, const_cast<char ICONV_CONST **>(from),
281 inbytesleft, to, outbytesleft);
282 if (converted == (size_t)(-1)) {
285 // As strange as it may seem, this
286 // does happen on windows when parsing
287 // comments with accented chars in
288 // tex2lyx. See the following thread
290 // http://thread.gmane.org/gmane.editors.lyx.devel/117636
294 return base::partial;
306 /// The narrow encoding
316 void setEncoding(Ios & ios, string const & encoding, ios_base::openmode mode)
318 // We must imbue the stream before opening the file
320 locale locale(global, new iconv_codecvt_facet(encoding, mode));
325 const char * iconv_codecvt_facet_exception::what() const noexcept
327 return "iconv problem in iconv_codecvt_facet initialization";
331 ifdocstream::ifdocstream() : base()
333 setEncoding(*this, "UTF-8", in);
337 ifdocstream::ifdocstream(SetEnc const & enc) : base()
339 setEncoding(*this, enc.encoding, in);
343 ifdocstream::ifdocstream(const char* s, ios_base::openmode mode,
344 string const & encoding)
347 setEncoding(*this, encoding, in);
352 ofdocstream::ofdocstream(): base()
354 setEncoding(*this, "UTF-8", out);
358 ofdocstream::ofdocstream(SetEnc const & enc) : base()
360 setEncoding(*this, enc.encoding, out);
364 ofdocstream::ofdocstream(const char* s, ios_base::openmode mode,
365 string const & encoding)
368 setEncoding(*this, encoding, out);
373 void ofdocstream::reset(string const & encoding)
375 setEncoding(*this, encoding, out);
380 SetEnc setEncoding(string const & encoding)
382 return SetEnc(encoding);
386 odocstream & operator<<(odocstream & os, SetEnc const & e)
388 if (has_facet<iconv_codecvt_facet>(os.rdbuf()->getloc())) {
389 // This stream must be a file stream, since we never imbue
390 // any other stream with a locale having a iconv_codecvt_facet.
391 iconv_codecvt_facet const & facet =
392 use_facet<iconv_codecvt_facet>(os.rdbuf()->getloc());
394 // FIXME Changing the codecvt facet of an open file is allowed,
395 // but unsafe for facets that use internal state (see the thread
396 // "iostreams: Does imbue() need to be called before open()?"
398 // Currently it seems to work with gcc and MSVC, but not with
400 // Avoid imbueing with the same encoding again if possible.
401 if (facet.encoding() == e.encoding)
404 // Flush the stream so that all pending output is written
405 // with the old encoding.
408 locale locale(os.rdbuf()->getloc(),
409 new iconv_codecvt_facet(e.encoding, ios_base::out));
416 idocstream & operator<<(idocstream & is, SetEnc const & e)
418 if (has_facet<iconv_codecvt_facet>(is.rdbuf()->getloc())) {
419 // This stream must be a file stream, since we never imbue
420 // any other stream with a locale having a iconv_codecvt_facet.
421 iconv_codecvt_facet const & facet =
422 use_facet<iconv_codecvt_facet>(is.rdbuf()->getloc());
424 // FIXME Changing the codecvt facet of an open file is allowed,
425 // but unsafe for facets that use internal state (see the thread
426 // "iostreams: Does imbue() need to be called before open()?"
428 // Currently it seems to work with gcc and MSVC, but not with
430 // Avoid imbueing with the same encoding again if possible.
431 if (facet.encoding() == e.encoding)
434 locale locale(is.rdbuf()->getloc(),
435 new iconv_codecvt_facet(e.encoding, ios_base::in));
442 #if ! defined(USE_WCHAR_T)
443 odocstream & operator<<(odocstream & os, char c)
453 #if ! defined(USE_WCHAR_T) && defined(__GNUC__)
454 // We get undefined references to these virtual methods. This looks like
455 // a bug in gcc. The implementation here does not do anything useful, since
456 // it is overridden in iconv_codecvt_facet.
459 template<> codecvt<lyx::char_type, char, mbstate_t>::result
460 codecvt<lyx::char_type, char, mbstate_t>::do_out(
461 mbstate_t &, const lyx::char_type *, const lyx::char_type *,
462 const lyx::char_type *&, char *, char *, char *&) const
468 template<> codecvt<lyx::char_type, char, mbstate_t>::result
469 codecvt<lyx::char_type, char, mbstate_t>::do_unshift(
470 mbstate_t &, char *, char *, char *&) const
476 template<> codecvt<lyx::char_type, char, mbstate_t>::result
477 codecvt<lyx::char_type, char, mbstate_t>::do_in(
478 mbstate_t &, const char *, const char *, const char *&,
479 lyx::char_type *, lyx::char_type *, lyx::char_type *&) const
486 int codecvt<lyx::char_type, char, mbstate_t>::do_encoding() const noexcept
493 bool codecvt<lyx::char_type, char, mbstate_t>::do_always_noconv() const noexcept
499 int codecvt<lyx::char_type, char, mbstate_t>::do_length(
500 mbstate_t &, const char *, const char *, size_t) const
506 int codecvt<lyx::char_type, char, mbstate_t>::do_max_length() const noexcept