]> git.lyx.org Git - lyx.git/blob - src/support/docstring.C
Make it possible to uses non-ascii labelstring, endlabelstring and
[lyx.git] / src / support / docstring.C
1 /**
2  * \file docstring.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 "docstring.h"
14 #include "unicode.h"
15
16 #include <locale>
17 #include <iostream>
18
19 #include <boost/assert.hpp>
20
21
22 namespace lyx {
23
24
25 docstring const from_ascii(char const * ascii)
26 {
27         docstring s;
28         for (char const * c = ascii; *c; ++c) {
29                 BOOST_ASSERT(static_cast<unsigned char>(*c) < 0x80);
30                 s.push_back(*c);
31         }
32         return s;
33 }
34
35
36 docstring const from_ascii(std::string const & ascii)
37 {
38         int const len = ascii.length();
39         for (int i = 0; i < len; ++i)
40                 BOOST_ASSERT(static_cast<unsigned char>(ascii[i]) < 0x80);
41         return docstring(ascii.begin(), ascii.end());
42 }
43
44
45 std::string const to_ascii(docstring const & ucs4)
46 {
47         int const len = ucs4.length();
48         std::string ascii;
49         ascii.resize(len);
50         for (int i = 0; i < len; ++i) {
51                 BOOST_ASSERT(ucs4[i] < 0x80);
52                 ascii[i] = static_cast<char>(ucs4[i]);
53         }
54         return ascii;
55 }
56
57
58 void utf8_to_ucs4(std::string const & utf8, docstring & ucs4)
59 {
60         static IconvProcessor iconv(ucs4_codeset, "UTF-8");
61
62         size_t n = utf8.size();
63         // as utf8 is a multi-byte encoding, there would be at most
64         // n characters:
65         ucs4.resize(n);
66         if (n == 0)
67                 return;
68
69         int maxoutsize = n * 4;
70         // basic_string::data() is not recognized by some old gcc version
71         // so we use &(ucs4[0]) instead.
72         char * outbuf = (char *)(&(ucs4[0]));
73         int bytes = iconv.convert(utf8.c_str(), n, outbuf, maxoutsize);
74
75         // adjust to the real converted size
76         ucs4.resize(bytes/4);
77 }
78
79
80 docstring const from_utf8(std::string const & utf8)
81 {
82         docstring ucs4;
83         utf8_to_ucs4(utf8, ucs4);
84         return ucs4;
85 }
86
87
88 std::string const to_utf8(docstring const & ucs4)
89 {
90         std::vector<char> const utf8 =
91                 ucs4_to_utf8(ucs4.data(), ucs4.size());
92         return std::string(utf8.begin(), utf8.end());
93 }
94
95
96 bool operator==(lyx::docstring const & l, char const * r)
97 {
98         int const len = l.length();
99         for (int i = 0; i < len; ++i) {
100                 BOOST_ASSERT(static_cast<unsigned char>(r[i]) < 0x80);
101                 if (!r[i])
102                         return false;
103                 if (l[i] != lyx::docstring::value_type(r[i]))
104                         return false;
105         }
106         return r[len] == '\0';
107 }
108
109
110 lyx::docstring operator+(lyx::docstring const & l, char const * r)
111 {
112         lyx::docstring s(l);
113         for (char const * c = r; *c; ++c) {
114                 BOOST_ASSERT(static_cast<unsigned char>(*c) < 0x80);
115                 s.push_back(*c);
116         }
117         return s;
118 }
119
120
121 lyx::docstring operator+(char const * l, lyx::docstring const & r)
122 {
123         lyx::docstring s;
124         for (char const * c = l; *c; ++c) {
125                 BOOST_ASSERT(static_cast<unsigned char>(*c) < 0x80);
126                 s.push_back(*c);
127         }
128         s += r;
129         return s;
130 }
131
132
133 lyx::docstring operator+(lyx::docstring const & l, char r)
134 {
135         BOOST_ASSERT(static_cast<unsigned char>(r) < 0x80);
136         return l + lyx::docstring::value_type(r);
137 }
138
139
140 lyx::docstring operator+(char l, lyx::docstring const & r)
141 {
142         BOOST_ASSERT(static_cast<unsigned char>(l) < 0x80);
143         return lyx::docstring::value_type(l) + r;
144 }
145
146
147 lyx::docstring & operator+=(lyx::docstring & l, char const * r)
148 {
149         for (char const * c = r; *c; ++c) {
150                 BOOST_ASSERT(static_cast<unsigned char>(*c) < 0x80);
151                 l.push_back(*c);
152         }
153         return l;
154 }
155
156
157 lyx::docstring & operator+=(lyx::docstring & l, char r)
158 {
159         BOOST_ASSERT(static_cast<unsigned char>(r) < 0x80);
160         l.push_back(r);
161         return l;
162 }
163
164 } // namespace lyx
165
166 #if (!defined(HAVE_WCHAR_T) || SIZEOF_WCHAR_T != 4) && defined(__GNUC__)
167
168 // gcc does not have proper locale facets for lyx::char_type if
169 // sizeof(wchar_t) == 2, so we have to implement them on our own.
170
171
172 // We get undefined references to these virtual methods. This looks like
173 // a bug in gcc. The implementation here does not do anything useful, since
174 // it is overriden in ascii_ctype_facet.
175 namespace std {
176 template<> ctype<lyx::char_type>::~ctype() {}
177 template<> bool
178 ctype<lyx::char_type>::do_is(ctype<lyx::char_type>::mask, lyx::char_type) const { return false; }
179 template<> lyx::char_type const *
180 ctype<lyx::char_type>::do_is(const lyx::char_type *, const lyx::char_type *, ctype<lyx::char_type>::mask *) const { return 0; }
181 template<> const lyx::char_type *
182 ctype<lyx::char_type>::do_scan_is(ctype<lyx::char_type>::mask, const lyx::char_type *, const lyx::char_type *) const { return 0; }
183 template<> const lyx::char_type *
184 ctype<lyx::char_type>::do_scan_not(ctype<lyx::char_type>::mask, const lyx::char_type *, const lyx::char_type *) const { return 0; }
185 template<> lyx::char_type ctype<lyx::char_type>::do_toupper(lyx::char_type) const { return 0; }
186 template<> const lyx::char_type * ctype<lyx::char_type>::do_toupper(lyx::char_type *, lyx::char_type const *) const { return 0; }
187 template<> lyx::char_type ctype<lyx::char_type>::do_tolower(lyx::char_type) const { return 0; }
188 template<> const lyx::char_type * ctype<lyx::char_type>::do_tolower(lyx::char_type *, lyx::char_type const *) const { return 0; }
189 template<> lyx::char_type ctype<lyx::char_type>::do_widen(char) const { return 0; }
190 template<> const char *
191 ctype<lyx::char_type>::do_widen(const char *, const char *, lyx::char_type *) const { return 0; }
192 template<> char
193 ctype<lyx::char_type>::do_narrow(const lyx::char_type, char) const { return 0; }
194 template<> const lyx::char_type *
195 ctype<lyx::char_type>::do_narrow(const lyx::char_type *, const lyx::char_type *, char, char *) const { return 0; }
196 }
197
198
199 namespace lyx {
200
201 class ctype_failure : public std::bad_cast {
202 public:
203         ctype_failure() throw() : std::bad_cast() {}
204         virtual ~ctype_failure() throw() {}
205         virtual const char* what() const throw()
206         {
207                 return "The ctype<lyx::char_type> locale facet does only support ASCII characters on this platform.";
208         }
209 };
210
211
212 class num_put_failure : public std::bad_cast {
213 public:
214         num_put_failure() throw() : std::bad_cast() {}
215         virtual ~num_put_failure() throw() {}
216         virtual const char* what() const throw()
217         {
218                 return "The num_put locale facet does only support ASCII characters on this platform.";
219         }
220 };
221
222
223 /// ctype facet for UCS4 characters. The implementation does only support pure
224 /// ASCII, since we do not need anything else for now.
225 /// The code is partly stolen from std::ctype<wchar_t> from gcc.
226 class ascii_ctype_facet : public std::ctype<lyx::char_type>
227 {
228 public:
229         typedef lyx::char_type char_type;
230         typedef wctype_t wmask_type;
231         explicit ascii_ctype_facet(size_t refs = 0) : std::ctype<char_type>(refs)
232         {
233                 M_initialize_ctype();
234         }
235 protected:
236         bool       M_narrow_ok;
237         char       M_narrow[128];
238         wint_t     M_widen[1 + static_cast<unsigned char>(-1)];
239         mask       M_bit[16];
240         wmask_type M_wmask[16];
241         wmask_type M_convert_to_wmask(const mask m) const
242         {
243                 wmask_type ret;
244                 switch (m) {
245                         case space:  ret = wctype("space");  break;
246                         case print:  ret = wctype("print");  break;
247                         case cntrl:  ret = wctype("cntrl");  break;
248                         case upper:  ret = wctype("upper");  break;
249                         case lower:  ret = wctype("lower");  break;
250                         case alpha:  ret = wctype("alpha");  break;
251                         case digit:  ret = wctype("digit");  break;
252                         case punct:  ret = wctype("punct");  break;
253                         case xdigit: ret = wctype("xdigit"); break;
254                         case alnum:  ret = wctype("alnum");  break;
255                         case graph:  ret = wctype("graph");  break;
256                         default:     ret = wmask_type();
257                 }
258                 return ret;
259         }
260         void M_initialize_ctype()
261         {
262                 wint_t i;
263                 for (i = 0; i < 128; ++i) {
264                         const int c = wctob(i);
265                         if (c == EOF)
266                                 break;
267                         else
268                                 M_narrow[i] = static_cast<char>(c);
269                 }
270                 if (i == 128)
271                         M_narrow_ok = true;
272                 else
273                         M_narrow_ok = false;
274                 for (size_t i = 0; i < sizeof(M_widen) / sizeof(wint_t); ++i)
275                         M_widen[i] = btowc(i);
276
277                 for (size_t i = 0; i <= 15; ++i) {
278                         M_bit[i] = static_cast<mask>(1 << i);
279                         M_wmask[i] = M_convert_to_wmask(M_bit[i]);
280                 }
281         }
282         virtual ~ascii_ctype_facet() {}
283         char_type do_toupper(char_type c) const
284         {
285                 if (c >= 0x80)
286                         throw ctype_failure();
287                 return toupper(static_cast<int>(c));
288         }
289         char_type const * do_toupper(char_type * lo, char_type const * hi) const
290         {
291                 while (lo < hi) {
292                         if (*lo >= 0x80)
293                                 throw ctype_failure();
294                         *lo = toupper(static_cast<int>(*lo));
295                         ++lo;
296                 }
297                 return hi;
298         }
299         char_type do_tolower(char_type c) const
300         {
301                 if (c >= 0x80)
302                         throw ctype_failure();
303                 return tolower(c);
304         }
305         char_type const * do_tolower(char_type * lo, char_type const * hi) const
306         {
307                 while (lo < hi) {
308                         if (*lo >= 0x80)
309                                 throw ctype_failure();
310                         *lo = tolower(*lo);
311                         ++lo;
312                 }
313                 return hi;
314         }
315         bool do_is(mask m, char_type c) const
316         {
317                 if (c >= 0x80)
318                         throw ctype_failure();
319                 // The code below works because c is in the ASCII range.
320                 // We could not use iswctype() which is designed for a 2byte
321                 // whar_t without encoding conversion otherwise.
322                 bool ret = false;
323                 // Generically, 15 (instead of 10) since we don't know the numerical
324                 // encoding of the various categories in /usr/include/ctype.h.
325                 const size_t bitmasksize = 15;
326                 for (size_t bitcur = 0; bitcur <= bitmasksize; ++bitcur)
327                         if (m & M_bit[bitcur] &&
328                             iswctype(static_cast<int>(c), M_wmask[bitcur])) {
329                                 ret = true;
330                                 break;
331                         }
332                 return ret;
333         }
334         char_type const * do_is(char_type const * lo, char_type const * hi, mask * vec) const
335         {
336                 for (;lo < hi; ++vec, ++lo) {
337                         if (*lo >= 0x80)
338                                 throw ctype_failure();
339                         // The code below works because c is in the ASCII range.
340                         // We could not use iswctype() which is designed for a 2byte
341                         // whar_t without encoding conversion otherwise.
342                         // Generically, 15 (instead of 10) since we don't know the numerical
343                         // encoding of the various categories in /usr/include/ctype.h.
344                         const size_t bitmasksize = 15;
345                         mask m = 0;
346                         for (size_t bitcur = 0; bitcur <= bitmasksize; ++bitcur)
347                                 if (iswctype(static_cast<int>(*lo), M_wmask[bitcur]))
348                                         m |= M_bit[bitcur];
349                         *vec = m;
350                 }
351                 return hi;
352         }
353         char_type const * do_scan_is(mask m, char_type const * lo, char_type const * hi) const
354         {
355                 while (lo < hi && !this->do_is(m, *lo))
356                         ++lo;
357                 return lo;
358         }
359         char_type const * do_scan_not(mask m, char_type const * lo, char_type const * hi) const
360         {
361                 while (lo < hi && this->do_is(m, *lo) != 0)
362                         ++lo;
363                 return lo;
364         }
365         char_type do_widen(char c) const
366         {
367                 if (static_cast<unsigned char>(c) < 0x80)
368                         return c;
369                 throw ctype_failure();
370         }
371         const char* do_widen(const char* lo, const char* hi, char_type* dest) const
372         {
373                 while (lo < hi) {
374                         if (static_cast<unsigned char>(*lo) >= 0x80)
375                                 throw ctype_failure();
376                         *dest = *lo;
377                         ++lo;
378                         ++dest;
379                 }
380                 return hi;
381         }
382         char do_narrow(char_type wc, char) const
383         {
384                 if (wc < 0x80)
385                         return static_cast<char>(wc);
386                 throw ctype_failure();
387         }
388         const char_type * do_narrow(const char_type * lo, const char_type * hi, char, char * dest) const
389         {
390                 while (lo < hi) {
391                         if (*lo < 0x80)
392                                 *dest = static_cast<char>(*lo);
393                         else
394                                 throw ctype_failure();
395                         ++lo;
396                         ++dest;
397                 }
398                 return hi;
399         }
400 };
401
402
403 /// Facet for outputting numbers to odocstreams as ascii.
404 /// Here we simply need defining the virtual do_put functions.
405 class ascii_num_put_facet : public std::num_put<lyx::char_type, std::ostreambuf_iterator<lyx::char_type, std::char_traits<lyx::char_type> > >
406 {
407         typedef std::ostreambuf_iterator<lyx::char_type, std::char_traits<lyx::char_type> > iter_type;
408 public:
409         ascii_num_put_facet(size_t refs = 0) : std::num_put<lyx::char_type, iter_type>(refs) {}
410
411         /// Facet for converting numbers to ascii strings.
412         class string_num_put_facet : public std::num_put<char, std::basic_string<char>::iterator>
413         {
414         public:
415                 string_num_put_facet() : std::num_put<char, std::basic_string<char>::iterator>(1) {}
416         };
417
418 protected:
419         iter_type
420         do_put(iter_type oit, std::ios_base & b, char_type fill, long v) const
421         {
422                 if (fill >= 0x80)
423                         throw num_put_failure();
424
425                 std::string s;
426                 // 64 is large enough
427                 s.resize(64);
428                 string_num_put_facet f;
429                 std::string::const_iterator cit = s.begin();
430                 std::string::const_iterator end =
431                         f.put(s.begin(), b, fill, v);
432                 for (; cit != end; ++cit, ++oit)
433                         *oit = *cit;
434
435                 return oit;
436         }
437 };
438
439
440 /// class to add our facets to the global locale
441 class locale_initializer {
442 public:
443         locale_initializer()
444         {
445                 std::locale global;
446                 std::locale const loc1(global, new ascii_ctype_facet);
447                 std::locale const loc2(loc1, new ascii_num_put_facet);
448                 std::locale::global(loc2);
449         }
450 };
451
452
453 namespace {
454
455 /// make sure that our facets get used
456 static locale_initializer initializer;
457
458 }
459 }
460 #endif