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/lassert.h"
16 #include "support/lstrings.h"
17 #include "support/qstring_helpers.h"
18 #include "support/unicode.h"
24 #if ! defined(USE_WCHAR_T) && defined(__GNUC__)
31 using lyx::support::isHexChar;
35 docstring const from_ascii(char const * ascii)
38 if (int n = strlen(ascii)) {
43 LASSERT(static_cast<unsigned char>(ascii[n]) < 0x80, /**/);
50 docstring const from_ascii(string const & ascii)
52 int const len = ascii.length();
53 for (int i = 0; i < len; ++i)
54 LASSERT(static_cast<unsigned char>(ascii[i]) < 0x80, /**/);
55 return docstring(ascii.begin(), ascii.end());
59 string const to_ascii(docstring const & ucs4)
61 int const len = ucs4.length();
64 for (int i = 0; i < len; ++i) {
65 LASSERT(ucs4[i] < 0x80, /**/);
66 ascii[i] = static_cast<char>(ucs4[i]);
72 IconvProcessor & utf8ToUcs4()
74 static IconvProcessor iconv(ucs4_codeset, "UTF-8");
80 void utf8_to_ucs4(string const & utf8, docstring & ucs4)
82 size_t n = utf8.size();
83 // as utf8 is a multi-byte encoding, there would be at most
89 int maxoutsize = n * 4;
90 // basic_string::data() is not recognized by some old gcc version
91 // so we use &(ucs4[0]) instead.
92 char * outbuf = (char *)(&(ucs4[0]));
93 int bytes = utf8ToUcs4().convert(utf8.c_str(), n, outbuf, maxoutsize);
95 // adjust to the real converted size
100 docstring const from_utf8(string const & utf8)
103 utf8_to_ucs4(utf8, ucs4);
108 string const to_utf8(docstring const & ucs4)
110 vector<char> const utf8 = ucs4_to_utf8(ucs4.data(), ucs4.size());
111 return string(utf8.begin(), utf8.end());
115 docstring const from_local8bit(string const & s)
117 return qstring_to_ucs4(QString::fromLocal8Bit(s.data(), s.length()));
121 /// Exception thrown by to_local8bit if the string could not be converted
122 class to_local8bit_failure : public bad_cast {
124 to_local8bit_failure() throw() : bad_cast() {}
125 virtual ~to_local8bit_failure() throw() {}
126 virtual const char* what() const throw()
128 return "A string could not be converted from unicode to the local 8 bit encoding.";
133 string const to_local8bit(docstring const & s)
135 // This conversion can fail, depending on input.
138 QByteArray const local = toqstr(s).toLocal8Bit();
140 throw to_local8bit_failure();
141 return string(local.begin(), local.end());
145 docstring const from_filesystem8bit(string const & s)
147 QByteArray const encoded(s.c_str(), s.length());
148 return qstring_to_ucs4(QFile::decodeName(encoded));
152 string const to_filesystem8bit(docstring const & s)
154 QByteArray const encoded = QFile::encodeName(toqstr(s));
155 return string(encoded.begin(), encoded.end());
159 string const to_iconv_encoding(docstring const & s, string const & encoding)
161 std::vector<char> const encoded =
162 ucs4_to_eightbit(s.data(), s.length(), encoding);
163 return string(encoded.begin(), encoded.end());
167 docstring const from_iconv_encoding(string const & s, string const & encoding)
169 std::vector<char_type> const ucs4 =
170 eightbit_to_ucs4(s.data(), s.length(), encoding);
171 return docstring(ucs4.begin(), ucs4.end());
175 docstring const normalize_c(docstring const & s)
177 return qstring_to_ucs4(toqstr(s).normalized(QString::NormalizationForm_C));
181 bool operator==(lyx::docstring const & l, char const * r)
183 lyx::docstring::const_iterator it = l.begin();
184 lyx::docstring::const_iterator end = l.end();
185 for (; it != end; ++it, ++r) {
186 LASSERT(static_cast<unsigned char>(*r) < 0x80, /**/);
189 if (*it != static_cast<lyx::docstring::value_type>(*r))
196 lyx::docstring operator+(lyx::docstring const & l, char const * r)
199 for (char const * c = r; *c; ++c) {
200 LASSERT(static_cast<unsigned char>(*c) < 0x80, /**/);
207 lyx::docstring operator+(char const * l, lyx::docstring const & r)
210 for (char const * c = l; *c; ++c) {
211 LASSERT(static_cast<unsigned char>(*c) < 0x80, /**/);
219 lyx::docstring operator+(lyx::docstring const & l, char r)
221 LASSERT(static_cast<unsigned char>(r) < 0x80, /**/);
223 s += docstring::value_type(r);
228 lyx::docstring operator+(char l, lyx::docstring const & r)
230 LASSERT(static_cast<unsigned char>(l) < 0x80, /**/);
231 return lyx::docstring::value_type(l) + r;
235 lyx::docstring & operator+=(lyx::docstring & l, char const * r)
237 for (char const * c = r; *c; ++c) {
238 LASSERT(static_cast<unsigned char>(*c) < 0x80, /**/);
245 lyx::docstring & operator+=(lyx::docstring & l, char r)
247 LASSERT(static_cast<unsigned char>(r) < 0x80, /**/);
254 #if ! defined(USE_WCHAR_T) && defined(__GNUC__)
256 // gcc does not have proper locale facets for lyx::char_type if
257 // sizeof(wchar_t) == 2, so we have to implement them on our own.
260 // We get undefined references to these virtual methods. This looks like
261 // a bug in gcc. The implementation here does not do anything useful, since
262 // it is overriden in ascii_ctype_facet.
264 template<> ctype<lyx::char_type>::~ctype() {}
266 ctype<lyx::char_type>::do_is(ctype<lyx::char_type>::mask, lyx::char_type) const { return false; }
267 template<> lyx::char_type const *
268 ctype<lyx::char_type>::do_is(const lyx::char_type *, const lyx::char_type *, ctype<lyx::char_type>::mask *) const { return 0; }
269 template<> const lyx::char_type *
270 ctype<lyx::char_type>::do_scan_is(ctype<lyx::char_type>::mask, const lyx::char_type *, const lyx::char_type *) const { return 0; }
271 template<> const lyx::char_type *
272 ctype<lyx::char_type>::do_scan_not(ctype<lyx::char_type>::mask, const lyx::char_type *, const lyx::char_type *) const { return 0; }
273 template<> lyx::char_type ctype<lyx::char_type>::do_toupper(lyx::char_type) const { return 0; }
274 template<> const lyx::char_type * ctype<lyx::char_type>::do_toupper(lyx::char_type *, lyx::char_type const *) const { return 0; }
275 template<> lyx::char_type ctype<lyx::char_type>::do_tolower(lyx::char_type) const { return 0; }
276 template<> const lyx::char_type * ctype<lyx::char_type>::do_tolower(lyx::char_type *, lyx::char_type const *) const { return 0; }
277 template<> lyx::char_type ctype<lyx::char_type>::do_widen(char) const { return 0; }
278 template<> const char *
279 ctype<lyx::char_type>::do_widen(const char *, const char *, lyx::char_type *) const { return 0; }
281 ctype<lyx::char_type>::do_narrow(const lyx::char_type, char) const { return 0; }
282 template<> const lyx::char_type *
283 ctype<lyx::char_type>::do_narrow(const lyx::char_type *, const lyx::char_type *, char, char *) const { return 0; }
289 class ctype_failure : public bad_cast {
291 ctype_failure() throw() : bad_cast() {}
292 virtual ~ctype_failure() throw() {}
293 virtual const char* what() const throw()
295 return "The ctype<lyx::char_type> locale facet does only support ASCII characters on this platform.";
300 class num_put_failure : public bad_cast {
302 num_put_failure() throw() : bad_cast() {}
303 virtual ~num_put_failure() throw() {}
304 virtual const char* what() const throw()
306 return "The num_put locale facet does only support ASCII characters on this platform.";
311 /// ctype facet for UCS4 characters. The implementation does only support pure
312 /// ASCII, since we do not need anything else for now.
313 /// The code is partly stolen from ctype<wchar_t> from gcc.
314 class ascii_ctype_facet : public ctype<lyx::char_type>
317 typedef lyx::char_type char_type;
318 typedef wctype_t wmask_type;
319 explicit ascii_ctype_facet(size_t refs = 0) : ctype<char_type>(refs)
321 M_initialize_ctype();
326 wint_t M_widen[1 + static_cast<unsigned char>(-1)];
328 wmask_type M_wmask[16];
329 wmask_type M_convert_to_wmask(const mask m) const
333 case space: ret = wctype("space"); break;
334 case print: ret = wctype("print"); break;
335 case cntrl: ret = wctype("cntrl"); break;
336 case upper: ret = wctype("upper"); break;
337 case lower: ret = wctype("lower"); break;
338 case alpha: ret = wctype("alpha"); break;
339 case digit: ret = wctype("digit"); break;
340 case punct: ret = wctype("punct"); break;
341 case xdigit: ret = wctype("xdigit"); break;
342 case alnum: ret = wctype("alnum"); break;
343 case graph: ret = wctype("graph"); break;
344 default: ret = wmask_type();
348 void M_initialize_ctype()
351 for (i = 0; i < 128; ++i) {
352 const int c = wctob(i);
356 M_narrow[i] = static_cast<char>(c);
362 for (size_t i = 0; i < sizeof(M_widen) / sizeof(wint_t); ++i)
363 M_widen[i] = btowc(i);
365 for (size_t i = 0; i <= 15; ++i) {
366 M_bit[i] = static_cast<mask>(1 << i);
367 M_wmask[i] = M_convert_to_wmask(M_bit[i]);
370 virtual ~ascii_ctype_facet() {}
371 char_type do_toupper(char_type c) const
374 throw ctype_failure();
375 return toupper(static_cast<int>(c));
377 char_type const * do_toupper(char_type * lo, char_type const * hi) const
381 throw ctype_failure();
382 *lo = toupper(static_cast<int>(*lo));
387 char_type do_tolower(char_type c) const
390 throw ctype_failure();
393 char_type const * do_tolower(char_type * lo, char_type const * hi) const
397 throw ctype_failure();
403 bool do_is(mask m, char_type c) const
406 throw ctype_failure();
407 // The code below works because c is in the ASCII range.
408 // We could not use iswctype() which is designed for a 2byte
409 // whar_t without encoding conversion otherwise.
411 // Generically, 15 (instead of 10) since we don't know the numerical
412 // encoding of the various categories in /usr/include/ctype.h.
413 const size_t bitmasksize = 15;
414 for (size_t bitcur = 0; bitcur <= bitmasksize; ++bitcur)
415 if (m & M_bit[bitcur] &&
416 iswctype(static_cast<int>(c), M_wmask[bitcur])) {
422 char_type const * do_is(char_type const * lo, char_type const * hi, mask * vec) const
424 for (;lo < hi; ++vec, ++lo) {
426 throw ctype_failure();
427 // The code below works because c is in the ASCII range.
428 // We could not use iswctype() which is designed for a 2byte
429 // whar_t without encoding conversion otherwise.
430 // Generically, 15 (instead of 10) since we don't know the numerical
431 // encoding of the various categories in /usr/include/ctype.h.
432 const size_t bitmasksize = 15;
434 for (size_t bitcur = 0; bitcur <= bitmasksize; ++bitcur)
435 if (iswctype(static_cast<int>(*lo), M_wmask[bitcur]))
441 char_type const * do_scan_is(mask m, char_type const * lo, char_type const * hi) const
443 while (lo < hi && !this->do_is(m, *lo))
447 char_type const * do_scan_not(mask m, char_type const * lo, char_type const * hi) const
449 while (lo < hi && this->do_is(m, *lo) != 0)
453 char_type do_widen(char c) const
455 if (static_cast<unsigned char>(c) < 0x80)
457 throw ctype_failure();
459 const char* do_widen(const char* lo, const char* hi, char_type* dest) const
462 if (static_cast<unsigned char>(*lo) >= 0x80)
463 throw ctype_failure();
470 char do_narrow(char_type wc, char) const
473 return static_cast<char>(wc);
474 throw ctype_failure();
476 const char_type * do_narrow(const char_type * lo, const char_type * hi, char, char * dest) const
480 *dest = static_cast<char>(*lo);
482 throw ctype_failure();
491 /// Facet for outputting numbers to odocstreams as ascii.
492 /// Here we simply need defining the virtual do_put functions.
493 class ascii_num_put_facet : public num_put<lyx::char_type, ostreambuf_iterator<lyx::char_type, char_traits<lyx::char_type> > >
495 typedef ostreambuf_iterator<lyx::char_type, char_traits<lyx::char_type> > iter_type;
497 ascii_num_put_facet(size_t refs = 0) : num_put<lyx::char_type, iter_type>(refs) {}
499 /// Facet for converting numbers to ascii strings.
500 class string_num_put_facet : public num_put<char, basic_string<char>::iterator>
503 string_num_put_facet() : num_put<char, basic_string<char>::iterator>(1) {}
508 do_put(iter_type oit, ios_base & b, char_type fill, bool v) const
510 return do_put_helper(oit, b, fill, v);
514 do_put(iter_type oit, ios_base & b, char_type fill, long v) const
516 return do_put_helper(oit, b, fill, v);
520 do_put(iter_type oit, ios_base & b, char_type fill, unsigned long v) const
522 return do_put_helper(oit, b, fill, v);
525 #ifdef _GLIBCXX_USE_LONG_LONG
527 do_put(iter_type oit, ios_base & b, char_type fill, long long v) const
529 return do_put_helper(oit, b, fill, v);
533 do_put(iter_type oit, ios_base & b, char_type fill, unsigned long long v) const
535 return do_put_helper(oit, b, fill, v);
540 do_put(iter_type oit, ios_base & b, char_type fill, double v) const
542 return do_put_helper(oit, b, fill, v);
546 do_put(iter_type oit, ios_base & b, char_type fill, long double v) const
548 return do_put_helper(oit, b, fill, v);
552 do_put(iter_type oit, ios_base & b, char_type fill, void const * v) const
554 return do_put_helper(oit, b, fill, v);
558 template <typename ValueType>
560 do_put_helper(iter_type oit, ios_base & b, char_type fill, ValueType v) const
563 throw num_put_failure();
565 streamsize const sz = b.width() > b.precision() ?
566 b.width() : b.precision();
567 // 64 is large enough, unless width or precision are bigger
568 streamsize const wd = (sz > 56 ? sz : 56) + 8;
570 string_num_put_facet f;
571 string::const_iterator cit = s.begin();
572 string::const_iterator end =
573 f.put(s.begin(), b, fill, v);
574 for (; cit != end; ++cit, ++oit)
582 /// Facet for inputting ascii representations of numbers from idocstreams.
583 /// Here we simply need defining the virtual do_get functions.
584 class ascii_num_get_facet : public num_get<lyx::char_type, istreambuf_iterator<lyx::char_type, char_traits<lyx::char_type> > >
586 typedef istreambuf_iterator<lyx::char_type, char_traits<lyx::char_type> > iter_type;
588 ascii_num_get_facet(size_t refs = 0) : num_get<lyx::char_type, iter_type>(refs) {}
590 /// Facet for converting ascii representation of numbers to a value.
591 class string_num_get_facet : public num_get<char, basic_string<char>::iterator>
594 string_num_get_facet() : num_get<char, basic_string<char>::iterator>(1) {}
597 /// Numpunct facet defining the I/O format.
598 class numpunct_facet : public numpunct<char>
601 numpunct_facet() : numpunct<char>(1) {}
606 do_get(iter_type iit, iter_type eit, ios_base & b,
607 ios_base::iostate & err, bool & v) const
609 if (b.flags() & ios_base::boolalpha) {
611 lyx::docstring const truename = from_local8bit(p.truename());
612 lyx::docstring const falsename = from_local8bit(p.falsename());
617 size_t const tsize = truename.size();
618 size_t const fsize = falsename.size();
619 for (; iit != eit; ++iit) {
622 bool true_ok = support::prefixIs(truename, s);
623 bool false_ok = support::prefixIs(falsename, s);
624 if (!true_ok && !false_ok) {
629 if ((true_ok && n == tsize) ||
630 (false_ok && n == fsize)) {
636 err = ios_base::goodbit;
637 v = truename == s ? true : false;
639 err = ios_base::failbit;
641 err |= ios_base::eofbit;
645 iter_type end = this->do_get(iit, eit, b, err, l);
646 if (!(err & ios_base::failbit)) {
652 err |= ios_base::failbit;
659 do_get(iter_type iit, iter_type eit, ios_base & b,
660 ios_base::iostate & err, long & v) const
662 return do_get_integer(iit, eit, b, err, v);
666 do_get(iter_type iit, iter_type eit, ios_base & b,
667 ios_base::iostate & err, unsigned short & v) const
669 return do_get_integer(iit, eit, b, err, v);
673 do_get(iter_type iit, iter_type eit, ios_base & b,
674 ios_base::iostate & err, unsigned int & v) const
676 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, unsigned long & v) const
683 return do_get_integer(iit, eit, b, err, v);
686 #ifdef _GLIBCXX_USE_LONG_LONG
688 do_get(iter_type iit, iter_type eit, ios_base & b,
689 ios_base::iostate & err, long long & v) const
691 return do_get_integer(iit, eit, b, err, v);
695 do_get(iter_type iit, iter_type eit, ios_base & b,
696 ios_base::iostate & err, unsigned long long & v) const
698 return do_get_integer(iit, eit, b, err, v);
703 do_get(iter_type iit, iter_type eit, ios_base & b,
704 ios_base::iostate & err, float & v) const
706 return do_get_float(iit, eit, b, err, v);
710 do_get(iter_type iit, iter_type eit, ios_base & b,
711 ios_base::iostate & err, double & v) const
713 return do_get_float(iit, eit, b, err, v);
717 do_get(iter_type iit, iter_type eit, ios_base & b,
718 ios_base::iostate & err, long double & v) const
720 return do_get_float(iit, eit, b, err, v);
724 do_get(iter_type iit, iter_type eit, ios_base & b,
725 ios_base::iostate & err, void * & v) const
728 iter_type end = do_get_integer(iit, eit, b, err, val);
729 if (!(err & ios_base::failbit))
730 v = reinterpret_cast<void *>(val);
735 template <typename ValueType>
737 do_get_integer(iter_type iit, iter_type eit, ios_base & b,
738 ios_base::iostate & err, ValueType & v) const
742 for (; iit != eit && isNumpunct(*iit); ++iit)
743 s += static_cast<char>(*iit);
744 // We add another character, not part of the numpunct facet,
745 // in order to avoid setting the eofbit in the stream state,
746 // which would prevent any further read. The space seems a
749 string_num_get_facet f;
750 f.get(s.begin(), s.end(), b, err, v);
752 err |= ios_base::eofbit;
757 bool isNumpunct(lyx::char_type const c) const
759 /// Only account for the standard numpunct "C" locale facet.
760 return c == '-' || c == '+'
761 || c == 'x' || c == 'X'
765 template <typename ValueType>
767 do_get_float(iter_type iit, iter_type eit, ios_base & b,
768 ios_base::iostate & err, ValueType & v) const
770 // Gather a string of the form
771 // [+-]? [0-9]* .? [0-9]* ([eE] [+-]? [0-9]+)?
776 char const dot = p.decimal_point();
777 char const sep = p.thousands_sep();
778 // Get an optional sign
779 if (iit != eit && (*iit == '-' || *iit == '+')) {
780 s += static_cast<char>(*iit);
783 for (; iit != eit && isDigitOrSep(*iit, sep); ++iit)
784 s += static_cast<char>(*iit);
785 if (iit != eit && *iit == dot) {
788 for (; iit != eit && isDigitOrSep(*iit, 0); ++iit)
789 s += static_cast<char>(*iit);
790 if (iit != eit && (*iit == 'e' || *iit == 'E')) {
791 s += static_cast<char>(*iit);
793 for (; iit != eit && isDigitOrSep(*iit, 0); ++iit)
794 s += static_cast<char>(*iit);
798 string_num_get_facet f;
799 f.get(s.begin(), s.end(), b, err, v);
801 err |= ios_base::eofbit;
806 bool isDigitOrSep(lyx::char_type const c, char const sep) const
808 return (c >= '0' && c <= '9') || (c != 0 && c == sep);
813 /// class to add our facets to the global locale
814 class locale_initializer {
819 locale const loc1(global, new ascii_ctype_facet);
820 locale const loc2(loc1, new ascii_num_put_facet);
821 locale const loc3(loc2, new ascii_num_get_facet);
822 locale::global(loc3);
829 /// make sure that our facets get used
830 static locale_initializer initializer;