-
Notifications
You must be signed in to change notification settings - Fork 6
/
c_bridge.c
118 lines (96 loc) · 3.38 KB
/
c_bridge.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include "c_bridge.h"
#include <string.h>
#include <unicode/utypes.h>
#include <unicode/ucsdet.h>
#include <stdlib.h>
#include <unicode/ucnv.h>
// See description in c_bridge.h
const int detectCharset(void *detector,
void *input,
int input_len,
int *status,
MatchData *matchBuffer,
int matchBufferSize) {
// Put input bytes in the detector.
ucsdet_setText((UCharsetDetector*)detector, (char*)input, input_len, status);
if (*status != U_ZERO_ERROR) {
return 0;
}
// Prepare vars for returned count and guesses.
int matchCount;
const UCharsetMatch **bestGuesses;
// Perform analysis and return all guesses and their count.
bestGuesses = ucsdet_detectAll((UCharsetDetector*)detector, &matchCount, status);
if (*status != U_ZERO_ERROR) {
return 0;
}
// Fill the matchBuffer. Its size is matchBufferSize, so it is filled with
// less or equal to matchBufferSize number of entries.
int i;
int retCount = matchCount > matchBufferSize ? matchBufferSize : matchCount;
for (i = 0; i < retCount; i++) {
const UCharsetMatch* bestGuess = bestGuesses[i];
const char *bestGuessedCharset = NULL;
const char *bestGuessedLanguage = NULL;
// Fill guessed encoding
bestGuessedCharset = ucsdet_getName(bestGuess, status);
if (*status != U_ZERO_ERROR) {
return 0;
}
// Fill guessed language
bestGuessedLanguage = ucsdet_getLanguage(bestGuess, status);
if (*status != U_ZERO_ERROR) {
return 0;
}
// Fill its confidence rating
int32_t conf = ucsdet_getConfidence(bestGuess, status);
if (*status != U_ZERO_ERROR) {
return 0;
}
matchBuffer[i].confidence = conf;
matchBuffer[i].charset = bestGuessedCharset;
matchBuffer[i].language = bestGuessedLanguage;
}
// Return the number of guesses put into matchBuffer.
return retCount;
}
// See description in c_bridge.h
int convertToUtf16(const char *srcEncoding,
UChar *dest,
int32_t destCapacity,
const char *src,
int32_t srcLength,
int *status){
UConverter *conv;
conv = ucnv_open(srcEncoding, status);
if (*status != U_ZERO_ERROR) {
return 0;
}
/* Convert from original encoding to UTF-16 */
int len = ucnv_toUChars(conv, dest, destCapacity, src, srcLength, status);
if (*status != U_ZERO_ERROR) {
return 0;
}
ucnv_close(conv);
return len;
}
// See description in c_bridge.h
int convertFromUtf16(const char *destEncoding,
char *dest,
int32_t destCapacity,
const UChar *src,
int32_t srcLength,
int *status){
UConverter *conv;
conv = ucnv_open(destEncoding, status);
if (*status != U_ZERO_ERROR) {
return 0;
}
/* Convert from UTF-16 to destination encoding */
int len = ucnv_fromUChars(conv, dest, destCapacity, src, srcLength, status);
if (*status != U_ZERO_ERROR) {
return 0;
}
ucnv_close(conv);
return len;
}