]> git.lyx.org Git - lyx.git/blob - src/support/docstring.cpp
tiger support on mac snow leopard, include Qt4 frameworks, smart build script with...
[lyx.git] / src / support / docstring.cpp
1 /**
2  * \file docstring.cpp
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 "support/docstring.h"
14
15 #include "support/lassert.h"
16 #include "support/lstrings.h"
17 #include "support/qstring_helpers.h"
18 #include "support/unicode.h"
19
20 #include <QFile>
21
22 #include <locale>
23 #include <iostream>
24 #include <typeinfo>
25
26 using namespace std;
27
28
29 namespace lyx {
30
31 docstring const from_ascii(char const * ascii)
32 {
33         docstring s;
34         if (int n = strlen(ascii)) {
35                 s.resize(n);
36                 char_type *d = &s[0];
37                 while (--n >= 0) {
38                         d[n] = ascii[n];
39                         LASSERT(static_cast<unsigned char>(ascii[n]) < 0x80, /**/);
40                 }
41         }
42         return s;
43 }
44
45
46 docstring const from_ascii(string const & ascii)
47 {
48         int const len = ascii.length();
49         for (int i = 0; i < len; ++i)
50                 LASSERT(static_cast<unsigned char>(ascii[i]) < 0x80, /**/);
51         return docstring(ascii.begin(), ascii.end());
52 }
53
54
55 string const to_ascii(docstring const & ucs4)
56 {
57         int const len = ucs4.length();
58         string ascii;
59         ascii.resize(len);
60         for (int i = 0; i < len; ++i) {
61                 LASSERT(ucs4[i] < 0x80, /**/);
62                 ascii[i] = static_cast<char>(ucs4[i]);
63         }
64         return ascii;
65 }
66
67
68 IconvProcessor & utf8ToUcs4()
69 {
70         static IconvProcessor iconv(ucs4_codeset, "UTF-8");
71         return iconv;
72 }
73
74
75
76 void utf8_to_ucs4(string const & utf8, docstring & ucs4)
77 {
78         size_t n = utf8.size();
79         // as utf8 is a multi-byte encoding, there would be at most
80         // n characters:
81         ucs4.resize(n);
82         if (n == 0)
83                 return;
84
85         int maxoutsize = n * 4;
86         // basic_string::data() is not recognized by some old gcc version
87         // so we use &(ucs4[0]) instead.
88         char * outbuf = (char *)(&(ucs4[0]));
89         int bytes = utf8ToUcs4().convert(utf8.c_str(), n, outbuf, maxoutsize);
90
91         // adjust to the real converted size
92         ucs4.resize(bytes/4);
93 }
94
95
96 docstring const from_utf8(string const & utf8)
97 {
98         docstring ucs4;
99         utf8_to_ucs4(utf8, ucs4);
100         return ucs4;
101 }
102
103
104 string const to_utf8(docstring const & ucs4)
105 {
106         vector<char> const utf8 = ucs4_to_utf8(ucs4.data(), ucs4.size());
107         return string(utf8.begin(), utf8.end());
108 }
109
110
111 docstring const from_local8bit(string const & s)
112 {
113         return qstring_to_ucs4(QString::fromLocal8Bit(s.data(), s.length()));
114 }
115
116
117 /// Exception thrown by to_local8bit if the string could not be converted
118 class to_local8bit_failure : public bad_cast {
119 public:
120         to_local8bit_failure() throw() : bad_cast() {}
121         virtual ~to_local8bit_failure() throw() {}
122         virtual const char* what() const throw()
123         {
124                 return "A string could not be converted from unicode to the local 8 bit encoding.";
125         }
126 };
127
128
129 string const to_local8bit(docstring const & s)
130 {
131         // This conversion can fail, depending on input.
132         if (s.empty())
133                 return string();
134         QByteArray const local = toqstr(s).toLocal8Bit();
135         if (local.size() == 0)
136                 throw to_local8bit_failure();
137         return string(local.begin(), local.end());
138 }
139
140
141 docstring const from_filesystem8bit(string const & s)
142 {
143         QByteArray const encoded(s.c_str(), s.length());
144         return qstring_to_ucs4(QFile::decodeName(encoded));
145 }
146
147
148 string const to_filesystem8bit(docstring const & s)
149 {
150         QByteArray const encoded = QFile::encodeName(toqstr(s));
151         return string(encoded.begin(), encoded.end());
152 }
153
154
155 string const to_iconv_encoding(docstring const & s, string const & encoding)
156 {
157         std::vector<char> const encoded =
158                 ucs4_to_eightbit(s.data(), s.length(), encoding);
159         return string(encoded.begin(), encoded.end());
160 }
161
162
163 docstring const from_iconv_encoding(string const & s, string const & encoding)
164 {
165         std::vector<char_type> const ucs4 =
166                 eightbit_to_ucs4(s.data(), s.length(), encoding);
167         return docstring(ucs4.begin(), ucs4.end());
168 }
169
170
171 docstring const normalize_c(docstring const & s)
172 {
173         return qstring_to_ucs4(toqstr(s).normalized(QString::NormalizationForm_C));
174 }
175
176
177 bool operator==(lyx::docstring const & l, char const * r)
178 {
179         lyx::docstring::const_iterator it = l.begin();
180         lyx::docstring::const_iterator end = l.end();
181         for (; it != end; ++it, ++r) {
182                 LASSERT(static_cast<unsigned char>(*r) < 0x80, /**/);
183                 if (!*r)
184                         return false;
185                 if (*it != static_cast<lyx::docstring::value_type>(*r))
186                         return false;
187         }
188         return *r == '\0';
189 }
190
191
192 lyx::docstring operator+(lyx::docstring const & l, char const * r)
193 {
194         lyx::docstring s(l);
195         for (char const * c = r; *c; ++c) {
196                 LASSERT(static_cast<unsigned char>(*c) < 0x80, /**/);
197                 s.push_back(*c);
198         }
199         return s;
200 }
201
202
203 lyx::docstring operator+(char const * l, lyx::docstring const & r)
204 {
205         lyx::docstring s;
206         for (char const * c = l; *c; ++c) {
207                 LASSERT(static_cast<unsigned char>(*c) < 0x80, /**/);
208                 s.push_back(*c);
209         }
210         s += r;
211         return s;
212 }
213
214
215 lyx::docstring operator+(lyx::docstring const & l, char r)
216 {
217         LASSERT(static_cast<unsigned char>(r) < 0x80, /**/);
218         docstring s = l;
219         s += docstring::value_type(r);
220         return s;
221 }
222
223
224 lyx::docstring operator+(char l, lyx::docstring const & r)
225 {
226         LASSERT(static_cast<unsigned char>(l) < 0x80, /**/);
227         return lyx::docstring::value_type(l) + r;
228 }
229
230
231 lyx::docstring & operator+=(lyx::docstring & l, char const * r)
232 {
233         for (char const * c = r; *c; ++c) {
234                 LASSERT(static_cast<unsigned char>(*c) < 0x80, /**/);
235                 l.push_back(*c);
236         }
237         return l;
238 }
239
240
241 lyx::docstring & operator+=(lyx::docstring & l, char r)
242 {
243         LASSERT(static_cast<unsigned char>(r) < 0x80, /**/);
244         l.push_back(r);
245         return l;
246 }
247
248 } // namespace lyx
249
250 #if ! defined(USE_WCHAR_T) && defined(__GNUC__)
251
252 // gcc does not have proper locale facets for lyx::char_type if
253 // sizeof(wchar_t) == 2, so we have to implement them on our own.
254
255
256 // We get undefined references to these virtual methods. This looks like
257 // a bug in gcc. The implementation here does not do anything useful, since
258 // it is overriden in ascii_ctype_facet.
259 namespace std {
260 template<> ctype<lyx::char_type>::~ctype() {}
261 template<> bool
262 ctype<lyx::char_type>::do_is(ctype<lyx::char_type>::mask, lyx::char_type) const { return false; }
263 template<> lyx::char_type const *
264 ctype<lyx::char_type>::do_is(const lyx::char_type *, const lyx::char_type *, ctype<lyx::char_type>::mask *) const { return 0; }
265 template<> const lyx::char_type *
266 ctype<lyx::char_type>::do_scan_is(ctype<lyx::char_type>::mask, const lyx::char_type *, const lyx::char_type *) const { return 0; }
267 template<> const lyx::char_type *
268 ctype<lyx::char_type>::do_scan_not(ctype<lyx::char_type>::mask, const lyx::char_type *, const lyx::char_type *) const { return 0; }
269 template<> lyx::char_type ctype<lyx::char_type>::do_toupper(lyx::char_type) const { return 0; }
270 template<> const lyx::char_type * ctype<lyx::char_type>::do_toupper(lyx::char_type *, lyx::char_type const *) const { return 0; }
271 template<> lyx::char_type ctype<lyx::char_type>::do_tolower(lyx::char_type) const { return 0; }
272 template<> const lyx::char_type * ctype<lyx::char_type>::do_tolower(lyx::char_type *, lyx::char_type const *) const { return 0; }
273 template<> lyx::char_type ctype<lyx::char_type>::do_widen(char) const { return 0; }
274 template<> const char *
275 ctype<lyx::char_type>::do_widen(const char *, const char *, lyx::char_type *) const { return 0; }
276 template<> char
277 ctype<lyx::char_type>::do_narrow(const lyx::char_type, char) const { return 0; }
278 template<> const lyx::char_type *
279 ctype<lyx::char_type>::do_narrow(const lyx::char_type *, const lyx::char_type *, char, char *) const { return 0; }
280 }
281
282
283 namespace lyx {
284
285 class ctype_failure : public bad_cast {
286 public:
287         ctype_failure() throw() : bad_cast() {}
288         virtual ~ctype_failure() throw() {}
289         virtual const char* what() const throw()
290         {
291                 return "The ctype<lyx::char_type> locale facet does only support ASCII characters on this platform.";
292         }
293 };
294
295
296 class num_put_failure : public bad_cast {
297 public:
298         num_put_failure() throw() : bad_cast() {}
299         virtual ~num_put_failure() throw() {}
300         virtual const char* what() const throw()
301         {
302                 return "The num_put locale facet does only support ASCII characters on this platform.";
303         }
304 };
305
306
307 /// ctype facet for UCS4 characters. The implementation does only support pure
308 /// ASCII, since we do not need anything else for now.
309 /// The code is partly stolen from ctype<wchar_t> from gcc.
310 class ascii_ctype_facet : public ctype<lyx::char_type>
311 {
312 public:
313         typedef lyx::char_type char_type;
314         typedef wctype_t wmask_type;
315         explicit ascii_ctype_facet(size_t refs = 0) : ctype<char_type>(refs)
316         {
317                 M_initialize_ctype();
318         }
319 protected:
320         bool       M_narrow_ok;
321         char       M_narrow[128];
322         wint_t     M_widen[1 + static_cast<unsigned char>(-1)];
323         mask       M_bit[16];
324         wmask_type M_wmask[16];
325         wmask_type M_convert_to_wmask(const mask m) const
326         {
327                 wmask_type ret;
328                 switch (m) {
329                         case space:  ret = wctype("space");  break;
330                         case print:  ret = wctype("print");  break;
331                         case cntrl:  ret = wctype("cntrl");  break;
332                         case upper:  ret = wctype("upper");  break;
333                         case lower:  ret = wctype("lower");  break;
334                         case alpha:  ret = wctype("alpha");  break;
335                         case digit:  ret = wctype("digit");  break;
336                         case punct:  ret = wctype("punct");  break;
337                         case xdigit: ret = wctype("xdigit"); break;
338                         case alnum:  ret = wctype("alnum");  break;
339                         case graph:  ret = wctype("graph");  break;
340                         default:     ret = wmask_type();
341                 }
342                 return ret;
343         }
344         void M_initialize_ctype()
345         {
346                 wint_t i;
347                 for (i = 0; i < 128; ++i) {
348                         const int c = wctob(i);
349                         if (c == EOF)
350                                 break;
351                         else
352                                 M_narrow[i] = static_cast<char>(c);
353                 }
354                 if (i == 128)
355                         M_narrow_ok = true;
356                 else
357                         M_narrow_ok = false;
358                 for (size_t i = 0; i < sizeof(M_widen) / sizeof(wint_t); ++i)
359                         M_widen[i] = btowc(i);
360
361                 for (size_t i = 0; i <= 15; ++i) {
362                         M_bit[i] = static_cast<mask>(1 << i);
363                         M_wmask[i] = M_convert_to_wmask(M_bit[i]);
364                 }
365         }
366         virtual ~ascii_ctype_facet() {}
367         char_type do_toupper(char_type c) const
368         {
369                 if (c >= 0x80)
370                         throw ctype_failure();
371                 return toupper(static_cast<int>(c));
372         }
373         char_type const * do_toupper(char_type * lo, char_type const * hi) const
374         {
375                 while (lo < hi) {
376                         if (*lo >= 0x80)
377                                 throw ctype_failure();
378                         *lo = toupper(static_cast<int>(*lo));
379                         ++lo;
380                 }
381                 return hi;
382         }
383         char_type do_tolower(char_type c) const
384         {
385                 if (c >= 0x80)
386                         throw ctype_failure();
387                 return tolower(c);
388         }
389         char_type const * do_tolower(char_type * lo, char_type const * hi) const
390         {
391                 while (lo < hi) {
392                         if (*lo >= 0x80)
393                                 throw ctype_failure();
394                         *lo = tolower(*lo);
395                         ++lo;
396                 }
397                 return hi;
398         }
399         bool do_is(mask m, char_type c) const
400         {
401                 if (c >= 0x80)
402                         throw ctype_failure();
403                 // The code below works because c is in the ASCII range.
404                 // We could not use iswctype() which is designed for a 2byte
405                 // whar_t without encoding conversion otherwise.
406                 bool ret = false;
407                 // Generically, 15 (instead of 10) since we don't know the numerical
408                 // encoding of the various categories in /usr/include/ctype.h.
409                 const size_t bitmasksize = 15;
410                 for (size_t bitcur = 0; bitcur <= bitmasksize; ++bitcur)
411                         if (m & M_bit[bitcur] &&
412                             iswctype(static_cast<int>(c), M_wmask[bitcur])) {
413                                 ret = true;
414                                 break;
415                         }
416                 return ret;
417         }
418         char_type const * do_is(char_type const * lo, char_type const * hi, mask * vec) const
419         {
420                 for (;lo < hi; ++vec, ++lo) {
421                         if (*lo >= 0x80)
422                                 throw ctype_failure();
423                         // The code below works because c is in the ASCII range.
424                         // We could not use iswctype() which is designed for a 2byte
425                         // whar_t without encoding conversion otherwise.
426                         // Generically, 15 (instead of 10) since we don't know the numerical
427                         // encoding of the various categories in /usr/include/ctype.h.
428                         const size_t bitmasksize = 15;
429                         mask m = 0;
430                         for (size_t bitcur = 0; bitcur <= bitmasksize; ++bitcur)
431                                 if (iswctype(static_cast<int>(*lo), M_wmask[bitcur]))
432                                         m |= M_bit[bitcur];
433                         *vec = m;
434                 }
435                 return hi;
436         }
437         char_type const * do_scan_is(mask m, char_type const * lo, char_type const * hi) const
438         {
439                 while (lo < hi && !this->do_is(m, *lo))
440                         ++lo;
441                 return lo;
442         }
443         char_type const * do_scan_not(mask m, char_type const * lo, char_type const * hi) const
444         {
445                 while (lo < hi && this->do_is(m, *lo) != 0)
446                         ++lo;
447                 return lo;
448         }
449         char_type do_widen(char c) const
450         {
451                 if (static_cast<unsigned char>(c) < 0x80)
452                         return c;
453                 throw ctype_failure();
454         }
455         const char* do_widen(const char* lo, const char* hi, char_type* dest) const
456         {
457                 while (lo < hi) {
458                         if (static_cast<unsigned char>(*lo) >= 0x80)
459                                 throw ctype_failure();
460                         *dest = *lo;
461                         ++lo;
462                         ++dest;
463                 }
464                 return hi;
465         }
466         char do_narrow(char_type wc, char) const
467         {
468                 if (wc < 0x80)
469                         return static_cast<char>(wc);
470                 throw ctype_failure();
471         }
472         const char_type * do_narrow(const char_type * lo, const char_type * hi, char, char * dest) const
473         {
474                 while (lo < hi) {
475                         if (*lo < 0x80)
476                                 *dest = static_cast<char>(*lo);
477                         else
478                                 throw ctype_failure();
479                         ++lo;
480                         ++dest;
481                 }
482                 return hi;
483         }
484 };
485
486
487 /// Facet for outputting numbers to odocstreams as ascii.
488 /// Here we simply need defining the virtual do_put functions.
489 class ascii_num_put_facet : public num_put<lyx::char_type, ostreambuf_iterator<lyx::char_type, char_traits<lyx::char_type> > >
490 {
491         typedef ostreambuf_iterator<lyx::char_type, char_traits<lyx::char_type> > iter_type;
492 public:
493         ascii_num_put_facet(size_t refs = 0) : num_put<lyx::char_type, iter_type>(refs) {}
494
495         /// Facet for converting numbers to ascii strings.
496         class string_num_put_facet : public num_put<char, basic_string<char>::iterator>
497         {
498         public:
499                 string_num_put_facet() : num_put<char, basic_string<char>::iterator>(1) {}
500         };
501
502 protected:
503         iter_type
504         do_put(iter_type oit, ios_base & b, char_type fill, bool v) const
505         {
506                 return do_put_helper(oit, b, fill, v);
507         }
508
509         iter_type
510         do_put(iter_type oit, ios_base & b, char_type fill, long v) const
511         {
512                 return do_put_helper(oit, b, fill, v);
513         }
514
515         iter_type
516         do_put(iter_type oit, ios_base & b, char_type fill, unsigned long v) const
517         {
518                 return do_put_helper(oit, b, fill, v);
519         }
520
521 #ifdef _GLIBCXX_USE_LONG_LONG
522         iter_type
523         do_put(iter_type oit, ios_base & b, char_type fill, long long v) const
524         {
525                 return do_put_helper(oit, b, fill, v);
526         }
527
528         iter_type
529         do_put(iter_type oit, ios_base & b, char_type fill, unsigned long long v) const
530         {
531                 return do_put_helper(oit, b, fill, v);
532         }
533 #endif
534
535         iter_type
536         do_put(iter_type oit, ios_base & b, char_type fill, double v) const
537         {
538                 return do_put_helper(oit, b, fill, v);
539         }
540
541         iter_type
542         do_put(iter_type oit, ios_base & b, char_type fill, long double v) const
543         {
544                 return do_put_helper(oit, b, fill, v);
545         }
546
547         iter_type
548         do_put(iter_type oit, ios_base & b, char_type fill, void const * v) const
549         {
550                 return do_put_helper(oit, b, fill, v);
551         }
552
553 private:
554         template <typename ValueType>
555         iter_type
556         do_put_helper(iter_type oit, ios_base & b, char_type fill, ValueType v) const
557         {
558                 if (fill >= 0x80)
559                         throw num_put_failure();
560
561                 streamsize const sz = b.width() > b.precision() ?
562                                            b.width() : b.precision();
563                 // 64 is large enough, unless width or precision are bigger
564                 streamsize const wd = (sz > 56 ? sz : 56) + 8;
565                 string s(wd, '\0');
566                 string_num_put_facet f;
567                 string::const_iterator cit = s.begin();
568                 string::const_iterator end =
569                         f.put(s.begin(), b, fill, v);
570                 for (; cit != end; ++cit, ++oit)
571                         *oit = *cit;
572
573                 return oit;
574         }
575 };
576
577
578 /// Facet for inputting ascii representations of numbers from idocstreams.
579 /// Here we simply need defining the virtual do_get functions.
580 class ascii_num_get_facet : public num_get<lyx::char_type, istreambuf_iterator<lyx::char_type, char_traits<lyx::char_type> > >
581 {
582         typedef istreambuf_iterator<lyx::char_type, char_traits<lyx::char_type> > iter_type;
583 public:
584         ascii_num_get_facet(size_t refs = 0) : num_get<lyx::char_type, iter_type>(refs) {}
585
586         /// Facet for converting ascii representation of numbers to a value.
587         class string_num_get_facet : public num_get<char, basic_string<char>::iterator>
588         {
589         public:
590                 string_num_get_facet() : num_get<char, basic_string<char>::iterator>(1) {}
591         };
592
593         /// Numpunct facet defining the I/O format.
594         class numpunct_facet : public numpunct<char>
595         {
596         public:
597                 numpunct_facet() : numpunct<char>(1) {}
598         };
599
600 protected:
601         iter_type
602         do_get(iter_type iit, iter_type eit, ios_base & b,
603                 ios_base::iostate & err, bool & v) const
604         {
605                 if (b.flags() & ios_base::boolalpha) {
606                         numpunct_facet p;
607                         lyx::docstring const truename = from_local8bit(p.truename());
608                         lyx::docstring const falsename = from_local8bit(p.falsename());
609                         lyx::docstring s;
610                         s.resize(16);
611                         bool ok = true;
612                         size_t n = 0;
613                         size_t const tsize = truename.size();
614                         size_t const fsize = falsename.size();
615                         for (; iit != eit; ++iit) {
616                                 s += *iit;
617                                 ++n;
618                                 bool true_ok = support::prefixIs(truename, s);
619                                 bool false_ok = support::prefixIs(falsename, s);
620                                 if (!true_ok && !false_ok) {
621                                         ++iit;
622                                         ok = false;
623                                         break;
624                                 }
625                                 if ((true_ok && n == tsize) ||
626                                     (false_ok && n == fsize)) {
627                                         ++iit;
628                                         break;
629                                 }
630                         }
631                         if (ok) {
632                                 err = ios_base::goodbit;
633                                 v = truename == s ? true : false;
634                         } else
635                                 err = ios_base::failbit;
636                         if (iit == eit)
637                                 err |= ios_base::eofbit;
638                         return iit;
639                 } else {
640                         long l;
641                         iter_type end = this->do_get(iit, eit, b, err, l);
642                         if (!(err & ios_base::failbit)) {
643                                 if (l == 0)
644                                         v = false;
645                                 else if (l == 1)
646                                         v = true;
647                                 else
648                                         err |= ios_base::failbit;
649                         }
650                         return end;
651                 }
652         }
653
654         iter_type
655         do_get(iter_type iit, iter_type eit, ios_base & b,
656                 ios_base::iostate & err, long & v) const
657         {
658                 return do_get_integer(iit, eit, b, err, v);
659         }
660
661         iter_type
662         do_get(iter_type iit, iter_type eit, ios_base & b,
663                 ios_base::iostate & err, unsigned short & v) const
664         {
665                 return do_get_integer(iit, eit, b, err, v);
666         }
667
668         iter_type
669         do_get(iter_type iit, iter_type eit, ios_base & b,
670                 ios_base::iostate & err, unsigned int & v) const
671         {
672                 return do_get_integer(iit, eit, b, err, v);
673         }
674
675         iter_type
676         do_get(iter_type iit, iter_type eit, ios_base & b,
677                 ios_base::iostate & err, unsigned long & v) const
678         {
679                 return do_get_integer(iit, eit, b, err, v);
680         }
681
682 #ifdef _GLIBCXX_USE_LONG_LONG
683         iter_type
684         do_get(iter_type iit, iter_type eit, ios_base & b,
685                 ios_base::iostate & err, long long & v) const
686         {
687                 return do_get_integer(iit, eit, b, err, v);
688         }
689
690         iter_type
691         do_get(iter_type iit, iter_type eit, ios_base & b,
692                 ios_base::iostate & err, unsigned long long & v) const
693         {
694                 return do_get_integer(iit, eit, b, err, v);
695         }
696 #endif
697
698         iter_type
699         do_get(iter_type iit, iter_type eit, ios_base & b,
700                 ios_base::iostate & err, float & v) const
701         {
702                 return do_get_float(iit, eit, b, err, v);
703         }
704
705         iter_type
706         do_get(iter_type iit, iter_type eit, ios_base & b,
707                 ios_base::iostate & err, double & v) const
708         {
709                 return do_get_float(iit, eit, b, err, v);
710         }
711
712         iter_type
713         do_get(iter_type iit, iter_type eit, ios_base & b,
714                 ios_base::iostate & err, long double & v) const
715         {
716                 return do_get_float(iit, eit, b, err, v);
717         }
718
719         iter_type
720         do_get(iter_type iit, iter_type eit, ios_base & b,
721                 ios_base::iostate & err, void * & v) const
722         {
723                 unsigned long val;
724                 iter_type end = do_get_integer(iit, eit, b, err, val);
725                 if (!(err & ios_base::failbit))
726                         v = reinterpret_cast<void *>(val);
727                 return end;
728         }
729
730 private:
731         template <typename ValueType>
732         iter_type
733         do_get_integer(iter_type iit, iter_type eit, ios_base & b,
734                         ios_base::iostate & err, ValueType & v) const
735         {
736                 string s;
737                 s.reserve(64);
738                 for (; iit != eit && isNumpunct(*iit); ++iit)
739                         s += static_cast<char>(*iit);
740                 // We add another character, not part of the numpunct facet,
741                 // in order to avoid setting the eofbit in the stream state,
742                 // which would prevent any further read. The space seems a
743                 // good choice here.
744                 s += ' ';
745                 string_num_get_facet f;
746                 f.get(s.begin(), s.end(), b, err, v);
747                 if (iit == eit)
748                     err |= ios_base::eofbit;
749
750                 return iit;
751         }
752
753         bool isNumpunct(lyx::char_type const c) const
754         {
755                 /// Only account for the standard numpunct "C" locale facet.
756                 return c < 0x80 && (c == '-' || c == '+' || isdigit(c)
757                         || ('a' <= c && c <= 'f') || ('A' <= c && c <= 'F')
758                         || c == 'x' || c == 'X');
759         }
760
761         template <typename ValueType>
762         iter_type
763         do_get_float(iter_type iit, iter_type eit, ios_base & b,
764                         ios_base::iostate & err, ValueType & v) const
765         {
766                 // Gather a string of the form
767                 // [+-]? [0-9]* .? [0-9]* ([eE] [+-]? [0-9]+)?
768                 string s;
769                 s.reserve(64);
770                 char c;
771                 numpunct_facet p;
772                 char const dot = p.decimal_point();
773                 char const sep = p.thousands_sep();
774                 // Get an optional sign
775                 if (iit != eit && (*iit == '-' || *iit == '+')) {
776                         s += static_cast<char>(*iit);
777                         ++iit;
778                 }
779                 for (; iit != eit && isDigitOrSep(*iit, sep); ++iit)
780                         s += static_cast<char>(*iit);
781                 if (iit != eit && *iit == dot) {
782                         s += dot;
783                         ++iit;
784                         for (; iit != eit && isDigitOrSep(*iit, 0); ++iit)
785                                 s += static_cast<char>(*iit);
786                         if (iit != eit && (*iit == 'e' || *iit == 'E')) {
787                                 s += static_cast<char>(*iit);
788                                 ++iit;
789                                 for (; iit != eit && isDigitOrSep(*iit, 0); ++iit)
790                                         s += static_cast<char>(*iit);
791                         }
792                 }
793                 s += '\n';
794                 string_num_get_facet f;
795                 f.get(s.begin(), s.end(), b, err, v);
796                 if (iit == eit)
797                     err |= ios_base::eofbit;
798
799                 return iit;
800         }
801
802         bool isDigitOrSep(lyx::char_type const c, char const sep) const
803         {
804                 return (c >= '0' && c <= '9') || (c != 0 && c == sep);
805         }
806 };
807
808
809 /// class to add our facets to the global locale
810 class locale_initializer {
811 public:
812         locale_initializer()
813         {
814                 locale global;
815                 locale const loc1(global, new ascii_ctype_facet);
816                 locale const loc2(loc1, new ascii_num_put_facet);
817                 locale const loc3(loc2, new ascii_num_get_facet);
818                 locale::global(loc3);
819         }
820 };
821
822
823 namespace {
824
825 /// make sure that our facets get used
826 static locale_initializer initializer;
827
828 }
829 }
830 #endif