]> pd.if.org Git - pdclib/blob - functions/locale/UnicodeData.py
Include usage data in UnicodeData.py docstring
[pdclib] / 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  #include <_PDCLIB_locale.h>\r
73 \r
74  _PDCLIB_wctype_t _PDCLIB_wctype[] = {\r
75 //   { value,\tflags,\tlower,\tupper\t}, // name\r
76  """)\r
77     for line in in_file:\r
78         (num_hex, name, category, combining_class, bidi_class, decomposition,\r
79          numeric_type, numeric_digit, numeric_value, mirrored, u1name, iso_com, \r
80          upper_case_hex, lower_case_hex, title_case_hex) = line.split(";")\r
81 \r
82         num       = int(num_hex, 16)\r
83         upper_case = int(upper_case_hex, 16) if len(upper_case_hex) else num\r
84         lower_case = int(lower_case_hex, 16) if len(lower_case_hex) else num\r
85         bits = categories.get(category, 0)\r
86 \r
87         if upper_case == 0 and lower_case == 0 and bits == 0:\r
88             continue\r
89 \r
90         out_file.write("    { 0x%X,\t0x%X,\t0x%X,\t0x%X }, // %s\n" % (\r
91             num, bits, lower_case, upper_case, name))\r
92     out_file.write('};\n\n')\r
93     out_file.write('size_t _PDCLIB_wctype_size = sizeof(_PDCLIB_wctype) / sizeof(_PDCLIB_wctype[0]);\n\n')\r
94 except:\r
95     in_file.close()\r
96     out_file.close()\r
97     os.remove('_PDCLIB_unicodedata.c')\r
98     raise\r
99 else:\r
100     in_file.close()\r
101     out_file.close()\r