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