]> git.lyx.org Git - lyx.git/blob - src/support/docstring.C
Provide do_put methods for inserting all remaining basic type values
[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 "qstring_helpers.h"
15 #include "unicode.h"
16
17 #include <locale>
18 #include <iostream>
19
20 #include <boost/assert.hpp>
21
22
23 namespace lyx {
24
25
26 docstring const from_ascii(char const * ascii)
27 {
28         docstring s;
29         for (char const * c = ascii; *c; ++c) {
30                 BOOST_ASSERT(static_cast<unsigned char>(*c) < 0x80);
31                 s.push_back(*c);
32         }
33         return s;
34 }
35
36
37 docstring const from_ascii(std::string const & ascii)
38 {
39         int const len = ascii.length();
40         for (int i = 0; i < len; ++i)
41                 BOOST_ASSERT(static_cast<unsigned char>(ascii[i]) < 0x80);
42         return docstring(ascii.begin(), ascii.end());
43 }
44
45
46 std::string const to_ascii(docstring const & ucs4)
47 {
48         int const len = ucs4.length();
49         std::string ascii;
50         ascii.resize(len);
51         for (int i = 0; i < len; ++i) {
52                 BOOST_ASSERT(ucs4[i] < 0x80);
53                 ascii[i] = static_cast<char>(ucs4[i]);
54         }
55         return ascii;
56 }
57
58
59 void utf8_to_ucs4(std::string const & utf8, docstring & ucs4)
60 {
61         size_t n = utf8.size();
62         // as utf8 is a multi-byte encoding, there would be at most
63         // n characters:
64         ucs4.resize(n);
65         if (n == 0)
66                 return;
67
68         int maxoutsize = n * 4;
69         // basic_string::data() is not recognized by some old gcc version
70         // so we use &(ucs4[0]) instead.
71         char * outbuf = (char *)(&(ucs4[0]));
72         int bytes = utf8ToUcs4().convert(utf8.c_str(), n, outbuf, maxoutsize);
73
74         // adjust to the real converted size
75         ucs4.resize(bytes/4);
76 }
77
78
79 docstring const from_utf8(std::string const & utf8)
80 {
81         docstring ucs4;
82         utf8_to_ucs4(utf8, ucs4);
83         return ucs4;
84 }
85
86
87 std::string const to_utf8(docstring const & ucs4)
88 {
89         std::vector<char> const utf8 =
90                 ucs4_to_utf8(ucs4.data(), ucs4.size());
91         return std::string(utf8.begin(), utf8.end());
92 }
93
94
95 docstring const from_local8bit(std::string const & s)
96 {
97         return qstring_to_ucs4(QString::fromLocal8Bit(s.data(), s.length()));
98 }
99
100
101 const char* to_local8bit_failure::what() const throw()
102 {
103         return "A string could not be converted from unicode to the local 8 bit encoding.";
104 }
105
106
107 std::string const to_local8bit(docstring const & s)
108 {
109         // This conversion can fail, depending on input.
110         if (s.empty())
111                 return std::string();
112         QByteArray const local = toqstr(s).toLocal8Bit();
113         if (local.size() == 0)
114                 throw to_local8bit_failure();
115         return std::string(local.begin(), local.end());
116 }
117
118
119 bool operator==(lyx::docstring const & l, char const * r)
120 {
121         int const len = l.length();
122         for (int i = 0; i < len; ++i) {
123                 BOOST_ASSERT(static_cast<unsigned char>(r[i]) < 0x80);
124                 if (!r[i])
125                         return false;
126                 if (l[i] != lyx::docstring::value_type(r[i]))
127                         return false;
128         }
129         return r[len] == '\0';
130 }
131
132
133 lyx::docstring operator+(lyx::docstring const & l, char const * r)
134 {
135         lyx::docstring s(l);
136         for (char const * c = r; *c; ++c) {
137                 BOOST_ASSERT(static_cast<unsigned char>(*c) < 0x80);
138                 s.push_back(*c);
139         }
140         return s;
141 }
142
143
144 lyx::docstring operator+(char const * l, lyx::docstring const & r)
145 {
146         lyx::docstring s;
147         for (char const * c = l; *c; ++c) {
148                 BOOST_ASSERT(static_cast<unsigned char>(*c) < 0x80);
149                 s.push_back(*c);
150         }
151         s += r;
152         return s;
153 }
154
155
156 lyx::docstring operator+(lyx::docstring const & l, char r)
157 {
158         BOOST_ASSERT(static_cast<unsigned char>(r) < 0x80);
159         return l + lyx::docstring::value_type(r);
160 }
161
162
163 lyx::docstring operator+(char l, lyx::docstring const & r)
164 {
165         BOOST_ASSERT(static_cast<unsigned char>(l) < 0x80);
166         return lyx::docstring::value_type(l) + r;
167 }
168
169
170 lyx::docstring & operator+=(lyx::docstring & l, char const * r)
171 {
172         for (char const * c = r; *c; ++c) {
173                 BOOST_ASSERT(static_cast<unsigned char>(*c) < 0x80);
174                 l.push_back(*c);
175         }
176         return l;
177 }
178
179
180 lyx::docstring & operator+=(lyx::docstring & l, char r)
181 {
182         BOOST_ASSERT(static_cast<unsigned char>(r) < 0x80);
183         l.push_back(r);
184         return l;
185 }
186
187 } // namespace lyx
188
189 #if (!defined(HAVE_WCHAR_T) || SIZEOF_WCHAR_T != 4) && defined(__GNUC__)
190
191 // gcc does not have proper locale facets for lyx::char_type if
192 // sizeof(wchar_t) == 2, so we have to implement them on our own.
193
194
195 // We get undefined references to these virtual methods. This looks like
196 // a bug in gcc. The implementation here does not do anything useful, since
197 // it is overriden in ascii_ctype_facet.
198 namespace std {
199 template<> ctype<lyx::char_type>::~ctype() {}
200 template<> bool
201 ctype<lyx::char_type>::do_is(ctype<lyx::char_type>::mask, lyx::char_type) const { return false; }
202 template<> lyx::char_type const *
203 ctype<lyx::char_type>::do_is(const lyx::char_type *, const lyx::char_type *, ctype<lyx::char_type>::mask *) const { return 0; }
204 template<> const lyx::char_type *
205 ctype<lyx::char_type>::do_scan_is(ctype<lyx::char_type>::mask, const lyx::char_type *, const lyx::char_type *) const { return 0; }
206 template<> const lyx::char_type *
207 ctype<lyx::char_type>::do_scan_not(ctype<lyx::char_type>::mask, const lyx::char_type *, const lyx::char_type *) const { return 0; }
208 template<> lyx::char_type ctype<lyx::char_type>::do_toupper(lyx::char_type) const { return 0; }
209 template<> const lyx::char_type * ctype<lyx::char_type>::do_toupper(lyx::char_type *, lyx::char_type const *) const { return 0; }
210 template<> lyx::char_type ctype<lyx::char_type>::do_tolower(lyx::char_type) const { return 0; }
211 template<> const lyx::char_type * ctype<lyx::char_type>::do_tolower(lyx::char_type *, lyx::char_type const *) const { return 0; }
212 template<> lyx::char_type ctype<lyx::char_type>::do_widen(char) const { return 0; }
213 template<> const char *
214 ctype<lyx::char_type>::do_widen(const char *, const char *, lyx::char_type *) const { return 0; }
215 template<> char
216 ctype<lyx::char_type>::do_narrow(const lyx::char_type, char) const { return 0; }
217 template<> const lyx::char_type *
218 ctype<lyx::char_type>::do_narrow(const lyx::char_type *, const lyx::char_type *, char, char *) const { return 0; }
219 }
220
221
222 namespace lyx {
223
224 class ctype_failure : public std::bad_cast {
225 public:
226         ctype_failure() throw() : std::bad_cast() {}
227         virtual ~ctype_failure() throw() {}
228         virtual const char* what() const throw()
229         {
230                 return "The ctype<lyx::char_type> locale facet does only support ASCII characters on this platform.";
231         }
232 };
233
234
235 class num_put_failure : public std::bad_cast {
236 public:
237         num_put_failure() throw() : std::bad_cast() {}
238         virtual ~num_put_failure() throw() {}
239         virtual const char* what() const throw()
240         {
241                 return "The num_put locale facet does only support ASCII characters on this platform.";
242         }
243 };
244
245
246 /// ctype facet for UCS4 characters. The implementation does only support pure
247 /// ASCII, since we do not need anything else for now.
248 /// The code is partly stolen from std::ctype<wchar_t> from gcc.
249 class ascii_ctype_facet : public std::ctype<lyx::char_type>
250 {
251 public:
252         typedef lyx::char_type char_type;
253         typedef wctype_t wmask_type;
254         explicit ascii_ctype_facet(size_t refs = 0) : std::ctype<char_type>(refs)
255         {
256                 M_initialize_ctype();
257         }
258 protected:
259         bool       M_narrow_ok;
260         char       M_narrow[128];
261         wint_t     M_widen[1 + static_cast<unsigned char>(-1)];
262         mask       M_bit[16];
263         wmask_type M_wmask[16];
264         wmask_type M_convert_to_wmask(const mask m) const
265         {
266                 wmask_type ret;
267                 switch (m) {
268                         case space:  ret = wctype("space");  break;
269                         case print:  ret = wctype("print");  break;
270                         case cntrl:  ret = wctype("cntrl");  break;
271                         case upper:  ret = wctype("upper");  break;
272                         case lower:  ret = wctype("lower");  break;
273                         case alpha:  ret = wctype("alpha");  break;
274                         case digit:  ret = wctype("digit");  break;
275                         case punct:  ret = wctype("punct");  break;
276                         case xdigit: ret = wctype("xdigit"); break;
277                         case alnum:  ret = wctype("alnum");  break;
278                         case graph:  ret = wctype("graph");  break;
279                         default:     ret = wmask_type();
280                 }
281                 return ret;
282         }
283         void M_initialize_ctype()
284         {
285                 wint_t i;
286                 for (i = 0; i < 128; ++i) {
287                         const int c = wctob(i);
288                         if (c == EOF)
289                                 break;
290                         else
291                                 M_narrow[i] = static_cast<char>(c);
292                 }
293                 if (i == 128)
294                         M_narrow_ok = true;
295                 else
296                         M_narrow_ok = false;
297                 for (size_t i = 0; i < sizeof(M_widen) / sizeof(wint_t); ++i)
298                         M_widen[i] = btowc(i);
299
300                 for (size_t i = 0; i <= 15; ++i) {
301                         M_bit[i] = static_cast<mask>(1 << i);
302                         M_wmask[i] = M_convert_to_wmask(M_bit[i]);
303                 }
304         }
305         virtual ~ascii_ctype_facet() {}
306         char_type do_toupper(char_type c) const
307         {
308                 if (c >= 0x80)
309                         throw ctype_failure();
310                 return toupper(static_cast<int>(c));
311         }
312         char_type const * do_toupper(char_type * lo, char_type const * hi) const
313         {
314                 while (lo < hi) {
315                         if (*lo >= 0x80)
316                                 throw ctype_failure();
317                         *lo = toupper(static_cast<int>(*lo));
318                         ++lo;
319                 }
320                 return hi;
321         }
322         char_type do_tolower(char_type c) const
323         {
324                 if (c >= 0x80)
325                         throw ctype_failure();
326                 return tolower(c);
327         }
328         char_type const * do_tolower(char_type * lo, char_type const * hi) const
329         {
330                 while (lo < hi) {
331                         if (*lo >= 0x80)
332                                 throw ctype_failure();
333                         *lo = tolower(*lo);
334                         ++lo;
335                 }
336                 return hi;
337         }
338         bool do_is(mask m, char_type c) const
339         {
340                 if (c >= 0x80)
341                         throw ctype_failure();
342                 // The code below works because c is in the ASCII range.
343                 // We could not use iswctype() which is designed for a 2byte
344                 // whar_t without encoding conversion otherwise.
345                 bool ret = false;
346                 // Generically, 15 (instead of 10) since we don't know the numerical
347                 // encoding of the various categories in /usr/include/ctype.h.
348                 const size_t bitmasksize = 15;
349                 for (size_t bitcur = 0; bitcur <= bitmasksize; ++bitcur)
350                         if (m & M_bit[bitcur] &&
351                             iswctype(static_cast<int>(c), M_wmask[bitcur])) {
352                                 ret = true;
353                                 break;
354                         }
355                 return ret;
356         }
357         char_type const * do_is(char_type const * lo, char_type const * hi, mask * vec) const
358         {
359                 for (;lo < hi; ++vec, ++lo) {
360                         if (*lo >= 0x80)
361                                 throw ctype_failure();
362                         // The code below works because c is in the ASCII range.
363                         // We could not use iswctype() which is designed for a 2byte
364                         // whar_t without encoding conversion otherwise.
365                         // Generically, 15 (instead of 10) since we don't know the numerical
366                         // encoding of the various categories in /usr/include/ctype.h.
367                         const size_t bitmasksize = 15;
368                         mask m = 0;
369                         for (size_t bitcur = 0; bitcur <= bitmasksize; ++bitcur)
370                                 if (iswctype(static_cast<int>(*lo), M_wmask[bitcur]))
371                                         m |= M_bit[bitcur];
372                         *vec = m;
373                 }
374                 return hi;
375         }
376         char_type const * do_scan_is(mask m, char_type const * lo, char_type const * hi) const
377         {
378                 while (lo < hi && !this->do_is(m, *lo))
379                         ++lo;
380                 return lo;
381         }
382         char_type const * do_scan_not(mask m, char_type const * lo, char_type const * hi) const
383         {
384                 while (lo < hi && this->do_is(m, *lo) != 0)
385                         ++lo;
386                 return lo;
387         }
388         char_type do_widen(char c) const
389         {
390                 if (static_cast<unsigned char>(c) < 0x80)
391                         return c;
392                 throw ctype_failure();
393         }
394         const char* do_widen(const char* lo, const char* hi, char_type* dest) const
395         {
396                 while (lo < hi) {
397                         if (static_cast<unsigned char>(*lo) >= 0x80)
398                                 throw ctype_failure();
399                         *dest = *lo;
400                         ++lo;
401                         ++dest;
402                 }
403                 return hi;
404         }
405         char do_narrow(char_type wc, char) const
406         {
407                 if (wc < 0x80)
408                         return static_cast<char>(wc);
409                 throw ctype_failure();
410         }
411         const char_type * do_narrow(const char_type * lo, const char_type * hi, char, char * dest) const
412         {
413                 while (lo < hi) {
414                         if (*lo < 0x80)
415                                 *dest = static_cast<char>(*lo);
416                         else
417                                 throw ctype_failure();
418                         ++lo;
419                         ++dest;
420                 }
421                 return hi;
422         }
423 };
424
425
426 /// Facet for outputting numbers to odocstreams as ascii.
427 /// Here we simply need defining the virtual do_put functions.
428 class ascii_num_put_facet : public std::num_put<lyx::char_type, std::ostreambuf_iterator<lyx::char_type, std::char_traits<lyx::char_type> > >
429 {
430         typedef std::ostreambuf_iterator<lyx::char_type, std::char_traits<lyx::char_type> > iter_type;
431 public:
432         ascii_num_put_facet(size_t refs = 0) : std::num_put<lyx::char_type, iter_type>(refs) {}
433
434         /// Facet for converting numbers to ascii strings.
435         class string_num_put_facet : public std::num_put<char, std::basic_string<char>::iterator>
436         {
437         public:
438                 string_num_put_facet() : std::num_put<char, std::basic_string<char>::iterator>(1) {}
439         };
440
441 protected:
442         iter_type
443         do_put(iter_type oit, std::ios_base & b, char_type fill, bool v) const
444         {
445                 return do_put_helper(oit, b, fill, v);
446         }
447
448         iter_type
449         do_put(iter_type oit, std::ios_base & b, char_type fill, long v) const
450         {
451                 return do_put_helper(oit, b, fill, v);
452         }
453
454         iter_type
455         do_put(iter_type oit, std::ios_base & b, char_type fill, unsigned long v) const
456         {
457                 return do_put_helper(oit, b, fill, v);
458         }
459
460 #ifdef _GLIBCXX_USE_LONG_LONG
461         iter_type
462         do_put(iter_type oit, std::ios_base & b, char_type fill, long long v) const
463         {
464                 return do_put_helper(oit, b, fill, v);
465         }
466
467         iter_type
468         do_put(iter_type oit, std::ios_base & b, char_type fill, unsigned long long v) const
469         {
470                 return do_put_helper(oit, b, fill, v);
471         }
472 #endif
473
474         iter_type
475         do_put(iter_type oit, std::ios_base & b, char_type fill, double v) const
476         {
477                 return do_put_helper(oit, b, fill, v);
478         }
479
480         iter_type
481         do_put(iter_type oit, std::ios_base & b, char_type fill, long double v) const
482         {
483                 return do_put_helper(oit, b, fill, v);
484         }
485
486         iter_type
487         do_put(iter_type oit, std::ios_base & b, char_type fill, void const * v) const
488         {
489                 return do_put_helper(oit, b, fill, v);
490         }
491
492 private:
493         template <typename ValueType>
494         iter_type
495         do_put_helper(iter_type oit, std::ios_base & b, char_type fill, ValueType v) const
496         {
497                 if (fill >= 0x80)
498                         throw num_put_failure();
499
500                 // 64 is large enough
501                 std::string s(64, '\0');
502                 string_num_put_facet f;
503                 std::string::const_iterator cit = s.begin();
504                 std::string::const_iterator end =
505                         f.put(s.begin(), b, fill, v);
506                 for (; cit != end; ++cit, ++oit)
507                         *oit = *cit;
508
509                 return oit;
510         }
511 };
512
513
514 /// Facet for inputting ascii representations of numbers from idocstreams.
515 /// Here we simply need defining the virtual do_get functions.
516 class ascii_num_get_facet : public std::num_get<lyx::char_type, std::istreambuf_iterator<lyx::char_type, std::char_traits<lyx::char_type> > >
517 {
518         typedef std::istreambuf_iterator<lyx::char_type, std::char_traits<lyx::char_type> > iter_type;
519 public:
520         ascii_num_get_facet(size_t refs = 0) : std::num_get<lyx::char_type, iter_type>(refs) {}
521
522         /// Facet for converting ascii representation of numbers to a value.
523         class string_num_get_facet : public std::num_get<char, std::basic_string<char>::iterator>
524         {
525         public:
526                 string_num_get_facet() : std::num_get<char, std::basic_string<char>::iterator>(1) {}
527         };
528
529 private:
530         bool isNumpunct(lyx::char_type const c) const
531         {
532                 /// Only account for the standard numpunct "C" locale facet.
533                 return c < 0x80 && (c == '-' || c == '+' || isdigit(c)
534                         || ('a' <= c && c <= 'f') || ('A' <= c && c <= 'F')
535                         || c == 'x' || c == 'X');
536         }
537
538 protected:
539         iter_type
540         do_get(iter_type iit, iter_type eit, std::ios_base & b,
541                 std::ios_base::iostate & err, long & v) const
542         {
543                 std::string s;
544                 s.reserve(64);
545                 for (; iit != eit && isNumpunct(*iit); ++iit)
546                         s += static_cast<char>(*iit);
547                 // We add another character, not part of the numpunct facet,
548                 // in order to avoid setting the eofbit in the stream state,
549                 // which would prevent any further read. The space seems a
550                 // good choice here.
551                 s += ' ';
552                 string_num_get_facet f;
553                 f.get(s.begin(), s.end(), b, err, v);
554
555                 return iit;
556         }
557 };
558
559
560 /// class to add our facets to the global locale
561 class locale_initializer {
562 public:
563         locale_initializer()
564         {
565                 std::locale global;
566                 std::locale const loc1(global, new ascii_ctype_facet);
567                 std::locale const loc2(loc1, new ascii_num_put_facet);
568                 std::locale const loc3(loc2, new ascii_num_get_facet);
569                 std::locale::global(loc3);
570         }
571 };
572
573
574 namespace {
575
576 /// make sure that our facets get used
577 static locale_initializer initializer;
578
579 }
580 }
581 #endif