1 | /*
|
---|
2 | * synergy -- mouse and keyboard sharing utility
|
---|
3 | * Copyright (C) 2002 Chris Schoeneman
|
---|
4 | *
|
---|
5 | * This package is free software; you can redistribute it and/or
|
---|
6 | * modify it under the terms of the GNU General Public License
|
---|
7 | * found in the file COPYING that should have accompanied this file.
|
---|
8 | *
|
---|
9 | * This package is distributed in the hope that it will be useful,
|
---|
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
12 | * GNU General Public License for more details.
|
---|
13 | */
|
---|
14 |
|
---|
15 | #ifndef CUNICODE_H
|
---|
16 | #define CUNICODE_H
|
---|
17 |
|
---|
18 | #include "CString.h"
|
---|
19 | #include "BasicTypes.h"
|
---|
20 |
|
---|
21 | //! Unicode utility functions
|
---|
22 | /*!
|
---|
23 | This class provides functions for converting between various Unicode
|
---|
24 | encodings and the current locale encoding.
|
---|
25 | */
|
---|
26 | class CUnicode {
|
---|
27 | public:
|
---|
28 | //! @name accessors
|
---|
29 | //@{
|
---|
30 |
|
---|
31 | //! Test UTF-8 string for validity
|
---|
32 | /*!
|
---|
33 | Returns true iff the string contains a valid sequence of UTF-8
|
---|
34 | encoded characters.
|
---|
35 | */
|
---|
36 | static bool isUTF8(const CString&);
|
---|
37 |
|
---|
38 | //! Convert from UTF-8 to UCS-2 encoding
|
---|
39 | /*!
|
---|
40 | Convert from UTF-8 to UCS-2. If errors is not NULL then *errors
|
---|
41 | is set to true iff any character could not be encoded in UCS-2.
|
---|
42 | Decoding errors do not set *errors.
|
---|
43 | */
|
---|
44 | static CString UTF8ToUCS2(const CString&, bool* errors = NULL);
|
---|
45 |
|
---|
46 | //! Convert from UTF-8 to UCS-4 encoding
|
---|
47 | /*!
|
---|
48 | Convert from UTF-8 to UCS-4. If errors is not NULL then *errors
|
---|
49 | is set to true iff any character could not be encoded in UCS-4.
|
---|
50 | Decoding errors do not set *errors.
|
---|
51 | */
|
---|
52 | static CString UTF8ToUCS4(const CString&, bool* errors = NULL);
|
---|
53 |
|
---|
54 | //! Convert from UTF-8 to UTF-16 encoding
|
---|
55 | /*!
|
---|
56 | Convert from UTF-8 to UTF-16. If errors is not NULL then *errors
|
---|
57 | is set to true iff any character could not be encoded in UTF-16.
|
---|
58 | Decoding errors do not set *errors.
|
---|
59 | */
|
---|
60 | static CString UTF8ToUTF16(const CString&, bool* errors = NULL);
|
---|
61 |
|
---|
62 | //! Convert from UTF-8 to UTF-32 encoding
|
---|
63 | /*!
|
---|
64 | Convert from UTF-8 to UTF-32. If errors is not NULL then *errors
|
---|
65 | is set to true iff any character could not be encoded in UTF-32.
|
---|
66 | Decoding errors do not set *errors.
|
---|
67 | */
|
---|
68 | static CString UTF8ToUTF32(const CString&, bool* errors = NULL);
|
---|
69 |
|
---|
70 | //! Convert from UTF-8 to the current locale encoding
|
---|
71 | /*!
|
---|
72 | Convert from UTF-8 to the current locale encoding. If errors is not
|
---|
73 | NULL then *errors is set to true iff any character could not be encoded.
|
---|
74 | Decoding errors do not set *errors.
|
---|
75 | */
|
---|
76 | static CString UTF8ToText(const CString&, bool* errors = NULL);
|
---|
77 |
|
---|
78 | //! Convert from UCS-2 to UTF-8
|
---|
79 | /*!
|
---|
80 | Convert from UCS-2 to UTF-8. If errors is not NULL then *errors is
|
---|
81 | set to true iff any character could not be decoded.
|
---|
82 | */
|
---|
83 | static CString UCS2ToUTF8(const CString&, bool* errors = NULL);
|
---|
84 |
|
---|
85 | //! Convert from UCS-4 to UTF-8
|
---|
86 | /*!
|
---|
87 | Convert from UCS-4 to UTF-8. If errors is not NULL then *errors is
|
---|
88 | set to true iff any character could not be decoded.
|
---|
89 | */
|
---|
90 | static CString UCS4ToUTF8(const CString&, bool* errors = NULL);
|
---|
91 |
|
---|
92 | //! Convert from UTF-16 to UTF-8
|
---|
93 | /*!
|
---|
94 | Convert from UTF-16 to UTF-8. If errors is not NULL then *errors is
|
---|
95 | set to true iff any character could not be decoded.
|
---|
96 | */
|
---|
97 | static CString UTF16ToUTF8(const CString&, bool* errors = NULL);
|
---|
98 |
|
---|
99 | //! Convert from UTF-32 to UTF-8
|
---|
100 | /*!
|
---|
101 | Convert from UTF-32 to UTF-8. If errors is not NULL then *errors is
|
---|
102 | set to true iff any character could not be decoded.
|
---|
103 | */
|
---|
104 | static CString UTF32ToUTF8(const CString&, bool* errors = NULL);
|
---|
105 |
|
---|
106 | //! Convert from the current locale encoding to UTF-8
|
---|
107 | /*!
|
---|
108 | Convert from the current locale encoding to UTF-8. If errors is not
|
---|
109 | NULL then *errors is set to true iff any character could not be decoded.
|
---|
110 | */
|
---|
111 | static CString textToUTF8(const CString&, bool* errors = NULL);
|
---|
112 |
|
---|
113 | //@}
|
---|
114 |
|
---|
115 | private:
|
---|
116 | // convert UTF8 to wchar_t string (using whatever encoding is native
|
---|
117 | // to the platform). caller must delete[] the returned string. the
|
---|
118 | // string is *not* nul terminated; the length (in characters) is
|
---|
119 | // returned in size.
|
---|
120 | static wchar_t* UTF8ToWideChar(const CString&,
|
---|
121 | UInt32& size, bool* errors);
|
---|
122 |
|
---|
123 | // convert nul terminated wchar_t string (in platform's native
|
---|
124 | // encoding) to UTF8.
|
---|
125 | static CString wideCharToUTF8(const wchar_t*,
|
---|
126 | UInt32 size, bool* errors);
|
---|
127 |
|
---|
128 | // internal conversion to UTF8
|
---|
129 | static CString doUCS2ToUTF8(const UInt8* src, UInt32 n, bool* errors);
|
---|
130 | static CString doUCS4ToUTF8(const UInt8* src, UInt32 n, bool* errors);
|
---|
131 | static CString doUTF16ToUTF8(const UInt8* src, UInt32 n, bool* errors);
|
---|
132 | static CString doUTF32ToUTF8(const UInt8* src, UInt32 n, bool* errors);
|
---|
133 |
|
---|
134 | // convert characters to/from UTF8
|
---|
135 | static UInt32 fromUTF8(const UInt8*& src, UInt32& size);
|
---|
136 | static void toUTF8(CString& dst, UInt32 c, bool* errors);
|
---|
137 |
|
---|
138 | private:
|
---|
139 | static UInt32 s_invalid;
|
---|
140 | static UInt32 s_replacement;
|
---|
141 | };
|
---|
142 |
|
---|
143 | #endif
|
---|