]> git.lyx.org Git - lyx.git/blob - src/support/docstream.C
Introduce wide streams. This fixes the remaining problems of plain text
[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
15 #include <cerrno>
16 #include <cstdio>
17 #include <iconv.h>
18 #include <locale>
19
20 namespace {
21
22 #ifdef WORDS_BIGENDIAN
23 char const * ucs4_codeset = "UCS-4BE";
24 #else
25 char const * ucs4_codeset = "UCS-4LE";
26 #endif
27 char const * utf8_codeset = "UTF-8";
28
29 // We use C IO throughout this file, because the facets might be used with
30 // lyxerr in the future.
31
32
33 class utf8_codecvt_facet_exception : public std::exception {
34 public:
35         virtual ~utf8_codecvt_facet_exception() throw() {}
36         virtual const char* what() const throw()
37         {
38                 return "iconv problem in utf8_codecvt_facet initialization";
39         }
40 };
41
42
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>
46 {
47         typedef std::codecvt<lyx::char_type, char, std::mbstate_t> base;
48 public:
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,
52                         size_t refs = 0)
53                 : base(refs)
54         {
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));
60                                 fflush(stderr);
61                                 throw utf8_codecvt_facet_exception();
62                         }
63                 } else
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));
70                                 fflush(stderr);
71                                 throw utf8_codecvt_facet_exception();
72                         }
73                 } else
74                         out_cd_ = (iconv_t)(-1);
75         }
76 protected:
77         virtual ~utf8_codecvt_facet()
78         {
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));
83                                 fflush(stderr);
84                         }
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));
89                                 fflush(stderr);
90                         }
91         }
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
96         {
97                 size_t inbytesleft = (from_end - from) * sizeof(intern_type);
98                 size_t outbytesleft = (to_end - to) * sizeof(extern_type);
99                 from_next = from;
100                 to_next = to;
101                 return do_iconv(out_cd_, reinterpret_cast<char const **>(&from_next),
102                                 &inbytesleft, &to_next, &outbytesleft);
103         }
104         virtual result do_unshift(state_type &, extern_type * to,
105                         extern_type *, extern_type *& to_next) const
106         {
107                 // utf8 does not use shifting
108                 to_next = to;
109                 return base::noconv;
110         }
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
116         {
117                 size_t inbytesleft = (from_end - from) * sizeof(extern_type);
118                 size_t outbytesleft = (to_end - to) * sizeof(intern_type);
119                 from_next = from;
120                 to_next = to;
121                 return do_iconv(in_cd_, &from_next, &inbytesleft,
122                                 reinterpret_cast<char **>(&to_next),
123                                 &outbytesleft);
124         }
125         virtual int do_encoding() const throw()
126         {
127                 return 0;
128         }
129         virtual bool do_always_noconv() const throw()
130         {
131                 return false;
132         }
133         virtual int do_length(state_type & /*state*/, extern_type const * from,
134                         extern_type const * end, size_t max) const
135         {
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.
142 #if 0
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);
148                 delete[] to;
149                 return to_next - to;
150 #else
151                 size_t const length = end - from;
152                 return std::min(length, max);
153 #endif
154         }
155         virtual int do_max_length() const throw()
156         {
157                 // UTF8 uses at most 6 bytes to represent one code point
158                 return 6;
159         }
160 private:
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
165         {
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)) {
170                         switch(errno) {
171                         case EINVAL:
172                         case E2BIG:
173                                 return base::partial;
174                         case EILSEQ:
175                         default:
176                                 fprintf(stderr, "Error %d returned from iconv: %s\n",
177                                         errno, strerror(errno));
178                                 fflush(stderr);
179                                 return base::error;
180                         }
181                 }
182                 if (*to == to_start)
183                         return base::noconv;
184                 return base::ok;
185         }
186         iconv_t in_cd_;
187         iconv_t out_cd_;
188 };
189
190 }
191
192
193 namespace lyx {
194
195
196 idocfstream::idocfstream() : base()
197 {
198         std::locale global;
199         std::locale locale(global, new utf8_codecvt_facet(in));
200         imbue(locale);
201 }
202
203         
204 idocfstream::idocfstream(const char* s, std::ios_base::openmode mode)
205         : base()
206 {
207         // We must imbue the stream before openening the file
208         std::locale global;
209         std::locale locale(global, new utf8_codecvt_facet(in));
210         imbue(locale);
211         open(s, mode);
212 }
213
214
215 odocfstream::odocfstream() : base()
216 {
217         std::locale global;
218         std::locale locale(global, new utf8_codecvt_facet(out));
219         imbue(locale);
220 }
221
222         
223 odocfstream::odocfstream(const char* s, std::ios_base::openmode mode)
224         : base()
225 {
226         // We must imbue the stream before openening the file
227         std::locale global;
228         std::locale locale(global, new utf8_codecvt_facet(out));
229         imbue(locale);
230         open(s, mode);
231 }
232
233 }