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"
23 char const * utf8_codeset = "UTF-8";
25 // We use C IO throughout this file, because the facets might be used with
26 // lyxerr in the future.
29 class utf8_codecvt_facet_exception : public std::exception {
31 virtual ~utf8_codecvt_facet_exception() throw() {}
32 virtual const char* what() const throw()
34 return "iconv problem in utf8_codecvt_facet initialization";
39 /// codecvt facet for conversion of UCS4 (internal representation) to UTF8
40 /// (external representation) or vice versa
41 class utf8_codecvt_facet : public std::codecvt<lyx::char_type, char, std::mbstate_t>
43 typedef std::codecvt<lyx::char_type, char, std::mbstate_t> base;
45 /// Constructor. You have to specify with \p inout whether you want
46 /// to use this facet only for input, only for output or for both.
47 explicit utf8_codecvt_facet(std::ios_base::openmode inout = std::ios_base::in | std::ios_base::out,
51 if (inout & std::ios_base::in) {
52 in_cd_ = iconv_open(ucs4_codeset, utf8_codeset);
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 utf8_codecvt_facet_exception();
60 in_cd_ = (iconv_t)(-1);
61 if (inout & std::ios_base::out) {
62 out_cd_ = iconv_open(utf8_codeset, 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 utf8_codecvt_facet_exception();
70 out_cd_ = (iconv_t)(-1);
73 virtual ~utf8_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 size_t inbytesleft = (from_end - from) * sizeof(intern_type);
94 size_t outbytesleft = (to_end - to) * sizeof(extern_type);
97 return do_iconv(out_cd_, reinterpret_cast<char const **>(&from_next),
98 &inbytesleft, &to_next, &outbytesleft);
100 virtual result do_unshift(state_type &, extern_type * to,
101 extern_type *, extern_type *& to_next) const
103 // utf8 does not use shifting
107 virtual result do_in(state_type &,
108 extern_type const * from, extern_type const * from_end,
109 extern_type const *& from_next,
110 intern_type * to, intern_type * to_end,
111 intern_type *& to_next) const
113 size_t inbytesleft = (from_end - from) * sizeof(extern_type);
114 size_t outbytesleft = (to_end - to) * sizeof(intern_type);
117 return do_iconv(in_cd_, &from_next, &inbytesleft,
118 reinterpret_cast<char **>(&to_next),
121 virtual int do_encoding() const throw()
125 virtual bool do_always_noconv() const throw()
129 virtual int do_length(state_type & /*state*/, extern_type const * from,
130 extern_type const * end, size_t max) const
132 // The docs are a bit unclear about this method.
133 // It seems that we should calculate the actual length of the
134 // converted sequence, but that would not make sense, since
135 // once could just do the conversion directly.
136 // Therefore we just return the number of unconverted
137 // characters, since that is the best guess we can do.
139 intern_type * to = new intern_type[max];
140 intern_type * to_end = to + max;
141 intern_type * to_next = to;
142 extern_type const * from_next = from;
143 do_in(state, from, end, from_next, to, to_end, to_next);
147 size_t const length = end - from;
148 return std::min(length, max);
151 virtual int do_max_length() const throw()
153 // UTF8 uses at most 6 bytes to represent one code point
157 /// Do the actual conversion. The interface is equivalent to that of
158 /// iconv() (but const correct).
159 inline base::result do_iconv(iconv_t cd, char const ** from,
160 size_t * inbytesleft, char ** to, size_t * outbytesleft) const
162 char const * to_start = *to;
163 size_t converted = iconv(cd, const_cast<char ICONV_CONST **>(from),
164 inbytesleft, to, outbytesleft);
165 if (converted == (size_t)(-1)) {
169 return base::partial;
172 fprintf(stderr, "Error %d returned from iconv: %s\n",
173 errno, strerror(errno));
192 idocfstream::idocfstream() : base()
195 std::locale locale(global, new utf8_codecvt_facet(in));
200 idocfstream::idocfstream(const char* s, std::ios_base::openmode mode)
203 // We must imbue the stream before openening the file
205 std::locale locale(global, new utf8_codecvt_facet(in));
211 odocfstream::odocfstream() : base()
214 std::locale locale(global, new utf8_codecvt_facet(out));
219 odocfstream::odocfstream(const char* s, std::ios_base::openmode mode)
222 // We must imbue the stream before openening the file
224 std::locale locale(global, new utf8_codecvt_facet(out));
231 #if (!defined(HAVE_WCHAR_T) || SIZEOF_WCHAR_T != 4) && defined(__GNUC__)
232 // We get undefined references to these virtual methods. This looks like
233 // a bug in gcc. The implementation here does not do anything useful, since
234 // it is overriden in utf8_codecvt_facet.
236 template<> codecvt<lyx::char_type, char, mbstate_t>::result
237 codecvt<lyx::char_type, char, mbstate_t>::do_out(mbstate_t &, const lyx::char_type *, const lyx::char_type *, const lyx::char_type *&,
238 char *, char *, char *&) const { return error; }
239 template<> codecvt<lyx::char_type, char, mbstate_t>::result
240 codecvt<lyx::char_type, char, mbstate_t>::do_unshift(mbstate_t &, char *, char *, char *&) const { return error; }
241 template<> codecvt<lyx::char_type, char, mbstate_t>::result
242 codecvt<lyx::char_type, char, mbstate_t>::do_in(mbstate_t &, const char *, const char *, const char *&,
243 lyx::char_type *, lyx::char_type *, lyx::char_type *&) const { return error; }
244 template<> int codecvt<lyx::char_type, char, mbstate_t>::do_encoding() const throw() { return 0; }
245 template<> bool codecvt<lyx::char_type, char, mbstate_t>::do_always_noconv() const throw() { return true; }
246 #if __GNUC__ == 3 && __GNUC_MINOR__ < 4
247 template<> int codecvt<lyx::char_type, char, mbstate_t>::do_length(mbstate_t const &, const char *, const char *, size_t) const { return 1; }
249 template<> int codecvt<lyx::char_type, char, mbstate_t>::do_length(mbstate_t &, const char *, const char *, size_t) const { return 1; }
251 template<> int codecvt<lyx::char_type, char, mbstate_t>::do_max_length() const throw() { return 4; }