michael@0: /* michael@0: * Copyright 1996, 1997, 1998 Computing Research Labs, michael@0: * New Mexico State University michael@0: * michael@0: * Permission is hereby granted, free of charge, to any person obtaining a michael@0: * copy of this software and associated documentation files (the "Software"), michael@0: * to deal in the Software without restriction, including without limitation michael@0: * the rights to use, copy, modify, merge, publish, distribute, sublicense, michael@0: * and/or sell copies of the Software, and to permit persons to whom the michael@0: * Software is furnished to do so, subject to the following conditions: michael@0: * michael@0: * The above copyright notice and this permission notice shall be included in michael@0: * all copies or substantial portions of the Software. michael@0: * michael@0: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR michael@0: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, michael@0: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL michael@0: * THE COMPUTING RESEARCH LAB OR NEW MEXICO STATE UNIVERSITY BE LIABLE FOR ANY michael@0: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT michael@0: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR michael@0: * THE USE OR OTHER DEALINGS IN THE SOFTWARE. michael@0: */ michael@0: #ifndef lint michael@0: #ifdef __GNUC__ michael@0: static char rcsid[] __attribute__ ((unused)) = "$Id: ucgendat.c,v 1.1 1999/01/08 00:19:21 ftang%netscape.com Exp $"; michael@0: #else michael@0: static char rcsid[] = "$Id: ucgendat.c,v 1.1 1999/01/08 00:19:21 ftang%netscape.com Exp $"; michael@0: #endif michael@0: #endif michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #ifndef WIN32 michael@0: #include michael@0: #endif michael@0: michael@0: #define ishdigit(cc) (((cc) >= '0' && (cc) <= '9') ||\ michael@0: ((cc) >= 'A' && (cc) <= 'F') ||\ michael@0: ((cc) >= 'a' && (cc) <= 'f')) michael@0: michael@0: /* michael@0: * A header written to the output file with the byte-order-mark and the number michael@0: * of property nodes. michael@0: */ michael@0: static unsigned short hdr[2] = {0xfeff, 0}; michael@0: michael@0: #define NUMPROPS 49 michael@0: #define NEEDPROPS (NUMPROPS + (4 - (NUMPROPS & 3))) michael@0: michael@0: typedef struct { michael@0: char *name; michael@0: int len; michael@0: } _prop_t; michael@0: michael@0: /* michael@0: * List of properties expected to be found in the Unicode Character Database michael@0: * including some implementation specific properties. michael@0: * michael@0: * The implementation specific properties are: michael@0: * Cm = Composed (can be decomposed) michael@0: * Nb = Non-breaking michael@0: * Sy = Symmetric (has left and right forms) michael@0: * Hd = Hex digit michael@0: * Qm = Quote marks michael@0: * Mr = Mirroring michael@0: * Ss = Space, other michael@0: * Cp = Defined character michael@0: */ michael@0: static _prop_t props[NUMPROPS] = { michael@0: {"Mn", 2}, {"Mc", 2}, {"Me", 2}, {"Nd", 2}, {"Nl", 2}, {"No", 2}, michael@0: {"Zs", 2}, {"Zl", 2}, {"Zp", 2}, {"Cc", 2}, {"Cf", 2}, {"Cs", 2}, michael@0: {"Co", 2}, {"Cn", 2}, {"Lu", 2}, {"Ll", 2}, {"Lt", 2}, {"Lm", 2}, michael@0: {"Lo", 2}, {"Pc", 2}, {"Pd", 2}, {"Ps", 2}, {"Pe", 2}, {"Po", 2}, michael@0: {"Sm", 2}, {"Sc", 2}, {"Sk", 2}, {"So", 2}, {"L", 1}, {"R", 1}, michael@0: {"EN", 2}, {"ES", 2}, {"ET", 2}, {"AN", 2}, {"CS", 2}, {"B", 1}, michael@0: {"S", 1}, {"WS", 2}, {"ON", 2}, michael@0: {"Cm", 2}, {"Nb", 2}, {"Sy", 2}, {"Hd", 2}, {"Qm", 2}, {"Mr", 2}, michael@0: {"Ss", 2}, {"Cp", 2}, {"Pi", 2}, {"Pf", 2} michael@0: }; michael@0: michael@0: typedef struct { michael@0: unsigned long *ranges; michael@0: unsigned short used; michael@0: unsigned short size; michael@0: } _ranges_t; michael@0: michael@0: static _ranges_t proptbl[NUMPROPS]; michael@0: michael@0: /* michael@0: * Make sure this array is sized to be on a 4-byte boundary at compile time. michael@0: */ michael@0: static unsigned short propcnt[NEEDPROPS]; michael@0: michael@0: /* michael@0: * Array used to collect a decomposition before adding it to the decomposition michael@0: * table. michael@0: */ michael@0: static unsigned long dectmp[64]; michael@0: static unsigned long dectmp_size; michael@0: michael@0: typedef struct { michael@0: unsigned long code; michael@0: unsigned short size; michael@0: unsigned short used; michael@0: unsigned long *decomp; michael@0: } _decomp_t; michael@0: michael@0: /* michael@0: * List of decomposition. Created and expanded in order as the characters are michael@0: * encountered. michael@0: */ michael@0: static _decomp_t *decomps; michael@0: static unsigned long decomps_used; michael@0: static unsigned long decomps_size; michael@0: michael@0: /* michael@0: * Types and lists for handling lists of case mappings. michael@0: */ michael@0: typedef struct { michael@0: unsigned long key; michael@0: unsigned long other1; michael@0: unsigned long other2; michael@0: } _case_t; michael@0: michael@0: static _case_t *upper; michael@0: static _case_t *lower; michael@0: static _case_t *title; michael@0: static unsigned long upper_used; michael@0: static unsigned long upper_size; michael@0: static unsigned long lower_used; michael@0: static unsigned long lower_size; michael@0: static unsigned long title_used; michael@0: static unsigned long title_size; michael@0: michael@0: /* michael@0: * Array used to collect case mappings before adding them to a list. michael@0: */ michael@0: static unsigned long cases[3]; michael@0: michael@0: /* michael@0: * An array to hold ranges for combining classes. michael@0: */ michael@0: static unsigned long *ccl; michael@0: static unsigned long ccl_used; michael@0: static unsigned long ccl_size; michael@0: michael@0: /* michael@0: * Structures for handling numbers. michael@0: */ michael@0: typedef struct { michael@0: unsigned long code; michael@0: unsigned long idx; michael@0: } _codeidx_t; michael@0: michael@0: typedef struct { michael@0: short numerator; michael@0: short denominator; michael@0: } _num_t; michael@0: michael@0: /* michael@0: * Arrays to hold the mapping of codes to numbers. michael@0: */ michael@0: static _codeidx_t *ncodes; michael@0: static unsigned long ncodes_used; michael@0: static unsigned long ncodes_size; michael@0: michael@0: static _num_t *nums; michael@0: static unsigned long nums_used; michael@0: static unsigned long nums_size; michael@0: michael@0: /* michael@0: * Array for holding numbers. michael@0: */ michael@0: static _num_t *nums; michael@0: static unsigned long nums_used; michael@0: static unsigned long nums_size; michael@0: michael@0: static void michael@0: #ifdef __STDC__ michael@0: add_range(unsigned long start, unsigned long end, char *p1, char *p2) michael@0: #else michael@0: add_range(start, end, p1, p2) michael@0: unsigned long start, end; michael@0: char *p1, *p2; michael@0: #endif michael@0: { michael@0: int i, j, k, len; michael@0: _ranges_t *rlp; michael@0: char *name; michael@0: michael@0: for (k = 0; k < 2; k++) { michael@0: if (k == 0) { michael@0: name = p1; michael@0: len = 2; michael@0: } else { michael@0: if (p2 == 0) michael@0: break; michael@0: michael@0: name = p2; michael@0: len = 1; michael@0: } michael@0: michael@0: for (i = 0; i < NUMPROPS; i++) { michael@0: if (props[i].len == len && memcmp(props[i].name, name, len) == 0) michael@0: break; michael@0: } michael@0: michael@0: if (i == NUMPROPS) michael@0: continue; michael@0: michael@0: rlp = &proptbl[i]; michael@0: michael@0: /* michael@0: * Resize the range list if necessary. michael@0: */ michael@0: if (rlp->used == rlp->size) { michael@0: if (rlp->size == 0) michael@0: rlp->ranges = (unsigned long *) michael@0: malloc(sizeof(unsigned long) << 3); michael@0: else michael@0: rlp->ranges = (unsigned long *) michael@0: realloc((char *) rlp->ranges, michael@0: sizeof(unsigned long) * (rlp->size + 8)); michael@0: rlp->size += 8; michael@0: } michael@0: michael@0: /* michael@0: * If this is the first code for this property list, just add it michael@0: * and return. michael@0: */ michael@0: if (rlp->used == 0) { michael@0: rlp->ranges[0] = start; michael@0: rlp->ranges[1] = end; michael@0: rlp->used += 2; michael@0: continue; michael@0: } michael@0: michael@0: /* michael@0: * Optimize the case of adding the range to the end. michael@0: */ michael@0: j = rlp->used - 1; michael@0: if (start > rlp->ranges[j]) { michael@0: j = rlp->used; michael@0: rlp->ranges[j++] = start; michael@0: rlp->ranges[j++] = end; michael@0: rlp->used = j; michael@0: continue; michael@0: } michael@0: michael@0: /* michael@0: * Need to locate the insertion point. michael@0: */ michael@0: for (i = 0; michael@0: i < rlp->used && start > rlp->ranges[i + 1] + 1; i += 2) ; michael@0: michael@0: /* michael@0: * If the start value lies in the current range, then simply set the michael@0: * new end point of the range to the end value passed as a parameter. michael@0: */ michael@0: if (rlp->ranges[i] <= start && start <= rlp->ranges[i + 1] + 1) { michael@0: rlp->ranges[i + 1] = end; michael@0: return; michael@0: } michael@0: michael@0: /* michael@0: * Shift following values up by two. michael@0: */ michael@0: for (j = rlp->used; j > i; j -= 2) { michael@0: rlp->ranges[j] = rlp->ranges[j - 2]; michael@0: rlp->ranges[j + 1] = rlp->ranges[j - 1]; michael@0: } michael@0: michael@0: /* michael@0: * Add the new range at the insertion point. michael@0: */ michael@0: rlp->ranges[i] = start; michael@0: rlp->ranges[i + 1] = end; michael@0: rlp->used += 2; michael@0: } michael@0: } michael@0: michael@0: static void michael@0: #ifdef __STDC__ michael@0: ordered_range_insert(unsigned long c, char *name, int len) michael@0: #else michael@0: ordered_range_insert(c, name, len) michael@0: unsigned long c; michael@0: char *name; michael@0: int len; michael@0: #endif michael@0: { michael@0: int i, j; michael@0: unsigned long s, e; michael@0: _ranges_t *rlp; michael@0: michael@0: if (len == 0) michael@0: return; michael@0: michael@0: for (i = 0; i < NUMPROPS; i++) { michael@0: if (props[i].len == len && memcmp(props[i].name, name, len) == 0) michael@0: break; michael@0: } michael@0: michael@0: if (i == NUMPROPS) michael@0: return; michael@0: michael@0: /* michael@0: * Have a match, so insert the code in order. michael@0: */ michael@0: rlp = &proptbl[i]; michael@0: michael@0: /* michael@0: * Resize the range list if necessary. michael@0: */ michael@0: if (rlp->used == rlp->size) { michael@0: if (rlp->size == 0) michael@0: rlp->ranges = (unsigned long *) michael@0: malloc(sizeof(unsigned long) << 3); michael@0: else michael@0: rlp->ranges = (unsigned long *) michael@0: realloc((char *) rlp->ranges, michael@0: sizeof(unsigned long) * (rlp->size + 8)); michael@0: rlp->size += 8; michael@0: } michael@0: michael@0: /* michael@0: * If this is the first code for this property list, just add it michael@0: * and return. michael@0: */ michael@0: if (rlp->used == 0) { michael@0: rlp->ranges[0] = rlp->ranges[1] = c; michael@0: rlp->used += 2; michael@0: return; michael@0: } michael@0: michael@0: /* michael@0: * Optimize the cases of extending the last range and adding new ranges to michael@0: * the end. michael@0: */ michael@0: j = rlp->used - 1; michael@0: e = rlp->ranges[j]; michael@0: s = rlp->ranges[j - 1]; michael@0: michael@0: if (c == e + 1) { michael@0: /* michael@0: * Extend the last range. michael@0: */ michael@0: rlp->ranges[j] = c; michael@0: return; michael@0: } michael@0: michael@0: if (c > e + 1) { michael@0: /* michael@0: * Start another range on the end. michael@0: */ michael@0: j = rlp->used; michael@0: rlp->ranges[j] = rlp->ranges[j + 1] = c; michael@0: rlp->used += 2; michael@0: return; michael@0: } michael@0: michael@0: if (c >= s) michael@0: /* michael@0: * The code is a duplicate of a code in the last range, so just return. michael@0: */ michael@0: return; michael@0: michael@0: /* michael@0: * The code should be inserted somewhere before the last range in the michael@0: * list. Locate the insertion point. michael@0: */ michael@0: for (i = 0; michael@0: i < rlp->used && c > rlp->ranges[i + 1] + 1; i += 2) ; michael@0: michael@0: s = rlp->ranges[i]; michael@0: e = rlp->ranges[i + 1]; michael@0: michael@0: if (c == e + 1) michael@0: /* michael@0: * Simply extend the current range. michael@0: */ michael@0: rlp->ranges[i + 1] = c; michael@0: else if (c < s) { michael@0: /* michael@0: * Add a new entry before the current location. Shift all entries michael@0: * before the current one up by one to make room. michael@0: */ michael@0: for (j = rlp->used; j > i; j -= 2) { michael@0: rlp->ranges[j] = rlp->ranges[j - 2]; michael@0: rlp->ranges[j + 1] = rlp->ranges[j - 1]; michael@0: } michael@0: rlp->ranges[i] = rlp->ranges[i + 1] = c; michael@0: michael@0: rlp->used += 2; michael@0: } michael@0: } michael@0: michael@0: static void michael@0: #ifdef __STDC__ michael@0: add_decomp(unsigned long code) michael@0: #else michael@0: add_decomp(code) michael@0: unsigned long code; michael@0: #endif michael@0: { michael@0: unsigned long i, j, size; michael@0: michael@0: /* michael@0: * Add the code to the composite property. michael@0: */ michael@0: ordered_range_insert(code, "Cm", 2); michael@0: michael@0: /* michael@0: * Locate the insertion point for the code. michael@0: */ michael@0: for (i = 0; i < decomps_used && code > decomps[i].code; i++) ; michael@0: michael@0: /* michael@0: * Allocate space for a new decomposition. michael@0: */ michael@0: if (decomps_used == decomps_size) { michael@0: if (decomps_size == 0) michael@0: decomps = (_decomp_t *) malloc(sizeof(_decomp_t) << 3); michael@0: else michael@0: decomps = (_decomp_t *) michael@0: realloc((char *) decomps, michael@0: sizeof(_decomp_t) * (decomps_size + 8)); michael@0: (void) memset((char *) (decomps + decomps_size), 0, michael@0: sizeof(_decomp_t) << 3); michael@0: decomps_size += 8; michael@0: } michael@0: michael@0: if (i < decomps_used && code != decomps[i].code) { michael@0: /* michael@0: * Shift the decomps up by one if the codes don't match. michael@0: */ michael@0: for (j = decomps_used; j > i; j--) michael@0: (void) memcpy((char *) &decomps[j], (char *) &decomps[j - 1], michael@0: sizeof(_decomp_t)); michael@0: } michael@0: michael@0: /* michael@0: * Insert or replace a decomposition. michael@0: */ michael@0: size = dectmp_size + (4 - (dectmp_size & 3)); michael@0: if (decomps[i].size < size) { michael@0: if (decomps[i].size == 0) michael@0: decomps[i].decomp = (unsigned long *) michael@0: malloc(sizeof(unsigned long) * size); michael@0: else michael@0: decomps[i].decomp = (unsigned long *) michael@0: realloc((char *) decomps[i].decomp, michael@0: sizeof(unsigned long) * size); michael@0: decomps[i].size = size; michael@0: } michael@0: michael@0: if (decomps[i].code != code) michael@0: decomps_used++; michael@0: michael@0: decomps[i].code = code; michael@0: decomps[i].used = dectmp_size; michael@0: (void) memcpy((char *) decomps[i].decomp, (char *) dectmp, michael@0: sizeof(unsigned long) * dectmp_size); michael@0: michael@0: } michael@0: michael@0: static void michael@0: #ifdef __STDC__ michael@0: add_title(unsigned long code) michael@0: #else michael@0: add_title(code) michael@0: unsigned long code; michael@0: #endif michael@0: { michael@0: unsigned long i, j; michael@0: michael@0: /* michael@0: * Always map the code to itself. michael@0: */ michael@0: cases[2] = code; michael@0: michael@0: if (title_used == title_size) { michael@0: if (title_size == 0) michael@0: title = (_case_t *) malloc(sizeof(_case_t) << 3); michael@0: else michael@0: title = (_case_t *) realloc((char *) title, michael@0: sizeof(_case_t) * (title_size + 8)); michael@0: title_size += 8; michael@0: } michael@0: michael@0: /* michael@0: * Locate the insertion point. michael@0: */ michael@0: for (i = 0; i < title_used && code > title[i].key; i++) ; michael@0: michael@0: if (i < title_used) { michael@0: /* michael@0: * Shift the array up by one. michael@0: */ michael@0: for (j = title_used; j > i; j--) michael@0: (void) memcpy((char *) &title[j], (char *) &title[j - 1], michael@0: sizeof(_case_t)); michael@0: } michael@0: michael@0: title[i].key = cases[2]; /* Title */ michael@0: title[i].other1 = cases[0]; /* Upper */ michael@0: title[i].other2 = cases[1]; /* Lower */ michael@0: michael@0: title_used++; michael@0: } michael@0: michael@0: static void michael@0: #ifdef __STDC__ michael@0: add_upper(unsigned long code) michael@0: #else michael@0: add_upper(code) michael@0: unsigned long code; michael@0: #endif michael@0: { michael@0: unsigned long i, j; michael@0: michael@0: /* michael@0: * Always map the code to itself. michael@0: */ michael@0: cases[0] = code; michael@0: michael@0: /* michael@0: * If the title case character is not present, then make it the same as michael@0: * the upper case. michael@0: */ michael@0: if (cases[2] == 0) michael@0: cases[2] = code; michael@0: michael@0: if (upper_used == upper_size) { michael@0: if (upper_size == 0) michael@0: upper = (_case_t *) malloc(sizeof(_case_t) << 3); michael@0: else michael@0: upper = (_case_t *) realloc((char *) upper, michael@0: sizeof(_case_t) * (upper_size + 8)); michael@0: upper_size += 8; michael@0: } michael@0: michael@0: /* michael@0: * Locate the insertion point. michael@0: */ michael@0: for (i = 0; i < upper_used && code > upper[i].key; i++) ; michael@0: michael@0: if (i < upper_used) { michael@0: /* michael@0: * Shift the array up by one. michael@0: */ michael@0: for (j = upper_used; j > i; j--) michael@0: (void) memcpy((char *) &upper[j], (char *) &upper[j - 1], michael@0: sizeof(_case_t)); michael@0: } michael@0: michael@0: upper[i].key = cases[0]; /* Upper */ michael@0: upper[i].other1 = cases[1]; /* Lower */ michael@0: upper[i].other2 = cases[2]; /* Title */ michael@0: michael@0: upper_used++; michael@0: } michael@0: michael@0: static void michael@0: #ifdef __STDC__ michael@0: add_lower(unsigned long code) michael@0: #else michael@0: add_lower(code) michael@0: unsigned long code; michael@0: #endif michael@0: { michael@0: unsigned long i, j; michael@0: michael@0: /* michael@0: * Always map the code to itself. michael@0: */ michael@0: cases[1] = code; michael@0: michael@0: /* michael@0: * If the title case character is empty, then make it the same as the michael@0: * upper case. michael@0: */ michael@0: if (cases[2] == 0) michael@0: cases[2] = cases[0]; michael@0: michael@0: if (lower_used == lower_size) { michael@0: if (lower_size == 0) michael@0: lower = (_case_t *) malloc(sizeof(_case_t) << 3); michael@0: else michael@0: lower = (_case_t *) realloc((char *) lower, michael@0: sizeof(_case_t) * (lower_size + 8)); michael@0: lower_size += 8; michael@0: } michael@0: michael@0: /* michael@0: * Locate the insertion point. michael@0: */ michael@0: for (i = 0; i < lower_used && code > lower[i].key; i++) ; michael@0: michael@0: if (i < lower_used) { michael@0: /* michael@0: * Shift the array up by one. michael@0: */ michael@0: for (j = lower_used; j > i; j--) michael@0: (void) memcpy((char *) &lower[j], (char *) &lower[j - 1], michael@0: sizeof(_case_t)); michael@0: } michael@0: michael@0: lower[i].key = cases[1]; /* Lower */ michael@0: lower[i].other1 = cases[0]; /* Upper */ michael@0: lower[i].other2 = cases[2]; /* Title */ michael@0: michael@0: lower_used++; michael@0: } michael@0: michael@0: static void michael@0: #ifdef __STDC__ michael@0: ordered_ccl_insert(unsigned long c, unsigned long ccl_code) michael@0: #else michael@0: ordered_ccl_insert(c, ccl_code) michael@0: unsigned long c, ccl_code; michael@0: #endif michael@0: { michael@0: unsigned long i, j; michael@0: michael@0: if (ccl_used == ccl_size) { michael@0: if (ccl_size == 0) michael@0: ccl = (unsigned long *) malloc(sizeof(unsigned long) * 24); michael@0: else michael@0: ccl = (unsigned long *) michael@0: realloc((char *) ccl, sizeof(unsigned long) * (ccl_size + 24)); michael@0: ccl_size += 24; michael@0: } michael@0: michael@0: /* michael@0: * Optimize adding the first item. michael@0: */ michael@0: if (ccl_used == 0) { michael@0: ccl[0] = ccl[1] = c; michael@0: ccl[2] = ccl_code; michael@0: ccl_used += 3; michael@0: return; michael@0: } michael@0: michael@0: /* michael@0: * Handle the special case of extending the range on the end. This michael@0: * requires that the combining class codes are the same. michael@0: */ michael@0: if (ccl_code == ccl[ccl_used - 1] && c == ccl[ccl_used - 2] + 1) { michael@0: ccl[ccl_used - 2] = c; michael@0: return; michael@0: } michael@0: michael@0: /* michael@0: * Handle the special case of adding another range on the end. michael@0: */ michael@0: if (c > ccl[ccl_used - 2] + 1 || michael@0: (c == ccl[ccl_used - 2] + 1 && ccl_code != ccl[ccl_used - 1])) { michael@0: ccl[ccl_used++] = c; michael@0: ccl[ccl_used++] = c; michael@0: ccl[ccl_used++] = ccl_code; michael@0: return; michael@0: } michael@0: michael@0: /* michael@0: * Locate either the insertion point or range for the code. michael@0: */ michael@0: for (i = 0; i < ccl_used && c > ccl[i + 1] + 1; i += 3) ; michael@0: michael@0: if (ccl_code == ccl[i + 2] && c == ccl[i + 1] + 1) { michael@0: /* michael@0: * Extend an existing range. michael@0: */ michael@0: ccl[i + 1] = c; michael@0: return; michael@0: } else if (c < ccl[i]) { michael@0: /* michael@0: * Start a new range before the current location. michael@0: */ michael@0: for (j = ccl_used; j > i; j -= 3) { michael@0: ccl[j] = ccl[j - 3]; michael@0: ccl[j - 1] = ccl[j - 4]; michael@0: ccl[j - 2] = ccl[j - 5]; michael@0: } michael@0: ccl[i] = ccl[i + 1] = c; michael@0: ccl[i + 2] = ccl_code; michael@0: } michael@0: } michael@0: michael@0: /* michael@0: * Adds a number if it does not already exist and returns an index value michael@0: * multiplied by 2. michael@0: */ michael@0: static unsigned long michael@0: #ifdef __STDC__ michael@0: make_number(short num, short denom) michael@0: #else michael@0: make_number(num, denom) michael@0: short num, denom; michael@0: #endif michael@0: { michael@0: unsigned long n; michael@0: michael@0: /* michael@0: * Determine if the number already exists. michael@0: */ michael@0: for (n = 0; n < nums_used; n++) { michael@0: if (nums[n].numerator == num && nums[n].denominator == denom) michael@0: return n << 1; michael@0: } michael@0: michael@0: if (nums_used == nums_size) { michael@0: if (nums_size == 0) michael@0: nums = (_num_t *) malloc(sizeof(_num_t) << 3); michael@0: else michael@0: nums = (_num_t *) realloc((char *) nums, michael@0: sizeof(_num_t) * (nums_size + 8)); michael@0: nums_size += 8; michael@0: } michael@0: michael@0: n = nums_used++; michael@0: nums[n].numerator = num; michael@0: nums[n].denominator = denom; michael@0: michael@0: return n << 1; michael@0: } michael@0: michael@0: static void michael@0: #ifdef __STDC__ michael@0: add_number(unsigned long code, short num, short denom) michael@0: #else michael@0: add_number(code, num, denom) michael@0: unsigned long code; michael@0: short num, denom; michael@0: #endif michael@0: { michael@0: unsigned long i, j; michael@0: michael@0: /* michael@0: * Insert the code in order. michael@0: */ michael@0: for (i = 0; i < ncodes_used && code > ncodes[i].code; i++) ; michael@0: michael@0: /* michael@0: * Handle the case of the codes matching and simply replace the number michael@0: * that was there before. michael@0: */ michael@0: if (ncodes_used > 0 && code == ncodes[i].code) { michael@0: ncodes[i].idx = make_number(num, denom); michael@0: return; michael@0: } michael@0: michael@0: /* michael@0: * Resize the array if necessary. michael@0: */ michael@0: if (ncodes_used == ncodes_size) { michael@0: if (ncodes_size == 0) michael@0: ncodes = (_codeidx_t *) malloc(sizeof(_codeidx_t) << 3); michael@0: else michael@0: ncodes = (_codeidx_t *) michael@0: realloc((char *) ncodes, sizeof(_codeidx_t) * (ncodes_size + 8)); michael@0: michael@0: ncodes_size += 8; michael@0: } michael@0: michael@0: /* michael@0: * Shift things around to insert the code if necessary. michael@0: */ michael@0: if (i < ncodes_used) { michael@0: for (j = ncodes_used; j > i; j--) { michael@0: ncodes[j].code = ncodes[j - 1].code; michael@0: ncodes[j].idx = ncodes[j - 1].idx; michael@0: } michael@0: } michael@0: ncodes[i].code = code; michael@0: ncodes[i].idx = make_number(num, denom); michael@0: michael@0: ncodes_used++; michael@0: } michael@0: michael@0: /* michael@0: * This routine assumes that the line is a valid Unicode Character Database michael@0: * entry. michael@0: */ michael@0: static void michael@0: #ifdef __STDC__ michael@0: read_cdata(FILE *in) michael@0: #else michael@0: read_cdata(in) michael@0: FILE *in; michael@0: #endif michael@0: { michael@0: unsigned long i, lineno, skip, code, ccl_code; michael@0: short wnum, neg, number[2]; michael@0: char line[512], *s, *e; michael@0: michael@0: lineno = skip = 0; michael@0: while (fscanf(in, "%[^\n]\n", line) != EOF) { michael@0: lineno++; michael@0: michael@0: /* michael@0: * Skip blank lines and lines that start with a '#'. michael@0: */ michael@0: if (line[0] == 0 || line[0] == '#') michael@0: continue; michael@0: michael@0: /* michael@0: * If lines need to be skipped, do it here. michael@0: */ michael@0: if (skip) { michael@0: skip--; michael@0: continue; michael@0: } michael@0: michael@0: /* michael@0: * Collect the code. The code can be up to 6 hex digits in length to michael@0: * allow surrogates to be specified. michael@0: */ michael@0: for (s = line, i = code = 0; *s != ';' && i < 6; i++, s++) { michael@0: code <<= 4; michael@0: if (*s >= '0' && *s <= '9') michael@0: code += *s - '0'; michael@0: else if (*s >= 'A' && *s <= 'F') michael@0: code += (*s - 'A') + 10; michael@0: else if (*s >= 'a' && *s <= 'f') michael@0: code += (*s - 'a') + 10; michael@0: } michael@0: michael@0: /* michael@0: * Handle the following special cases: michael@0: * 1. 4E00-9FA5 CJK Ideographs. michael@0: * 2. AC00-D7A3 Hangul Syllables. michael@0: * 3. D800-DFFF Surrogates. michael@0: * 4. E000-F8FF Private Use Area. michael@0: * 5. F900-FA2D Han compatibility. michael@0: */ michael@0: switch (code) { michael@0: case 0x4e00: michael@0: /* michael@0: * The Han ideographs. michael@0: */ michael@0: add_range(0x4e00, 0x9fff, "Lo", "L"); michael@0: michael@0: /* michael@0: * Add the characters to the defined category. michael@0: */ michael@0: add_range(0x4e00, 0x9fa5, "Cp", 0); michael@0: michael@0: skip = 1; michael@0: break; michael@0: case 0xac00: michael@0: /* michael@0: * The Hangul syllables. michael@0: */ michael@0: add_range(0xac00, 0xd7a3, "Lo", "L"); michael@0: michael@0: /* michael@0: * Add the characters to the defined category. michael@0: */ michael@0: add_range(0xac00, 0xd7a3, "Cp", 0); michael@0: michael@0: skip = 1; michael@0: break; michael@0: case 0xd800: michael@0: /* michael@0: * Make a range of all surrogates and assume some default michael@0: * properties. michael@0: */ michael@0: add_range(0x010000, 0x10ffff, "Cs", "L"); michael@0: skip = 5; michael@0: break; michael@0: case 0xe000: michael@0: /* michael@0: * The Private Use area. Add with a default set of properties. michael@0: */ michael@0: add_range(0xe000, 0xf8ff, "Co", "L"); michael@0: skip = 1; michael@0: break; michael@0: case 0xf900: michael@0: /* michael@0: * The CJK compatibility area. michael@0: */ michael@0: add_range(0xf900, 0xfaff, "Lo", "L"); michael@0: michael@0: /* michael@0: * Add the characters to the defined category. michael@0: */ michael@0: add_range(0xf900, 0xfaff, "Cp", 0); michael@0: michael@0: skip = 1; michael@0: } michael@0: michael@0: if (skip) michael@0: continue; michael@0: michael@0: /* michael@0: * Add the code to the defined category. michael@0: */ michael@0: ordered_range_insert(code, "Cp", 2); michael@0: michael@0: /* michael@0: * Locate the first character property field. michael@0: */ michael@0: for (i = 0; *s != 0 && i < 2; s++) { michael@0: if (*s == ';') michael@0: i++; michael@0: } michael@0: for (e = s; *e && *e != ';'; e++) ; michael@0: michael@0: ordered_range_insert(code, s, e - s); michael@0: michael@0: /* michael@0: * Locate the combining class code. michael@0: */ michael@0: for (s = e; *s != 0 && i < 3; s++) { michael@0: if (*s == ';') michael@0: i++; michael@0: } michael@0: michael@0: /* michael@0: * Convert the combining class code from decimal. michael@0: */ michael@0: for (ccl_code = 0, e = s; *e && *e != ';'; e++) michael@0: ccl_code = (ccl_code * 10) + (*e - '0'); michael@0: michael@0: /* michael@0: * Add the code if it not 0. michael@0: */ michael@0: if (ccl_code != 0) michael@0: ordered_ccl_insert(code, ccl_code); michael@0: michael@0: /* michael@0: * Locate the second character property field. michael@0: */ michael@0: for (s = e; *s != 0 && i < 4; s++) { michael@0: if (*s == ';') michael@0: i++; michael@0: } michael@0: for (e = s; *e && *e != ';'; e++) ; michael@0: michael@0: ordered_range_insert(code, s, e - s); michael@0: michael@0: /* michael@0: * Check for a decomposition. michael@0: */ michael@0: s = ++e; michael@0: if (*s != ';' && *s != '<') { michael@0: /* michael@0: * Collect the codes of the decomposition. michael@0: */ michael@0: for (dectmp_size = 0; *s != ';'; ) { michael@0: /* michael@0: * Skip all leading non-hex digits. michael@0: */ michael@0: while (!ishdigit(*s)) michael@0: s++; michael@0: michael@0: for (dectmp[dectmp_size] = 0; ishdigit(*s); s++) { michael@0: dectmp[dectmp_size] <<= 4; michael@0: if (*s >= '0' && *s <= '9') michael@0: dectmp[dectmp_size] += *s - '0'; michael@0: else if (*s >= 'A' && *s <= 'F') michael@0: dectmp[dectmp_size] += (*s - 'A') + 10; michael@0: else if (*s >= 'a' && *s <= 'f') michael@0: dectmp[dectmp_size] += (*s - 'a') + 10; michael@0: } michael@0: dectmp_size++; michael@0: } michael@0: michael@0: /* michael@0: * If there is more than one code in the temporary decomposition michael@0: * array, then add the character with its decomposition. michael@0: */ michael@0: if (dectmp_size > 1) michael@0: add_decomp(code); michael@0: } michael@0: michael@0: /* michael@0: * Skip to the number field. michael@0: */ michael@0: for (i = 0; i < 3 && *s; s++) { michael@0: if (*s == ';') michael@0: i++; michael@0: } michael@0: michael@0: /* michael@0: * Scan the number in. michael@0: */ michael@0: number[0] = number[1] = 0; michael@0: for (e = s, neg = wnum = 0; *e && *e != ';'; e++) { michael@0: if (*e == '-') { michael@0: neg = 1; michael@0: continue; michael@0: } michael@0: michael@0: if (*e == '/') { michael@0: /* michael@0: * Move the the denominator of the fraction. michael@0: */ michael@0: if (neg) michael@0: number[wnum] *= -1; michael@0: neg = 0; michael@0: e++; michael@0: wnum++; michael@0: } michael@0: number[wnum] = (number[wnum] * 10) + (*e - '0'); michael@0: } michael@0: michael@0: if (e > s) { michael@0: /* michael@0: * Adjust the denominator in case of integers and add the number. michael@0: */ michael@0: if (wnum == 0) michael@0: number[1] = number[0]; michael@0: michael@0: add_number(code, number[0], number[1]); michael@0: } michael@0: michael@0: /* michael@0: * Skip to the start of the possible case mappings. michael@0: */ michael@0: for (s = e, i = 0; i < 4 && *s; s++) { michael@0: if (*s == ';') michael@0: i++; michael@0: } michael@0: michael@0: /* michael@0: * Collect the case mappings. michael@0: */ michael@0: cases[0] = cases[1] = cases[2] = 0; michael@0: for (i = 0; i < 3; i++) { michael@0: while (ishdigit(*s)) { michael@0: cases[i] <<= 4; michael@0: if (*s >= '0' && *s <= '9') michael@0: cases[i] += *s - '0'; michael@0: else if (*s >= 'A' && *s <= 'F') michael@0: cases[i] += (*s - 'A') + 10; michael@0: else if (*s >= 'a' && *s <= 'f') michael@0: cases[i] += (*s - 'a') + 10; michael@0: s++; michael@0: } michael@0: if (*s == ';') michael@0: s++; michael@0: } michael@0: if (cases[0] && cases[1]) michael@0: /* michael@0: * Add the upper and lower mappings for a title case character. michael@0: */ michael@0: add_title(code); michael@0: else if (cases[1]) michael@0: /* michael@0: * Add the lower and title case mappings for the upper case michael@0: * character. michael@0: */ michael@0: add_upper(code); michael@0: else if (cases[0]) michael@0: /* michael@0: * Add the upper and title case mappings for the lower case michael@0: * character. michael@0: */ michael@0: add_lower(code); michael@0: } michael@0: } michael@0: michael@0: static _decomp_t * michael@0: #ifdef __STDC__ michael@0: find_decomp(unsigned long code) michael@0: #else michael@0: find_decomp(code) michael@0: unsigned long code; michael@0: #endif michael@0: { michael@0: long l, r, m; michael@0: michael@0: l = 0; michael@0: r = decomps_used - 1; michael@0: while (l <= r) { michael@0: m = (l + r) >> 1; michael@0: if (code > decomps[m].code) michael@0: l = m + 1; michael@0: else if (code < decomps[m].code) michael@0: r = m - 1; michael@0: else michael@0: return &decomps[m]; michael@0: } michael@0: return 0; michael@0: } michael@0: michael@0: static void michael@0: #ifdef __STDC__ michael@0: decomp_it(_decomp_t *d) michael@0: #else michael@0: decomp_it(d) michael@0: _decomp_t *d; michael@0: #endif michael@0: { michael@0: unsigned long i; michael@0: _decomp_t *dp; michael@0: michael@0: for (i = 0; i < d->used; i++) { michael@0: if ((dp = find_decomp(d->decomp[i])) != 0) michael@0: decomp_it(dp); michael@0: else michael@0: dectmp[dectmp_size++] = d->decomp[i]; michael@0: } michael@0: } michael@0: michael@0: /* michael@0: * Expand all decompositions by recursively decomposing each character michael@0: * in the decomposition. michael@0: */ michael@0: static void michael@0: #ifdef __STDC__ michael@0: expand_decomp(void) michael@0: #else michael@0: expand_decomp() michael@0: #endif michael@0: { michael@0: unsigned long i; michael@0: michael@0: for (i = 0; i < decomps_used; i++) { michael@0: dectmp_size = 0; michael@0: decomp_it(&decomps[i]); michael@0: if (dectmp_size > 0) michael@0: add_decomp(decomps[i].code); michael@0: } michael@0: } michael@0: michael@0: static void michael@0: #ifdef __STDC__ michael@0: write_cdata(char *opath) michael@0: #else michael@0: write_cdata(opath) michael@0: char *opath; michael@0: #endif michael@0: { michael@0: FILE *out; michael@0: unsigned long i, idx, bytes, nprops; michael@0: unsigned short casecnt[2]; michael@0: char path[BUFSIZ]; michael@0: michael@0: /***************************************************************** michael@0: * michael@0: * Generate the ctype data. michael@0: * michael@0: *****************************************************************/ michael@0: michael@0: /* michael@0: * Open the ctype.dat file. michael@0: */ michael@0: sprintf(path, "%s/ctype.dat", opath); michael@0: if ((out = fopen(path, "wb")) == 0) michael@0: return; michael@0: michael@0: /* michael@0: * Collect the offsets for the properties. The offsets array is michael@0: * on a 4-byte boundary to keep things efficient for architectures michael@0: * that need such a thing. michael@0: */ michael@0: for (i = idx = 0; i < NUMPROPS; i++) { michael@0: propcnt[i] = (proptbl[i].used != 0) ? idx : 0xffff; michael@0: idx += proptbl[i].used; michael@0: } michael@0: michael@0: /* michael@0: * Add the sentinel index which is used by the binary search as the upper michael@0: * bound for a search. michael@0: */ michael@0: propcnt[i] = idx; michael@0: michael@0: /* michael@0: * Record the actual number of property lists. This may be different than michael@0: * the number of offsets actually written because of aligning on a 4-byte michael@0: * boundary. michael@0: */ michael@0: hdr[1] = NUMPROPS; michael@0: michael@0: /* michael@0: * Calculate the byte count needed and pad the property counts array to a michael@0: * 4-byte boundary. michael@0: */ michael@0: if ((bytes = sizeof(unsigned short) * (NUMPROPS + 1)) & 3) michael@0: bytes += 4 - (bytes & 3); michael@0: nprops = bytes / sizeof(unsigned short); michael@0: bytes += sizeof(unsigned long) * idx; michael@0: michael@0: /* michael@0: * Write the header. michael@0: */ michael@0: fwrite((char *) hdr, sizeof(unsigned short), 2, out); michael@0: michael@0: /* michael@0: * Write the byte count. michael@0: */ michael@0: fwrite((char *) &bytes, sizeof(unsigned long), 1, out); michael@0: michael@0: /* michael@0: * Write the property list counts. michael@0: */ michael@0: fwrite((char *) propcnt, sizeof(unsigned short), nprops, out); michael@0: michael@0: /* michael@0: * Write the property lists. michael@0: */ michael@0: for (i = 0; i < NUMPROPS; i++) { michael@0: if (proptbl[i].used > 0) michael@0: fwrite((char *) proptbl[i].ranges, sizeof(unsigned long), michael@0: proptbl[i].used, out); michael@0: } michael@0: michael@0: fclose(out); michael@0: michael@0: /***************************************************************** michael@0: * michael@0: * Generate the case mapping data. michael@0: * michael@0: *****************************************************************/ michael@0: michael@0: /* michael@0: * Open the case.dat file. michael@0: */ michael@0: sprintf(path, "%s/case.dat", opath); michael@0: if ((out = fopen(path, "wb")) == 0) michael@0: return; michael@0: michael@0: /* michael@0: * Write the case mapping tables. michael@0: */ michael@0: hdr[1] = upper_used + lower_used + title_used; michael@0: casecnt[0] = upper_used; michael@0: casecnt[1] = lower_used; michael@0: michael@0: /* michael@0: * Write the header. michael@0: */ michael@0: fwrite((char *) hdr, sizeof(unsigned short), 2, out); michael@0: michael@0: /* michael@0: * Write the upper and lower case table sizes. michael@0: */ michael@0: fwrite((char *) casecnt, sizeof(unsigned short), 2, out); michael@0: michael@0: if (upper_used > 0) michael@0: /* michael@0: * Write the upper case table. michael@0: */ michael@0: fwrite((char *) upper, sizeof(_case_t), upper_used, out); michael@0: michael@0: if (lower_used > 0) michael@0: /* michael@0: * Write the lower case table. michael@0: */ michael@0: fwrite((char *) lower, sizeof(_case_t), lower_used, out); michael@0: michael@0: if (title_used > 0) michael@0: /* michael@0: * Write the title case table. michael@0: */ michael@0: fwrite((char *) title, sizeof(_case_t), title_used, out); michael@0: michael@0: fclose(out); michael@0: michael@0: /***************************************************************** michael@0: * michael@0: * Generate the decomposition data. michael@0: * michael@0: *****************************************************************/ michael@0: michael@0: /* michael@0: * Fully expand all decompositions before generating the output file. michael@0: */ michael@0: expand_decomp(); michael@0: michael@0: /* michael@0: * Open the decomp.dat file. michael@0: */ michael@0: sprintf(path, "%s/decomp.dat", opath); michael@0: if ((out = fopen(path, "wb")) == 0) michael@0: return; michael@0: michael@0: hdr[1] = decomps_used; michael@0: michael@0: /* michael@0: * Write the header. michael@0: */ michael@0: fwrite((char *) hdr, sizeof(unsigned short), 2, out); michael@0: michael@0: /* michael@0: * Write a temporary byte count which will be calculated as the michael@0: * decompositions are written out. michael@0: */ michael@0: bytes = 0; michael@0: fwrite((char *) &bytes, sizeof(unsigned long), 1, out); michael@0: michael@0: if (decomps_used) { michael@0: /* michael@0: * Write the list of decomp nodes. michael@0: */ michael@0: for (i = idx = 0; i < decomps_used; i++) { michael@0: fwrite((char *) &decomps[i].code, sizeof(unsigned long), 1, out); michael@0: fwrite((char *) &idx, sizeof(unsigned long), 1, out); michael@0: idx += decomps[i].used; michael@0: } michael@0: michael@0: /* michael@0: * Write the sentinel index as the last decomp node. michael@0: */ michael@0: fwrite((char *) &idx, sizeof(unsigned long), 1, out); michael@0: michael@0: /* michael@0: * Write the decompositions themselves. michael@0: */ michael@0: for (i = 0; i < decomps_used; i++) michael@0: fwrite((char *) decomps[i].decomp, sizeof(unsigned long), michael@0: decomps[i].used, out); michael@0: michael@0: /* michael@0: * Seek back to the beginning and write the byte count. michael@0: */ michael@0: bytes = (sizeof(unsigned long) * idx) + michael@0: (sizeof(unsigned long) * ((hdr[1] << 1) + 1)); michael@0: fseek(out, sizeof(unsigned short) << 1, 0L); michael@0: fwrite((char *) &bytes, sizeof(unsigned long), 1, out); michael@0: michael@0: fclose(out); michael@0: } michael@0: michael@0: /***************************************************************** michael@0: * michael@0: * Generate the combining class data. michael@0: * michael@0: *****************************************************************/ michael@0: michael@0: /* michael@0: * Open the cmbcl.dat file. michael@0: */ michael@0: sprintf(path, "%s/cmbcl.dat", opath); michael@0: if ((out = fopen(path, "wb")) == 0) michael@0: return; michael@0: michael@0: /* michael@0: * Set the number of ranges used. Each range has a combining class which michael@0: * means each entry is a 3-tuple. michael@0: */ michael@0: hdr[1] = ccl_used / 3; michael@0: michael@0: /* michael@0: * Write the header. michael@0: */ michael@0: fwrite((char *) hdr, sizeof(unsigned short), 2, out); michael@0: michael@0: /* michael@0: * Write out the byte count to maintain header size. michael@0: */ michael@0: bytes = ccl_used * sizeof(unsigned long); michael@0: fwrite((char *) &bytes, sizeof(unsigned long), 1, out); michael@0: michael@0: if (ccl_used > 0) michael@0: /* michael@0: * Write the combining class ranges out. michael@0: */ michael@0: fwrite((char *) ccl, sizeof(unsigned long), ccl_used, out); michael@0: michael@0: fclose(out); michael@0: michael@0: /***************************************************************** michael@0: * michael@0: * Generate the number data. michael@0: * michael@0: *****************************************************************/ michael@0: michael@0: /* michael@0: * Open the num.dat file. michael@0: */ michael@0: sprintf(path, "%s/num.dat", opath); michael@0: if ((out = fopen(path, "wb")) == 0) michael@0: return; michael@0: michael@0: /* michael@0: * The count part of the header will be the total number of codes that michael@0: * have numbers. michael@0: */ michael@0: hdr[1] = (unsigned short) (ncodes_used << 1); michael@0: bytes = (ncodes_used * sizeof(_codeidx_t)) + (nums_used * sizeof(_num_t)); michael@0: michael@0: /* michael@0: * Write the header. michael@0: */ michael@0: fwrite((char *) hdr, sizeof(unsigned short), 2, out); michael@0: michael@0: /* michael@0: * Write out the byte count to maintain header size. michael@0: */ michael@0: fwrite((char *) &bytes, sizeof(unsigned long), 1, out); michael@0: michael@0: /* michael@0: * Now, if number mappings exist, write them out. michael@0: */ michael@0: if (ncodes_used > 0) { michael@0: fwrite((char *) ncodes, sizeof(_codeidx_t), ncodes_used, out); michael@0: fwrite((char *) nums, sizeof(_num_t), nums_used, out); michael@0: } michael@0: michael@0: fclose(out); michael@0: } michael@0: michael@0: void michael@0: #ifdef __STDC__ michael@0: main(int argc, char *argv[]) michael@0: #else michael@0: main(argc, argv) michael@0: int argc; michael@0: char *argv[]; michael@0: #endif michael@0: { michael@0: FILE *in; michael@0: char *prog, *opath; michael@0: michael@0: if ((prog = strrchr(argv[0], '/')) != 0) michael@0: prog++; michael@0: else michael@0: prog = argv[0]; michael@0: michael@0: opath = 0; michael@0: in = stdin; michael@0: michael@0: argc--; michael@0: argv++; michael@0: michael@0: while (argc > 0) { michael@0: if (argv[0][0] == '-' && argv[0][1] == 'o') { michael@0: argc--; michael@0: argv++; michael@0: opath = argv[0]; michael@0: } else { michael@0: if (in != stdin) michael@0: fclose(in); michael@0: if ((in = fopen(argv[0], "rb")) == 0) michael@0: fprintf(stderr, "%s: unable to open ctype file %s\n", michael@0: prog, argv[0]); michael@0: else { michael@0: read_cdata(in); michael@0: fclose(in); michael@0: in = 0; michael@0: } michael@0: } michael@0: argc--; michael@0: argv++; michael@0: } michael@0: michael@0: if (opath == 0) michael@0: opath = "."; michael@0: write_cdata(opath); michael@0: michael@0: exit(0); michael@0: }