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