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"
24 using lyx::ucs4_codeset;
27 #if defined(_MSC_VER) && (_MSC_VER >= 1600)
28 std::locale::id numpunct<lyx::char_type>::id;
34 // We use C IO throughout this file, because the facets might be used with
35 // lyxerr in the future.
38 /// codecvt facet for conversion of UCS4 (internal representation) to UTF8
39 /// (external representation) or vice versa
40 class iconv_codecvt_facet : public codecvt<lyx::char_type, char, mbstate_t>
42 typedef codecvt<lyx::char_type, char, mbstate_t> base;
44 /// Constructor. You have to specify with \p inout whether you want
45 /// to use this facet only for input, only for output or for both.
46 explicit iconv_codecvt_facet(string const & encoding = "UTF-8",
47 ios_base::openmode inout = ios_base::in | ios_base::out,
49 : base(refs), encoding_(encoding)
51 if (inout & ios_base::in) {
52 in_cd_ = iconv_open(ucs4_codeset, encoding.c_str());
53 if (in_cd_ == (iconv_t)(-1)) {
54 fprintf(stderr, "Error %d returned from iconv_open(in_cd_): %s\n",
55 errno, strerror(errno));
57 throw lyx::iconv_codecvt_facet_exception();
60 in_cd_ = (iconv_t)(-1);
61 if (inout & ios_base::out) {
62 out_cd_ = iconv_open(encoding.c_str(), ucs4_codeset);
63 if (out_cd_ == (iconv_t)(-1)) {
64 fprintf(stderr, "Error %d returned from iconv_open(out_cd_): %s\n",
65 errno, strerror(errno));
67 throw lyx::iconv_codecvt_facet_exception();
70 out_cd_ = (iconv_t)(-1);
73 virtual ~iconv_codecvt_facet()
75 if (in_cd_ != (iconv_t)(-1))
76 if (iconv_close(in_cd_) == -1) {
77 fprintf(stderr, "Error %d returned from iconv_close(in_cd_): %s\n",
78 errno, strerror(errno));
81 if (out_cd_ != (iconv_t)(-1))
82 if (iconv_close(out_cd_) == -1) {
83 fprintf(stderr, "Error %d returned from iconv_close(out_cd_): %s\n",
84 errno, strerror(errno));
88 virtual result do_out(state_type &, intern_type const * from,
89 intern_type const * from_end, intern_type const *& from_next,
90 extern_type * to, extern_type * to_end,
91 extern_type *& to_next) const
93 #define WORKAROUND_ICONV_BUG 1
94 #if WORKAROUND_ICONV_BUG
95 // Due to a bug in some iconv versions, when the last char
96 // in the buffer is a wide char and the output encoding is
97 // ISO-2022-JP and we are going to switch to another encoding,
98 // the appropriate escape sequence for changing the character
99 // set is not output (see bugs 5216, 5280, and also 5489).
100 // As a workaround, we append a nul char in order to force
101 // a switch to ASCII, and then remove it from output after
103 intern_type * from_new = 0;
104 intern_type const * from_old = from;
106 if (*(from_end - 1) >= 0x80 && encoding_ == "ISO-2022-JP") {
107 size_t len = from_end - from;
108 from_new = new intern_type[len + 1];
109 memcpy(from_new, from, len * sizeof(intern_type));
111 from_end = from_new + len + 1;
116 size_t inbytesleft = (from_end - from) * sizeof(intern_type);
117 size_t outbytesleft = (to_end - to) * sizeof(extern_type);
118 #if WORKAROUND_ICONV_BUG
119 outbytesleft += extra * sizeof(extern_type);
123 result const retval = do_iconv(out_cd_,
124 reinterpret_cast<char const **>(&from_next),
125 &inbytesleft, &to_next, &outbytesleft);
126 #if WORKAROUND_ICONV_BUG
127 // Remove from output the nul char that we inserted at the end
128 // of the input buffer in order to circumvent an iconv bug.
132 from_next = from_old + (from_next - from);
137 if (retval == base::error) {
139 "Error %d returned from iconv when converting from %s to %s: %s\n",
140 errno, ucs4_codeset, encoding_.c_str(),
142 fputs("Converted input:", stderr);
143 for (intern_type const * i = from; i < from_next; ++i) {
144 unsigned int const c = *i;
145 fprintf(stderr, " 0x%04x", c);
147 unsigned int const c = *from_next;
148 fprintf(stderr, "\nStopped at: 0x%04x\n", c);
149 fputs("Unconverted input:", stderr);
150 for (intern_type const * i = from_next + 1; i < from_end; ++i) {
151 unsigned int const c = *i;
152 fprintf(stderr, " 0x%04x", c);
154 fputs("\nConverted output:", stderr);
155 for (extern_type const * i = to; i < to_next; ++i) {
156 // extern_type may be signed, avoid output of
157 // something like 0xffffffc2
158 unsigned int const c =
159 *reinterpret_cast<unsigned char const *>(i);
160 fprintf(stderr, " 0x%02x", c);
167 virtual result do_unshift(state_type &, extern_type * to,
168 extern_type *, extern_type *& to_next) const
170 // utf8 does not use shifting
174 virtual result do_in(state_type &,
175 extern_type const * from, extern_type const * from_end,
176 extern_type const *& from_next,
177 intern_type * to, intern_type * to_end,
178 intern_type *& to_next) const
180 size_t inbytesleft = (from_end - from) * sizeof(extern_type);
181 size_t outbytesleft = (to_end - to) * sizeof(intern_type);
184 result const retval = do_iconv(in_cd_, &from_next, &inbytesleft,
185 reinterpret_cast<char **>(&to_next),
187 if (retval == base::error) {
189 "Error %d returned from iconv when converting from %s to %s: %s\n",
190 errno, encoding_.c_str(), ucs4_codeset,
192 fputs("Converted input:", stderr);
193 for (extern_type const * i = from; i < from_next; ++i) {
194 // extern_type may be signed, avoid output of
195 // something like 0xffffffc2
196 unsigned int const c =
197 *reinterpret_cast<unsigned char const *>(i);
198 fprintf(stderr, " 0x%02x", c);
200 unsigned int const c =
201 *reinterpret_cast<unsigned char const *>(from_next);
202 fprintf(stderr, "\nStopped at: 0x%02x\n", c);
203 fputs("Unconverted input:", stderr);
204 for (extern_type const * i = from_next + 1; i < from_end; ++i) {
205 unsigned int const c =
206 *reinterpret_cast<unsigned char const *>(i);
207 fprintf(stderr, " 0x%02x", c);
209 fputs("\nConverted output:", stderr);
210 for (intern_type const * i = to; i < to_next; ++i) {
211 unsigned int const c = *i;
212 fprintf(stderr, " 0x%02x", c);
219 virtual int do_encoding() const throw()
223 virtual bool do_always_noconv() const throw()
227 virtual int do_length(state_type & /*state*/, extern_type const * from,
228 extern_type const * end, size_t max) const
230 // The docs are a bit unclear about this method.
231 // It seems that we should calculate the actual length of the
232 // converted sequence, but that would not make sense, since
233 // once could just do the conversion directly.
234 // Therefore we just return the number of unconverted
235 // characters, since that is the best guess we can do.
237 intern_type * to = new intern_type[max];
238 intern_type * to_end = to + max;
239 intern_type * to_next = to;
240 extern_type const * from_next = from;
241 do_in(state, from, end, from_next, to, to_end, to_next);
245 size_t const length = end - from;
246 return min(length, max);
249 virtual int do_max_length() const throw()
251 return lyx::max_encoded_bytes(encoding_);
254 /// Do the actual conversion. The interface is equivalent to that of
255 /// iconv() (but const correct).
256 inline base::result do_iconv(iconv_t cd, char const ** from,
257 size_t * inbytesleft, char ** to, size_t * outbytesleft) const
259 char const * const to_start = *to;
260 size_t converted = iconv(cd, const_cast<char ICONV_CONST **>(from),
261 inbytesleft, to, outbytesleft);
262 if (converted == (size_t)(-1)) {
265 // As strange as it may seem, this
266 // does happen on windows when parsing
267 // comments with accented chars in
268 // tex2lyx. See the following thread
270 // http://thread.gmane.org/gmane.editors.lyx.devel/117636
274 return base::partial;
286 /// The narrow encoding
296 void setEncoding(Ios & ios, string const & encoding, ios_base::openmode mode)
298 // We must imbue the stream before openening the file
300 locale locale(global, new iconv_codecvt_facet(encoding, mode));
305 const char * iconv_codecvt_facet_exception::what() const throw()
307 return "iconv problem in iconv_codecvt_facet initialization";
311 ifdocstream::ifdocstream() : base()
313 setEncoding(*this, "UTF-8", in);
317 ifdocstream::ifdocstream(const char* s, ios_base::openmode mode,
318 string const & encoding)
321 setEncoding(*this, encoding, in);
326 ofdocstream::ofdocstream(): base()
328 setEncoding(*this, "UTF-8", out);
332 ofdocstream::ofdocstream(const char* s, ios_base::openmode mode,
333 string const & encoding)
336 setEncoding(*this, encoding, out);
341 void ofdocstream::reset(string const & encoding)
343 setEncoding(*this, encoding, out);
348 SetEnc setEncoding(string const & encoding)
350 return SetEnc(encoding);
354 odocstream & operator<<(odocstream & os, SetEnc e)
356 if (has_facet<iconv_codecvt_facet>(os.rdbuf()->getloc())) {
357 // This stream must be a file stream, since we never imbue
358 // any other stream with a locale having a iconv_codecvt_facet.
359 // Flush the stream so that all pending output is written
360 // with the old encoding.
362 locale locale(os.rdbuf()->getloc(),
363 new iconv_codecvt_facet(e.encoding, ios_base::out));
364 // FIXME Does changing the codecvt facet of an open file
365 // stream always work? It does with gcc 4.1, but I have read
366 // somewhere that it does not with MSVC.
367 // What does the standard say?
374 //CHECKME: I just copied the code above, and have no idea whether it
375 //is correct... (JMarc)
376 idocstream & operator<<(idocstream & is, SetEnc e)
378 if (has_facet<iconv_codecvt_facet>(is.rdbuf()->getloc())) {
379 // This stream must be a file stream, since we never imbue
380 // any other stream with a locale having a iconv_codecvt_facet.
381 // Flush the stream so that all pending output is written
382 // with the old encoding.
384 locale locale(is.rdbuf()->getloc(),
385 new iconv_codecvt_facet(e.encoding, ios_base::in));
386 // FIXME Does changing the codecvt facet of an open file
387 // stream always work? It does with gcc 4.1, but I have read
388 // somewhere that it does not with MSVC.
389 // What does the standard say?
396 #if ! defined(USE_WCHAR_T)
397 odocstream & operator<<(odocstream & os, char c)
406 #if ! defined(USE_WCHAR_T) && defined(__GNUC__)
407 // We get undefined references to these virtual methods. This looks like
408 // a bug in gcc. The implementation here does not do anything useful, since
409 // it is overriden in iconv_codecvt_facet.
412 template<> codecvt<lyx::char_type, char, mbstate_t>::result
413 codecvt<lyx::char_type, char, mbstate_t>::do_out(
414 mbstate_t &, const lyx::char_type *, const lyx::char_type *,
415 const lyx::char_type *&, char *, char *, char *&) const
421 template<> codecvt<lyx::char_type, char, mbstate_t>::result
422 codecvt<lyx::char_type, char, mbstate_t>::do_unshift(
423 mbstate_t &, char *, char *, char *&) const
429 template<> codecvt<lyx::char_type, char, mbstate_t>::result
430 codecvt<lyx::char_type, char, mbstate_t>::do_in(
431 mbstate_t &, const char *, const char *, const char *&,
432 lyx::char_type *, lyx::char_type *, lyx::char_type *&) const
439 int codecvt<lyx::char_type, char, mbstate_t>::do_encoding() const throw()
446 bool codecvt<lyx::char_type, char, mbstate_t>::do_always_noconv() const throw()
451 #if __GNUC__ == 3 && __GNUC_MINOR__ < 4
454 int codecvt<lyx::char_type, char, mbstate_t>::do_length(
455 mbstate_t const &, const char *, const char *, size_t) const
463 int codecvt<lyx::char_type, char, mbstate_t>::do_length(
464 mbstate_t &, const char *, const char *, size_t) const
472 int codecvt<lyx::char_type, char, mbstate_t>::do_max_length() const throw()