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 #ifdef WORDS_BIGENDIAN
23 char const * ucs4_codeset = "UCS-4BE";
25 char const * ucs4_codeset = "UCS-4LE";
27 char const * utf8_codeset = "UTF-8";
29 // We use C IO throughout this file, because the facets might be used with
30 // lyxerr in the future.
33 class utf8_codecvt_facet_exception : public std::exception {
35 virtual ~utf8_codecvt_facet_exception() throw() {}
36 virtual const char* what() const throw()
38 return "iconv problem in utf8_codecvt_facet initialization";
43 /// codecvt facet for conversion of UCS4 (internal representation) to UTF8
44 /// (external representation) or vice versa
45 class utf8_codecvt_facet : public std::codecvt<lyx::char_type, char, std::mbstate_t>
47 typedef std::codecvt<lyx::char_type, char, std::mbstate_t> base;
49 /// Constructor. You have to specify with \p inout whether you want
50 /// to use this facet only for input, only for output or for both.
51 explicit utf8_codecvt_facet(std::ios_base::openmode inout = std::ios_base::in | std::ios_base::out,
55 if (inout & std::ios_base::in) {
56 in_cd_ = iconv_open(ucs4_codeset, utf8_codeset);
57 if (in_cd_ == (iconv_t)(-1)) {
58 fprintf(stderr, "Error %d returned from iconv_open(in_cd_): %s\n",
59 errno, strerror(errno));
61 throw utf8_codecvt_facet_exception();
64 in_cd_ = (iconv_t)(-1);
65 if (inout & std::ios_base::out) {
66 out_cd_ = iconv_open(utf8_codeset, ucs4_codeset);
67 if (out_cd_ == (iconv_t)(-1)) {
68 fprintf(stderr, "Error %d returned from iconv_open(out_cd_): %s\n",
69 errno, strerror(errno));
71 throw utf8_codecvt_facet_exception();
74 out_cd_ = (iconv_t)(-1);
77 virtual ~utf8_codecvt_facet()
79 if (in_cd_ != (iconv_t)(-1))
80 if (iconv_close(in_cd_) == -1) {
81 fprintf(stderr, "Error %d returned from iconv_close(in_cd_): %s\n",
82 errno, strerror(errno));
85 if (out_cd_ != (iconv_t)(-1))
86 if (iconv_close(out_cd_) == -1) {
87 fprintf(stderr, "Error %d returned from iconv_close(out_cd_): %s\n",
88 errno, strerror(errno));
92 virtual result do_out(state_type &, intern_type const * from,
93 intern_type const * from_end, intern_type const *& from_next,
94 extern_type * to, extern_type * to_end,
95 extern_type *& to_next) const
97 size_t inbytesleft = (from_end - from) * sizeof(intern_type);
98 size_t outbytesleft = (to_end - to) * sizeof(extern_type);
101 return do_iconv(out_cd_, reinterpret_cast<char const **>(&from_next),
102 &inbytesleft, &to_next, &outbytesleft);
104 virtual result do_unshift(state_type &, extern_type * to,
105 extern_type *, extern_type *& to_next) const
107 // utf8 does not use shifting
111 virtual result do_in(state_type &,
112 extern_type const * from, extern_type const * from_end,
113 extern_type const *& from_next,
114 intern_type * to, intern_type * to_end,
115 intern_type *& to_next) const
117 size_t inbytesleft = (from_end - from) * sizeof(extern_type);
118 size_t outbytesleft = (to_end - to) * sizeof(intern_type);
121 return do_iconv(in_cd_, &from_next, &inbytesleft,
122 reinterpret_cast<char **>(&to_next),
125 virtual int do_encoding() const throw()
129 virtual bool do_always_noconv() const throw()
133 virtual int do_length(state_type & /*state*/, extern_type const * from,
134 extern_type const * end, size_t max) const
136 // The docs are a bit unclear about this method.
137 // It seems that we should calculate the actual length of the
138 // converted sequence, but that would not make sense, since
139 // once could just do the conversion directly.
140 // Therefore we just return the number of unconverted
141 // characters, since that is the best guess we can do.
143 intern_type * to = new intern_type[max];
144 intern_type * to_end = to + max;
145 intern_type * to_next = to;
146 extern_type const * from_next = from;
147 do_in(state, from, end, from_next, to, to_end, to_next);
151 size_t const length = end - from;
152 return std::min(length, max);
155 virtual int do_max_length() const throw()
157 // UTF8 uses at most 6 bytes to represent one code point
161 /// Do the actual conversion. The interface is equivalent to that of
162 /// iconv() (but const correct).
163 inline base::result do_iconv(iconv_t cd, char const ** from,
164 size_t * inbytesleft, char ** to, size_t * outbytesleft) const
166 char const * to_start = *to;
167 size_t converted = iconv(cd, const_cast<char ICONV_CONST **>(from),
168 inbytesleft, to, outbytesleft);
169 if (converted == (size_t)(-1)) {
173 return base::partial;
176 fprintf(stderr, "Error %d returned from iconv: %s\n",
177 errno, strerror(errno));
196 idocfstream::idocfstream() : base()
199 std::locale locale(global, new utf8_codecvt_facet(in));
204 idocfstream::idocfstream(const char* s, std::ios_base::openmode mode)
207 // We must imbue the stream before openening the file
209 std::locale locale(global, new utf8_codecvt_facet(in));
215 odocfstream::odocfstream() : base()
218 std::locale locale(global, new utf8_codecvt_facet(out));
223 odocfstream::odocfstream(const char* s, std::ios_base::openmode mode)
226 // We must imbue the stream before openening the file
228 std::locale locale(global, new utf8_codecvt_facet(out));
235 #if (!defined(HAVE_WCHAR_T) || SIZEOF_WCHAR_T != 4) && defined(__GNUC__)
236 // We get undefined references to these virtual methods. This looks like
237 // a bug in gcc. The implementation here does not do anything useful, since
238 // it is overriden in utf8_codecvt_facet.
240 template<> codecvt<lyx::char_type, char, mbstate_t>::result
241 codecvt<lyx::char_type, char, mbstate_t>::do_out(mbstate_t &, const lyx::char_type *, const lyx::char_type *, const lyx::char_type *&,
242 char *, char *, char *&) const { return error; }
243 template<> codecvt<lyx::char_type, char, mbstate_t>::result
244 codecvt<lyx::char_type, char, mbstate_t>::do_unshift(mbstate_t &, char *, char *, char *&) const { return error; }
245 template<> codecvt<lyx::char_type, char, mbstate_t>::result
246 codecvt<lyx::char_type, char, mbstate_t>::do_in(mbstate_t &, const char *, const char *, const char *&,
247 lyx::char_type *, lyx::char_type *, lyx::char_type *&) const { return error; }
248 template<> int codecvt<lyx::char_type, char, mbstate_t>::do_encoding() const throw() { return 0; }
249 template<> bool codecvt<lyx::char_type, char, mbstate_t>::do_always_noconv() const throw() { return true; }
250 #if __GNUC__ == 3 && __GNUC_MINOR__ < 4
251 template<> int codecvt<lyx::char_type, char, mbstate_t>::do_length(mbstate_t const &, const char *, const char *, size_t) const { return 1; }
253 template<> int codecvt<lyx::char_type, char, mbstate_t>::do_length(mbstate_t &, const char *, const char *, size_t) const { return 1; }
255 template<> int codecvt<lyx::char_type, char, mbstate_t>::do_max_length() const throw() { return 4; }