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"
31 docstring const from_ascii(char const * ascii)
34 if (int n = strlen(ascii)) {
39 LASSERT(static_cast<unsigned char>(ascii[n]) < 0x80, /**/);
46 docstring const from_ascii(string const & ascii)
48 int const len = ascii.length();
49 for (int i = 0; i < len; ++i)
50 LASSERT(static_cast<unsigned char>(ascii[i]) < 0x80, /**/);
51 return docstring(ascii.begin(), ascii.end());
55 string const to_ascii(docstring const & ucs4)
57 int const len = ucs4.length();
60 for (int i = 0; i < len; ++i) {
61 LASSERT(ucs4[i] < 0x80, /**/);
62 ascii[i] = static_cast<char>(ucs4[i]);
68 IconvProcessor & utf8ToUcs4()
70 static IconvProcessor iconv(ucs4_codeset, "UTF-8");
76 void utf8_to_ucs4(string const & utf8, docstring & ucs4)
78 size_t n = utf8.size();
79 // as utf8 is a multi-byte encoding, there would be at most
85 int maxoutsize = n * 4;
86 // basic_string::data() is not recognized by some old gcc version
87 // so we use &(ucs4[0]) instead.
88 char * outbuf = (char *)(&(ucs4[0]));
89 int bytes = utf8ToUcs4().convert(utf8.c_str(), n, outbuf, maxoutsize);
91 // adjust to the real converted size
96 docstring const from_utf8(string const & utf8)
99 utf8_to_ucs4(utf8, ucs4);
104 string const to_utf8(docstring const & ucs4)
106 vector<char> const utf8 = ucs4_to_utf8(ucs4.data(), ucs4.size());
107 return string(utf8.begin(), utf8.end());
111 docstring const from_local8bit(string const & s)
113 return qstring_to_ucs4(QString::fromLocal8Bit(s.data(), s.length()));
117 /// Exception thrown by to_local8bit if the string could not be converted
118 class to_local8bit_failure : public bad_cast {
120 to_local8bit_failure() throw() : bad_cast() {}
121 virtual ~to_local8bit_failure() throw() {}
122 virtual const char* what() const throw()
124 return "A string could not be converted from unicode to the local 8 bit encoding.";
129 string const to_local8bit(docstring const & s)
131 // This conversion can fail, depending on input.
134 QByteArray const local = toqstr(s).toLocal8Bit();
135 if (local.size() == 0)
136 throw to_local8bit_failure();
137 return string(local.begin(), local.end());
141 docstring const from_filesystem8bit(string const & s)
143 QByteArray const encoded(s.c_str(), s.length());
144 return qstring_to_ucs4(QFile::decodeName(encoded));
148 string const to_filesystem8bit(docstring const & s)
150 QByteArray const encoded = QFile::encodeName(toqstr(s));
151 return string(encoded.begin(), encoded.end());
155 docstring const normalize_c(docstring const & s)
157 return qstring_to_ucs4(toqstr(s).normalized(QString::NormalizationForm_C));
161 bool operator==(lyx::docstring const & l, char const * r)
163 lyx::docstring::const_iterator it = l.begin();
164 lyx::docstring::const_iterator end = l.end();
165 for (; it != end; ++it, ++r) {
166 LASSERT(static_cast<unsigned char>(*r) < 0x80, /**/);
169 if (*it != static_cast<lyx::docstring::value_type>(*r))
176 lyx::docstring operator+(lyx::docstring const & l, char const * r)
179 for (char const * c = r; *c; ++c) {
180 LASSERT(static_cast<unsigned char>(*c) < 0x80, /**/);
187 lyx::docstring operator+(char const * l, lyx::docstring const & r)
190 for (char const * c = l; *c; ++c) {
191 LASSERT(static_cast<unsigned char>(*c) < 0x80, /**/);
199 lyx::docstring operator+(lyx::docstring const & l, char r)
201 LASSERT(static_cast<unsigned char>(r) < 0x80, /**/);
203 s += docstring::value_type(r);
208 lyx::docstring operator+(char l, lyx::docstring const & r)
210 LASSERT(static_cast<unsigned char>(l) < 0x80, /**/);
211 return lyx::docstring::value_type(l) + r;
215 lyx::docstring & operator+=(lyx::docstring & l, char const * r)
217 for (char const * c = r; *c; ++c) {
218 LASSERT(static_cast<unsigned char>(*c) < 0x80, /**/);
225 lyx::docstring & operator+=(lyx::docstring & l, char r)
227 LASSERT(static_cast<unsigned char>(r) < 0x80, /**/);
234 #if ! defined(USE_WCHAR_T) && defined(__GNUC__)
236 // gcc does not have proper locale facets for lyx::char_type if
237 // sizeof(wchar_t) == 2, so we have to implement them on our own.
240 // We get undefined references to these virtual methods. This looks like
241 // a bug in gcc. The implementation here does not do anything useful, since
242 // it is overriden in ascii_ctype_facet.
244 template<> ctype<lyx::char_type>::~ctype() {}
246 ctype<lyx::char_type>::do_is(ctype<lyx::char_type>::mask, lyx::char_type) const { return false; }
247 template<> lyx::char_type const *
248 ctype<lyx::char_type>::do_is(const lyx::char_type *, const lyx::char_type *, ctype<lyx::char_type>::mask *) const { return 0; }
249 template<> const lyx::char_type *
250 ctype<lyx::char_type>::do_scan_is(ctype<lyx::char_type>::mask, const lyx::char_type *, const lyx::char_type *) const { return 0; }
251 template<> const lyx::char_type *
252 ctype<lyx::char_type>::do_scan_not(ctype<lyx::char_type>::mask, const lyx::char_type *, const lyx::char_type *) const { return 0; }
253 template<> lyx::char_type ctype<lyx::char_type>::do_toupper(lyx::char_type) const { return 0; }
254 template<> const lyx::char_type * ctype<lyx::char_type>::do_toupper(lyx::char_type *, lyx::char_type const *) const { return 0; }
255 template<> lyx::char_type ctype<lyx::char_type>::do_tolower(lyx::char_type) const { return 0; }
256 template<> const lyx::char_type * ctype<lyx::char_type>::do_tolower(lyx::char_type *, lyx::char_type const *) const { return 0; }
257 template<> lyx::char_type ctype<lyx::char_type>::do_widen(char) const { return 0; }
258 template<> const char *
259 ctype<lyx::char_type>::do_widen(const char *, const char *, lyx::char_type *) const { return 0; }
261 ctype<lyx::char_type>::do_narrow(const lyx::char_type, char) const { return 0; }
262 template<> const lyx::char_type *
263 ctype<lyx::char_type>::do_narrow(const lyx::char_type *, const lyx::char_type *, char, char *) const { return 0; }
269 class ctype_failure : public bad_cast {
271 ctype_failure() throw() : bad_cast() {}
272 virtual ~ctype_failure() throw() {}
273 virtual const char* what() const throw()
275 return "The ctype<lyx::char_type> locale facet does only support ASCII characters on this platform.";
280 class num_put_failure : public bad_cast {
282 num_put_failure() throw() : bad_cast() {}
283 virtual ~num_put_failure() throw() {}
284 virtual const char* what() const throw()
286 return "The num_put locale facet does only support ASCII characters on this platform.";
291 /// ctype facet for UCS4 characters. The implementation does only support pure
292 /// ASCII, since we do not need anything else for now.
293 /// The code is partly stolen from ctype<wchar_t> from gcc.
294 class ascii_ctype_facet : public ctype<lyx::char_type>
297 typedef lyx::char_type char_type;
298 typedef wctype_t wmask_type;
299 explicit ascii_ctype_facet(size_t refs = 0) : ctype<char_type>(refs)
301 M_initialize_ctype();
306 wint_t M_widen[1 + static_cast<unsigned char>(-1)];
308 wmask_type M_wmask[16];
309 wmask_type M_convert_to_wmask(const mask m) const
313 case space: ret = wctype("space"); break;
314 case print: ret = wctype("print"); break;
315 case cntrl: ret = wctype("cntrl"); break;
316 case upper: ret = wctype("upper"); break;
317 case lower: ret = wctype("lower"); break;
318 case alpha: ret = wctype("alpha"); break;
319 case digit: ret = wctype("digit"); break;
320 case punct: ret = wctype("punct"); break;
321 case xdigit: ret = wctype("xdigit"); break;
322 case alnum: ret = wctype("alnum"); break;
323 case graph: ret = wctype("graph"); break;
324 default: ret = wmask_type();
328 void M_initialize_ctype()
331 for (i = 0; i < 128; ++i) {
332 const int c = wctob(i);
336 M_narrow[i] = static_cast<char>(c);
342 for (size_t i = 0; i < sizeof(M_widen) / sizeof(wint_t); ++i)
343 M_widen[i] = btowc(i);
345 for (size_t i = 0; i <= 15; ++i) {
346 M_bit[i] = static_cast<mask>(1 << i);
347 M_wmask[i] = M_convert_to_wmask(M_bit[i]);
350 virtual ~ascii_ctype_facet() {}
351 char_type do_toupper(char_type c) const
354 throw ctype_failure();
355 return toupper(static_cast<int>(c));
357 char_type const * do_toupper(char_type * lo, char_type const * hi) const
361 throw ctype_failure();
362 *lo = toupper(static_cast<int>(*lo));
367 char_type do_tolower(char_type c) const
370 throw ctype_failure();
373 char_type const * do_tolower(char_type * lo, char_type const * hi) const
377 throw ctype_failure();
383 bool do_is(mask m, char_type c) const
386 throw ctype_failure();
387 // The code below works because c is in the ASCII range.
388 // We could not use iswctype() which is designed for a 2byte
389 // whar_t without encoding conversion otherwise.
391 // Generically, 15 (instead of 10) since we don't know the numerical
392 // encoding of the various categories in /usr/include/ctype.h.
393 const size_t bitmasksize = 15;
394 for (size_t bitcur = 0; bitcur <= bitmasksize; ++bitcur)
395 if (m & M_bit[bitcur] &&
396 iswctype(static_cast<int>(c), M_wmask[bitcur])) {
402 char_type const * do_is(char_type const * lo, char_type const * hi, mask * vec) const
404 for (;lo < hi; ++vec, ++lo) {
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.
410 // Generically, 15 (instead of 10) since we don't know the numerical
411 // encoding of the various categories in /usr/include/ctype.h.
412 const size_t bitmasksize = 15;
414 for (size_t bitcur = 0; bitcur <= bitmasksize; ++bitcur)
415 if (iswctype(static_cast<int>(*lo), M_wmask[bitcur]))
421 char_type const * do_scan_is(mask m, char_type const * lo, char_type const * hi) const
423 while (lo < hi && !this->do_is(m, *lo))
427 char_type const * do_scan_not(mask m, char_type const * lo, char_type const * hi) const
429 while (lo < hi && this->do_is(m, *lo) != 0)
433 char_type do_widen(char c) const
435 if (static_cast<unsigned char>(c) < 0x80)
437 throw ctype_failure();
439 const char* do_widen(const char* lo, const char* hi, char_type* dest) const
442 if (static_cast<unsigned char>(*lo) >= 0x80)
443 throw ctype_failure();
450 char do_narrow(char_type wc, char) const
453 return static_cast<char>(wc);
454 throw ctype_failure();
456 const char_type * do_narrow(const char_type * lo, const char_type * hi, char, char * dest) const
460 *dest = static_cast<char>(*lo);
462 throw ctype_failure();
471 /// Facet for outputting numbers to odocstreams as ascii.
472 /// Here we simply need defining the virtual do_put functions.
473 class ascii_num_put_facet : public num_put<lyx::char_type, ostreambuf_iterator<lyx::char_type, char_traits<lyx::char_type> > >
475 typedef ostreambuf_iterator<lyx::char_type, char_traits<lyx::char_type> > iter_type;
477 ascii_num_put_facet(size_t refs = 0) : num_put<lyx::char_type, iter_type>(refs) {}
479 /// Facet for converting numbers to ascii strings.
480 class string_num_put_facet : public num_put<char, basic_string<char>::iterator>
483 string_num_put_facet() : num_put<char, basic_string<char>::iterator>(1) {}
488 do_put(iter_type oit, ios_base & b, char_type fill, bool v) const
490 return do_put_helper(oit, b, fill, v);
494 do_put(iter_type oit, ios_base & b, char_type fill, long v) const
496 return do_put_helper(oit, b, fill, v);
500 do_put(iter_type oit, ios_base & b, char_type fill, unsigned long v) const
502 return do_put_helper(oit, b, fill, v);
505 #ifdef _GLIBCXX_USE_LONG_LONG
507 do_put(iter_type oit, ios_base & b, char_type fill, long long v) const
509 return do_put_helper(oit, b, fill, v);
513 do_put(iter_type oit, ios_base & b, char_type fill, unsigned long long v) const
515 return do_put_helper(oit, b, fill, v);
520 do_put(iter_type oit, ios_base & b, char_type fill, double v) const
522 return do_put_helper(oit, b, fill, v);
526 do_put(iter_type oit, ios_base & b, char_type fill, long double v) const
528 return do_put_helper(oit, b, fill, v);
532 do_put(iter_type oit, ios_base & b, char_type fill, void const * v) const
534 return do_put_helper(oit, b, fill, v);
538 template <typename ValueType>
540 do_put_helper(iter_type oit, ios_base & b, char_type fill, ValueType v) const
543 throw num_put_failure();
545 streamsize const sz = b.width() > b.precision() ?
546 b.width() : b.precision();
547 // 64 is large enough, unless width or precision are bigger
548 streamsize const wd = (sz > 56 ? sz : 56) + 8;
550 string_num_put_facet f;
551 string::const_iterator cit = s.begin();
552 string::const_iterator end =
553 f.put(s.begin(), b, fill, v);
554 for (; cit != end; ++cit, ++oit)
562 /// Facet for inputting ascii representations of numbers from idocstreams.
563 /// Here we simply need defining the virtual do_get functions.
564 class ascii_num_get_facet : public num_get<lyx::char_type, istreambuf_iterator<lyx::char_type, char_traits<lyx::char_type> > >
566 typedef istreambuf_iterator<lyx::char_type, char_traits<lyx::char_type> > iter_type;
568 ascii_num_get_facet(size_t refs = 0) : num_get<lyx::char_type, iter_type>(refs) {}
570 /// Facet for converting ascii representation of numbers to a value.
571 class string_num_get_facet : public num_get<char, basic_string<char>::iterator>
574 string_num_get_facet() : num_get<char, basic_string<char>::iterator>(1) {}
577 /// Numpunct facet defining the I/O format.
578 class numpunct_facet : public numpunct<char>
581 numpunct_facet() : numpunct<char>(1) {}
586 do_get(iter_type iit, iter_type eit, ios_base & b,
587 ios_base::iostate & err, bool & v) const
589 if (b.flags() & ios_base::boolalpha) {
591 lyx::docstring const truename = from_local8bit(p.truename());
592 lyx::docstring const falsename = from_local8bit(p.falsename());
597 size_t const tsize = truename.size();
598 size_t const fsize = falsename.size();
599 for (; iit != eit; ++iit) {
602 bool true_ok = support::prefixIs(truename, s);
603 bool false_ok = support::prefixIs(falsename, s);
604 if (!true_ok && !false_ok) {
609 if ((true_ok && n == tsize) ||
610 (false_ok && n == fsize)) {
616 err = ios_base::goodbit;
617 v = truename == s ? true : false;
619 err = ios_base::failbit;
621 err |= ios_base::eofbit;
625 iter_type end = this->do_get(iit, eit, b, err, l);
626 if (!(err & ios_base::failbit)) {
632 err |= ios_base::failbit;
639 do_get(iter_type iit, iter_type eit, ios_base & b,
640 ios_base::iostate & err, long & v) const
642 return do_get_integer(iit, eit, b, err, v);
646 do_get(iter_type iit, iter_type eit, ios_base & b,
647 ios_base::iostate & err, unsigned short & v) const
649 return do_get_integer(iit, eit, b, err, v);
653 do_get(iter_type iit, iter_type eit, ios_base & b,
654 ios_base::iostate & err, unsigned int & v) const
656 return do_get_integer(iit, eit, b, err, v);
660 do_get(iter_type iit, iter_type eit, ios_base & b,
661 ios_base::iostate & err, unsigned long & v) const
663 return do_get_integer(iit, eit, b, err, v);
666 #ifdef _GLIBCXX_USE_LONG_LONG
668 do_get(iter_type iit, iter_type eit, ios_base & b,
669 ios_base::iostate & err, long long & v) const
671 return do_get_integer(iit, eit, b, err, v);
675 do_get(iter_type iit, iter_type eit, ios_base & b,
676 ios_base::iostate & err, unsigned long long & v) const
678 return do_get_integer(iit, eit, b, err, v);
683 do_get(iter_type iit, iter_type eit, ios_base & b,
684 ios_base::iostate & err, float & v) const
686 return do_get_float(iit, eit, b, err, v);
690 do_get(iter_type iit, iter_type eit, ios_base & b,
691 ios_base::iostate & err, double & v) const
693 return do_get_float(iit, eit, b, err, v);
697 do_get(iter_type iit, iter_type eit, ios_base & b,
698 ios_base::iostate & err, long double & v) const
700 return do_get_float(iit, eit, b, err, v);
704 do_get(iter_type iit, iter_type eit, ios_base & b,
705 ios_base::iostate & err, void * & v) const
708 iter_type end = do_get_integer(iit, eit, b, err, val);
709 if (!(err & ios_base::failbit))
710 v = reinterpret_cast<void *>(val);
715 template <typename ValueType>
717 do_get_integer(iter_type iit, iter_type eit, ios_base & b,
718 ios_base::iostate & err, ValueType & v) const
722 for (; iit != eit && isNumpunct(*iit); ++iit)
723 s += static_cast<char>(*iit);
724 // We add another character, not part of the numpunct facet,
725 // in order to avoid setting the eofbit in the stream state,
726 // which would prevent any further read. The space seems a
729 string_num_get_facet f;
730 f.get(s.begin(), s.end(), b, err, v);
732 err |= ios_base::eofbit;
737 bool isNumpunct(lyx::char_type const c) const
739 /// Only account for the standard numpunct "C" locale facet.
740 return c < 0x80 && (c == '-' || c == '+' || isdigit(c)
741 || ('a' <= c && c <= 'f') || ('A' <= c && c <= 'F')
742 || c == 'x' || c == 'X');
745 template <typename ValueType>
747 do_get_float(iter_type iit, iter_type eit, ios_base & b,
748 ios_base::iostate & err, ValueType & v) const
750 // Gather a string of the form
751 // [+-]? [0-9]* .? [0-9]* ([eE] [+-]? [0-9]+)?
756 char const dot = p.decimal_point();
757 char const sep = p.thousands_sep();
758 // Get an optional sign
759 if (iit != eit && (*iit == '-' || *iit == '+')) {
760 s += static_cast<char>(*iit);
763 for (; iit != eit && isDigitOrSep(*iit, sep); ++iit)
764 s += static_cast<char>(*iit);
765 if (iit != eit && *iit == dot) {
768 for (; iit != eit && isDigitOrSep(*iit, 0); ++iit)
769 s += static_cast<char>(*iit);
770 if (iit != eit && (*iit == 'e' || *iit == 'E')) {
771 s += static_cast<char>(*iit);
773 for (; iit != eit && isDigitOrSep(*iit, 0); ++iit)
774 s += static_cast<char>(*iit);
778 string_num_get_facet f;
779 f.get(s.begin(), s.end(), b, err, v);
781 err |= ios_base::eofbit;
786 bool isDigitOrSep(lyx::char_type const c, char const sep) const
788 return (c >= '0' && c <= '9') || (c != 0 && c == sep);
793 /// class to add our facets to the global locale
794 class locale_initializer {
799 locale const loc1(global, new ascii_ctype_facet);
800 locale const loc2(loc1, new ascii_num_put_facet);
801 locale const loc3(loc2, new ascii_num_get_facet);
802 locale::global(loc3);
809 /// make sure that our facets get used
810 static locale_initializer initializer;