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 "docstring.h"
14 #include "qstring_helpers.h"
22 #include <boost/assert.hpp>
28 docstring const from_ascii(char const * ascii)
31 for (char const * c = ascii; *c; ++c) {
32 BOOST_ASSERT(static_cast<unsigned char>(*c) < 0x80);
39 docstring const from_ascii(std::string const & ascii)
41 int const len = ascii.length();
42 for (int i = 0; i < len; ++i)
43 BOOST_ASSERT(static_cast<unsigned char>(ascii[i]) < 0x80);
44 return docstring(ascii.begin(), ascii.end());
48 std::string const to_ascii(docstring const & ucs4)
50 int const len = ucs4.length();
53 for (int i = 0; i < len; ++i) {
54 BOOST_ASSERT(ucs4[i] < 0x80);
55 ascii[i] = static_cast<char>(ucs4[i]);
61 IconvProcessor & utf8ToUcs4()
63 static IconvProcessor iconv(ucs4_codeset, "UTF-8");
69 void utf8_to_ucs4(std::string const & utf8, docstring & ucs4)
71 size_t n = utf8.size();
72 // as utf8 is a multi-byte encoding, there would be at most
78 int maxoutsize = n * 4;
79 // basic_string::data() is not recognized by some old gcc version
80 // so we use &(ucs4[0]) instead.
81 char * outbuf = (char *)(&(ucs4[0]));
82 int bytes = utf8ToUcs4().convert(utf8.c_str(), n, outbuf, maxoutsize);
84 // adjust to the real converted size
89 docstring const from_utf8(std::string const & utf8)
92 utf8_to_ucs4(utf8, ucs4);
97 std::string const to_utf8(docstring const & ucs4)
99 std::vector<char> const utf8 =
100 ucs4_to_utf8(ucs4.data(), ucs4.size());
101 return std::string(utf8.begin(), utf8.end());
105 docstring const from_local8bit(std::string const & s)
107 return qstring_to_ucs4(QString::fromLocal8Bit(s.data(), s.length()));
111 const char* to_local8bit_failure::what() const throw()
113 return "A string could not be converted from unicode to the local 8 bit encoding.";
117 std::string const to_local8bit(docstring const & s)
119 // This conversion can fail, depending on input.
121 return std::string();
122 QByteArray const local = toqstr(s).toLocal8Bit();
123 if (local.size() == 0)
124 throw to_local8bit_failure();
125 return std::string(local.begin(), local.end());
129 docstring const from_filesystem8bit(std::string const & s)
131 QByteArray const encoded(s.c_str(), s.length());
132 return qstring_to_ucs4(QFile::decodeName(encoded));
136 std::string const to_filesystem8bit(docstring const & s)
138 QByteArray const encoded = QFile::encodeName(toqstr(s));
139 return std::string(encoded.begin(), encoded.end());
143 docstring const normalize_c(docstring const & s)
145 return qstring_to_ucs4(toqstr(s).normalized(QString::NormalizationForm_C));
149 bool operator==(lyx::docstring const & l, char const * r)
151 lyx::docstring::const_iterator it = l.begin();
152 lyx::docstring::const_iterator end = l.end();
153 for (; it != end; ++it, ++r) {
154 BOOST_ASSERT(static_cast<unsigned char>(*r) < 0x80);
157 if (*it != static_cast<lyx::docstring::value_type>(*r))
164 lyx::docstring operator+(lyx::docstring const & l, char const * r)
167 for (char const * c = r; *c; ++c) {
168 BOOST_ASSERT(static_cast<unsigned char>(*c) < 0x80);
175 lyx::docstring operator+(char const * l, lyx::docstring const & r)
178 for (char const * c = l; *c; ++c) {
179 BOOST_ASSERT(static_cast<unsigned char>(*c) < 0x80);
187 lyx::docstring operator+(lyx::docstring const & l, char r)
189 BOOST_ASSERT(static_cast<unsigned char>(r) < 0x80);
190 return l + lyx::docstring::value_type(r);
194 lyx::docstring operator+(char l, lyx::docstring const & r)
196 BOOST_ASSERT(static_cast<unsigned char>(l) < 0x80);
197 return lyx::docstring::value_type(l) + r;
201 lyx::docstring & operator+=(lyx::docstring & l, char const * r)
203 for (char const * c = r; *c; ++c) {
204 BOOST_ASSERT(static_cast<unsigned char>(*c) < 0x80);
211 lyx::docstring & operator+=(lyx::docstring & l, char r)
213 BOOST_ASSERT(static_cast<unsigned char>(r) < 0x80);
220 #if ! defined(USE_WCHAR_T) && defined(__GNUC__)
222 // gcc does not have proper locale facets for lyx::char_type if
223 // sizeof(wchar_t) == 2, so we have to implement them on our own.
226 // We get undefined references to these virtual methods. This looks like
227 // a bug in gcc. The implementation here does not do anything useful, since
228 // it is overriden in ascii_ctype_facet.
230 template<> ctype<lyx::char_type>::~ctype() {}
232 ctype<lyx::char_type>::do_is(ctype<lyx::char_type>::mask, lyx::char_type) const { return false; }
233 template<> lyx::char_type const *
234 ctype<lyx::char_type>::do_is(const lyx::char_type *, const lyx::char_type *, ctype<lyx::char_type>::mask *) const { return 0; }
235 template<> const lyx::char_type *
236 ctype<lyx::char_type>::do_scan_is(ctype<lyx::char_type>::mask, const lyx::char_type *, const lyx::char_type *) const { return 0; }
237 template<> const lyx::char_type *
238 ctype<lyx::char_type>::do_scan_not(ctype<lyx::char_type>::mask, const lyx::char_type *, const lyx::char_type *) const { return 0; }
239 template<> lyx::char_type ctype<lyx::char_type>::do_toupper(lyx::char_type) const { return 0; }
240 template<> const lyx::char_type * ctype<lyx::char_type>::do_toupper(lyx::char_type *, lyx::char_type const *) const { return 0; }
241 template<> lyx::char_type ctype<lyx::char_type>::do_tolower(lyx::char_type) const { return 0; }
242 template<> const lyx::char_type * ctype<lyx::char_type>::do_tolower(lyx::char_type *, lyx::char_type const *) const { return 0; }
243 template<> lyx::char_type ctype<lyx::char_type>::do_widen(char) const { return 0; }
244 template<> const char *
245 ctype<lyx::char_type>::do_widen(const char *, const char *, lyx::char_type *) const { return 0; }
247 ctype<lyx::char_type>::do_narrow(const lyx::char_type, char) const { return 0; }
248 template<> const lyx::char_type *
249 ctype<lyx::char_type>::do_narrow(const lyx::char_type *, const lyx::char_type *, char, char *) const { return 0; }
255 class ctype_failure : public std::bad_cast {
257 ctype_failure() throw() : std::bad_cast() {}
258 virtual ~ctype_failure() throw() {}
259 virtual const char* what() const throw()
261 return "The ctype<lyx::char_type> locale facet does only support ASCII characters on this platform.";
266 class num_put_failure : public std::bad_cast {
268 num_put_failure() throw() : std::bad_cast() {}
269 virtual ~num_put_failure() throw() {}
270 virtual const char* what() const throw()
272 return "The num_put locale facet does only support ASCII characters on this platform.";
277 /// ctype facet for UCS4 characters. The implementation does only support pure
278 /// ASCII, since we do not need anything else for now.
279 /// The code is partly stolen from std::ctype<wchar_t> from gcc.
280 class ascii_ctype_facet : public std::ctype<lyx::char_type>
283 typedef lyx::char_type char_type;
284 typedef wctype_t wmask_type;
285 explicit ascii_ctype_facet(size_t refs = 0) : std::ctype<char_type>(refs)
287 M_initialize_ctype();
292 wint_t M_widen[1 + static_cast<unsigned char>(-1)];
294 wmask_type M_wmask[16];
295 wmask_type M_convert_to_wmask(const mask m) const
299 case space: ret = wctype("space"); break;
300 case print: ret = wctype("print"); break;
301 case cntrl: ret = wctype("cntrl"); break;
302 case upper: ret = wctype("upper"); break;
303 case lower: ret = wctype("lower"); break;
304 case alpha: ret = wctype("alpha"); break;
305 case digit: ret = wctype("digit"); break;
306 case punct: ret = wctype("punct"); break;
307 case xdigit: ret = wctype("xdigit"); break;
308 case alnum: ret = wctype("alnum"); break;
309 case graph: ret = wctype("graph"); break;
310 default: ret = wmask_type();
314 void M_initialize_ctype()
317 for (i = 0; i < 128; ++i) {
318 const int c = wctob(i);
322 M_narrow[i] = static_cast<char>(c);
328 for (size_t i = 0; i < sizeof(M_widen) / sizeof(wint_t); ++i)
329 M_widen[i] = btowc(i);
331 for (size_t i = 0; i <= 15; ++i) {
332 M_bit[i] = static_cast<mask>(1 << i);
333 M_wmask[i] = M_convert_to_wmask(M_bit[i]);
336 virtual ~ascii_ctype_facet() {}
337 char_type do_toupper(char_type c) const
340 throw ctype_failure();
341 return toupper(static_cast<int>(c));
343 char_type const * do_toupper(char_type * lo, char_type const * hi) const
347 throw ctype_failure();
348 *lo = toupper(static_cast<int>(*lo));
353 char_type do_tolower(char_type c) const
356 throw ctype_failure();
359 char_type const * do_tolower(char_type * lo, char_type const * hi) const
363 throw ctype_failure();
369 bool do_is(mask m, char_type c) const
372 throw ctype_failure();
373 // The code below works because c is in the ASCII range.
374 // We could not use iswctype() which is designed for a 2byte
375 // whar_t without encoding conversion otherwise.
377 // Generically, 15 (instead of 10) since we don't know the numerical
378 // encoding of the various categories in /usr/include/ctype.h.
379 const size_t bitmasksize = 15;
380 for (size_t bitcur = 0; bitcur <= bitmasksize; ++bitcur)
381 if (m & M_bit[bitcur] &&
382 iswctype(static_cast<int>(c), M_wmask[bitcur])) {
388 char_type const * do_is(char_type const * lo, char_type const * hi, mask * vec) const
390 for (;lo < hi; ++vec, ++lo) {
392 throw ctype_failure();
393 // The code below works because c is in the ASCII range.
394 // We could not use iswctype() which is designed for a 2byte
395 // whar_t without encoding conversion otherwise.
396 // Generically, 15 (instead of 10) since we don't know the numerical
397 // encoding of the various categories in /usr/include/ctype.h.
398 const size_t bitmasksize = 15;
400 for (size_t bitcur = 0; bitcur <= bitmasksize; ++bitcur)
401 if (iswctype(static_cast<int>(*lo), M_wmask[bitcur]))
407 char_type const * do_scan_is(mask m, char_type const * lo, char_type const * hi) const
409 while (lo < hi && !this->do_is(m, *lo))
413 char_type const * do_scan_not(mask m, char_type const * lo, char_type const * hi) const
415 while (lo < hi && this->do_is(m, *lo) != 0)
419 char_type do_widen(char c) const
421 if (static_cast<unsigned char>(c) < 0x80)
423 throw ctype_failure();
425 const char* do_widen(const char* lo, const char* hi, char_type* dest) const
428 if (static_cast<unsigned char>(*lo) >= 0x80)
429 throw ctype_failure();
436 char do_narrow(char_type wc, char) const
439 return static_cast<char>(wc);
440 throw ctype_failure();
442 const char_type * do_narrow(const char_type * lo, const char_type * hi, char, char * dest) const
446 *dest = static_cast<char>(*lo);
448 throw ctype_failure();
457 /// Facet for outputting numbers to odocstreams as ascii.
458 /// Here we simply need defining the virtual do_put functions.
459 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 typedef std::ostreambuf_iterator<lyx::char_type, std::char_traits<lyx::char_type> > iter_type;
463 ascii_num_put_facet(size_t refs = 0) : std::num_put<lyx::char_type, iter_type>(refs) {}
465 /// Facet for converting numbers to ascii strings.
466 class string_num_put_facet : public std::num_put<char, std::basic_string<char>::iterator>
469 string_num_put_facet() : std::num_put<char, std::basic_string<char>::iterator>(1) {}
474 do_put(iter_type oit, std::ios_base & b, char_type fill, bool v) const
476 return do_put_helper(oit, b, fill, v);
480 do_put(iter_type oit, std::ios_base & b, char_type fill, long v) const
482 return do_put_helper(oit, b, fill, v);
486 do_put(iter_type oit, std::ios_base & b, char_type fill, unsigned long v) const
488 return do_put_helper(oit, b, fill, v);
491 #ifdef _GLIBCXX_USE_LONG_LONG
493 do_put(iter_type oit, std::ios_base & b, char_type fill, long long v) const
495 return do_put_helper(oit, b, fill, v);
499 do_put(iter_type oit, std::ios_base & b, char_type fill, unsigned long long v) const
501 return do_put_helper(oit, b, fill, v);
506 do_put(iter_type oit, std::ios_base & b, char_type fill, double v) const
508 return do_put_helper(oit, b, fill, v);
512 do_put(iter_type oit, std::ios_base & b, char_type fill, long double v) const
514 return do_put_helper(oit, b, fill, v);
518 do_put(iter_type oit, std::ios_base & b, char_type fill, void const * v) const
520 return do_put_helper(oit, b, fill, v);
524 template <typename ValueType>
526 do_put_helper(iter_type oit, std::ios_base & b, char_type fill, ValueType v) const
529 throw num_put_failure();
531 std::streamsize const sz = b.width() > b.precision() ?
532 b.width() : b.precision();
533 // 64 is large enough, unless width or precision are bigger
534 std::streamsize const wd = (sz > 56 ? sz : 56) + 8;
535 std::string s(wd, '\0');
536 string_num_put_facet f;
537 std::string::const_iterator cit = s.begin();
538 std::string::const_iterator end =
539 f.put(s.begin(), b, fill, v);
540 for (; cit != end; ++cit, ++oit)
548 /// Facet for inputting ascii representations of numbers from idocstreams.
549 /// Here we simply need defining the virtual do_get functions.
550 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 typedef std::istreambuf_iterator<lyx::char_type, std::char_traits<lyx::char_type> > iter_type;
554 ascii_num_get_facet(size_t refs = 0) : std::num_get<lyx::char_type, iter_type>(refs) {}
556 /// Facet for converting ascii representation of numbers to a value.
557 class string_num_get_facet : public std::num_get<char, std::basic_string<char>::iterator>
560 string_num_get_facet() : std::num_get<char, std::basic_string<char>::iterator>(1) {}
563 /// Numpunct facet defining the I/O format.
564 class numpunct_facet : public std::numpunct<char>
567 numpunct_facet() : std::numpunct<char>(1) {}
572 do_get(iter_type iit, iter_type eit, std::ios_base & b,
573 std::ios_base::iostate & err, bool & v) const
575 // This facet has been adapted from the STLPort library
576 if (b.flags() & std::ios_base::boolalpha) {
578 lyx::docstring const truename = from_local8bit(p.truename());
579 lyx::docstring const falsename = from_local8bit(p.falsename());
581 bool false_ok = true;
583 for (; iit != eit; ++iit) {
584 lyx::char_type c = *iit;
585 true_ok = true_ok && (c == truename[n]);
586 false_ok = false_ok && (c == falsename[n]);
588 if ((!true_ok && !false_ok) ||
589 (true_ok && n >= truename.size()) ||
590 (false_ok && n >= falsename.size())) {
595 if (true_ok && n < truename.size())
597 if (false_ok && n < falsename.size())
599 if (true_ok || false_ok) {
600 err = std::ios_base::goodbit;
603 err = std::ios_base::failbit;
605 err |= std::ios_base::eofbit;
609 iter_type end = this->do_get(iit, eit, b, err, l);
610 if (!(err & std::ios_base::failbit)) {
616 err |= std::ios_base::failbit;
623 do_get(iter_type iit, iter_type eit, std::ios_base & b,
624 std::ios_base::iostate & err, long & v) const
626 return do_get_integer(iit, eit, b, err, v);
630 do_get(iter_type iit, iter_type eit, std::ios_base & b,
631 std::ios_base::iostate & err, unsigned short & v) const
633 return do_get_integer(iit, eit, b, err, v);
637 do_get(iter_type iit, iter_type eit, std::ios_base & b,
638 std::ios_base::iostate & err, unsigned int & v) const
640 return do_get_integer(iit, eit, b, err, v);
644 do_get(iter_type iit, iter_type eit, std::ios_base & b,
645 std::ios_base::iostate & err, unsigned long & v) const
647 return do_get_integer(iit, eit, b, err, v);
650 #ifdef _GLIBCXX_USE_LONG_LONG
652 do_get(iter_type iit, iter_type eit, std::ios_base & b,
653 std::ios_base::iostate & err, long long & v) const
655 return do_get_integer(iit, eit, b, err, v);
659 do_get(iter_type iit, iter_type eit, std::ios_base & b,
660 std::ios_base::iostate & err, unsigned long long & v) const
662 return do_get_integer(iit, eit, b, err, v);
667 do_get(iter_type iit, iter_type eit, std::ios_base & b,
668 std::ios_base::iostate & err, float & v) const
670 return do_get_float(iit, eit, b, err, v);
674 do_get(iter_type iit, iter_type eit, std::ios_base & b,
675 std::ios_base::iostate & err, double & v) const
677 return do_get_float(iit, eit, b, err, v);
681 do_get(iter_type iit, iter_type eit, std::ios_base & b,
682 std::ios_base::iostate & err, long double & v) const
684 return do_get_float(iit, eit, b, err, v);
688 do_get(iter_type iit, iter_type eit, std::ios_base & b,
689 std::ios_base::iostate & err, void * & v) const
692 iter_type end = do_get_integer(iit, eit, b, err, val);
693 if (!(err & std::ios_base::failbit))
694 v = reinterpret_cast<void *>(val);
699 template <typename ValueType>
701 do_get_integer(iter_type iit, iter_type eit, std::ios_base & b,
702 std::ios_base::iostate & err, ValueType & v) const
706 for (; iit != eit && isNumpunct(*iit); ++iit)
707 s += static_cast<char>(*iit);
708 // We add another character, not part of the numpunct facet,
709 // in order to avoid setting the eofbit in the stream state,
710 // which would prevent any further read. The space seems a
713 string_num_get_facet f;
714 f.get(s.begin(), s.end(), b, err, v);
716 err |= std::ios_base::eofbit;
721 bool isNumpunct(lyx::char_type const c) const
723 /// Only account for the standard numpunct "C" locale facet.
724 return c < 0x80 && (c == '-' || c == '+' || isdigit(c)
725 || ('a' <= c && c <= 'f') || ('A' <= c && c <= 'F')
726 || c == 'x' || c == 'X');
729 template <typename ValueType>
731 do_get_float(iter_type iit, iter_type eit, std::ios_base & b,
732 std::ios_base::iostate & err, ValueType & v) const
734 // Gather a string of the form
735 // [+-]? [0-9]* .? [0-9]* ([eE] [+-]? [0-9]+)?
740 char const dot = p.decimal_point();
741 char const sep = p.thousands_sep();
742 // Get an optional sign
743 if (iit != eit && (*iit == '-' || *iit == '+')) {
744 s += static_cast<char>(*iit);
747 for (; iit != eit && isDigitOrSep(*iit, sep); ++iit)
748 s += static_cast<char>(*iit);
749 if (iit != eit && *iit == dot) {
752 for (; iit != eit && isDigitOrSep(*iit, 0); ++iit)
753 s += static_cast<char>(*iit);
754 if (iit != eit && (*iit == 'e' || *iit == 'E')) {
755 s += static_cast<char>(*iit);
757 for (; iit != eit && isDigitOrSep(*iit, 0); ++iit)
758 s += static_cast<char>(*iit);
762 string_num_get_facet f;
763 f.get(s.begin(), s.end(), b, err, v);
765 err |= std::ios_base::eofbit;
770 bool isDigitOrSep(lyx::char_type const c, char const sep) const
772 return (c >= '0' && c <= '9') || (c != 0 && c == sep);
777 /// class to add our facets to the global locale
778 class locale_initializer {
783 std::locale const loc1(global, new ascii_ctype_facet);
784 std::locale const loc2(loc1, new ascii_num_put_facet);
785 std::locale const loc3(loc2, new ascii_num_get_facet);
786 std::locale::global(loc3);
793 /// make sure that our facets get used
794 static locale_initializer initializer;