3 * Copyright 2001 Computing Research Labs, New Mexico State University
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE COMPUTING RESEARCH LAB OR NEW MEXICO STATE UNIVERSITY BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
20 * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 /* $Id: ucgendat.c,v 1.4 2001/01/02 18:46:20 mleisher Exp $" */
29 #include <ac/string.h>
30 #include <ac/unistd.h>
32 #define ishdigit(cc) (((cc) >= '0' && (cc) <= '9') ||\
33 ((cc) >= 'A' && (cc) <= 'F') ||\
34 ((cc) >= 'a' && (cc) <= 'f'))
37 * A header written to the output file with the byte-order-mark and the number
40 static unsigned short hdr[2] = {0xfeff, 0};
43 #define NEEDPROPS (NUMPROPS + (4 - (NUMPROPS & 3)))
51 * List of properties expected to be found in the Unicode Character Database
52 * including some implementation specific properties.
54 * The implementation specific properties are:
55 * Cm = Composed (can be decomposed)
57 * Sy = Symmetric (has left and right forms)
62 * Cp = Defined character
64 static _prop_t props[NUMPROPS] = {
65 {"Mn", 2}, {"Mc", 2}, {"Me", 2}, {"Nd", 2}, {"Nl", 2}, {"No", 2},
66 {"Zs", 2}, {"Zl", 2}, {"Zp", 2}, {"Cc", 2}, {"Cf", 2}, {"Cs", 2},
67 {"Co", 2}, {"Cn", 2}, {"Lu", 2}, {"Ll", 2}, {"Lt", 2}, {"Lm", 2},
68 {"Lo", 2}, {"Pc", 2}, {"Pd", 2}, {"Ps", 2}, {"Pe", 2}, {"Po", 2},
69 {"Sm", 2}, {"Sc", 2}, {"Sk", 2}, {"So", 2}, {"L", 1}, {"R", 1},
70 {"EN", 2}, {"ES", 2}, {"ET", 2}, {"AN", 2}, {"CS", 2}, {"B", 1},
71 {"S", 1}, {"WS", 2}, {"ON", 2},
72 {"Cm", 2}, {"Nb", 2}, {"Sy", 2}, {"Hd", 2}, {"Qm", 2}, {"Mr", 2},
73 {"Ss", 2}, {"Cp", 2}, {"Pi", 2}, {"Pf", 2}, {"AL", 2}
77 unsigned long *ranges;
82 static _ranges_t proptbl[NUMPROPS];
85 * Make sure this array is sized to be on a 4-byte boundary at compile time.
87 static unsigned short propcnt[NEEDPROPS];
90 * Array used to collect a decomposition before adding it to the decomposition
93 static unsigned long dectmp[64];
94 static unsigned long dectmp_size;
100 unsigned long *decomp;
104 * List of decomposition. Created and expanded in order as the characters are
107 static _decomp_t *decomps;
108 static unsigned long decomps_used;
109 static unsigned long decomps_size;
112 * Composition exclusion table stuff.
114 #define COMPEX_SET(c) (compexs[(c) >> 15] |= (1 << ((c) & 31)))
115 #define COMPEX_TEST(c) (compexs[(c) >> 15] & (1 << ((c) & 31)))
116 static unsigned long compexs[2048];
119 * Struct for holding a composition pair, and array of composition pairs
128 static _comp_t *comps;
129 static unsigned long comps_used;
132 * Types and lists for handling lists of case mappings.
136 unsigned long other1;
137 unsigned long other2;
140 static _case_t *upper;
141 static _case_t *lower;
142 static _case_t *title;
143 static unsigned long upper_used;
144 static unsigned long upper_size;
145 static unsigned long lower_used;
146 static unsigned long lower_size;
147 static unsigned long title_used;
148 static unsigned long title_size;
151 * Array used to collect case mappings before adding them to a list.
153 static unsigned long cases[3];
156 * An array to hold ranges for combining classes.
158 static unsigned long *ccl;
159 static unsigned long ccl_used;
160 static unsigned long ccl_size;
163 * Structures for handling numbers.
176 * Arrays to hold the mapping of codes to numbers.
178 static _codeidx_t *ncodes;
179 static unsigned long ncodes_used;
180 static unsigned long ncodes_size;
183 static unsigned long nums_used;
184 static unsigned long nums_size;
187 * Array for holding numbers.
190 static unsigned long nums_used;
191 static unsigned long nums_size;
194 add_range(unsigned long start, unsigned long end, char *p1, char *p2)
200 for (k = 0; k < 2; k++) {
212 for (i = 0; i < NUMPROPS; i++) {
213 if (props[i].len == len && memcmp(props[i].name, name, len) == 0)
223 * Resize the range list if necessary.
225 if (rlp->used == rlp->size) {
227 rlp->ranges = (unsigned long *)
228 malloc(sizeof(unsigned long) << 3);
230 rlp->ranges = (unsigned long *)
231 realloc((char *) rlp->ranges,
232 sizeof(unsigned long) * (rlp->size + 8));
237 * If this is the first code for this property list, just add it
240 if (rlp->used == 0) {
241 rlp->ranges[0] = start;
242 rlp->ranges[1] = end;
248 * Optimize the case of adding the range to the end.
251 if (start > rlp->ranges[j]) {
253 rlp->ranges[j++] = start;
254 rlp->ranges[j++] = end;
260 * Need to locate the insertion point.
263 i < rlp->used && start > rlp->ranges[i + 1] + 1; i += 2) ;
266 * If the start value lies in the current range, then simply set the
267 * new end point of the range to the end value passed as a parameter.
269 if (rlp->ranges[i] <= start && start <= rlp->ranges[i + 1] + 1) {
270 rlp->ranges[i + 1] = end;
275 * Shift following values up by two.
277 for (j = rlp->used; j > i; j -= 2) {
278 rlp->ranges[j] = rlp->ranges[j - 2];
279 rlp->ranges[j + 1] = rlp->ranges[j - 1];
283 * Add the new range at the insertion point.
285 rlp->ranges[i] = start;
286 rlp->ranges[i + 1] = end;
292 ordered_range_insert(unsigned long c, char *name, int len)
302 * Deal with directionality codes introduced in Unicode 3.0.
304 if ((len == 2 && memcmp(name, "BN", 2) == 0) ||
306 (memcmp(name, "NSM", 3) == 0 || memcmp(name, "PDF", 3) == 0 ||
307 memcmp(name, "LRE", 3) == 0 || memcmp(name, "LRO", 3) == 0 ||
308 memcmp(name, "RLE", 3) == 0 || memcmp(name, "RLO", 3) == 0))) {
310 * Mark all of these as Other Neutral to preserve compatibility with
317 for (i = 0; i < NUMPROPS; i++) {
318 if (props[i].len == len && memcmp(props[i].name, name, len) == 0)
326 * Have a match, so insert the code in order.
331 * Resize the range list if necessary.
333 if (rlp->used == rlp->size) {
335 rlp->ranges = (unsigned long *)
336 malloc(sizeof(unsigned long) << 3);
338 rlp->ranges = (unsigned long *)
339 realloc((char *) rlp->ranges,
340 sizeof(unsigned long) * (rlp->size + 8));
345 * If this is the first code for this property list, just add it
348 if (rlp->used == 0) {
349 rlp->ranges[0] = rlp->ranges[1] = c;
355 * Optimize the cases of extending the last range and adding new ranges to
360 s = rlp->ranges[j - 1];
364 * Extend the last range.
372 * Start another range on the end.
375 rlp->ranges[j] = rlp->ranges[j + 1] = c;
382 * The code is a duplicate of a code in the last range, so just return.
387 * The code should be inserted somewhere before the last range in the
388 * list. Locate the insertion point.
391 i < rlp->used && c > rlp->ranges[i + 1] + 1; i += 2) ;
394 e = rlp->ranges[i + 1];
398 * Simply extend the current range.
400 rlp->ranges[i + 1] = c;
403 * Add a new entry before the current location. Shift all entries
404 * before the current one up by one to make room.
406 for (j = rlp->used; j > i; j -= 2) {
407 rlp->ranges[j] = rlp->ranges[j - 2];
408 rlp->ranges[j + 1] = rlp->ranges[j - 1];
410 rlp->ranges[i] = rlp->ranges[i + 1] = c;
417 add_decomp(unsigned long code)
419 unsigned long i, j, size;
422 * Add the code to the composite property.
424 ordered_range_insert(code, "Cm", 2);
427 * Locate the insertion point for the code.
429 for (i = 0; i < decomps_used && code > decomps[i].code; i++) ;
432 * Allocate space for a new decomposition.
434 if (decomps_used == decomps_size) {
435 if (decomps_size == 0)
436 decomps = (_decomp_t *) malloc(sizeof(_decomp_t) << 3);
438 decomps = (_decomp_t *)
439 realloc((char *) decomps,
440 sizeof(_decomp_t) * (decomps_size + 8));
441 (void) memset((char *) (decomps + decomps_size), '\0',
442 sizeof(_decomp_t) << 3);
446 if (i < decomps_used && code != decomps[i].code) {
448 * Shift the decomps up by one if the codes don't match.
450 for (j = decomps_used; j > i; j--)
451 (void) AC_MEMCPY((char *) &decomps[j], (char *) &decomps[j - 1],
456 * Insert or replace a decomposition.
458 size = dectmp_size + (4 - (dectmp_size & 3));
459 if (decomps[i].size < size) {
460 if (decomps[i].size == 0)
461 decomps[i].decomp = (unsigned long *)
462 malloc(sizeof(unsigned long) * size);
464 decomps[i].decomp = (unsigned long *)
465 realloc((char *) decomps[i].decomp,
466 sizeof(unsigned long) * size);
467 decomps[i].size = size;
470 if (decomps[i].code != code)
473 decomps[i].code = code;
474 decomps[i].used = dectmp_size;
475 (void) AC_MEMCPY((char *) decomps[i].decomp, (char *) dectmp,
476 sizeof(unsigned long) * dectmp_size);
479 * NOTICE: This needs changing later so it is more general than simply
480 * pairs. This calculation is done here to simplify allocation elsewhere.
482 if (dectmp_size == 2)
487 add_title(unsigned long code)
492 * Always map the code to itself.
496 if (title_used == title_size) {
498 title = (_case_t *) malloc(sizeof(_case_t) << 3);
500 title = (_case_t *) realloc((char *) title,
501 sizeof(_case_t) * (title_size + 8));
506 * Locate the insertion point.
508 for (i = 0; i < title_used && code > title[i].key; i++) ;
510 if (i < title_used) {
512 * Shift the array up by one.
514 for (j = title_used; j > i; j--)
515 (void) AC_MEMCPY((char *) &title[j], (char *) &title[j - 1],
519 title[i].key = cases[2]; /* Title */
520 title[i].other1 = cases[0]; /* Upper */
521 title[i].other2 = cases[1]; /* Lower */
527 add_upper(unsigned long code)
532 * Always map the code to itself.
537 * If the title case character is not present, then make it the same as
543 if (upper_used == upper_size) {
545 upper = (_case_t *) malloc(sizeof(_case_t) << 3);
547 upper = (_case_t *) realloc((char *) upper,
548 sizeof(_case_t) * (upper_size + 8));
553 * Locate the insertion point.
555 for (i = 0; i < upper_used && code > upper[i].key; i++) ;
557 if (i < upper_used) {
559 * Shift the array up by one.
561 for (j = upper_used; j > i; j--)
562 (void) AC_MEMCPY((char *) &upper[j], (char *) &upper[j - 1],
566 upper[i].key = cases[0]; /* Upper */
567 upper[i].other1 = cases[1]; /* Lower */
568 upper[i].other2 = cases[2]; /* Title */
574 add_lower(unsigned long code)
579 * Always map the code to itself.
584 * If the title case character is empty, then make it the same as the
590 if (lower_used == lower_size) {
592 lower = (_case_t *) malloc(sizeof(_case_t) << 3);
594 lower = (_case_t *) realloc((char *) lower,
595 sizeof(_case_t) * (lower_size + 8));
600 * Locate the insertion point.
602 for (i = 0; i < lower_used && code > lower[i].key; i++) ;
604 if (i < lower_used) {
606 * Shift the array up by one.
608 for (j = lower_used; j > i; j--)
609 (void) AC_MEMCPY((char *) &lower[j], (char *) &lower[j - 1],
613 lower[i].key = cases[1]; /* Lower */
614 lower[i].other1 = cases[0]; /* Upper */
615 lower[i].other2 = cases[2]; /* Title */
621 ordered_ccl_insert(unsigned long c, unsigned long ccl_code)
625 if (ccl_used == ccl_size) {
627 ccl = (unsigned long *) malloc(sizeof(unsigned long) * 24);
629 ccl = (unsigned long *)
630 realloc((char *) ccl, sizeof(unsigned long) * (ccl_size + 24));
635 * Optimize adding the first item.
645 * Handle the special case of extending the range on the end. This
646 * requires that the combining class codes are the same.
648 if (ccl_code == ccl[ccl_used - 1] && c == ccl[ccl_used - 2] + 1) {
649 ccl[ccl_used - 2] = c;
654 * Handle the special case of adding another range on the end.
656 if (c > ccl[ccl_used - 2] + 1 ||
657 (c == ccl[ccl_used - 2] + 1 && ccl_code != ccl[ccl_used - 1])) {
660 ccl[ccl_used++] = ccl_code;
665 * Locate either the insertion point or range for the code.
667 for (i = 0; i < ccl_used && c > ccl[i + 1] + 1; i += 3) ;
669 if (ccl_code == ccl[i + 2] && c == ccl[i + 1] + 1) {
671 * Extend an existing range.
675 } else if (c < ccl[i]) {
677 * Start a new range before the current location.
679 for (j = ccl_used; j > i; j -= 3) {
681 ccl[j - 1] = ccl[j - 4];
682 ccl[j - 2] = ccl[j - 5];
684 ccl[i] = ccl[i + 1] = c;
685 ccl[i + 2] = ccl_code;
690 * Adds a number if it does not already exist and returns an index value
694 make_number(short num, short denom)
699 * Determine if the number already exists.
701 for (n = 0; n < nums_used; n++) {
702 if (nums[n].numerator == num && nums[n].denominator == denom)
706 if (nums_used == nums_size) {
708 nums = (_num_t *) malloc(sizeof(_num_t) << 3);
710 nums = (_num_t *) realloc((char *) nums,
711 sizeof(_num_t) * (nums_size + 8));
716 nums[n].numerator = num;
717 nums[n].denominator = denom;
723 add_number(unsigned long code, short num, short denom)
728 * Insert the code in order.
730 for (i = 0; i < ncodes_used && code > ncodes[i].code; i++) ;
733 * Handle the case of the codes matching and simply replace the number
734 * that was there before.
736 if (ncodes_used > 0 && code == ncodes[i].code) {
737 ncodes[i].idx = make_number(num, denom);
742 * Resize the array if necessary.
744 if (ncodes_used == ncodes_size) {
745 if (ncodes_size == 0)
746 ncodes = (_codeidx_t *) malloc(sizeof(_codeidx_t) << 3);
748 ncodes = (_codeidx_t *)
749 realloc((char *) ncodes, sizeof(_codeidx_t) * (ncodes_size + 8));
755 * Shift things around to insert the code if necessary.
757 if (i < ncodes_used) {
758 for (j = ncodes_used; j > i; j--) {
759 ncodes[j].code = ncodes[j - 1].code;
760 ncodes[j].idx = ncodes[j - 1].idx;
763 ncodes[i].code = code;
764 ncodes[i].idx = make_number(num, denom);
770 * This routine assumes that the line is a valid Unicode Character Database
776 unsigned long i, lineno, skip, code, ccl_code;
777 short wnum, neg, number[2];
778 char line[512], *s, *e;
781 while (fscanf(in, "%[^\n]\n", line) != EOF) {
785 * Skip blank lines and lines that start with a '#'.
787 if (line[0] == 0 || line[0] == '#')
791 * If lines need to be skipped, do it here.
799 * Collect the code. The code can be up to 6 hex digits in length to
800 * allow surrogates to be specified.
802 for (s = line, i = code = 0; *s != ';' && i < 6; i++, s++) {
804 if (*s >= '0' && *s <= '9')
806 else if (*s >= 'A' && *s <= 'F')
807 code += (*s - 'A') + 10;
808 else if (*s >= 'a' && *s <= 'f')
809 code += (*s - 'a') + 10;
813 * Handle the following special cases:
814 * 1. 4E00-9FA5 CJK Ideographs.
815 * 2. AC00-D7A3 Hangul Syllables.
816 * 3. D800-DFFF Surrogates.
817 * 4. E000-F8FF Private Use Area.
818 * 5. F900-FA2D Han compatibility.
823 * The Han ideographs.
825 add_range(0x4e00, 0x9fff, "Lo", "L");
828 * Add the characters to the defined category.
830 add_range(0x4e00, 0x9fa5, "Cp", 0);
836 * The Hangul syllables.
838 add_range(0xac00, 0xd7a3, "Lo", "L");
841 * Add the characters to the defined category.
843 add_range(0xac00, 0xd7a3, "Cp", 0);
849 * Make a range of all surrogates and assume some default
852 add_range(0x010000, 0x10ffff, "Cs", "L");
857 * The Private Use area. Add with a default set of properties.
859 add_range(0xe000, 0xf8ff, "Co", "L");
864 * The CJK compatibility area.
866 add_range(0xf900, 0xfaff, "Lo", "L");
869 * Add the characters to the defined category.
871 add_range(0xf900, 0xfaff, "Cp", 0);
880 * Add the code to the defined category.
882 ordered_range_insert(code, "Cp", 2);
885 * Locate the first character property field.
887 for (i = 0; *s != 0 && i < 2; s++) {
891 for (e = s; *e && *e != ';'; e++) ;
893 ordered_range_insert(code, s, e - s);
896 * Locate the combining class code.
898 for (s = e; *s != 0 && i < 3; s++) {
904 * Convert the combining class code from decimal.
906 for (ccl_code = 0, e = s; *e && *e != ';'; e++)
907 ccl_code = (ccl_code * 10) + (*e - '0');
910 * Add the code if it not 0.
913 ordered_ccl_insert(code, ccl_code);
916 * Locate the second character property field.
918 for (s = e; *s != 0 && i < 4; s++) {
922 for (e = s; *e && *e != ';'; e++) ;
924 ordered_range_insert(code, s, e - s);
927 * Check for a decomposition.
930 if (*s != ';' && *s != '<') {
932 * Collect the codes of the decomposition.
934 for (dectmp_size = 0; *s != ';'; ) {
936 * Skip all leading non-hex digits.
938 while (!ishdigit(*s))
941 for (dectmp[dectmp_size] = 0; ishdigit(*s); s++) {
942 dectmp[dectmp_size] <<= 4;
943 if (*s >= '0' && *s <= '9')
944 dectmp[dectmp_size] += *s - '0';
945 else if (*s >= 'A' && *s <= 'F')
946 dectmp[dectmp_size] += (*s - 'A') + 10;
947 else if (*s >= 'a' && *s <= 'f')
948 dectmp[dectmp_size] += (*s - 'a') + 10;
954 * If there are any codes in the temporary decomposition array,
955 * then add the character with its decomposition.
962 * Skip to the number field.
964 for (i = 0; i < 3 && *s; s++) {
970 * Scan the number in.
972 number[0] = number[1] = 0;
973 for (e = s, neg = wnum = 0; *e && *e != ';'; e++) {
981 * Move the the denominator of the fraction.
989 number[wnum] = (number[wnum] * 10) + (*e - '0');
994 * Adjust the denominator in case of integers and add the number.
997 number[1] = number[0];
999 add_number(code, number[0], number[1]);
1003 * Skip to the start of the possible case mappings.
1005 for (s = e, i = 0; i < 4 && *s; s++) {
1011 * Collect the case mappings.
1013 cases[0] = cases[1] = cases[2] = 0;
1014 for (i = 0; i < 3; i++) {
1015 while (ishdigit(*s)) {
1017 if (*s >= '0' && *s <= '9')
1018 cases[i] += *s - '0';
1019 else if (*s >= 'A' && *s <= 'F')
1020 cases[i] += (*s - 'A') + 10;
1021 else if (*s >= 'a' && *s <= 'f')
1022 cases[i] += (*s - 'a') + 10;
1028 if (cases[0] && cases[1])
1030 * Add the upper and lower mappings for a title case character.
1035 * Add the lower and title case mappings for the upper case
1041 * Add the upper and title case mappings for the lower case
1049 find_decomp(unsigned long code)
1054 r = decomps_used - 1;
1057 if (code > decomps[m].code)
1059 else if (code < decomps[m].code)
1068 decomp_it(_decomp_t *d)
1073 for (i = 0; i < d->used; i++) {
1074 if ((dp = find_decomp(d->decomp[i])) != 0)
1077 dectmp[dectmp_size++] = d->decomp[i];
1082 * Expand all decompositions by recursively decomposing each character
1083 * in the decomposition.
1090 for (i = 0; i < decomps_used; i++) {
1092 decomp_it(&decomps[i]);
1093 if (dectmp_size > 0)
1094 add_decomp(decomps[i].code);
1099 cmpcomps(_comp_t *comp1, _comp_t *comp2)
1101 long diff = comp1->code1 - comp2->code1;
1104 diff = comp1->code2 - comp2->code2;
1109 * Load composition exclusion data
1112 read_compexdata(FILE *in)
1114 unsigned short i, code;
1117 (void) memset((char *) compexs, 0, sizeof(unsigned long) << 11);
1119 while (fscanf(in, "%[^\n]\n", line) != EOF) {
1121 * Skip blank lines and lines that start with a '#'.
1123 if (line[0] == 0 || line[0] == '#')
1127 * Collect the code. Assume max 4 digits
1130 for (s = line, i = code = 0; *s != '#' && i < 4; i++, s++) {
1132 if (*s >= '0' && *s <= '9')
1134 else if (*s >= 'A' && *s <= 'F')
1135 code += (*s - 'A') + 10;
1136 else if (*s >= 'a' && *s <= 'f')
1137 code += (*s - 'a') + 10;
1144 * Creates array of compositions from decomposition array
1149 unsigned long i, cu;
1151 comps = (_comp_t *) malloc(comps_used * sizeof(_comp_t));
1153 for (i = cu = 0; i < decomps_used; i++) {
1154 if (decomps[i].used != 2 || COMPEX_TEST(decomps[i].code))
1156 comps[cu].comp = decomps[i].code;
1157 comps[cu].count = 2;
1158 comps[cu].code1 = decomps[i].decomp[0];
1159 comps[cu].code2 = decomps[i].decomp[1];
1162 qsort(comps, comps_used, sizeof(_comp_t),
1163 (int (*)(const void *, const void *)) cmpcomps);
1167 write_cdata(char *opath)
1170 unsigned long i, idx, bytes, nprops;
1171 unsigned short casecnt[2];
1174 /*****************************************************************
1176 * Generate the ctype data.
1178 *****************************************************************/
1181 * Open the ctype.dat file.
1183 sprintf(path, "%s/ctype.dat", opath);
1184 if ((out = fopen(path, "wb")) == 0)
1188 * Collect the offsets for the properties. The offsets array is
1189 * on a 4-byte boundary to keep things efficient for architectures
1190 * that need such a thing.
1192 for (i = idx = 0; i < NUMPROPS; i++) {
1193 propcnt[i] = (proptbl[i].used != 0) ? idx : 0xffff;
1194 idx += proptbl[i].used;
1198 * Add the sentinel index which is used by the binary search as the upper
1199 * bound for a search.
1204 * Record the actual number of property lists. This may be different than
1205 * the number of offsets actually written because of aligning on a 4-byte
1211 * Calculate the byte count needed and pad the property counts array to a
1214 if ((bytes = sizeof(unsigned short) * (NUMPROPS + 1)) & 3)
1215 bytes += 4 - (bytes & 3);
1216 nprops = bytes / sizeof(unsigned short);
1217 bytes += sizeof(unsigned long) * idx;
1222 fwrite((char *) hdr, sizeof(unsigned short), 2, out);
1225 * Write the byte count.
1227 fwrite((char *) &bytes, sizeof(unsigned long), 1, out);
1230 * Write the property list counts.
1232 fwrite((char *) propcnt, sizeof(unsigned short), nprops, out);
1235 * Write the property lists.
1237 for (i = 0; i < NUMPROPS; i++) {
1238 if (proptbl[i].used > 0)
1239 fwrite((char *) proptbl[i].ranges, sizeof(unsigned long),
1240 proptbl[i].used, out);
1245 /*****************************************************************
1247 * Generate the case mapping data.
1249 *****************************************************************/
1252 * Open the case.dat file.
1254 sprintf(path, "%s/case.dat", opath);
1255 if ((out = fopen(path, "wb")) == 0)
1259 * Write the case mapping tables.
1261 hdr[1] = upper_used + lower_used + title_used;
1262 casecnt[0] = upper_used;
1263 casecnt[1] = lower_used;
1268 fwrite((char *) hdr, sizeof(unsigned short), 2, out);
1271 * Write the upper and lower case table sizes.
1273 fwrite((char *) casecnt, sizeof(unsigned short), 2, out);
1277 * Write the upper case table.
1279 fwrite((char *) upper, sizeof(_case_t), upper_used, out);
1283 * Write the lower case table.
1285 fwrite((char *) lower, sizeof(_case_t), lower_used, out);
1289 * Write the title case table.
1291 fwrite((char *) title, sizeof(_case_t), title_used, out);
1295 /*****************************************************************
1297 * Generate the composition data.
1299 *****************************************************************/
1302 * Create compositions from decomposition data
1307 * Open the comp.dat file.
1309 sprintf(path, "%s/comp.dat", opath);
1310 if ((out = fopen(path, "wb")) == 0)
1316 hdr[1] = (unsigned short) comps_used * 4;
1317 fwrite((char *) hdr, sizeof(unsigned short), 2, out);
1320 * Write out the byte count to maintain header size.
1322 bytes = comps_used * sizeof(_comp_t);
1323 fwrite((char *) &bytes, sizeof(unsigned long), 1, out);
1326 * Now, if comps exist, write them out.
1329 fwrite((char *) comps, sizeof(_comp_t), comps_used, out);
1333 /*****************************************************************
1335 * Generate the decomposition data.
1337 *****************************************************************/
1340 * Fully expand all decompositions before generating the output file.
1345 * Open the decomp.dat file.
1347 sprintf(path, "%s/decomp.dat", opath);
1348 if ((out = fopen(path, "wb")) == 0)
1351 hdr[1] = decomps_used;
1356 fwrite((char *) hdr, sizeof(unsigned short), 2, out);
1359 * Write a temporary byte count which will be calculated as the
1360 * decompositions are written out.
1363 fwrite((char *) &bytes, sizeof(unsigned long), 1, out);
1367 * Write the list of decomp nodes.
1369 for (i = idx = 0; i < decomps_used; i++) {
1370 fwrite((char *) &decomps[i].code, sizeof(unsigned long), 1, out);
1371 fwrite((char *) &idx, sizeof(unsigned long), 1, out);
1372 idx += decomps[i].used;
1376 * Write the sentinel index as the last decomp node.
1378 fwrite((char *) &idx, sizeof(unsigned long), 1, out);
1381 * Write the decompositions themselves.
1383 for (i = 0; i < decomps_used; i++)
1384 fwrite((char *) decomps[i].decomp, sizeof(unsigned long),
1385 decomps[i].used, out);
1388 * Seek back to the beginning and write the byte count.
1390 bytes = (sizeof(unsigned long) * idx) +
1391 (sizeof(unsigned long) * ((hdr[1] << 1) + 1));
1392 fseek(out, sizeof(unsigned short) << 1, 0L);
1393 fwrite((char *) &bytes, sizeof(unsigned long), 1, out);
1398 /*****************************************************************
1400 * Generate the combining class data.
1402 *****************************************************************/
1405 * Open the cmbcl.dat file.
1407 sprintf(path, "%s/cmbcl.dat", opath);
1408 if ((out = fopen(path, "wb")) == 0)
1412 * Set the number of ranges used. Each range has a combining class which
1413 * means each entry is a 3-tuple.
1415 hdr[1] = ccl_used / 3;
1420 fwrite((char *) hdr, sizeof(unsigned short), 2, out);
1423 * Write out the byte count to maintain header size.
1425 bytes = ccl_used * sizeof(unsigned long);
1426 fwrite((char *) &bytes, sizeof(unsigned long), 1, out);
1430 * Write the combining class ranges out.
1432 fwrite((char *) ccl, sizeof(unsigned long), ccl_used, out);
1436 /*****************************************************************
1438 * Generate the number data.
1440 *****************************************************************/
1443 * Open the num.dat file.
1445 sprintf(path, "%s/num.dat", opath);
1446 if ((out = fopen(path, "wb")) == 0)
1450 * The count part of the header will be the total number of codes that
1453 hdr[1] = (unsigned short) (ncodes_used << 1);
1454 bytes = (ncodes_used * sizeof(_codeidx_t)) + (nums_used * sizeof(_num_t));
1459 fwrite((char *) hdr, sizeof(unsigned short), 2, out);
1462 * Write out the byte count to maintain header size.
1464 fwrite((char *) &bytes, sizeof(unsigned long), 1, out);
1467 * Now, if number mappings exist, write them out.
1469 if (ncodes_used > 0) {
1470 fwrite((char *) ncodes, sizeof(_codeidx_t), ncodes_used, out);
1471 fwrite((char *) nums, sizeof(_num_t), nums_used, out);
1481 "Usage: %s [-o output-directory|-x composition-exclusions]", prog);
1482 fprintf(stderr, " datafile1 datafile2 ...\n\n");
1484 "-o output-directory\n\t\tWrite the output files to a different");
1485 fprintf(stderr, " directory (default: .).\n");
1487 "-x composition-exclusion\n\t\tFile of composition codes");
1488 fprintf(stderr, " that should be excluded.\n");
1493 main(int argc, char *argv[])
1498 if ((prog = strrchr(argv[0], '/')) != 0)
1510 if (argv[0][0] == '-') {
1511 switch (argv[0][1]) {
1520 if ((in = fopen(argv[0], "rb")) == 0)
1522 "%s: unable to open composition exclusion file %s\n",
1525 read_compexdata(in);
1534 if (in != stdin && in != NULL)
1536 if ((in = fopen(argv[0], "rb")) == 0)
1537 fprintf(stderr, "%s: unable to open ctype file %s\n",