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"
22 using lyx::ucs4_codeset;
23 using lyx::ucs2_codeset;
28 char const * utf8_codeset = "UTF-8";
30 // We use C IO throughout this file, because the facets might be used with
31 // lyxerr in the future.
34 class utf8_codecvt_facet_exception : public std::exception {
36 virtual ~utf8_codecvt_facet_exception() throw() {}
37 virtual const char* what() const throw()
39 return "iconv problem in utf8_codecvt_facet initialization";
44 /// codecvt facet for conversion of UCS4 (internal representation) to UTF8
45 /// (external representation) or vice versa
46 class utf8_codecvt_facet : public std::codecvt<lyx::char_type, char, std::mbstate_t>
48 typedef std::codecvt<lyx::char_type, char, std::mbstate_t> base;
50 /// Constructor. You have to specify with \p inout whether you want
51 /// to use this facet only for input, only for output or for both.
52 explicit utf8_codecvt_facet(std::ios_base::openmode inout = std::ios_base::in | std::ios_base::out,
56 if (inout & std::ios_base::in) {
57 in_cd_ = iconv_open(ucs4_codeset, utf8_codeset);
58 if (in_cd_ == (iconv_t)(-1)) {
59 fprintf(stderr, "Error %d returned from iconv_open(in_cd_): %s\n",
60 errno, strerror(errno));
62 throw utf8_codecvt_facet_exception();
65 in_cd_ = (iconv_t)(-1);
66 if (inout & std::ios_base::out) {
67 out_cd_ = iconv_open(utf8_codeset, ucs4_codeset);
68 if (out_cd_ == (iconv_t)(-1)) {
69 fprintf(stderr, "Error %d returned from iconv_open(out_cd_): %s\n",
70 errno, strerror(errno));
72 throw utf8_codecvt_facet_exception();
75 out_cd_ = (iconv_t)(-1);
78 virtual ~utf8_codecvt_facet()
80 if (in_cd_ != (iconv_t)(-1))
81 if (iconv_close(in_cd_) == -1) {
82 fprintf(stderr, "Error %d returned from iconv_close(in_cd_): %s\n",
83 errno, strerror(errno));
86 if (out_cd_ != (iconv_t)(-1))
87 if (iconv_close(out_cd_) == -1) {
88 fprintf(stderr, "Error %d returned from iconv_close(out_cd_): %s\n",
89 errno, strerror(errno));
93 virtual result do_out(state_type &, intern_type const * from,
94 intern_type const * from_end, intern_type const *& from_next,
95 extern_type * to, extern_type * to_end,
96 extern_type *& to_next) const
98 size_t inbytesleft = (from_end - from) * sizeof(intern_type);
99 size_t outbytesleft = (to_end - to) * sizeof(extern_type);
102 return do_iconv(out_cd_, reinterpret_cast<char const **>(&from_next),
103 &inbytesleft, &to_next, &outbytesleft);
105 virtual result do_unshift(state_type &, extern_type * to,
106 extern_type *, extern_type *& to_next) const
108 // utf8 does not use shifting
112 virtual result do_in(state_type &,
113 extern_type const * from, extern_type const * from_end,
114 extern_type const *& from_next,
115 intern_type * to, intern_type * to_end,
116 intern_type *& to_next) const
118 size_t inbytesleft = (from_end - from) * sizeof(extern_type);
119 size_t outbytesleft = (to_end - to) * sizeof(intern_type);
122 return do_iconv(in_cd_, &from_next, &inbytesleft,
123 reinterpret_cast<char **>(&to_next),
126 virtual int do_encoding() const throw()
130 virtual bool do_always_noconv() const throw()
134 virtual int do_length(state_type & /*state*/, extern_type const * from,
135 extern_type const * end, size_t max) const
137 // The docs are a bit unclear about this method.
138 // It seems that we should calculate the actual length of the
139 // converted sequence, but that would not make sense, since
140 // once could just do the conversion directly.
141 // Therefore we just return the number of unconverted
142 // characters, since that is the best guess we can do.
144 intern_type * to = new intern_type[max];
145 intern_type * to_end = to + max;
146 intern_type * to_next = to;
147 extern_type const * from_next = from;
148 do_in(state, from, end, from_next, to, to_end, to_next);
152 size_t const length = end - from;
153 return std::min(length, max);
156 virtual int do_max_length() const throw()
158 // UTF8 uses at most 6 bytes to represent one code point
162 /// Do the actual conversion. The interface is equivalent to that of
163 /// iconv() (but const correct).
164 inline base::result do_iconv(iconv_t cd, char const ** from,
165 size_t * inbytesleft, char ** to, size_t * outbytesleft) const
167 char const * to_start = *to;
168 size_t converted = iconv(cd, const_cast<char ICONV_CONST **>(from),
169 inbytesleft, to, outbytesleft);
170 if (converted == (size_t)(-1)) {
174 return base::partial;
177 fprintf(stderr, "Error %d returned from iconv: %s\n",
178 errno, strerror(errno));
197 idocfstream::idocfstream() : base()
200 std::locale locale(global, new utf8_codecvt_facet(in));
205 idocfstream::idocfstream(const char* s, std::ios_base::openmode mode)
208 // We must imbue the stream before openening the file
210 std::locale locale(global, new utf8_codecvt_facet(in));
216 odocfstream::odocfstream() : base()
219 std::locale locale(global, new utf8_codecvt_facet(out));
224 odocfstream::odocfstream(const char* s, std::ios_base::openmode mode)
227 // We must imbue the stream before openening the file
229 std::locale locale(global, new utf8_codecvt_facet(out));
236 #if (!defined(HAVE_WCHAR_T) || SIZEOF_WCHAR_T != 4) && defined(__GNUC__)
237 // We get undefined references to these virtual methods. This looks like
238 // a bug in gcc. The implementation here does not do anything useful, since
239 // it is overriden in utf8_codecvt_facet.
241 template<> codecvt<lyx::char_type, char, mbstate_t>::result
242 codecvt<lyx::char_type, char, mbstate_t>::do_out(mbstate_t &, const lyx::char_type *, const lyx::char_type *, const lyx::char_type *&,
243 char *, char *, char *&) const { return error; }
244 template<> codecvt<lyx::char_type, char, mbstate_t>::result
245 codecvt<lyx::char_type, char, mbstate_t>::do_unshift(mbstate_t &, char *, char *, char *&) const { return error; }
246 template<> codecvt<lyx::char_type, char, mbstate_t>::result
247 codecvt<lyx::char_type, char, mbstate_t>::do_in(mbstate_t &, const char *, const char *, const char *&,
248 lyx::char_type *, lyx::char_type *, lyx::char_type *&) const { return error; }
249 template<> int codecvt<lyx::char_type, char, mbstate_t>::do_encoding() const throw() { return 0; }
250 template<> bool codecvt<lyx::char_type, char, mbstate_t>::do_always_noconv() const throw() { return true; }
251 #if __GNUC__ == 3 && __GNUC_MINOR__ < 4
252 template<> int codecvt<lyx::char_type, char, mbstate_t>::do_length(mbstate_t const &, const char *, const char *, size_t) const { return 1; }
254 template<> int codecvt<lyx::char_type, char, mbstate_t>::do_length(mbstate_t &, const char *, const char *, size_t) const { return 1; }
256 template<> int codecvt<lyx::char_type, char, mbstate_t>::do_max_length() const throw() { return 4; }