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