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
17U_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
24U_CDECL_BEGIN
25
26U_CAPI UCharsetDetector * U_EXPORT2
27ucsdet_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
43U_CAPI void U_EXPORT2
44ucsdet_close(UCharsetDetector *ucsd)
45{
46    CharsetDetector *csd = (CharsetDetector *) ucsd;
47    delete csd;
48}
49
50U_CAPI void U_EXPORT2
51ucsdet_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
60U_CAPI const char * U_EXPORT2
61ucsdet_getName(const UCharsetMatch *ucsm, UErrorCode *status)
62{
63    if(U_FAILURE(*status)) {
64        return NULL;
65    }
66
67    return ((CharsetMatch *) ucsm)->getName();
68}
69
70U_CAPI int32_t U_EXPORT2
71ucsdet_getConfidence(const UCharsetMatch *ucsm, UErrorCode *status)
72{
73    if(U_FAILURE(*status)) {
74        return 0;
75    }
76
77    return ((CharsetMatch *) ucsm)->getConfidence();
78}
79
80U_CAPI const char * U_EXPORT2
81ucsdet_getLanguage(const UCharsetMatch *ucsm, UErrorCode *status)
82{
83    if(U_FAILURE(*status)) {
84        return NULL;
85    }
86
87    return ((CharsetMatch *) ucsm)->getLanguage();
88}
89
90U_CAPI const UCharsetMatch * U_EXPORT2
91ucsdet_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
100U_CAPI void U_EXPORT2
101ucsdet_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
110U_CAPI const UCharsetMatch**
111ucsdet_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
141U_CAPI  UBool U_EXPORT2
142ucsdet_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
152U_CAPI  UBool U_EXPORT2
153ucsdet_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
168U_CAPI  int32_t U_EXPORT2
169ucsdet_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}
178U_CDECL_END
179
180#endif
181