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