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