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 "docstream.h"
29 using lyx::ucs4_codeset;
36 // We use C IO throughout this file, because the facets might be used with
37 // lyxerr in the future.
40 /// codecvt facet for conversion of UCS4 (internal representation) to UTF8
41 /// (external representation) or vice versa
42 class iconv_codecvt_facet : public std::codecvt<lyx::char_type, char, std::mbstate_t>
44 typedef std::codecvt<lyx::char_type, char, std::mbstate_t> base;
46 /// Constructor. You have to specify with \p inout whether you want
47 /// to use this facet only for input, only for output or for both.
48 explicit iconv_codecvt_facet(string const & encoding = "UTF-8",
49 std::ios_base::openmode inout = std::ios_base::in | std::ios_base::out,
51 : base(refs), encoding_(encoding)
53 if (inout & std::ios_base::in) {
54 in_cd_ = iconv_open(ucs4_codeset, encoding.c_str());
55 if (in_cd_ == (iconv_t)(-1)) {
56 fprintf(stderr, "Error %d returned from iconv_open(in_cd_): %s\n",
57 errno, strerror(errno));
59 throw lyx::iconv_codecvt_facet_exception();
62 in_cd_ = (iconv_t)(-1);
63 if (inout & std::ios_base::out) {
64 out_cd_ = iconv_open(encoding.c_str(), ucs4_codeset);
65 if (out_cd_ == (iconv_t)(-1)) {
66 fprintf(stderr, "Error %d returned from iconv_open(out_cd_): %s\n",
67 errno, strerror(errno));
69 throw lyx::iconv_codecvt_facet_exception();
72 out_cd_ = (iconv_t)(-1);
75 virtual ~iconv_codecvt_facet()
77 if (in_cd_ != (iconv_t)(-1))
78 if (iconv_close(in_cd_) == -1) {
79 fprintf(stderr, "Error %d returned from iconv_close(in_cd_): %s\n",
80 errno, strerror(errno));
83 if (out_cd_ != (iconv_t)(-1))
84 if (iconv_close(out_cd_) == -1) {
85 fprintf(stderr, "Error %d returned from iconv_close(out_cd_): %s\n",
86 errno, strerror(errno));
90 virtual result do_out(state_type &, intern_type const * from,
91 intern_type const * from_end, intern_type const *& from_next,
92 extern_type * to, extern_type * to_end,
93 extern_type *& to_next) const
95 size_t inbytesleft = (from_end - from) * sizeof(intern_type);
96 size_t outbytesleft = (to_end - to) * sizeof(extern_type);
99 result const retval = do_iconv(out_cd_,
100 reinterpret_cast<char const **>(&from_next),
101 &inbytesleft, &to_next, &outbytesleft);
102 if (retval == base::error) {
104 "Error %d returned from iconv when converting from %s to %s: %s\n",
105 errno, ucs4_codeset, encoding_.c_str(),
107 fputs("Converted input:", stderr);
108 for (intern_type const * i = from; i < from_next; ++i) {
109 unsigned int const c = *i;
110 fprintf(stderr, " 0x%04x", c);
112 unsigned int const c = *from_next;
113 fprintf(stderr, "\nStopped at: 0x%04x\n", c);
114 fputs("Unconverted input:", stderr);
115 for (intern_type const * i = from_next + 1; i < from_end; ++i) {
116 unsigned int const c = *i;
117 fprintf(stderr, " 0x%04x", c);
119 fputs("\nConverted output:", stderr);
120 for (extern_type const * i = to; i < to_next; ++i) {
121 // extern_type may be signed, avoid output of
122 // something like 0xffffffc2
123 unsigned int const c =
124 *reinterpret_cast<unsigned char const *>(i);
125 fprintf(stderr, " 0x%02x", c);
132 virtual result do_unshift(state_type &, extern_type * to,
133 extern_type *, extern_type *& to_next) const
135 // utf8 does not use shifting
139 virtual result do_in(state_type &,
140 extern_type const * from, extern_type const * from_end,
141 extern_type const *& from_next,
142 intern_type * to, intern_type * to_end,
143 intern_type *& to_next) const
145 size_t inbytesleft = (from_end - from) * sizeof(extern_type);
146 size_t outbytesleft = (to_end - to) * sizeof(intern_type);
149 result const retval = do_iconv(in_cd_, &from_next, &inbytesleft,
150 reinterpret_cast<char **>(&to_next),
152 if (retval == base::error) {
154 "Error %d returned from iconv when converting from %s to %s: %s\n",
155 errno, encoding_.c_str(), ucs4_codeset,
157 fputs("Converted input:", stderr);
158 for (extern_type const * i = from; i < from_next; ++i) {
159 // extern_type may be signed, avoid output of
160 // something like 0xffffffc2
161 unsigned int const c =
162 *reinterpret_cast<unsigned char const *>(i);
163 fprintf(stderr, " 0x%02x", c);
165 unsigned int const c =
166 *reinterpret_cast<unsigned char const *>(from_next);
167 fprintf(stderr, "\nStopped at: 0x%02x\n", c);
168 fputs("Unconverted input:", stderr);
169 for (extern_type const * i = from_next + 1; i < from_end; ++i) {
170 unsigned int const c =
171 *reinterpret_cast<unsigned char const *>(i);
172 fprintf(stderr, " 0x%02x", c);
174 fputs("\nConverted output:", stderr);
175 for (intern_type const * i = to; i < to_next; ++i) {
176 unsigned int const c = *i;
177 fprintf(stderr, " 0x%02x", c);
184 virtual int do_encoding() const throw()
188 virtual bool do_always_noconv() const throw()
192 virtual int do_length(state_type & /*state*/, extern_type const * from,
193 extern_type const * end, size_t max) const
195 // The docs are a bit unclear about this method.
196 // It seems that we should calculate the actual length of the
197 // converted sequence, but that would not make sense, since
198 // once could just do the conversion directly.
199 // Therefore we just return the number of unconverted
200 // characters, since that is the best guess we can do.
202 intern_type * to = new intern_type[max];
203 intern_type * to_end = to + max;
204 intern_type * to_next = to;
205 extern_type const * from_next = from;
206 do_in(state, from, end, from_next, to, to_end, to_next);
210 size_t const length = end - from;
211 return std::min(length, max);
214 virtual int do_max_length() const throw()
216 // UTF8 uses at most 4 bytes to represent one UCS4 code point
217 // (see RFC 3629). RFC 2279 specifies 6 bytes, but that
218 // information is outdated, and RFC 2279 has been superseded by
220 // All other encodings encode one UCS4 code point in one byte
221 // (and can therefore only encode a subset of UCS4)
222 return encoding_ == "UTF-8" ? 4 : 1;
225 /// Do the actual conversion. The interface is equivalent to that of
226 /// iconv() (but const correct).
227 inline base::result do_iconv(iconv_t cd, char const ** from,
228 size_t * inbytesleft, char ** to, size_t * outbytesleft) const
230 char const * const to_start = *to;
231 size_t converted = iconv(cd, const_cast<char ICONV_CONST **>(from),
232 inbytesleft, to, outbytesleft);
233 if (converted == (size_t)(-1)) {
237 return base::partial;
249 /// The narrow encoding
250 std::string encoding_;
259 const char * iconv_codecvt_facet_exception::what() const throw()
261 return "iconv problem in iconv_codecvt_facet initialization";
265 idocfstream::idocfstream(string const & encoding) : base()
268 std::locale locale(global, new iconv_codecvt_facet(encoding, in));
273 idocfstream::idocfstream(const char* s, std::ios_base::openmode mode,
274 string const & encoding)
277 // We must imbue the stream before openening the file
279 std::locale locale(global, new iconv_codecvt_facet(encoding, in));
285 odocfstream::odocfstream(string const & encoding) : base()
288 std::locale locale(global, new iconv_codecvt_facet(encoding, out));
293 odocfstream::odocfstream(const char* s, std::ios_base::openmode mode,
294 string const & encoding)
297 // We must imbue the stream before openening the file
299 std::locale locale(global, new iconv_codecvt_facet(encoding, out));
305 SetEnc setEncoding(string const & encoding)
307 return SetEnc(encoding);
311 odocstream & operator<<(odocstream & os, SetEnc e)
313 if (std::has_facet<iconv_codecvt_facet>(os.rdbuf()->getloc())) {
314 // This stream must be a file stream, since we never imbue
315 // any other stream with a locale having a iconv_codecvt_facet.
316 // Flush the stream so that all pending output is written
317 // with the old encoding.
319 std::locale locale(os.rdbuf()->getloc(),
320 new iconv_codecvt_facet(e.encoding, std::ios_base::out));
321 // FIXME Does changing the codecvt facet of an open file
322 // stream always work? It does with gcc 4.1, but I have read
323 // somewhere that it does not with MSVC.
324 // What does the standard say?
332 #if (!defined(HAVE_WCHAR_T) || SIZEOF_WCHAR_T != 4) && defined(__GNUC__)
333 // We get undefined references to these virtual methods. This looks like
334 // a bug in gcc. The implementation here does not do anything useful, since
335 // it is overriden in iconv_codecvt_facet.
337 template<> codecvt<lyx::char_type, char, mbstate_t>::result
338 codecvt<lyx::char_type, char, mbstate_t>::do_out(mbstate_t &, const lyx::char_type *, const lyx::char_type *, const lyx::char_type *&,
339 char *, char *, char *&) const { return error; }
340 template<> codecvt<lyx::char_type, char, mbstate_t>::result
341 codecvt<lyx::char_type, char, mbstate_t>::do_unshift(mbstate_t &, char *, char *, char *&) const { return error; }
342 template<> codecvt<lyx::char_type, char, mbstate_t>::result
343 codecvt<lyx::char_type, char, mbstate_t>::do_in(mbstate_t &, const char *, const char *, const char *&,
344 lyx::char_type *, lyx::char_type *, lyx::char_type *&) const { return error; }
345 template<> int codecvt<lyx::char_type, char, mbstate_t>::do_encoding() const throw() { return 0; }
346 template<> bool codecvt<lyx::char_type, char, mbstate_t>::do_always_noconv() const throw() { return true; }
347 #if __GNUC__ == 3 && __GNUC_MINOR__ < 4
348 template<> int codecvt<lyx::char_type, char, mbstate_t>::do_length(mbstate_t const &, const char *, const char *, size_t) const { return 1; }
350 template<> int codecvt<lyx::char_type, char, mbstate_t>::do_length(mbstate_t &, const char *, const char *, size_t) const { return 1; }
352 template<> int codecvt<lyx::char_type, char, mbstate_t>::do_max_length() const throw() { return 4; }