]> git.lyx.org Git - lyx.git/blob - src/support/docstream.C
trivial changes for all-in-one compilation
[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 #ifdef min
22 #undef min
23 #endif
24
25 #ifdef max
26 #undef max
27 #endif
28
29 using lyx::ucs4_codeset;
30
31 using std::string;
32
33
34 namespace {
35
36 // We use C IO throughout this file, because the facets might be used with
37 // lyxerr in the future.
38
39
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>
43 {
44         typedef std::codecvt<lyx::char_type, char, std::mbstate_t> base;
45 public:
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,
50                         size_t refs = 0)
51                 : base(refs), encoding_(encoding)
52         {
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));
58                                 fflush(stderr);
59                                 throw lyx::iconv_codecvt_facet_exception();
60                         }
61                 } else
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));
68                                 fflush(stderr);
69                                 throw lyx::iconv_codecvt_facet_exception();
70                         }
71                 } else
72                         out_cd_ = (iconv_t)(-1);
73         }
74 protected:
75         virtual ~iconv_codecvt_facet()
76         {
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));
81                                 fflush(stderr);
82                         }
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));
87                                 fflush(stderr);
88                         }
89         }
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
94         {
95                 size_t inbytesleft = (from_end - from) * sizeof(intern_type);
96                 size_t outbytesleft = (to_end - to) * sizeof(extern_type);
97                 from_next = from;
98                 to_next = to;
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) {
103                         fprintf(stderr,
104                                 "Error %d returned from iconv when converting from %s to %s: %s\n",
105                                 errno, ucs4_codeset, encoding_.c_str(),
106                                 strerror(errno));
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);
111                         }
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);
118                         }
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);
126                         }
127                         fputc('\n', stderr);
128                         fflush(stderr);
129                 }
130                 return retval;
131         }
132         virtual result do_unshift(state_type &, extern_type * to,
133                         extern_type *, extern_type *& to_next) const
134         {
135                 // utf8 does not use shifting
136                 to_next = to;
137                 return base::noconv;
138         }
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
144         {
145                 size_t inbytesleft = (from_end - from) * sizeof(extern_type);
146                 size_t outbytesleft = (to_end - to) * sizeof(intern_type);
147                 from_next = from;
148                 to_next = to;
149                 result const retval = do_iconv(in_cd_, &from_next, &inbytesleft,
150                                 reinterpret_cast<char **>(&to_next),
151                                 &outbytesleft);
152                 if (retval == base::error) {
153                         fprintf(stderr,
154                                 "Error %d returned from iconv when converting from %s to %s: %s\n",
155                                 errno, encoding_.c_str(), ucs4_codeset,
156                                 strerror(errno));
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);
164                         }
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);
173                         }
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);
178                         }
179                         fputc('\n', stderr);
180                         fflush(stderr);
181                 }
182                 return retval;
183         }
184         virtual int do_encoding() const throw()
185         {
186                 return 0;
187         }
188         virtual bool do_always_noconv() const throw()
189         {
190                 return false;
191         }
192         virtual int do_length(state_type & /*state*/, extern_type const * from,
193                         extern_type const * end, size_t max) const
194         {
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.
201 #if 0
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);
207                 delete[] to;
208                 return to_next - to;
209 #else
210                 size_t const length = end - from;
211                 return std::min(length, max);
212 #endif
213         }
214         virtual int do_max_length() const throw()
215         {
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
219                 // RFC 3629.
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;
223         }
224 private:
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
229         {
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)) {
234                         switch(errno) {
235                         case EINVAL:
236                         case E2BIG:
237                                 return base::partial;
238                         case EILSEQ:
239                         default:
240                                 return base::error;
241                         }
242                 }
243                 if (*to == to_start)
244                         return base::noconv;
245                 return base::ok;
246         }
247         iconv_t in_cd_;
248         iconv_t out_cd_;
249         /// The narrow encoding
250         std::string encoding_;
251 };
252
253 } // namespace anon
254
255
256 namespace lyx {
257
258
259 const char * iconv_codecvt_facet_exception::what() const throw()
260 {
261         return "iconv problem in iconv_codecvt_facet initialization";
262 }
263
264
265 idocfstream::idocfstream(string const & encoding) : base()
266 {
267         std::locale global;
268         std::locale locale(global, new iconv_codecvt_facet(encoding, in));
269         imbue(locale);
270 }
271
272         
273 idocfstream::idocfstream(const char* s, std::ios_base::openmode mode,
274                          string const & encoding)
275         : base()
276 {
277         // We must imbue the stream before openening the file
278         std::locale global;
279         std::locale locale(global, new iconv_codecvt_facet(encoding, in));
280         imbue(locale);
281         open(s, mode);
282 }
283
284
285 odocfstream::odocfstream(string const & encoding) : base()
286 {
287         std::locale global;
288         std::locale locale(global, new iconv_codecvt_facet(encoding, out));
289         imbue(locale);
290 }
291
292
293 odocfstream::odocfstream(const char* s, std::ios_base::openmode mode,
294                          string const & encoding)
295         : base()
296 {
297         // We must imbue the stream before openening the file
298         std::locale global;
299         std::locale locale(global, new iconv_codecvt_facet(encoding, out));
300         imbue(locale);
301         open(s, mode);
302 }
303
304
305 SetEnc setEncoding(string const & encoding)
306 {
307         return SetEnc(encoding);
308 }
309
310
311 odocstream & operator<<(odocstream & os, SetEnc e)
312 {
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.
318                 os.flush();
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?
325                 os.imbue(locale);
326         }
327         return os;
328 }
329
330 }
331
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.
336 namespace std {
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; }
349 #else
350 template<> int codecvt<lyx::char_type, char, mbstate_t>::do_length(mbstate_t &, const char *, const char *, size_t) const { return 1; }
351 #endif
352 template<> int codecvt<lyx::char_type, char, mbstate_t>::do_max_length() const throw() { return 4; }
353 }
354 #endif