-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patht2s-lookup.cpp
85 lines (64 loc) · 1.36 KB
/
t2s-lookup.cpp
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
#include <stdio.h>
#include "t2s-file.h"
#include "gcin.h"
static int N;
static FILE *fp;
static int k_lookup(char *s, char out[])
{
unsigned int key;
bzero(&key, sizeof(key));
u8cpy((char *)&key, s);
int bot=0, top=N-1;
while (bot <= top) {
int mid = (bot + top)/2;
T2S t;
fseek(fp, mid*sizeof(T2S), SEEK_SET);
fread(&t, sizeof(T2S), 1, fp);
if (key > t.a)
bot=mid+1;
else
if (key < t.a)
top=mid-1;
else
return u8cpy(out, (char *)&t.b);
}
return u8cpy(out, s);
}
#include <sys/stat.h>
static int translate(char *fname, char *str, int strN, char **out)
{
char fullname[128];
if (!strN) {
*out = strdup(str);
return 0;
}
get_sys_table_file_name(fname, fullname);
if ((fp=fopen(fullname, "rb"))==NULL)
p_err("cannot open %s %s", fname, fullname);
struct stat st;
stat(fullname, &st);
N = st.st_size / sizeof(T2S);
char *p=str;
char *endp = str + strN;
int opN=0;
char *op = NULL;
while (p < endp) {
op = (char *)realloc(op, opN+5);
opN += k_lookup(p, &op[opN]);
p+=utf8_sz(p);
}
fclose(fp);
*out = op;
op[opN]=0;
return opN;
}
int trad2sim(char *str, int strN, char **out)
{
return translate("t2s.dat", str, strN, out);
}
int sim2trad(char *str, int strN, char **out)
{
puts(str);
return translate("s2t.dat", str, strN, out);
puts(*out);
}