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