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