-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconverter.c
322 lines (305 loc) · 9.42 KB
/
converter.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#include "icu_common.h"
extern VALUE rb_cUString;
extern VALUE icu_ustr_new_set(UChar * ptr, long len, long capa);
extern VALUE rb_cUConverter;
#define UCONVERTER(obj) ((UConverter *)DATA_PTR(obj))
static void icu4r_cnv_free(UConverter * conv)
{
ucnv_close(conv);
}
static VALUE icu4r_cnv_alloc(VALUE klass)
{
return Data_Wrap_Struct(klass, 0, icu4r_cnv_free, 0);
}
/**
* call-seq:
* conv = UConverter.new(name)
*
* Creates new converter, by given name. Name must be a Ruby String and may contain
* additional options, e.g.:
*
* "SCSU,locale=ja" # Converter option for specifying a locale
* "UTF-7,version=1" # Converter option for specifying a version selector (0..9) for some converters.
* "ibm-1047,swaplfnl" # Converter option for EBCDIC SBCS or mixed-SBCS/DBCS (stateful) codepages.
*
* To get list of available converters call UConverter.list_available
*/
VALUE icu4r_cnv_init(VALUE self, VALUE name)
{
UConverter * converter;
UErrorCode status = U_ZERO_ERROR;
Check_Type(name, T_STRING);
converter = ucnv_open(RSTRING_PTR(name), &status);
ICU_RAISE(status);
DATA_PTR(self) = converter;
return self;
}
/**
* call-seq:
* UConverter.list_available # => Array
*
* Returns the names of available converters.
*/
VALUE icu4r_cnv_list(VALUE self)
{
VALUE ret ;
int32_t count, i;
count = ucnv_countAvailable();
ret = rb_ary_new2(count);
for( i = 0; i < count ; i++)
{
rb_ary_store(ret, i, rb_str_new2(ucnv_getAvailableName(i)));
}
return ret;
}
/**
* call-seq:
* converter.subst_chars
*
* Returns substitution characters as multiple bytes
*/
VALUE icu4r_cnv_get_subst_chars(VALUE self)
{
char buf[16];
int8_t len = 16;
UErrorCode status = U_ZERO_ERROR;
ucnv_getSubstChars(UCONVERTER(self), buf, &len, &status);
ICU_RAISE(status);
return rb_str_new(buf, len);
}
/**
* call-seq:
* converter.subst_chars=chars
*
* Sets the substitution chars when converting from unicode to a codepage.
* The substitution is specified as a string of 1-4 bytes
*/
VALUE icu4r_cnv_set_subst_chars(VALUE self, VALUE str)
{
UErrorCode status = U_ZERO_ERROR;
Check_Type(str, T_STRING);
ucnv_setSubstChars(UCONVERTER(self), RSTRING_PTR(str), RSTRING_LEN(str), &status);
ICU_RAISE(status);
return Qnil;
}
/**
* call-seq:
* conv.name
*
* Gets the internal, canonical name of the converter.
*/
VALUE icu4r_cnv_name(VALUE self)
{
UConverter * cnv = UCONVERTER(self);
UErrorCode status = U_ZERO_ERROR;
return rb_str_new2(ucnv_getName(cnv, &status));
}
/**
* call-seq:
* converter.reset
*
* Resets the state of a converter to the default state.
* This is used in the case of an error, to restart a conversion from a known default state.
* It will also empty the internal output buffers.
*/
VALUE icu4r_cnv_reset(VALUE self)
{
UConverter * cnv = UCONVERTER(self);
ucnv_reset(cnv);
return Qnil;
}
/**
* call-seq:
* conv.from_u(ustring) -> String
*
* Convert the Unicode string into a codepage string using an existing UConverter.
*/
VALUE icu4r_cnv_from_unicode(VALUE self, VALUE str)
{
UConverter * conv = UCONVERTER(self);
UErrorCode status = U_ZERO_ERROR;
int32_t enclen, capa;
char * buf;
VALUE s = Qnil;
Check_Class(str, rb_cUString);
capa = ICU_LEN(str) + 1;
buf = ALLOC_N(char, capa);
enclen = ucnv_fromUChars(conv, buf, capa-1, ICU_PTR(str), ICU_LEN(str), &status);
if (U_BUFFER_OVERFLOW_ERROR == status) {
REALLOC_N(buf, char, enclen + 1);
status = 0;
ucnv_fromUChars(conv, buf, enclen, ICU_PTR(str), ICU_LEN(str), &status);
}
if( U_FAILURE(status) ){
free(buf);
rb_raise(rb_eArgError, u_errorName(status));
}
s = rb_str_new(buf, enclen);
return s;
}
/**
* call-seq:
* conv.to_u(string) -> UString
*
* Convert the codepage string into a Unicode string using an existing UConverter.
*/
VALUE icu4r_cnv_to_unicode(VALUE self, VALUE str)
{
UConverter * conv = UCONVERTER(self);
UErrorCode status = U_ZERO_ERROR;
long len, capa;
VALUE s;
UChar * buf;
Check_Type(str, T_STRING);
capa = RSTRING_LEN(str) + 1;
buf = ALLOC_N(UChar, capa);
len = ucnv_toUChars(conv, buf, capa-1, RSTRING_PTR(str), RSTRING_LEN(str), &status);
if (U_BUFFER_OVERFLOW_ERROR == status) {
capa = len+1;
REALLOC_N(buf, UChar, capa);
status = 0;
len = ucnv_toUChars(conv, buf, capa-1, RSTRING_PTR(str), RSTRING_LEN(str), &status);
if (U_FAILURE(status)) {
free(buf);
rb_raise(rb_eArgError, u_errorName(status));
}
}
s = icu_ustr_new_set(buf, len, capa);
return s;
}
#define BUF_SIZE 1024
/**
* call-seq:
* conv.convert(other_conv, string)
*
* Convert from one external charset to another using two existing UConverters,
* ignoring the location of errors.
*/
VALUE icu4r_cnv_convert_to(VALUE self, VALUE other, VALUE src)
{
UConverter * cnv, * other_cnv;
UErrorCode status = U_ZERO_ERROR;
UChar pivotBuffer[BUF_SIZE];
UChar *pivot, *pivot2;
char * target,buffer[BUF_SIZE], *target_limit;
const char * src_ptr, * src_end;
VALUE ret;
Check_Class(other, rb_cUConverter);
Check_Type(src, T_STRING);
pivot=pivot2=pivotBuffer;
cnv = UCONVERTER(self);
other_cnv = UCONVERTER(other);
src_ptr = RSTRING_PTR(src);
src_end = src_ptr + RSTRING_LEN(src);
ret = rb_str_new2("");
ucnv_reset(other_cnv);
ucnv_reset(cnv);
target_limit = buffer+BUF_SIZE;
do {
status = U_ZERO_ERROR;
target = buffer;
ucnv_convertEx( other_cnv, cnv, &target, target_limit,
&src_ptr, src_end, pivotBuffer, &pivot, &pivot2, pivotBuffer+BUF_SIZE, FALSE, TRUE, &status);
if(U_FAILURE(status) && status != U_BUFFER_OVERFLOW_ERROR) {
ICU_RAISE(status);
}
rb_str_buf_cat(ret, buffer, (int32_t)(target-buffer));
} while (status == U_BUFFER_OVERFLOW_ERROR);
return ret;
}
/**
* call-seq:
* UConverter.convert(to_converter_name, from_converter_name, source) # => String
*
* Convert from one external charset to another.
* Internally, two converters are opened according to the name arguments, then the text is converted to and from using them.
*/
VALUE icu4r_cnv_convert(VALUE self, VALUE to_conv_name, VALUE from_conv_name, VALUE src)
{
UErrorCode status = U_ZERO_ERROR;
char * target = NULL;
int32_t target_capa, len;
VALUE ret;
target_capa = ucnv_convert( RSTRING_PTR(to_conv_name), RSTRING_PTR(from_conv_name),
target, 0,
RSTRING_PTR(src), RSTRING_LEN(src), &status);
if(status == U_BUFFER_OVERFLOW_ERROR){
status = U_ZERO_ERROR;
target_capa += 1;
target = ALLOC_N(char, target_capa);
len = ucnv_convert( RSTRING_PTR(to_conv_name), RSTRING_PTR(from_conv_name),
target, target_capa,
RSTRING_PTR(src), RSTRING_LEN(src), &status);
if(U_FAILURE(status)){
free(target);
ICU_RAISE(status);
}
ret = rb_str_new(target, len);
free(target);
return ret;
} else ICU_RAISE(status);
return rb_str_new2("");
}
/**
* call-seq:
* UConverter.std_names(conv_name, std_name)
*
* Returns list of alias names for a given converter that are recognized by a standard; MIME and IANA are such standards
*/
VALUE icu4r_cnv_standard_names(VALUE self, VALUE cnv_name, VALUE std_name)
{
UEnumeration * name_list;
UErrorCode status = U_ZERO_ERROR;
VALUE ret ;
char * name;
int32_t len;
Check_Type(cnv_name, T_STRING);
Check_Type(std_name, T_STRING);
name_list = ucnv_openStandardNames(RSTRING_PTR(cnv_name), RSTRING_PTR(std_name), &status);
ICU_RAISE(status);
ret = rb_ary_new();
while( (name = (char*)uenum_next(name_list, &len, &status))) {
rb_ary_push(ret, rb_str_new2(name));
}
uenum_close(name_list);
return ret;
}
/**
* call-seq:
* UConverter.all_names
*
* Returns all of the canonical converter names, regardless of the ability to open each converter.
*/
VALUE icu4r_cnv_all_names(VALUE self)
{
UEnumeration * name_list;
UErrorCode status = U_ZERO_ERROR;
VALUE ret ;
char * name;
int32_t len;
name_list = ucnv_openAllNames(&status);
ICU_RAISE(status);
ret = rb_ary_new();
while( (name = (char*)uenum_next(name_list, &len, &status))) {
rb_ary_push(ret, rb_str_new2(name));
}
uenum_close(name_list);
return ret;
}
void initialize_converter(void)
{
rb_cUConverter = rb_define_class("UConverter", rb_cObject);
rb_define_alloc_func(rb_cUConverter, icu4r_cnv_alloc);
rb_define_method(rb_cUConverter, "initialize", icu4r_cnv_init, 1);
rb_define_method(rb_cUConverter, "to_u", icu4r_cnv_to_unicode, 1);
rb_define_method(rb_cUConverter, "from_u", icu4r_cnv_from_unicode, 1);
rb_define_method(rb_cUConverter, "reset", icu4r_cnv_reset, 0);
rb_define_method(rb_cUConverter, "name", icu4r_cnv_name, 0);
rb_define_method(rb_cUConverter, "convert", icu4r_cnv_convert_to, 2);
rb_define_method(rb_cUConverter, "subst_chars=", icu4r_cnv_set_subst_chars, 1);
rb_define_method(rb_cUConverter, "subst_chars", icu4r_cnv_get_subst_chars, 0);
rb_define_singleton_method(rb_cUConverter, "convert", icu4r_cnv_convert, 3);
rb_define_singleton_method(rb_cUConverter, "list_available", icu4r_cnv_list, 0);
rb_define_singleton_method(rb_cUConverter, "std_names", icu4r_cnv_standard_names, 2);
rb_define_singleton_method(rb_cUConverter, "all_names", icu4r_cnv_all_names, 0);
}