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