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