1 #!/usr/local/bin/python
2 # -*- coding: iso-8859-1 -*-
6 # Copyright (c) 2004 Kungliga Tekniska Högskolan
7 # (Royal Institute of Technology, Stockholm, Sweden).
10 # Redistribution and use in source and binary forms, with or without
11 # modification, are permitted provided that the following conditions
14 # 1. Redistributions of source code must retain the above copyright
15 # notice, this list of conditions and the following disclaimer.
17 # 2. Redistributions in binary form must reproduce the above copyright
18 # notice, this list of conditions and the following disclaimer in the
19 # documentation and/or other materials provided with the distribution.
21 # 3. Neither the name of the Institute nor the names of its contributors
22 # may be used to endorse or promote products derived from this software
23 # without specific prior written permission.
25 # THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
26 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 # ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
29 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45 if len(sys
.argv
) != 4:
46 print "usage: %s UnicodeData.txt"
47 " CompositionExclusions-3.2.0.txt out-dir" % sys
.argv
[0]
50 ud
= UnicodeData
.read(sys
.argv
[1])
53 """Return a sorted list of the keys of a dict"""
58 trans
= dict([(k
, [re
.sub('<[a-zA-Z]+>', '', v
[4]), v
[0]])
59 for k
,v
in ud
.items() if v
[4]])
62 for v
in trans
.values():
63 maxLength
= max(maxLength
, len(v
[0].split()))
65 normalize_h
= generate
.Header('%s/normalize_table.h' % sys
.argv
[3])
66 normalize_c
= generate
.Implementation('%s/normalize_table.c' % sys
.argv
[3])
68 normalize_h
.file.write(
70 #include <krb5-types.h>
72 #define MAX_LENGTH_CANON %u
76 unsigned short val_len;
77 unsigned short val_offset;
80 extern const struct translation _wind_normalize_table[];
82 extern const uint32_t _wind_normalize_val_table[];
84 extern const size_t _wind_normalize_table_size;
88 unsigned char next_start;
89 unsigned char next_end;
90 unsigned short next_offset;
93 extern const struct canon_node _wind_canon_table[];
95 extern const unsigned short _wind_canon_next_table[];
98 normalize_c
.file.write(
100 #include "normalize_table.h"
102 const struct translation _wind_normalize_table[] = {
105 normalizeValTable
= []
107 for k
in sortedKeys(trans
) :
109 (key
, value
, description
) = k
, v
[0], v
[1]
110 vec
= [int(x
, 0x10) for x
in value
.split()];
111 offset
= util
.subList(normalizeValTable
, vec
)
113 offset
= len(normalizeValTable
)
114 normalizeValTable
.extend(vec
) # [("0x%s" % i) for i in vec])
115 normalize_c
.file.write(" {0x%x, %u, %u}, /* %s */\n"
116 % (key
, len(vec
), offset
, description
))
118 normalize_c
.file.write(
123 normalize_c
.file.write(
124 "const size_t _wind_normalize_table_size = %u;\n\n" % len(trans
))
126 normalize_c
.file.write("const uint32_t _wind_normalize_val_table[] = {\n")
128 for v
in normalizeValTable
:
129 normalize_c
.file.write(" 0x%x,\n" % v
)
131 normalize_c
.file.write("};\n\n");
133 exclusions
= UnicodeData
.read(sys
.argv
[2])
135 inv
= dict([(''.join(["%05x" % int(x
, 0x10) for x
in v
[4].split(' ')]),
137 for k
,v
in ud
.items()
138 if v
[4] and not re
.search('<[a-zA-Z]+> *', v
[4]) and not exclusions
.has_key(k
)])
145 """add a new table"""
149 tables
[ret
] = [0] + [None] * 16
152 def add(table
, k
, v
):
153 """add an entry (k, v) to table (recursively)"""
157 i
= int(k
[0], 0x10) + 1
159 table
[i
] = createTable()
160 add(tables
[table
[i
]], k
[1:], v
)
164 for k
,v
in inv
.items():
165 add(tables
[top
], k
, v
)
172 for k
in sortedKeys(tables
) :
174 tableToNext
[k
] = len(next_table
)
177 while start
< 16 and l
[start
] == None:
180 while end
> start
and l
[end
- 1] == None:
182 tableStart
[k
] = start
185 for i
in range(start
, end
):
193 normalize_c
.file.write("const struct canon_node _wind_canon_table[] = {\n")
195 for k
in sortedKeys(tables
) :
197 normalize_c
.file.write(" {0x%x, %u, %u, %u},\n" %
198 (t
[0], tableStart
[k
], tableEnd
[k
], tableToNext
[k
]))
200 normalize_c
.file.write("};\n\n")
202 normalize_c
.file.write("const unsigned short _wind_canon_next_table[] = {\n")
205 normalize_c
.file.write(" %u,\n" % k
)
207 normalize_c
.file.write("};\n\n")