]> git.saurik.com Git - apple/icu.git/blob - icuSources/i18n/ucsdet.cpp
ICU-8.11.4.tar.gz
[apple/icu.git] / icuSources / i18n / ucsdet.cpp
1 /*
2 ********************************************************************************
3 * Copyright (C) 2005-2006, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 ********************************************************************************
6 */
7
8 #include "unicode/utypes.h"
9
10 #if !UCONFIG_NO_CONVERSION
11 #include "unicode/ucsdet.h"
12 #include "csdetect.h"
13 #include "csmatch.h"
14
15 #include "cmemory.h"
16
17 #define ARRAY_SIZE(array) (sizeof array / sizeof array[0])
18
19 #define NEW_ARRAY(type,count) (type *) uprv_malloc((count) * sizeof(type))
20 #define DELETE_ARRAY(array) uprv_free((void *) (array))
21
22 U_CDECL_BEGIN
23
24 U_CAPI UCharsetDetector * U_EXPORT2
25 ucsdet_open(UErrorCode *status)
26 {
27 if(U_FAILURE(*status)) {
28 return 0;
29 }
30
31 CharsetDetector* csd = new CharsetDetector(*status);
32
33 if (U_FAILURE(*status)) {
34 delete csd;
35 csd = NULL;
36 }
37
38 return (UCharsetDetector *) csd;
39 }
40
41 U_CAPI void U_EXPORT2
42 ucsdet_close(UCharsetDetector *ucsd)
43 {
44 CharsetDetector *csd = (CharsetDetector *) ucsd;
45 delete csd;
46 }
47
48 U_CAPI void U_EXPORT2
49 ucsdet_setText(UCharsetDetector *ucsd, const char *textIn, int32_t len, UErrorCode *status)
50 {
51 if(U_FAILURE(*status)) {
52 return;
53 }
54
55 if (ucsd == NULL) {
56 *status = U_ILLEGAL_ARGUMENT_ERROR;
57 return;
58 }
59
60 CharsetDetector *csd = (CharsetDetector *) ucsd;
61
62 csd->setText(textIn, len);
63 }
64
65 U_CAPI const char * U_EXPORT2
66 ucsdet_getName(const UCharsetMatch *ucsm, UErrorCode *status)
67 {
68 if(U_FAILURE(*status)) {
69 return NULL;
70 }
71
72 if (ucsm == NULL) {
73 *status = U_ILLEGAL_ARGUMENT_ERROR;
74 return NULL;
75 }
76
77 CharsetMatch *csm = (CharsetMatch *) ucsm;
78
79 return csm->getName();
80 }
81
82 U_CAPI int32_t U_EXPORT2
83 ucsdet_getConfidence(const UCharsetMatch *ucsm, UErrorCode *status)
84 {
85 if(U_FAILURE(*status)) {
86 return 0;
87 }
88
89 if (ucsm == NULL) {
90 *status = U_ILLEGAL_ARGUMENT_ERROR;
91 return 0;
92 }
93
94 CharsetMatch *csm = (CharsetMatch *) ucsm;
95
96 return csm->getConfidence();
97 }
98
99 U_CAPI const char * U_EXPORT2
100 ucsdet_getLanguage(const UCharsetMatch *ucsm, UErrorCode *status)
101 {
102 if(U_FAILURE(*status)) {
103 return NULL;
104 }
105
106 if (ucsm == NULL) {
107 *status = U_ILLEGAL_ARGUMENT_ERROR;
108 return NULL;
109 }
110
111 CharsetMatch *csm = (CharsetMatch *) ucsm;
112
113 return csm->getLanguage();
114 }
115
116 U_CAPI const UCharsetMatch * U_EXPORT2
117 ucsdet_detect(UCharsetDetector *ucsd, UErrorCode *status)
118 {
119 if(U_FAILURE(*status)) {
120 return NULL;
121 }
122
123 if (ucsd == NULL) {
124 *status = U_ILLEGAL_ARGUMENT_ERROR;
125 return NULL;
126 }
127
128 CharsetDetector *csd = (CharsetDetector *) ucsd;
129
130 return (const UCharsetMatch *) csd->detect(*status);
131 }
132
133 U_CAPI void U_EXPORT2
134 ucsdet_setDeclaredEncoding(UCharsetDetector *ucsd, const char *encoding, int32_t length, UErrorCode *status)
135 {
136 if(U_FAILURE(*status)) {
137 return;
138 }
139
140 if (ucsd == NULL) {
141 *status = U_ILLEGAL_ARGUMENT_ERROR;
142 return;
143 }
144
145 CharsetDetector *csd = (CharsetDetector *) ucsd;
146
147 csd->setDeclaredEncoding(encoding,length);
148 }
149
150 U_CAPI const UCharsetMatch**
151 ucsdet_detectAll(UCharsetDetector *ucsd,
152 int32_t *maxMatchesFound, UErrorCode *status)
153 {
154 if(U_FAILURE(*status)) {
155 return NULL;
156 }
157
158 if (ucsd == NULL) {
159 *status = U_ILLEGAL_ARGUMENT_ERROR;
160 return NULL;
161 }
162
163 CharsetDetector *csd = (CharsetDetector *) ucsd;
164
165 return (const UCharsetMatch**)csd->detectAll(*maxMatchesFound,*status);
166 }
167
168 // U_CAPI const char * U_EXPORT2
169 // ucsdet_getDetectableCharsetName(const UCharsetDetector *csd, int32_t index, UErrorCode *status)
170 // {
171 // if(U_FAILURE(*status)) {
172 // return 0;
173 // }
174 // return csd->getCharsetName(index,*status);
175 // }
176
177 // U_CAPI int32_t U_EXPORT2
178 // ucsdet_getDetectableCharsetsCount(const UCharsetDetector *csd, UErrorCode *status)
179 // {
180 // if(U_FAILURE(*status)) {
181 // return -1;
182 // }
183 // return UCharsetDetector::getDetectableCount();
184 // }
185
186 U_CAPI UBool U_EXPORT2
187 ucsdet_isInputFilterEnabled(const UCharsetDetector *ucsd)
188 {
189 // todo: could use an error return...
190 if (ucsd == NULL) {
191 return FALSE;
192 }
193
194 CharsetDetector *csd = (CharsetDetector *) ucsd;
195
196 return csd->getStripTagsFlag();
197 }
198
199 U_CAPI UBool U_EXPORT2
200 ucsdet_enableInputFilter(UCharsetDetector *ucsd, UBool filter)
201 {
202 // todo: could use an error return...
203 if (ucsd == NULL) {
204 return FALSE;
205 }
206
207 CharsetDetector *csd = (CharsetDetector *) ucsd;
208 UBool prev = csd->getStripTagsFlag();
209
210 csd->setStripTagsFlag(filter);
211
212 return prev;
213 }
214
215 U_CAPI int32_t U_EXPORT2
216 ucsdet_getUChars(const UCharsetMatch *ucsm,
217 UChar *buf, int32_t cap, UErrorCode *status)
218 {
219 if(U_FAILURE(*status)) {
220 return 0;
221 }
222
223 if (ucsm == NULL) {
224 *status = U_ILLEGAL_ARGUMENT_ERROR;
225 return 0;
226 }
227
228 CharsetMatch *csm = (CharsetMatch *) ucsm;
229
230 return csm->getUChars(buf, cap, status);
231 }
232 U_CDECL_END
233
234 #endif