]> git.lyx.org Git - lyx.git/blob - src/support/docstream.C
MacOSX compile fix.
[lyx.git] / src / support / docstream.C
1 /**
2  * \file docstream.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Georg Baum
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "docstream.h"
14 #include "unicode.h"
15
16 #include <cerrno>
17 #include <cstdio>
18 #include <iconv.h>
19 #include <locale>
20
21 namespace {
22
23 char const * utf8_codeset = "UTF-8";
24
25 // We use C IO throughout this file, because the facets might be used with
26 // lyxerr in the future.
27
28
29 class utf8_codecvt_facet_exception : public std::exception {
30 public:
31         virtual ~utf8_codecvt_facet_exception() throw() {}
32         virtual const char* what() const throw()
33         {
34                 return "iconv problem in utf8_codecvt_facet initialization";
35         }
36 };
37
38
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>
42 {
43         typedef std::codecvt<lyx::char_type, char, std::mbstate_t> base;
44 public:
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,
48                         size_t refs = 0)
49                 : base(refs)
50         {
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));
56                                 fflush(stderr);
57                                 throw utf8_codecvt_facet_exception();
58                         }
59                 } else
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));
66                                 fflush(stderr);
67                                 throw utf8_codecvt_facet_exception();
68                         }
69                 } else
70                         out_cd_ = (iconv_t)(-1);
71         }
72 protected:
73         virtual ~utf8_codecvt_facet()
74         {
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));
79                                 fflush(stderr);
80                         }
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));
85                                 fflush(stderr);
86                         }
87         }
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
92         {
93                 size_t inbytesleft = (from_end - from) * sizeof(intern_type);
94                 size_t outbytesleft = (to_end - to) * sizeof(extern_type);
95                 from_next = from;
96                 to_next = to;
97                 return do_iconv(out_cd_, reinterpret_cast<char const **>(&from_next),
98                                 &inbytesleft, &to_next, &outbytesleft);
99         }
100         virtual result do_unshift(state_type &, extern_type * to,
101                         extern_type *, extern_type *& to_next) const
102         {
103                 // utf8 does not use shifting
104                 to_next = to;
105                 return base::noconv;
106         }
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
112         {
113                 size_t inbytesleft = (from_end - from) * sizeof(extern_type);
114                 size_t outbytesleft = (to_end - to) * sizeof(intern_type);
115                 from_next = from;
116                 to_next = to;
117                 return do_iconv(in_cd_, &from_next, &inbytesleft,
118                                 reinterpret_cast<char **>(&to_next),
119                                 &outbytesleft);
120         }
121         virtual int do_encoding() const throw()
122         {
123                 return 0;
124         }
125         virtual bool do_always_noconv() const throw()
126         {
127                 return false;
128         }
129         virtual int do_length(state_type & /*state*/, extern_type const * from,
130                         extern_type const * end, size_t max) const
131         {
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.
138 #if 0
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);
144                 delete[] to;
145                 return to_next - to;
146 #else
147                 size_t const length = end - from;
148                 return std::min(length, max);
149 #endif
150         }
151         virtual int do_max_length() const throw()
152         {
153                 // UTF8 uses at most 6 bytes to represent one code point
154                 return 6;
155         }
156 private:
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
161         {
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)) {
166                         switch(errno) {
167                         case EINVAL:
168                         case E2BIG:
169                                 return base::partial;
170                         case EILSEQ:
171                         default:
172                                 fprintf(stderr, "Error %d returned from iconv: %s\n",
173                                         errno, strerror(errno));
174                                 fflush(stderr);
175                                 return base::error;
176                         }
177                 }
178                 if (*to == to_start)
179                         return base::noconv;
180                 return base::ok;
181         }
182         iconv_t in_cd_;
183         iconv_t out_cd_;
184 };
185
186 }
187
188
189 namespace lyx {
190
191
192 idocfstream::idocfstream() : base()
193 {
194         std::locale global;
195         std::locale locale(global, new utf8_codecvt_facet(in));
196         imbue(locale);
197 }
198
199         
200 idocfstream::idocfstream(const char* s, std::ios_base::openmode mode)
201         : base()
202 {
203         // We must imbue the stream before openening the file
204         std::locale global;
205         std::locale locale(global, new utf8_codecvt_facet(in));
206         imbue(locale);
207         open(s, mode);
208 }
209
210
211 odocfstream::odocfstream() : base()
212 {
213         std::locale global;
214         std::locale locale(global, new utf8_codecvt_facet(out));
215         imbue(locale);
216 }
217
218         
219 odocfstream::odocfstream(const char* s, std::ios_base::openmode mode)
220         : base()
221 {
222         // We must imbue the stream before openening the file
223         std::locale global;
224         std::locale locale(global, new utf8_codecvt_facet(out));
225         imbue(locale);
226         open(s, mode);
227 }
228
229 }
230
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.
235 namespace std {
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; }
248 #else
249 template<> int codecvt<lyx::char_type, char, mbstate_t>::do_length(mbstate_t &, const char *, const char *, size_t) const { return 1; }
250 #endif
251 template<> int codecvt<lyx::char_type, char, mbstate_t>::do_max_length() const throw() { return 4; }
252 }
253 #endif