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