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