]> pd.if.org Git - pdclib.old/blob - functions/locale/UnicodeData.py
PDCLIB-1
[pdclib.old] / functions / locale / UnicodeData.py
1 #!/usr/bin/python\r
2 # -*- coding: <encoding name> -*-\r
3 # Unicode Data Converter\r
4 #\r
5 # This file is part of the Public Domain C Library (PDCLib).\r
6 # Permission is granted to use, modify, and / or redistribute at will.\r
7 """\r
8 Converts the character information provdied by Unicode in the UnicodeData.txt\r
9 file from the Unicode character database into a table for use by PDCLib.\r
10 \r
11 Usage: Download the UnicodeData.txt file to the same directory as this script \r
12 and then run it. Both Python 2 and 3 are supported.\r
13 \r
14 Download the data from\r
15     ftp://ftp.unicode.org/Public/UNIDATA/UnicodeData.txt\r
16 """\r
17 import os\r
18 \r
19 # MUST BE KEPT SYNCHRONIZED WITH _PDCLIB_locale.h\r
20 BIT_ALPHA =   1\r
21 BIT_BLANK =   2\r
22 BIT_CNTRL =   4\r
23 BIT_GRAPH =   8\r
24 BIT_PUNCT =  16\r
25 BIT_SPACE =  32\r
26 BIT_LOWER =  64\r
27 BIT_UPPER = 128\r
28 BIT_DIGIT = 256\r
29 \r
30 # Category to bitfield mapping\r
31 categories = {\r
32     'Lu': BIT_ALPHA | BIT_GRAPH | BIT_UPPER,    # Uppercase\r
33     'Ll': BIT_ALPHA | BIT_GRAPH | BIT_LOWER,    # Lowercase\r
34     'Lt': BIT_ALPHA | BIT_GRAPH | BIT_UPPER,    # Title case. Upper?\r
35     'Lm': BIT_ALPHA | BIT_GRAPH,                # Modifier. Case?\r
36     'Lo': BIT_ALPHA | BIT_GRAPH,                # "Other" letter (e.g. Ideograph)\r
37     'Nd': BIT_DIGIT | BIT_GRAPH,                # Decimal digit\r
38     'Nl': BIT_GRAPH,                            # Letter-like numeric character\r
39     'No': BIT_GRAPH,                            # Other numeric\r
40     'Pc': BIT_PUNCT | BIT_GRAPH,                # Connecting punctuation\r
41     'Pd': BIT_PUNCT | BIT_GRAPH,                # Dash punctuation\r
42     'Ps': BIT_PUNCT | BIT_GRAPH,                # Opening punctuation\r
43     'Pe': BIT_PUNCT | BIT_GRAPH,                # Closing punctuation\r
44     'Pi': BIT_PUNCT | BIT_GRAPH,                # Opening quote\r
45     'Pf': BIT_PUNCT | BIT_GRAPH,                # Closing quote\r
46     'Po': BIT_PUNCT | BIT_GRAPH,                # Other punctuation\r
47     'Sm': BIT_GRAPH,                            # Mathematical symbol\r
48     'Sc': BIT_GRAPH,                            # Currency symbol\r
49     'Sk': BIT_GRAPH,                            # Non-letterlike modifier symbol\r
50     'So': BIT_GRAPH,                            # Other symbol\r
51     'Zs': BIT_SPACE | BIT_GRAPH | BIT_BLANK,    # Non-zero-width space character\r
52     'Zl': BIT_SPACE | BIT_GRAPH,                # Line separator\r
53     'Zp': BIT_SPACE | BIT_GRAPH,                # Paragraph separator\r
54     'Cc': BIT_CNTRL,                            # C0/C1 control codes\r
55 }\r
56 \r
57 in_file  = open('UnicodeData.txt', 'r')\r
58 out_file = open('_PDCLIB_unicodedata.c', 'w')\r
59 try:\r
60     out_file.write("""\r
61 /* Unicode Character Information ** AUTOMATICALLY GENERATED FILE **\r
62  *\r
63  * This file is part of the PDCLib public domain C Library, but is automatically\r
64  * generated from the Unicode character data information file found at\r
65  *   ftp://ftp.unicode.org/Public/UNIDATA/UnicodeData.txt\r
66  * \r
67  * As a result, the licensing that applies to that file also applies to this \r
68  * file. The licensing which applies to the Unicode character data can be found \r
69  * in Exhibit 1 of the Unicode Terms of Use, found at\r
70  *   http://www.unicode.org/copyright.html#Exhibit1\r
71  */\r
72  #ifndef REGTEST\r
73  #include <_PDCLIB_locale.h>\r
74 \r
75  _PDCLIB_wcinfo_t _PDCLIB_wcinfo[] = {\r
76 //   { value,\tflags,\tlower,\tupper\t}, // name\r
77  """)\r
78     for line in in_file:\r
79         (num_hex, name, category, combining_class, bidi_class, decomposition,\r
80          numeric_type, numeric_digit, numeric_value, mirrored, u1name, iso_com, \r
81          upper_case_hex, lower_case_hex, title_case_hex) = line.split(";")\r
82 \r
83         num       = int(num_hex, 16)\r
84         upper_case = int(upper_case_hex, 16) if len(upper_case_hex) else num\r
85         lower_case = int(lower_case_hex, 16) if len(lower_case_hex) else num\r
86         bits = categories.get(category, 0)\r
87 \r
88         if upper_case == 0 and lower_case == 0 and bits == 0:\r
89             continue\r
90 \r
91         out_file.write("    { 0x%X,\t0x%X,\t0x%X,\t0x%X }, // %s\n" % (\r
92             num, bits, lower_case, upper_case, name))\r
93     out_file.write('};\n\n')\r
94     out_file.write("""\r
95 size_t _PDCLIB_wcinfo_size = sizeof(_PDCLIB_wcinfo) / sizeof(_PDCLIB_wcinfo[0]);\r
96 #endif\r
97 \r
98 #ifdef TEST\r
99 #include <_PDCLIB_test.h>\r
100 int main( void )\r
101 {\r
102     return TEST_RESULTS;\r
103 }\r
104 #endif\r
105 \r
106 """)\r
107 except:\r
108     in_file.close()\r
109     out_file.close()\r
110     os.remove('_PDCLIB_unicodedata.c')\r
111     raise\r
112 else:\r
113     in_file.close()\r
114     out_file.close()\r