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