1 /*
2  * Copyright (C) 2020 The HIME team, Taiwan
3  * Copyright (C) 2010 Edward Der-Hua Liu, Hsin-Chu, Taiwan
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation version 2.1
8  * of the License.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18  */
19 
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 
24 #include "t2s-file.h"
25 #include "util.h"
26 
27 #define SIZE 3000
28 
29 static T2S t2s[SIZE], s2t[SIZE];
30 static int t2sn;
31 
qcmp(const void * aa0,const void * bb0)32 static int qcmp (const void *aa0, const void *bb0) {
33     const T2S *aa = (const T2S *) aa0;
34     const T2S *bb = (const T2S *) bb0;
35 
36     if (aa->a > bb->a)
37         return 1;
38     if (aa->a < bb->a)
39         return -1;
40     return 0;
41 }
42 
gen(T2S * t,const char * name)43 static void gen (T2S *t, const char *name) {
44     qsort (t, t2sn, sizeof (T2S), qcmp);
45     FILE *fw = fopen (name, "w");
46 
47     if (!fw)
48         p_err ("cannot write %s", name);
49 
50     fwrite (t, sizeof (T2S), t2sn, fw);
51     fclose (fw);
52 }
53 
main(void)54 int main (void) {
55     /*
56      * This data file is maintained by caleb-, ONLY for conversion
57      * from Traditional Chinese to Simplified Chinese.
58      * (Single Chinese glyph, one to one conversion.)
59      *
60      * However, "hime-sim2trad" also use this file to do "S to T"
61      * conversion, so the conversion result is not very ideal.
62      */
63     t2sn = 0;
64     const char *fname = "t2s-file.table";
65     FILE *fp = fopen (fname, "r");
66 
67     if (!fp)
68         dbg ("cannot open %s", fname);
69 
70     while (!feof (fp) && t2sn < SIZE) {
71         char tt[128];
72         tt[0] = '\0';
73 
74         fgets (tt, sizeof (tt), fp);
75         if (!tt[0])
76             break;
77 
78         char a[9], b[9];
79         memset (a, 0, sizeof (a));
80         memset (b, 0, sizeof (b));
81 
82         sscanf (tt, "%s %s", a, b);
83 
84         memcpy (&t2s[t2sn].a, a, sizeof (t2s[0].a));
85         memcpy (&t2s[t2sn].b, b, sizeof (t2s[0].b));
86         memcpy (&s2t[t2sn].b, a, sizeof (s2t[0].a));
87         memcpy (&s2t[t2sn].a, b, sizeof (s2t[0].b));
88 
89         t2sn++;
90     }
91 
92     gen (t2s, "t2s.dat");
93     gen (s2t, "s2t.dat");
94 
95     return 0;
96 }
97