3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "support/docstring.h"
15 #include "support/lstrings.h"
16 #include "support/qstring_helpers.h"
17 #include "support/unicode.h"
24 #include <boost/assert.hpp>
31 docstring const from_ascii(char const * ascii)
34 for (char const * c = ascii; *c; ++c) {
35 BOOST_ASSERT(static_cast<unsigned char>(*c) < 0x80);
42 docstring const from_ascii(string const & ascii)
44 int const len = ascii.length();
45 for (int i = 0; i < len; ++i)
46 BOOST_ASSERT(static_cast<unsigned char>(ascii[i]) < 0x80);
47 return docstring(ascii.begin(), ascii.end());
51 string const to_ascii(docstring const & ucs4)
53 int const len = ucs4.length();
56 for (int i = 0; i < len; ++i) {
57 BOOST_ASSERT(ucs4[i] < 0x80);
58 ascii[i] = static_cast<char>(ucs4[i]);
64 IconvProcessor & utf8ToUcs4()
66 static IconvProcessor iconv(ucs4_codeset, "UTF-8");
72 void utf8_to_ucs4(string const & utf8, docstring & ucs4)
74 size_t n = utf8.size();
75 // as utf8 is a multi-byte encoding, there would be at most
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);
87 // adjust to the real converted size
92 docstring const from_utf8(string const & utf8)
95 utf8_to_ucs4(utf8, ucs4);
100 string const to_utf8(docstring const & ucs4)
102 vector<char> const utf8 =
103 ucs4_to_utf8(ucs4.data(), ucs4.size());
104 return string(utf8.begin(), utf8.end());
108 docstring const from_local8bit(string const & s)
110 return qstring_to_ucs4(QString::fromLocal8Bit(s.data(), s.length()));
114 /// Exception thrown by to_local8bit if the string could not be converted
115 class to_local8bit_failure : public bad_cast {
117 to_local8bit_failure() throw() : bad_cast() {}
118 virtual ~to_local8bit_failure() throw() {}
119 virtual const char* what() const throw()
121 return "A string could not be converted from unicode to the local 8 bit encoding.";
126 string const to_local8bit(docstring const & s)
128 // This conversion can fail, depending on input.
131 QByteArray const local = toqstr(s).toLocal8Bit();
132 if (local.size() == 0)
133 throw to_local8bit_failure();
134 return string(local.begin(), local.end());
138 docstring const from_filesystem8bit(string const & s)
140 QByteArray const encoded(s.c_str(), s.length());
141 return qstring_to_ucs4(QFile::decodeName(encoded));
145 string const to_filesystem8bit(docstring const & s)
147 QByteArray const encoded = QFile::encodeName(toqstr(s));
148 return string(encoded.begin(), encoded.end());
152 docstring const normalize_c(docstring const & s)
154 return qstring_to_ucs4(toqstr(s).normalized(QString::NormalizationForm_C));
158 bool operator==(lyx::docstring const & l, char const * r)
160 lyx::docstring::const_iterator it = l.begin();
161 lyx::docstring::const_iterator end = l.end();
162 for (; it != end; ++it, ++r) {
163 BOOST_ASSERT(static_cast<unsigned char>(*r) < 0x80);
166 if (*it != static_cast<lyx::docstring::value_type>(*r))
173 lyx::docstring operator+(lyx::docstring const & l, char const * r)
176 for (char const * c = r; *c; ++c) {
177 BOOST_ASSERT(static_cast<unsigned char>(*c) < 0x80);
184 lyx::docstring operator+(char const * l, lyx::docstring const & r)
187 for (char const * c = l; *c; ++c) {
188 BOOST_ASSERT(static_cast<unsigned char>(*c) < 0x80);
196 lyx::docstring operator+(lyx::docstring const & l, char r)
198 BOOST_ASSERT(static_cast<unsigned char>(r) < 0x80);
200 s += docstring::value_type(r);
205 lyx::docstring operator+(char l, lyx::docstring const & r)
207 BOOST_ASSERT(static_cast<unsigned char>(l) < 0x80);
208 return lyx::docstring::value_type(l) + r;
212 lyx::docstring & operator+=(lyx::docstring & l, char const * r)
214 for (char const * c = r; *c; ++c) {
215 BOOST_ASSERT(static_cast<unsigned char>(*c) < 0x80);
222 lyx::docstring & operator+=(lyx::docstring & l, char r)
224 BOOST_ASSERT(static_cast<unsigned char>(r) < 0x80);
231 #if ! defined(USE_WCHAR_T) && defined(__GNUC__)
233 // gcc does not have proper locale facets for lyx::char_type if
234 // sizeof(wchar_t) == 2, so we have to implement them on our own.
237 // We get undefined references to these virtual methods. This looks like
238 // a bug in gcc. The implementation here does not do anything useful, since
239 // it is overriden in ascii_ctype_facet.
241 template<> ctype<lyx::char_type>::~ctype() {}
243 ctype<lyx::char_type>::do_is(ctype<lyx::char_type>::mask, lyx::char_type) const { return false; }
244 template<> lyx::char_type const *
245 ctype<lyx::char_type>::do_is(const lyx::char_type *, const lyx::char_type *, ctype<lyx::char_type>::mask *) const { return 0; }
246 template<> const lyx::char_type *
247 ctype<lyx::char_type>::do_scan_is(ctype<lyx::char_type>::mask, const lyx::char_type *, const lyx::char_type *) const { return 0; }
248 template<> const lyx::char_type *
249 ctype<lyx::char_type>::do_scan_not(ctype<lyx::char_type>::mask, const lyx::char_type *, const lyx::char_type *) const { return 0; }
250 template<> lyx::char_type ctype<lyx::char_type>::do_toupper(lyx::char_type) const { return 0; }
251 template<> const lyx::char_type * ctype<lyx::char_type>::do_toupper(lyx::char_type *, lyx::char_type const *) const { return 0; }
252 template<> lyx::char_type ctype<lyx::char_type>::do_tolower(lyx::char_type) const { return 0; }
253 template<> const lyx::char_type * ctype<lyx::char_type>::do_tolower(lyx::char_type *, lyx::char_type const *) const { return 0; }
254 template<> lyx::char_type ctype<lyx::char_type>::do_widen(char) const { return 0; }
255 template<> const char *
256 ctype<lyx::char_type>::do_widen(const char *, const char *, lyx::char_type *) const { return 0; }
258 ctype<lyx::char_type>::do_narrow(const lyx::char_type, char) const { return 0; }
259 template<> const lyx::char_type *
260 ctype<lyx::char_type>::do_narrow(const lyx::char_type *, const lyx::char_type *, char, char *) const { return 0; }
266 class ctype_failure : public bad_cast {
268 ctype_failure() throw() : bad_cast() {}
269 virtual ~ctype_failure() throw() {}
270 virtual const char* what() const throw()
272 return "The ctype<lyx::char_type> locale facet does only support ASCII characters on this platform.";
277 class num_put_failure : public bad_cast {
279 num_put_failure() throw() : bad_cast() {}
280 virtual ~num_put_failure() throw() {}
281 virtual const char* what() const throw()
283 return "The num_put locale facet does only support ASCII characters on this platform.";
288 /// ctype facet for UCS4 characters. The implementation does only support pure
289 /// ASCII, since we do not need anything else for now.
290 /// The code is partly stolen from ctype<wchar_t> from gcc.
291 class ascii_ctype_facet : public ctype<lyx::char_type>
294 typedef lyx::char_type char_type;
295 typedef wctype_t wmask_type;
296 explicit ascii_ctype_facet(size_t refs = 0) : ctype<char_type>(refs)
298 M_initialize_ctype();
303 wint_t M_widen[1 + static_cast<unsigned char>(-1)];
305 wmask_type M_wmask[16];
306 wmask_type M_convert_to_wmask(const mask m) const
310 case space: ret = wctype("space"); break;
311 case print: ret = wctype("print"); break;
312 case cntrl: ret = wctype("cntrl"); break;
313 case upper: ret = wctype("upper"); break;
314 case lower: ret = wctype("lower"); break;
315 case alpha: ret = wctype("alpha"); break;
316 case digit: ret = wctype("digit"); break;
317 case punct: ret = wctype("punct"); break;
318 case xdigit: ret = wctype("xdigit"); break;
319 case alnum: ret = wctype("alnum"); break;
320 case graph: ret = wctype("graph"); break;
321 default: ret = wmask_type();
325 void M_initialize_ctype()
328 for (i = 0; i < 128; ++i) {
329 const int c = wctob(i);
333 M_narrow[i] = static_cast<char>(c);
339 for (size_t i = 0; i < sizeof(M_widen) / sizeof(wint_t); ++i)
340 M_widen[i] = btowc(i);
342 for (size_t i = 0; i <= 15; ++i) {
343 M_bit[i] = static_cast<mask>(1 << i);
344 M_wmask[i] = M_convert_to_wmask(M_bit[i]);
347 virtual ~ascii_ctype_facet() {}
348 char_type do_toupper(char_type c) const
351 throw ctype_failure();
352 return toupper(static_cast<int>(c));
354 char_type const * do_toupper(char_type * lo, char_type const * hi) const
358 throw ctype_failure();
359 *lo = toupper(static_cast<int>(*lo));
364 char_type do_tolower(char_type c) const
367 throw ctype_failure();
370 char_type const * do_tolower(char_type * lo, char_type const * hi) const
374 throw ctype_failure();
380 bool do_is(mask m, char_type c) const
383 throw ctype_failure();
384 // The code below works because c is in the ASCII range.
385 // We could not use iswctype() which is designed for a 2byte
386 // whar_t without encoding conversion otherwise.
388 // Generically, 15 (instead of 10) since we don't know the numerical
389 // encoding of the various categories in /usr/include/ctype.h.
390 const size_t bitmasksize = 15;
391 for (size_t bitcur = 0; bitcur <= bitmasksize; ++bitcur)
392 if (m & M_bit[bitcur] &&
393 iswctype(static_cast<int>(c), M_wmask[bitcur])) {
399 char_type const * do_is(char_type const * lo, char_type const * hi, mask * vec) const
401 for (;lo < hi; ++vec, ++lo) {
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 // Generically, 15 (instead of 10) since we don't know the numerical
408 // encoding of the various categories in /usr/include/ctype.h.
409 const size_t bitmasksize = 15;
411 for (size_t bitcur = 0; bitcur <= bitmasksize; ++bitcur)
412 if (iswctype(static_cast<int>(*lo), M_wmask[bitcur]))
418 char_type const * do_scan_is(mask m, char_type const * lo, char_type const * hi) const
420 while (lo < hi && !this->do_is(m, *lo))
424 char_type const * do_scan_not(mask m, char_type const * lo, char_type const * hi) const
426 while (lo < hi && this->do_is(m, *lo) != 0)
430 char_type do_widen(char c) const
432 if (static_cast<unsigned char>(c) < 0x80)
434 throw ctype_failure();
436 const char* do_widen(const char* lo, const char* hi, char_type* dest) const
439 if (static_cast<unsigned char>(*lo) >= 0x80)
440 throw ctype_failure();
447 char do_narrow(char_type wc, char) const
450 return static_cast<char>(wc);
451 throw ctype_failure();
453 const char_type * do_narrow(const char_type * lo, const char_type * hi, char, char * dest) const
457 *dest = static_cast<char>(*lo);
459 throw ctype_failure();
468 /// Facet for outputting numbers to odocstreams as ascii.
469 /// Here we simply need defining the virtual do_put functions.
470 class ascii_num_put_facet : public num_put<lyx::char_type, ostreambuf_iterator<lyx::char_type, char_traits<lyx::char_type> > >
472 typedef ostreambuf_iterator<lyx::char_type, char_traits<lyx::char_type> > iter_type;
474 ascii_num_put_facet(size_t refs = 0) : num_put<lyx::char_type, iter_type>(refs) {}
476 /// Facet for converting numbers to ascii strings.
477 class string_num_put_facet : public num_put<char, basic_string<char>::iterator>
480 string_num_put_facet() : num_put<char, basic_string<char>::iterator>(1) {}
485 do_put(iter_type oit, ios_base & b, char_type fill, bool v) const
487 return do_put_helper(oit, b, fill, v);
491 do_put(iter_type oit, ios_base & b, char_type fill, long v) const
493 return do_put_helper(oit, b, fill, v);
497 do_put(iter_type oit, ios_base & b, char_type fill, unsigned long v) const
499 return do_put_helper(oit, b, fill, v);
502 #ifdef _GLIBCXX_USE_LONG_LONG
504 do_put(iter_type oit, ios_base & b, char_type fill, long long v) const
506 return do_put_helper(oit, b, fill, v);
510 do_put(iter_type oit, ios_base & b, char_type fill, unsigned long long v) const
512 return do_put_helper(oit, b, fill, v);
517 do_put(iter_type oit, ios_base & b, char_type fill, double v) const
519 return do_put_helper(oit, b, fill, v);
523 do_put(iter_type oit, ios_base & b, char_type fill, long double v) const
525 return do_put_helper(oit, b, fill, v);
529 do_put(iter_type oit, ios_base & b, char_type fill, void const * v) const
531 return do_put_helper(oit, b, fill, v);
535 template <typename ValueType>
537 do_put_helper(iter_type oit, ios_base & b, char_type fill, ValueType v) const
540 throw num_put_failure();
542 streamsize const sz = b.width() > b.precision() ?
543 b.width() : b.precision();
544 // 64 is large enough, unless width or precision are bigger
545 streamsize const wd = (sz > 56 ? sz : 56) + 8;
547 string_num_put_facet f;
548 string::const_iterator cit = s.begin();
549 string::const_iterator end =
550 f.put(s.begin(), b, fill, v);
551 for (; cit != end; ++cit, ++oit)
559 /// Facet for inputting ascii representations of numbers from idocstreams.
560 /// Here we simply need defining the virtual do_get functions.
561 class ascii_num_get_facet : public num_get<lyx::char_type, istreambuf_iterator<lyx::char_type, char_traits<lyx::char_type> > >
563 typedef istreambuf_iterator<lyx::char_type, char_traits<lyx::char_type> > iter_type;
565 ascii_num_get_facet(size_t refs = 0) : num_get<lyx::char_type, iter_type>(refs) {}
567 /// Facet for converting ascii representation of numbers to a value.
568 class string_num_get_facet : public num_get<char, basic_string<char>::iterator>
571 string_num_get_facet() : num_get<char, basic_string<char>::iterator>(1) {}
574 /// Numpunct facet defining the I/O format.
575 class numpunct_facet : public numpunct<char>
578 numpunct_facet() : numpunct<char>(1) {}
583 do_get(iter_type iit, iter_type eit, ios_base & b,
584 ios_base::iostate & err, bool & v) const
586 if (b.flags() & ios_base::boolalpha) {
588 lyx::docstring const truename = from_local8bit(p.truename());
589 lyx::docstring const falsename = from_local8bit(p.falsename());
594 size_t const tsize = truename.size();
595 size_t const fsize = falsename.size();
596 for (; iit != eit; ++iit) {
599 bool true_ok = support::prefixIs(truename, s);
600 bool false_ok = support::prefixIs(falsename, s);
601 if (!true_ok && !false_ok) {
606 if ((true_ok && n == tsize) ||
607 (false_ok && n == fsize)) {
613 err = ios_base::goodbit;
614 v = truename == s ? true : false;
616 err = ios_base::failbit;
618 err |= ios_base::eofbit;
622 iter_type end = this->do_get(iit, eit, b, err, l);
623 if (!(err & ios_base::failbit)) {
629 err |= ios_base::failbit;
636 do_get(iter_type iit, iter_type eit, ios_base & b,
637 ios_base::iostate & err, long & v) const
639 return do_get_integer(iit, eit, b, err, v);
643 do_get(iter_type iit, iter_type eit, ios_base & b,
644 ios_base::iostate & err, unsigned short & v) const
646 return do_get_integer(iit, eit, b, err, v);
650 do_get(iter_type iit, iter_type eit, ios_base & b,
651 ios_base::iostate & err, unsigned int & v) const
653 return do_get_integer(iit, eit, b, err, v);
657 do_get(iter_type iit, iter_type eit, ios_base & b,
658 ios_base::iostate & err, unsigned long & v) const
660 return do_get_integer(iit, eit, b, err, v);
663 #ifdef _GLIBCXX_USE_LONG_LONG
665 do_get(iter_type iit, iter_type eit, ios_base & b,
666 ios_base::iostate & err, long long & v) const
668 return do_get_integer(iit, eit, b, err, v);
672 do_get(iter_type iit, iter_type eit, ios_base & b,
673 ios_base::iostate & err, unsigned long long & v) const
675 return do_get_integer(iit, eit, b, err, v);
680 do_get(iter_type iit, iter_type eit, ios_base & b,
681 ios_base::iostate & err, float & v) const
683 return do_get_float(iit, eit, b, err, v);
687 do_get(iter_type iit, iter_type eit, ios_base & b,
688 ios_base::iostate & err, double & v) const
690 return do_get_float(iit, eit, b, err, v);
694 do_get(iter_type iit, iter_type eit, ios_base & b,
695 ios_base::iostate & err, long double & v) const
697 return do_get_float(iit, eit, b, err, v);
701 do_get(iter_type iit, iter_type eit, ios_base & b,
702 ios_base::iostate & err, void * & v) const
705 iter_type end = do_get_integer(iit, eit, b, err, val);
706 if (!(err & ios_base::failbit))
707 v = reinterpret_cast<void *>(val);
712 template <typename ValueType>
714 do_get_integer(iter_type iit, iter_type eit, ios_base & b,
715 ios_base::iostate & err, ValueType & v) const
719 for (; iit != eit && isNumpunct(*iit); ++iit)
720 s += static_cast<char>(*iit);
721 // We add another character, not part of the numpunct facet,
722 // in order to avoid setting the eofbit in the stream state,
723 // which would prevent any further read. The space seems a
726 string_num_get_facet f;
727 f.get(s.begin(), s.end(), b, err, v);
729 err |= ios_base::eofbit;
734 bool isNumpunct(lyx::char_type const c) const
736 /// Only account for the standard numpunct "C" locale facet.
737 return c < 0x80 && (c == '-' || c == '+' || isdigit(c)
738 || ('a' <= c && c <= 'f') || ('A' <= c && c <= 'F')
739 || c == 'x' || c == 'X');
742 template <typename ValueType>
744 do_get_float(iter_type iit, iter_type eit, ios_base & b,
745 ios_base::iostate & err, ValueType & v) const
747 // Gather a string of the form
748 // [+-]? [0-9]* .? [0-9]* ([eE] [+-]? [0-9]+)?
753 char const dot = p.decimal_point();
754 char const sep = p.thousands_sep();
755 // Get an optional sign
756 if (iit != eit && (*iit == '-' || *iit == '+')) {
757 s += static_cast<char>(*iit);
760 for (; iit != eit && isDigitOrSep(*iit, sep); ++iit)
761 s += static_cast<char>(*iit);
762 if (iit != eit && *iit == dot) {
765 for (; iit != eit && isDigitOrSep(*iit, 0); ++iit)
766 s += static_cast<char>(*iit);
767 if (iit != eit && (*iit == 'e' || *iit == 'E')) {
768 s += static_cast<char>(*iit);
770 for (; iit != eit && isDigitOrSep(*iit, 0); ++iit)
771 s += static_cast<char>(*iit);
775 string_num_get_facet f;
776 f.get(s.begin(), s.end(), b, err, v);
778 err |= ios_base::eofbit;
783 bool isDigitOrSep(lyx::char_type const c, char const sep) const
785 return (c >= '0' && c <= '9') || (c != 0 && c == sep);
790 /// class to add our facets to the global locale
791 class locale_initializer {
796 locale const loc1(global, new ascii_ctype_facet);
797 locale const loc2(loc1, new ascii_num_put_facet);
798 locale const loc3(loc2, new ascii_num_get_facet);
799 locale::global(loc3);
806 /// make sure that our facets get used
807 static locale_initializer initializer;