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