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