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