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