c6d25d8d |
1 | /* |
2 | * sbcs.c - routines to handle single-byte character sets. |
3 | */ |
4 | |
5 | #include "charset.h" |
6 | #include "internal.h" |
7 | |
8 | /* |
9 | * The charset_spec for any single-byte character set should |
10 | * provide read_sbcs() as its read function, and its `data' field |
11 | * should be a wchar_t string constant containing the 256 entries |
12 | * of the translation table. |
13 | */ |
14 | |
15 | void read_sbcs(charset_spec const *charset, long int input_chr, |
16 | charset_state *state, |
17 | void (*emit)(void *ctx, long int output), void *emitctx) |
18 | { |
19 | const struct sbcs_data *sd = charset->data; |
20 | |
21 | UNUSEDARG(state); |
22 | |
23 | emit(emitctx, sd->sbcs2ucs[input_chr]); |
24 | } |
25 | |
26 | int write_sbcs(charset_spec const *charset, long int input_chr, |
27 | charset_state *state, |
28 | void (*emit)(void *ctx, long int output), void *emitctx) |
29 | { |
30 | const struct sbcs_data *sd = charset->data; |
31 | int i, j, k, c; |
32 | |
33 | UNUSEDARG(state); |
34 | |
35 | if (input_chr == -1) |
36 | return TRUE; /* stateless; no cleanup required */ |
37 | |
38 | /* |
39 | * Binary-search in the ucs2sbcs table. |
40 | */ |
41 | i = -1; |
42 | j = sd->nvalid; |
43 | while (i+1 < j) { |
44 | k = (i+j)/2; |
45 | c = sd->ucs2sbcs[k]; |
3cca0edf |
46 | if (input_chr < (long int)sd->sbcs2ucs[c]) |
c6d25d8d |
47 | j = k; |
3cca0edf |
48 | else if (input_chr > (long int)sd->sbcs2ucs[c]) |
c6d25d8d |
49 | i = k; |
50 | else { |
51 | emit(emitctx, c); |
52 | return TRUE; |
53 | } |
54 | } |
55 | return FALSE; |
56 | } |