|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 #ifndef nsIUnicodeEncoder_h___ |
|
7 #define nsIUnicodeEncoder_h___ |
|
8 |
|
9 #include "nscore.h" |
|
10 #include "nsError.h" |
|
11 #include "nsISupports.h" |
|
12 |
|
13 // Interface ID for our Unicode Encoder interface |
|
14 // {2B2CA3D0-A4C9-11d2-8AA1-00600811A836} |
|
15 #define NS_IUNICODEENCODER_IID \ |
|
16 { 0x2b2ca3d0, 0xa4c9, 0x11d2, \ |
|
17 { 0x8a, 0xa1, 0x0, 0x60, 0x8, 0x11, 0xa8, 0x36 }} |
|
18 |
|
19 // Interface ID for our Unicode Character Encoder interface |
|
20 // {299BCCD0-C6DF-11d2-8AA8-00600811A836} |
|
21 #define NS_IUNICHARENCODER_IID \ |
|
22 { 0x299bccd0, 0xc6df, 0x11d2, \ |
|
23 {0x8a, 0xa8, 0x0, 0x60, 0x8, 0x11, 0xa8, 0x36 }} |
|
24 |
|
25 |
|
26 #define NS_UNICODEENCODER_CONTRACTID_BASE "@mozilla.org/intl/unicode/encoder;1?charset=" |
|
27 |
|
28 /** |
|
29 * Interface which converts a single character from Unicode into a given |
|
30 * charset. |
|
31 * |
|
32 * @created 17/Feb/1999 |
|
33 * @author Catalin Rotaru [CATA] |
|
34 */ |
|
35 class nsIUnicharEncoder : public nsISupports |
|
36 { |
|
37 public: |
|
38 NS_DECLARE_STATIC_IID_ACCESSOR(NS_IUNICHARENCODER_IID) |
|
39 |
|
40 /** |
|
41 * Converts a character from Unicode to a Charset. |
|
42 */ |
|
43 NS_IMETHOD Convert(char16_t aChar, char * aDest, int32_t * aDestLength) = 0; |
|
44 }; |
|
45 |
|
46 NS_DEFINE_STATIC_IID_ACCESSOR(nsIUnicharEncoder, NS_IUNICHARENCODER_IID) |
|
47 |
|
48 // |
|
49 // Malloc an Encoder (unicode -> charset) buffer if the |
|
50 // result won't fit in the static buffer |
|
51 // |
|
52 // p = the buffer pointer (char*) |
|
53 // e = encoder (nsIUnicodeEncoder*) |
|
54 // s = string (char16_t*) |
|
55 // l = string length (int32_t) |
|
56 // sb = static buffer (char[]) |
|
57 // sbl = static buffer length (uint32_t) |
|
58 // al = actual buffer length (int32_t) |
|
59 // |
|
60 #define ENCODER_BUFFER_ALLOC_IF_NEEDED(p,e,s,l,sb,sbl,al) \ |
|
61 PR_BEGIN_MACRO \ |
|
62 if (e \ |
|
63 && NS_SUCCEEDED((e)->GetMaxLength((s), (l), &(al)))\ |
|
64 && ((al) > (int32_t)(sbl)) \ |
|
65 && (nullptr!=((p)=(char*)nsMemory::Alloc((al)+1))) \ |
|
66 ) { \ |
|
67 } \ |
|
68 else { \ |
|
69 (p) = (char*)(sb); \ |
|
70 (al) = (sbl); \ |
|
71 } \ |
|
72 PR_END_MACRO |
|
73 |
|
74 // |
|
75 // Free the Encoder buffer if it was allocated |
|
76 // |
|
77 #define ENCODER_BUFFER_FREE_IF_NEEDED(p,sb) \ |
|
78 PR_BEGIN_MACRO \ |
|
79 if ((p) != (char*)(sb)) \ |
|
80 nsMemory::Free(p); \ |
|
81 PR_END_MACRO |
|
82 |
|
83 /** |
|
84 * Interface for a Converter from Unicode into a Charset. |
|
85 * |
|
86 * @created 23/Nov/1998 |
|
87 * @author Catalin Rotaru [CATA] |
|
88 */ |
|
89 class nsIUnicodeEncoder : public nsISupports |
|
90 { |
|
91 public: |
|
92 NS_DECLARE_STATIC_IID_ACCESSOR(NS_IUNICODEENCODER_IID) |
|
93 |
|
94 enum { |
|
95 kOnError_Signal, // on an error, stop and signal |
|
96 kOnError_CallBack, // on an error, call the error handler |
|
97 kOnError_Replace // on an error, replace with a different character |
|
98 }; |
|
99 |
|
100 /** |
|
101 * Converts the data from Unicode to a Charset. |
|
102 * |
|
103 * About the byte ordering: |
|
104 * - The input stream is Unicode, having the byte order which is internal |
|
105 * for the machine on which the converter is running on. |
|
106 * - For output, if the converter cares (that depends of the charset, for |
|
107 * example a singlebyte will ignore the byte ordering) it should assume |
|
108 * network order. If necessary and requested, we can add a method |
|
109 * SetOutputByteOrder() so that the reverse order can be used, too. That |
|
110 * method would have as default the assumed network order. |
|
111 * |
|
112 * For the last converted char, even if there is not enough output |
|
113 * space, a partial output must be done until all available space will be |
|
114 * used. The rest of the output should be buffered until more space becomes |
|
115 * available. But this is not also true about the error handling method!!! |
|
116 * So be very, very careful... |
|
117 * |
|
118 * @param aSrc [IN] the source data buffer |
|
119 * @param aSrcLength [IN/OUT] the length of source data buffer; after |
|
120 * conversion will contain the number of Unicode |
|
121 * characters read |
|
122 * @param aDest [OUT] the destination data buffer |
|
123 * @param aDestLength [IN/OUT] the length of the destination data buffer; |
|
124 * after conversion will contain the number of bytes |
|
125 * written |
|
126 * @return NS_OK_UENC_MOREOUTPUT if only a partial conversion |
|
127 * was done; more output space is needed to continue |
|
128 * NS_OK_UENC_MOREINPUT if only a partial conversion |
|
129 * was done; more input is needed to continue. This can |
|
130 * occur when the last UTF-16 code point in the input is |
|
131 * the first of a surrogate pair. |
|
132 * NS_ERROR_UENC_NOMAPPING if character without mapping |
|
133 * was encountered and the behavior was set to "signal". |
|
134 */ |
|
135 NS_IMETHOD Convert(const char16_t * aSrc, int32_t * aSrcLength, |
|
136 char * aDest, int32_t * aDestLength) = 0; |
|
137 |
|
138 /** |
|
139 * Finishes the conversion. The converter has the possibility to write some |
|
140 * extra data and flush its final state. |
|
141 * |
|
142 * @param aDest [OUT] the destination data buffer |
|
143 * @param aDestLength [IN/OUT] the length of destination data buffer; after |
|
144 * conversion it will contain the number of bytes written |
|
145 * @return NS_OK_UENC_MOREOUTPUT if only a partial conversion |
|
146 * was done; more output space is needed to continue |
|
147 */ |
|
148 NS_IMETHOD Finish(char * aDest, int32_t * aDestLength) = 0; |
|
149 |
|
150 /** |
|
151 * Returns a quick estimation of the size of the buffer needed to hold the |
|
152 * converted data. Remember: this estimation is >= with the actual size of |
|
153 * the buffer needed. It will be computed for the "worst case" |
|
154 * |
|
155 * @param aSrc [IN] the source data buffer |
|
156 * @param aSrcLength [IN] the length of source data buffer |
|
157 * @param aDestLength [OUT] the needed size of the destination buffer |
|
158 * @return NS_OK_UENC_EXACTLENGTH if an exact length was computed |
|
159 * NS_OK if all we have is an approximation |
|
160 */ |
|
161 NS_IMETHOD GetMaxLength(const char16_t * aSrc, int32_t aSrcLength, |
|
162 int32_t * aDestLength) = 0; |
|
163 |
|
164 /** |
|
165 * Resets the charset converter so it may be recycled for a completely |
|
166 * different and urelated buffer of data. |
|
167 */ |
|
168 NS_IMETHOD Reset() = 0; |
|
169 |
|
170 /** |
|
171 * Specify what to do when a character cannot be mapped into the dest charset |
|
172 * |
|
173 * @param aOrder [IN] the behavior; taken from the enum |
|
174 */ |
|
175 NS_IMETHOD SetOutputErrorBehavior(int32_t aBehavior, |
|
176 nsIUnicharEncoder * aEncoder, char16_t aChar) = 0; |
|
177 }; |
|
178 |
|
179 NS_DEFINE_STATIC_IID_ACCESSOR(nsIUnicodeEncoder, NS_IUNICODEENCODER_IID) |
|
180 |
|
181 #endif /* nsIUnicodeEncoder_h___ */ |