d28cd8094f51b6e28d7233777e8269a36dff560e
[libs/core.git] / jis.py
1 #!/usr/bin/python
2
3 import sys
4 import os
5
6 in_fn=sys.argv[1]
7 out_fn=os.path.splitext(in_fn)[0]+".table"
8
9 data=[]
10 for line in file(in_fn):
11         line=line.strip()
12         if line[0]=='#':
13                 continue
14         parts=line.split(None, 3)
15         code=eval(parts[1])
16         code2=((code&0xFF)-0x21)+(((code>>8)&0xFF)-0x21)*94
17         data.append((code, eval(parts[2]), code2))
18
19 out=file(out_fn, "w")
20 out.write("namespace {\n\n")
21
22 out.write("const unsigned short jisx0208_to_ucs_table[94*94] =\n{\n\t")
23 i=0
24 for code in xrange(94*94):
25         if code>0:
26                 out.write(", ")
27                 if (code%94%12)==0 or (code%94)==0:
28                         out.write("\n\t")
29         if i<len(data) and data[i][2]==code:
30                 out.write("0x%04X"%data[i][1])
31                 i+=1
32         else:
33                 out.write("0     ")
34 out.write("\n};\n\n")
35
36 data.sort(lambda x,y: cmp(x[1],y[1]))
37 out.write("struct UcsToJis\n{\n\tunsigned short ucs;\n\tunsigned short jis;\n};\nconst UcsToJis ucs_to_jisx0208_table[] =\n{\n\t")
38 for i in xrange(len(data)):
39         if i>0:
40                 out.write(", ")
41                 if (i%6)==0:
42                         out.write("\n\t")
43         out.write("{ 0x%04X, 0x%04X }"%(data[i][1], data[i][0]))
44 out.write("\n};\n\n")
45
46 out.write("unsigned ucs_to_jisx0208_table_size=%d;\n\n"%len(data))
47
48 out.write("}\n")