4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
7 * \author Lars Gullik Bjønnes
9 * Full author contact details are available in file CREDITS.
11 * A collection of unicode conversion functions, using iconv.
14 #ifndef LYX_SUPPORT_UNICODE_H
15 #define LYX_SUPPORT_UNICODE_H
17 #include "support/strfwd.h"
18 #include "support/unique_ptr.h"
28 * Wrapper for iconv(3).
30 * According to the POSIX standard, all specified functions are thread-safe,
31 * with some exceptions. The iconv() function is not listed as an exception:
32 * http://pubs.opengroup.org/onlinepubs/9699919799/xrat/V4_xsh_chap02.html#tag_22_02_09_09
33 * http://man7.org/linux/man-pages/man7/pthreads.7.html
35 * Therefore, you can use as many instances of this class in parallel as you
36 * like. However, you need to ensure that each instance is only used by one
37 * thread at any given time. If this condition is not met you get nasty
38 * mixtures of different thread data as in bug 7240.
40 * From a performance point of view it is best to use one static instance
41 * per thread for each in/out encoding pair. This can e.g. be achieved by
42 * using helpers for thread-local storage such as QThreadStorage or
43 * boost::thread_specific_ptr. A single static instance protected by a mutex
44 * would work as well, and might be preferrable for exotic encoding pairs.
45 * Creating local IconvProcessor instances should be avoided because of the
46 * overhead in iconv_open().
51 /// \return true if the processor is ready to use.
53 std::string const tocode_;
54 std::string const fromcode_;
56 unique_ptr<Handler> h_;
58 IconvProcessor(std::string tocode, std::string fromcode);
59 /// convert any data from \c fromcode to \c tocode unicode format.
60 /// \return the number of bytes of the converted output buffer.
61 int convert(char const * in_buffer, size_t in_size,
62 char * out_buffer, size_t max_out_size);
64 std::string to() const { return tocode_; }
67 /// Get the global IconvProcessor instance of the current thread for
68 /// utf8->ucs4 conversions
69 IconvProcessor & utf8ToUcs4();
71 // A single codepoint conversion for utf8_to_ucs4 does not make
72 // sense, so that function is left out.
74 std::vector<char_type> utf8_to_ucs4(std::vector<char> const & utf8str);
76 std::vector<char_type> utf8_to_ucs4(char const * utf8str, size_t ls);
80 std::vector<char_type> utf16_to_ucs4(unsigned short const * s, size_t ls);
84 std::vector<unsigned short> ucs4_to_utf16(char_type const * s, size_t ls);
86 /// Get the global IconvProcessor instance of the current thread for
87 /// ucs4->utf8 conversions
88 IconvProcessor & ucs4ToUtf8();
92 std::vector<char> ucs4_to_utf8(char_type c);
94 std::vector<char> ucs4_to_utf8(std::vector<char_type> const & ucs4str);
96 std::vector<char> ucs4_to_utf8(char_type const * ucs4str, size_t ls);
98 /// convert \p s from encoding \p encoding to ucs4.
99 /// \p encoding must be a valid iconv 8bit encoding
100 std::vector<char_type>
101 eightbit_to_ucs4(char const * s, size_t ls, std::string const & encoding);
103 /// convert \p s from ucs4 to encoding \p encoding.
104 /// \p encoding must be a valid iconv 8bit encoding
105 std::vector<char> ucs4_to_eightbit(char_type const * ucs4str,
106 size_t ls, std::string const & encoding);
108 /// convert ucs4 character \p c to encoding \p encoding.
109 /// \p encoding must be a valid iconv 8bit encoding
110 char ucs4_to_eightbit(char_type c, std::string const & encoding);
113 void ucs4_to_multibytes(char_type ucs4, std::vector<char> & out,
114 std::string const & encoding);
116 extern char const * ucs4_codeset;
118 /// How many bytes does one UCS4 code point use at most in encoding \p encoding?
119 int max_encoded_bytes(std::string const & encoding);