]> git.lyx.org Git - lyx.git/blob - src/support/docstring.C
add GuiView parent to QToc for proper memory management.
[lyx.git] / src / support / docstring.C
1 /**
2  * \file docstring.C
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 "docstring.h"
14 #include "qstring_helpers.h"
15 #include "unicode.h"
16
17 #include <locale>
18 #include <iostream>
19
20 #include <QFile>
21
22 #include <boost/assert.hpp>
23
24
25 namespace lyx {
26
27
28 docstring const from_ascii(char const * ascii)
29 {
30         docstring s;
31         for (char const * c = ascii; *c; ++c) {
32                 BOOST_ASSERT(static_cast<unsigned char>(*c) < 0x80);
33                 s.push_back(*c);
34         }
35         return s;
36 }
37
38
39 docstring const from_ascii(std::string const & ascii)
40 {
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());
45 }
46
47
48 std::string const to_ascii(docstring const & ucs4)
49 {
50         int const len = ucs4.length();
51         std::string ascii;
52         ascii.resize(len);
53         for (int i = 0; i < len; ++i) {
54                 BOOST_ASSERT(ucs4[i] < 0x80);
55                 ascii[i] = static_cast<char>(ucs4[i]);
56         }
57         return ascii;
58 }
59
60
61 IconvProcessor & utf8ToUcs4()
62 {
63         static IconvProcessor iconv(ucs4_codeset, "UTF-8");
64         return iconv;
65 }
66
67
68
69 void utf8_to_ucs4(std::string const & utf8, docstring & ucs4)
70 {
71         size_t n = utf8.size();
72         // as utf8 is a multi-byte encoding, there would be at most
73         // n characters:
74         ucs4.resize(n);
75         if (n == 0)
76                 return;
77
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);
83
84         // adjust to the real converted size
85         ucs4.resize(bytes/4);
86 }
87
88
89 docstring const from_utf8(std::string const & utf8)
90 {
91         docstring ucs4;
92         utf8_to_ucs4(utf8, ucs4);
93         return ucs4;
94 }
95
96
97 std::string const to_utf8(docstring const & ucs4)
98 {
99         std::vector<char> const utf8 =
100                 ucs4_to_utf8(ucs4.data(), ucs4.size());
101         return std::string(utf8.begin(), utf8.end());
102 }
103
104
105 docstring const from_local8bit(std::string const & s)
106 {
107         return qstring_to_ucs4(QString::fromLocal8Bit(s.data(), s.length()));
108 }
109
110
111 const char* to_local8bit_failure::what() const throw()
112 {
113         return "A string could not be converted from unicode to the local 8 bit encoding.";
114 }
115
116
117 std::string const to_local8bit(docstring const & s)
118 {
119         // This conversion can fail, depending on input.
120         if (s.empty())
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());
126 }
127
128
129 docstring const from_filesystem8bit(std::string const & s)
130 {
131         QByteArray const encoded(s.c_str(), s.length());
132         return qstring_to_ucs4(QFile::decodeName(encoded));
133 }
134
135
136 std::string const to_filesystem8bit(docstring const & s)
137 {
138         QByteArray const encoded = QFile::encodeName(toqstr(s));
139         return std::string(encoded.begin(), encoded.end());
140 }
141
142
143 docstring const normalize_kc(docstring const & s)
144 {
145         return qstring_to_ucs4(toqstr(s).normalized(QString::NormalizationForm_KC));
146 }
147
148
149 bool operator==(lyx::docstring const & l, char const * r)
150 {
151         int const len = l.length();
152         for (int i = 0; i < len; ++i) {
153                 BOOST_ASSERT(static_cast<unsigned char>(r[i]) < 0x80);
154                 if (!r[i])
155                         return false;
156                 if (l[i] != lyx::docstring::value_type(r[i]))
157                         return false;
158         }
159         return r[len] == '\0';
160 }
161
162
163 lyx::docstring operator+(lyx::docstring const & l, char const * r)
164 {
165         lyx::docstring s(l);
166         for (char const * c = r; *c; ++c) {
167                 BOOST_ASSERT(static_cast<unsigned char>(*c) < 0x80);
168                 s.push_back(*c);
169         }
170         return s;
171 }
172
173
174 lyx::docstring operator+(char const * l, lyx::docstring const & r)
175 {
176         lyx::docstring s;
177         for (char const * c = l; *c; ++c) {
178                 BOOST_ASSERT(static_cast<unsigned char>(*c) < 0x80);
179                 s.push_back(*c);
180         }
181         s += r;
182         return s;
183 }
184
185
186 lyx::docstring operator+(lyx::docstring const & l, char r)
187 {
188         BOOST_ASSERT(static_cast<unsigned char>(r) < 0x80);
189         return l + lyx::docstring::value_type(r);
190 }
191
192
193 lyx::docstring operator+(char l, lyx::docstring const & r)
194 {
195         BOOST_ASSERT(static_cast<unsigned char>(l) < 0x80);
196         return lyx::docstring::value_type(l) + r;
197 }
198
199
200 lyx::docstring & operator+=(lyx::docstring & l, char const * r)
201 {
202         for (char const * c = r; *c; ++c) {
203                 BOOST_ASSERT(static_cast<unsigned char>(*c) < 0x80);
204                 l.push_back(*c);
205         }
206         return l;
207 }
208
209
210 lyx::docstring & operator+=(lyx::docstring & l, char r)
211 {
212         BOOST_ASSERT(static_cast<unsigned char>(r) < 0x80);
213         l.push_back(r);
214         return l;
215 }
216
217 } // namespace lyx
218
219 #if (!defined(HAVE_WCHAR_T) || SIZEOF_WCHAR_T != 4) && defined(__GNUC__)
220
221 // gcc does not have proper locale facets for lyx::char_type if
222 // sizeof(wchar_t) == 2, so we have to implement them on our own.
223
224
225 // We get undefined references to these virtual methods. This looks like
226 // a bug in gcc. The implementation here does not do anything useful, since
227 // it is overriden in ascii_ctype_facet.
228 namespace std {
229 template<> ctype<lyx::char_type>::~ctype() {}
230 template<> bool
231 ctype<lyx::char_type>::do_is(ctype<lyx::char_type>::mask, lyx::char_type) const { return false; }
232 template<> lyx::char_type const *
233 ctype<lyx::char_type>::do_is(const lyx::char_type *, const lyx::char_type *, ctype<lyx::char_type>::mask *) const { return 0; }
234 template<> const lyx::char_type *
235 ctype<lyx::char_type>::do_scan_is(ctype<lyx::char_type>::mask, const lyx::char_type *, const lyx::char_type *) const { return 0; }
236 template<> const lyx::char_type *
237 ctype<lyx::char_type>::do_scan_not(ctype<lyx::char_type>::mask, const lyx::char_type *, const lyx::char_type *) const { return 0; }
238 template<> lyx::char_type ctype<lyx::char_type>::do_toupper(lyx::char_type) const { return 0; }
239 template<> const lyx::char_type * ctype<lyx::char_type>::do_toupper(lyx::char_type *, lyx::char_type const *) const { return 0; }
240 template<> lyx::char_type ctype<lyx::char_type>::do_tolower(lyx::char_type) const { return 0; }
241 template<> const lyx::char_type * ctype<lyx::char_type>::do_tolower(lyx::char_type *, lyx::char_type const *) const { return 0; }
242 template<> lyx::char_type ctype<lyx::char_type>::do_widen(char) const { return 0; }
243 template<> const char *
244 ctype<lyx::char_type>::do_widen(const char *, const char *, lyx::char_type *) const { return 0; }
245 template<> char
246 ctype<lyx::char_type>::do_narrow(const lyx::char_type, char) const { return 0; }
247 template<> const lyx::char_type *
248 ctype<lyx::char_type>::do_narrow(const lyx::char_type *, const lyx::char_type *, char, char *) const { return 0; }
249 }
250
251
252 namespace lyx {
253
254 class ctype_failure : public std::bad_cast {
255 public:
256         ctype_failure() throw() : std::bad_cast() {}
257         virtual ~ctype_failure() throw() {}
258         virtual const char* what() const throw()
259         {
260                 return "The ctype<lyx::char_type> locale facet does only support ASCII characters on this platform.";
261         }
262 };
263
264
265 class num_put_failure : public std::bad_cast {
266 public:
267         num_put_failure() throw() : std::bad_cast() {}
268         virtual ~num_put_failure() throw() {}
269         virtual const char* what() const throw()
270         {
271                 return "The num_put locale facet does only support ASCII characters on this platform.";
272         }
273 };
274
275
276 /// ctype facet for UCS4 characters. The implementation does only support pure
277 /// ASCII, since we do not need anything else for now.
278 /// The code is partly stolen from std::ctype<wchar_t> from gcc.
279 class ascii_ctype_facet : public std::ctype<lyx::char_type>
280 {
281 public:
282         typedef lyx::char_type char_type;
283         typedef wctype_t wmask_type;
284         explicit ascii_ctype_facet(size_t refs = 0) : std::ctype<char_type>(refs)
285         {
286                 M_initialize_ctype();
287         }
288 protected:
289         bool       M_narrow_ok;
290         char       M_narrow[128];
291         wint_t     M_widen[1 + static_cast<unsigned char>(-1)];
292         mask       M_bit[16];
293         wmask_type M_wmask[16];
294         wmask_type M_convert_to_wmask(const mask m) const
295         {
296                 wmask_type ret;
297                 switch (m) {
298                         case space:  ret = wctype("space");  break;
299                         case print:  ret = wctype("print");  break;
300                         case cntrl:  ret = wctype("cntrl");  break;
301                         case upper:  ret = wctype("upper");  break;
302                         case lower:  ret = wctype("lower");  break;
303                         case alpha:  ret = wctype("alpha");  break;
304                         case digit:  ret = wctype("digit");  break;
305                         case punct:  ret = wctype("punct");  break;
306                         case xdigit: ret = wctype("xdigit"); break;
307                         case alnum:  ret = wctype("alnum");  break;
308                         case graph:  ret = wctype("graph");  break;
309                         default:     ret = wmask_type();
310                 }
311                 return ret;
312         }
313         void M_initialize_ctype()
314         {
315                 wint_t i;
316                 for (i = 0; i < 128; ++i) {
317                         const int c = wctob(i);
318                         if (c == EOF)
319                                 break;
320                         else
321                                 M_narrow[i] = static_cast<char>(c);
322                 }
323                 if (i == 128)
324                         M_narrow_ok = true;
325                 else
326                         M_narrow_ok = false;
327                 for (size_t i = 0; i < sizeof(M_widen) / sizeof(wint_t); ++i)
328                         M_widen[i] = btowc(i);
329
330                 for (size_t i = 0; i <= 15; ++i) {
331                         M_bit[i] = static_cast<mask>(1 << i);
332                         M_wmask[i] = M_convert_to_wmask(M_bit[i]);
333                 }
334         }
335         virtual ~ascii_ctype_facet() {}
336         char_type do_toupper(char_type c) const
337         {
338                 if (c >= 0x80)
339                         throw ctype_failure();
340                 return toupper(static_cast<int>(c));
341         }
342         char_type const * do_toupper(char_type * lo, char_type const * hi) const
343         {
344                 while (lo < hi) {
345                         if (*lo >= 0x80)
346                                 throw ctype_failure();
347                         *lo = toupper(static_cast<int>(*lo));
348                         ++lo;
349                 }
350                 return hi;
351         }
352         char_type do_tolower(char_type c) const
353         {
354                 if (c >= 0x80)
355                         throw ctype_failure();
356                 return tolower(c);
357         }
358         char_type const * do_tolower(char_type * lo, char_type const * hi) const
359         {
360                 while (lo < hi) {
361                         if (*lo >= 0x80)
362                                 throw ctype_failure();
363                         *lo = tolower(*lo);
364                         ++lo;
365                 }
366                 return hi;
367         }
368         bool do_is(mask m, char_type c) const
369         {
370                 if (c >= 0x80)
371                         throw ctype_failure();
372                 // The code below works because c is in the ASCII range.
373                 // We could not use iswctype() which is designed for a 2byte
374                 // whar_t without encoding conversion otherwise.
375                 bool ret = false;
376                 // Generically, 15 (instead of 10) since we don't know the numerical
377                 // encoding of the various categories in /usr/include/ctype.h.
378                 const size_t bitmasksize = 15;
379                 for (size_t bitcur = 0; bitcur <= bitmasksize; ++bitcur)
380                         if (m & M_bit[bitcur] &&
381                             iswctype(static_cast<int>(c), M_wmask[bitcur])) {
382                                 ret = true;
383                                 break;
384                         }
385                 return ret;
386         }
387         char_type const * do_is(char_type const * lo, char_type const * hi, mask * vec) const
388         {
389                 for (;lo < hi; ++vec, ++lo) {
390                         if (*lo >= 0x80)
391                                 throw ctype_failure();
392                         // The code below works because c is in the ASCII range.
393                         // We could not use iswctype() which is designed for a 2byte
394                         // whar_t without encoding conversion otherwise.
395                         // Generically, 15 (instead of 10) since we don't know the numerical
396                         // encoding of the various categories in /usr/include/ctype.h.
397                         const size_t bitmasksize = 15;
398                         mask m = 0;
399                         for (size_t bitcur = 0; bitcur <= bitmasksize; ++bitcur)
400                                 if (iswctype(static_cast<int>(*lo), M_wmask[bitcur]))
401                                         m |= M_bit[bitcur];
402                         *vec = m;
403                 }
404                 return hi;
405         }
406         char_type const * do_scan_is(mask m, char_type const * lo, char_type const * hi) const
407         {
408                 while (lo < hi && !this->do_is(m, *lo))
409                         ++lo;
410                 return lo;
411         }
412         char_type const * do_scan_not(mask m, char_type const * lo, char_type const * hi) const
413         {
414                 while (lo < hi && this->do_is(m, *lo) != 0)
415                         ++lo;
416                 return lo;
417         }
418         char_type do_widen(char c) const
419         {
420                 if (static_cast<unsigned char>(c) < 0x80)
421                         return c;
422                 throw ctype_failure();
423         }
424         const char* do_widen(const char* lo, const char* hi, char_type* dest) const
425         {
426                 while (lo < hi) {
427                         if (static_cast<unsigned char>(*lo) >= 0x80)
428                                 throw ctype_failure();
429                         *dest = *lo;
430                         ++lo;
431                         ++dest;
432                 }
433                 return hi;
434         }
435         char do_narrow(char_type wc, char) const
436         {
437                 if (wc < 0x80)
438                         return static_cast<char>(wc);
439                 throw ctype_failure();
440         }
441         const char_type * do_narrow(const char_type * lo, const char_type * hi, char, char * dest) const
442         {
443                 while (lo < hi) {
444                         if (*lo < 0x80)
445                                 *dest = static_cast<char>(*lo);
446                         else
447                                 throw ctype_failure();
448                         ++lo;
449                         ++dest;
450                 }
451                 return hi;
452         }
453 };
454
455
456 /// Facet for outputting numbers to odocstreams as ascii.
457 /// Here we simply need defining the virtual do_put functions.
458 class ascii_num_put_facet : public std::num_put<lyx::char_type, std::ostreambuf_iterator<lyx::char_type, std::char_traits<lyx::char_type> > >
459 {
460         typedef std::ostreambuf_iterator<lyx::char_type, std::char_traits<lyx::char_type> > iter_type;
461 public:
462         ascii_num_put_facet(size_t refs = 0) : std::num_put<lyx::char_type, iter_type>(refs) {}
463
464         /// Facet for converting numbers to ascii strings.
465         class string_num_put_facet : public std::num_put<char, std::basic_string<char>::iterator>
466         {
467         public:
468                 string_num_put_facet() : std::num_put<char, std::basic_string<char>::iterator>(1) {}
469         };
470
471 protected:
472         iter_type
473         do_put(iter_type oit, std::ios_base & b, char_type fill, bool v) const
474         {
475                 return do_put_helper(oit, b, fill, v);
476         }
477
478         iter_type
479         do_put(iter_type oit, std::ios_base & b, char_type fill, long v) const
480         {
481                 return do_put_helper(oit, b, fill, v);
482         }
483
484         iter_type
485         do_put(iter_type oit, std::ios_base & b, char_type fill, unsigned long v) const
486         {
487                 return do_put_helper(oit, b, fill, v);
488         }
489
490 #ifdef _GLIBCXX_USE_LONG_LONG
491         iter_type
492         do_put(iter_type oit, std::ios_base & b, char_type fill, long long v) const
493         {
494                 return do_put_helper(oit, b, fill, v);
495         }
496
497         iter_type
498         do_put(iter_type oit, std::ios_base & b, char_type fill, unsigned long long v) const
499         {
500                 return do_put_helper(oit, b, fill, v);
501         }
502 #endif
503
504         iter_type
505         do_put(iter_type oit, std::ios_base & b, char_type fill, double v) const
506         {
507                 return do_put_helper(oit, b, fill, v);
508         }
509
510         iter_type
511         do_put(iter_type oit, std::ios_base & b, char_type fill, long double v) const
512         {
513                 return do_put_helper(oit, b, fill, v);
514         }
515
516         iter_type
517         do_put(iter_type oit, std::ios_base & b, char_type fill, void const * v) const
518         {
519                 return do_put_helper(oit, b, fill, v);
520         }
521
522 private:
523         template <typename ValueType>
524         iter_type
525         do_put_helper(iter_type oit, std::ios_base & b, char_type fill, ValueType v) const
526         {
527                 if (fill >= 0x80)
528                         throw num_put_failure();
529
530                 std::streamsize const sz = b.width() > b.precision() ?
531                                            b.width() : b.precision();
532                 // 64 is large enough, unless width or precision are bigger
533                 std::streamsize const wd = (sz > 56 ? sz : 56) + 8;
534                 std::string s(wd, '\0');
535                 string_num_put_facet f;
536                 std::string::const_iterator cit = s.begin();
537                 std::string::const_iterator end =
538                         f.put(s.begin(), b, fill, v);
539                 for (; cit != end; ++cit, ++oit)
540                         *oit = *cit;
541
542                 return oit;
543         }
544 };
545
546
547 /// Facet for inputting ascii representations of numbers from idocstreams.
548 /// Here we simply need defining the virtual do_get functions.
549 class ascii_num_get_facet : public std::num_get<lyx::char_type, std::istreambuf_iterator<lyx::char_type, std::char_traits<lyx::char_type> > >
550 {
551         typedef std::istreambuf_iterator<lyx::char_type, std::char_traits<lyx::char_type> > iter_type;
552 public:
553         ascii_num_get_facet(size_t refs = 0) : std::num_get<lyx::char_type, iter_type>(refs) {}
554
555         /// Facet for converting ascii representation of numbers to a value.
556         class string_num_get_facet : public std::num_get<char, std::basic_string<char>::iterator>
557         {
558         public:
559                 string_num_get_facet() : std::num_get<char, std::basic_string<char>::iterator>(1) {}
560         };
561
562 private:
563         bool isNumpunct(lyx::char_type const c) const
564         {
565                 /// Only account for the standard numpunct "C" locale facet.
566                 return c < 0x80 && (c == '-' || c == '+' || isdigit(c)
567                         || ('a' <= c && c <= 'f') || ('A' <= c && c <= 'F')
568                         || c == 'x' || c == 'X');
569         }
570
571 protected:
572         iter_type
573         do_get(iter_type iit, iter_type eit, std::ios_base & b,
574                 std::ios_base::iostate & err, long & v) const
575         {
576                 std::string s;
577                 s.reserve(64);
578                 for (; iit != eit && isNumpunct(*iit); ++iit)
579                         s += static_cast<char>(*iit);
580                 // We add another character, not part of the numpunct facet,
581                 // in order to avoid setting the eofbit in the stream state,
582                 // which would prevent any further read. The space seems a
583                 // good choice here.
584                 s += ' ';
585                 string_num_get_facet f;
586                 f.get(s.begin(), s.end(), b, err, v);
587
588                 return iit;
589         }
590 };
591
592
593 /// class to add our facets to the global locale
594 class locale_initializer {
595 public:
596         locale_initializer()
597         {
598                 std::locale global;
599                 std::locale const loc1(global, new ascii_ctype_facet);
600                 std::locale const loc2(loc1, new ascii_num_put_facet);
601                 std::locale const loc3(loc2, new ascii_num_get_facet);
602                 std::locale::global(loc3);
603         }
604 };
605
606
607 namespace {
608
609 /// make sure that our facets get used
610 static locale_initializer initializer;
611
612 }
613 }
614 #endif