/src/mozilla-central/intl/icu/source/i18n/ucsdet.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | // © 2016 and later: Unicode, Inc. and others. |
2 | | // License & terms of use: http://www.unicode.org/copyright.html |
3 | | /* |
4 | | ******************************************************************************** |
5 | | * Copyright (C) 2005-2016, International Business Machines |
6 | | * Corporation and others. All Rights Reserved. |
7 | | ******************************************************************************** |
8 | | */ |
9 | | |
10 | | #include "unicode/utypes.h" |
11 | | |
12 | | #if !UCONFIG_NO_CONVERSION |
13 | | #include "unicode/ucsdet.h" |
14 | | #include "csdetect.h" |
15 | | #include "csmatch.h" |
16 | | #include "csrsbcs.h" |
17 | | #include "csrmbcs.h" |
18 | | #include "csrutf8.h" |
19 | | #include "csrucode.h" |
20 | | #include "csr2022.h" |
21 | | |
22 | | #include "cmemory.h" |
23 | | |
24 | | U_NAMESPACE_USE |
25 | | |
26 | | #define NEW_ARRAY(type,count) (type *) uprv_malloc((count) * sizeof(type)) |
27 | | #define DELETE_ARRAY(array) uprv_free((void *) (array)) |
28 | | |
29 | | U_CDECL_BEGIN |
30 | | |
31 | | U_CAPI UCharsetDetector * U_EXPORT2 |
32 | | ucsdet_open(UErrorCode *status) |
33 | 0 | { |
34 | 0 | if(U_FAILURE(*status)) { |
35 | 0 | return 0; |
36 | 0 | } |
37 | 0 | |
38 | 0 | CharsetDetector* csd = new CharsetDetector(*status); |
39 | 0 |
|
40 | 0 | if (U_FAILURE(*status)) { |
41 | 0 | delete csd; |
42 | 0 | csd = NULL; |
43 | 0 | } |
44 | 0 |
|
45 | 0 | return (UCharsetDetector *) csd; |
46 | 0 | } |
47 | | |
48 | | U_CAPI void U_EXPORT2 |
49 | | ucsdet_close(UCharsetDetector *ucsd) |
50 | 0 | { |
51 | 0 | CharsetDetector *csd = (CharsetDetector *) ucsd; |
52 | 0 | delete csd; |
53 | 0 | } |
54 | | |
55 | | U_CAPI void U_EXPORT2 |
56 | | ucsdet_setText(UCharsetDetector *ucsd, const char *textIn, int32_t len, UErrorCode *status) |
57 | 0 | { |
58 | 0 | if(U_FAILURE(*status)) { |
59 | 0 | return; |
60 | 0 | } |
61 | 0 | |
62 | 0 | ((CharsetDetector *) ucsd)->setText(textIn, len); |
63 | 0 | } |
64 | | |
65 | | U_CAPI const char * U_EXPORT2 |
66 | | ucsdet_getName(const UCharsetMatch *ucsm, UErrorCode *status) |
67 | 0 | { |
68 | 0 | if(U_FAILURE(*status)) { |
69 | 0 | return NULL; |
70 | 0 | } |
71 | 0 | |
72 | 0 | return ((CharsetMatch *) ucsm)->getName(); |
73 | 0 | } |
74 | | |
75 | | U_CAPI int32_t U_EXPORT2 |
76 | | ucsdet_getConfidence(const UCharsetMatch *ucsm, UErrorCode *status) |
77 | 0 | { |
78 | 0 | if(U_FAILURE(*status)) { |
79 | 0 | return 0; |
80 | 0 | } |
81 | 0 | |
82 | 0 | return ((CharsetMatch *) ucsm)->getConfidence(); |
83 | 0 | } |
84 | | |
85 | | U_CAPI const char * U_EXPORT2 |
86 | | ucsdet_getLanguage(const UCharsetMatch *ucsm, UErrorCode *status) |
87 | 0 | { |
88 | 0 | if(U_FAILURE(*status)) { |
89 | 0 | return NULL; |
90 | 0 | } |
91 | 0 | |
92 | 0 | return ((CharsetMatch *) ucsm)->getLanguage(); |
93 | 0 | } |
94 | | |
95 | | U_CAPI const UCharsetMatch * U_EXPORT2 |
96 | | ucsdet_detect(UCharsetDetector *ucsd, UErrorCode *status) |
97 | 0 | { |
98 | 0 | if(U_FAILURE(*status)) { |
99 | 0 | return NULL; |
100 | 0 | } |
101 | 0 | |
102 | 0 | return (const UCharsetMatch *) ((CharsetDetector *) ucsd)->detect(*status); |
103 | 0 | } |
104 | | |
105 | | U_CAPI void U_EXPORT2 |
106 | | ucsdet_setDeclaredEncoding(UCharsetDetector *ucsd, const char *encoding, int32_t length, UErrorCode *status) |
107 | 0 | { |
108 | 0 | if(U_FAILURE(*status)) { |
109 | 0 | return; |
110 | 0 | } |
111 | 0 | |
112 | 0 | ((CharsetDetector *) ucsd)->setDeclaredEncoding(encoding,length); |
113 | 0 | } |
114 | | |
115 | | U_CAPI const UCharsetMatch** |
116 | | ucsdet_detectAll(UCharsetDetector *ucsd, |
117 | | int32_t *maxMatchesFound, UErrorCode *status) |
118 | 0 | { |
119 | 0 | if(U_FAILURE(*status)) { |
120 | 0 | return NULL; |
121 | 0 | } |
122 | 0 | |
123 | 0 | CharsetDetector *csd = (CharsetDetector *) ucsd; |
124 | 0 |
|
125 | 0 | return (const UCharsetMatch**)csd->detectAll(*maxMatchesFound,*status); |
126 | 0 | } |
127 | | |
128 | | // U_CAPI const char * U_EXPORT2 |
129 | | // ucsdet_getDetectableCharsetName(const UCharsetDetector *csd, int32_t index, UErrorCode *status) |
130 | | // { |
131 | | // if(U_FAILURE(*status)) { |
132 | | // return 0; |
133 | | // } |
134 | | // return csd->getCharsetName(index,*status); |
135 | | // } |
136 | | |
137 | | // U_CAPI int32_t U_EXPORT2 |
138 | | // ucsdet_getDetectableCharsetsCount(const UCharsetDetector *csd, UErrorCode *status) |
139 | | // { |
140 | | // if(U_FAILURE(*status)) { |
141 | | // return -1; |
142 | | // } |
143 | | // return UCharsetDetector::getDetectableCount(); |
144 | | // } |
145 | | |
146 | | U_CAPI UBool U_EXPORT2 |
147 | | ucsdet_isInputFilterEnabled(const UCharsetDetector *ucsd) |
148 | 0 | { |
149 | 0 | // todo: could use an error return... |
150 | 0 | if (ucsd == NULL) { |
151 | 0 | return FALSE; |
152 | 0 | } |
153 | 0 |
|
154 | 0 | return ((CharsetDetector *) ucsd)->getStripTagsFlag(); |
155 | 0 | } |
156 | | |
157 | | U_CAPI UBool U_EXPORT2 |
158 | | ucsdet_enableInputFilter(UCharsetDetector *ucsd, UBool filter) |
159 | 0 | { |
160 | 0 | // todo: could use an error return... |
161 | 0 | if (ucsd == NULL) { |
162 | 0 | return FALSE; |
163 | 0 | } |
164 | 0 |
|
165 | 0 | CharsetDetector *csd = (CharsetDetector *) ucsd; |
166 | 0 | UBool prev = csd->getStripTagsFlag(); |
167 | 0 |
|
168 | 0 | csd->setStripTagsFlag(filter); |
169 | 0 |
|
170 | 0 | return prev; |
171 | 0 | } |
172 | | |
173 | | U_CAPI int32_t U_EXPORT2 |
174 | | ucsdet_getUChars(const UCharsetMatch *ucsm, |
175 | | UChar *buf, int32_t cap, UErrorCode *status) |
176 | 0 | { |
177 | 0 | if(U_FAILURE(*status)) { |
178 | 0 | return 0; |
179 | 0 | } |
180 | 0 | |
181 | 0 | return ((CharsetMatch *) ucsm)->getUChars(buf, cap, status); |
182 | 0 | } |
183 | | |
184 | | U_CAPI void U_EXPORT2 |
185 | | ucsdet_setDetectableCharset(UCharsetDetector *ucsd, const char *encoding, UBool enabled, UErrorCode *status) |
186 | 0 | { |
187 | 0 | ((CharsetDetector *)ucsd)->setDetectableCharset(encoding, enabled, *status); |
188 | 0 | } |
189 | | |
190 | | U_CAPI UEnumeration * U_EXPORT2 |
191 | | ucsdet_getAllDetectableCharsets(const UCharsetDetector * /*ucsd*/, UErrorCode *status) |
192 | 0 | { |
193 | 0 | return CharsetDetector::getAllDetectableCharsets(*status); |
194 | 0 | } |
195 | | |
196 | | U_DRAFT UEnumeration * U_EXPORT2 |
197 | | ucsdet_getDetectableCharsets(const UCharsetDetector *ucsd, UErrorCode *status) |
198 | 0 | { |
199 | 0 | return ((CharsetDetector *)ucsd)->getDetectableCharsets(*status); |
200 | 0 | } |
201 | | |
202 | | U_CDECL_END |
203 | | |
204 | | |
205 | | #endif |