Home | History | Annotate | Line # | Download | only in ucdata
ucgendat.c revision 1.1.1.3
      1  1.1.1.2  lukem /*	$NetBSD: ucgendat.c,v 1.1.1.3 2010/12/12 15:21:57 adam Exp $	*/
      2  1.1.1.2  lukem 
      3  1.1.1.3   adam /* OpenLDAP: pkg/ldap/libraries/liblunicode/ucdata/ucgendat.c,v 1.39.2.5 2010/04/13 20:23:04 kurt Exp */
      4      1.1  lukem /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
      5      1.1  lukem  *
      6  1.1.1.3   adam  * Copyright 1998-2010 The OpenLDAP Foundation.
      7      1.1  lukem  * All rights reserved.
      8      1.1  lukem  *
      9      1.1  lukem  * Redistribution and use in source and binary forms, with or without
     10      1.1  lukem  * modification, are permitted only as authorized by the OpenLDAP
     11      1.1  lukem  * Public License.
     12      1.1  lukem  *
     13      1.1  lukem  * A copy of this license is available in file LICENSE in the
     14      1.1  lukem  * top-level directory of the distribution or, alternatively, at
     15      1.1  lukem  * <http://www.OpenLDAP.org/license.html>.
     16      1.1  lukem  */
     17      1.1  lukem /* Copyright 2001 Computing Research Labs, New Mexico State University
     18      1.1  lukem  *
     19      1.1  lukem  * Permission is hereby granted, free of charge, to any person obtaining a
     20      1.1  lukem  * copy of this software and associated documentation files (the "Software"),
     21      1.1  lukem  * to deal in the Software without restriction, including without limitation
     22      1.1  lukem  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     23      1.1  lukem  * and/or sell copies of the Software, and to permit persons to whom the
     24      1.1  lukem  * Software is furnished to do so, subject to the following conditions:
     25      1.1  lukem  *
     26      1.1  lukem  * The above copyright notice and this permission notice shall be included in
     27      1.1  lukem  * all copies or substantial portions of the Software.
     28      1.1  lukem  *
     29      1.1  lukem  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     30      1.1  lukem  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     31      1.1  lukem  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     32      1.1  lukem  * THE COMPUTING RESEARCH LAB OR NEW MEXICO STATE UNIVERSITY BE LIABLE FOR ANY
     33      1.1  lukem  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
     34      1.1  lukem  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
     35      1.1  lukem  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
     36      1.1  lukem  */
     37  1.1.1.2  lukem /* Id: ucgendat.c,v 1.4 2001/01/02 18:46:20 mleisher Exp" */
     38      1.1  lukem 
     39      1.1  lukem #include "portable.h"
     40      1.1  lukem #include "ldap_config.h"
     41      1.1  lukem 
     42      1.1  lukem #include <stdio.h>
     43      1.1  lukem #include <ac/ctype.h>
     44      1.1  lukem #include <ac/stdlib.h>
     45      1.1  lukem #include <ac/string.h>
     46      1.1  lukem #include <ac/unistd.h>
     47      1.1  lukem 
     48      1.1  lukem #include <ac/bytes.h>
     49      1.1  lukem 
     50      1.1  lukem #include <lutil.h>
     51      1.1  lukem 
     52      1.1  lukem #ifndef HARDCODE_DATA
     53      1.1  lukem #define	HARDCODE_DATA	1
     54      1.1  lukem #endif
     55      1.1  lukem 
     56      1.1  lukem #undef ishdigit
     57      1.1  lukem #define ishdigit(cc) (((cc) >= '0' && (cc) <= '9') ||\
     58      1.1  lukem                       ((cc) >= 'A' && (cc) <= 'F') ||\
     59      1.1  lukem                       ((cc) >= 'a' && (cc) <= 'f'))
     60      1.1  lukem 
     61      1.1  lukem /*
     62      1.1  lukem  * A header written to the output file with the byte-order-mark and the number
     63      1.1  lukem  * of property nodes.
     64      1.1  lukem  */
     65      1.1  lukem static ac_uint2 hdr[2] = {0xfeff, 0};
     66      1.1  lukem 
     67      1.1  lukem #define NUMPROPS 50
     68      1.1  lukem #define NEEDPROPS (NUMPROPS + (4 - (NUMPROPS & 3)))
     69      1.1  lukem 
     70      1.1  lukem typedef struct {
     71      1.1  lukem     char *name;
     72      1.1  lukem     int len;
     73      1.1  lukem } _prop_t;
     74      1.1  lukem 
     75      1.1  lukem /*
     76      1.1  lukem  * List of properties expected to be found in the Unicode Character Database
     77      1.1  lukem  * including some implementation specific properties.
     78      1.1  lukem  *
     79      1.1  lukem  * The implementation specific properties are:
     80      1.1  lukem  * Cm = Composed (can be decomposed)
     81      1.1  lukem  * Nb = Non-breaking
     82      1.1  lukem  * Sy = Symmetric (has left and right forms)
     83      1.1  lukem  * Hd = Hex digit
     84      1.1  lukem  * Qm = Quote marks
     85      1.1  lukem  * Mr = Mirroring
     86      1.1  lukem  * Ss = Space, other
     87      1.1  lukem  * Cp = Defined character
     88      1.1  lukem  */
     89      1.1  lukem static _prop_t props[NUMPROPS] = {
     90      1.1  lukem     {"Mn", 2}, {"Mc", 2}, {"Me", 2}, {"Nd", 2}, {"Nl", 2}, {"No", 2},
     91      1.1  lukem     {"Zs", 2}, {"Zl", 2}, {"Zp", 2}, {"Cc", 2}, {"Cf", 2}, {"Cs", 2},
     92      1.1  lukem     {"Co", 2}, {"Cn", 2}, {"Lu", 2}, {"Ll", 2}, {"Lt", 2}, {"Lm", 2},
     93      1.1  lukem     {"Lo", 2}, {"Pc", 2}, {"Pd", 2}, {"Ps", 2}, {"Pe", 2}, {"Po", 2},
     94      1.1  lukem     {"Sm", 2}, {"Sc", 2}, {"Sk", 2}, {"So", 2}, {"L",  1}, {"R",  1},
     95      1.1  lukem     {"EN", 2}, {"ES", 2}, {"ET", 2}, {"AN", 2}, {"CS", 2}, {"B",  1},
     96      1.1  lukem     {"S",  1}, {"WS", 2}, {"ON", 2},
     97      1.1  lukem     {"Cm", 2}, {"Nb", 2}, {"Sy", 2}, {"Hd", 2}, {"Qm", 2}, {"Mr", 2},
     98      1.1  lukem     {"Ss", 2}, {"Cp", 2}, {"Pi", 2}, {"Pf", 2}, {"AL", 2}
     99      1.1  lukem };
    100      1.1  lukem 
    101      1.1  lukem typedef struct {
    102      1.1  lukem     ac_uint4 *ranges;
    103      1.1  lukem     ac_uint2 used;
    104      1.1  lukem     ac_uint2 size;
    105      1.1  lukem } _ranges_t;
    106      1.1  lukem 
    107      1.1  lukem static _ranges_t proptbl[NUMPROPS];
    108      1.1  lukem 
    109      1.1  lukem /*
    110      1.1  lukem  * Make sure this array is sized to be on a 4-byte boundary at compile time.
    111      1.1  lukem  */
    112      1.1  lukem static ac_uint2 propcnt[NEEDPROPS];
    113      1.1  lukem 
    114      1.1  lukem /*
    115      1.1  lukem  * Array used to collect a decomposition before adding it to the decomposition
    116      1.1  lukem  * table.
    117      1.1  lukem  */
    118      1.1  lukem static ac_uint4 dectmp[64];
    119      1.1  lukem static ac_uint4 dectmp_size;
    120      1.1  lukem 
    121      1.1  lukem typedef struct {
    122      1.1  lukem     ac_uint4 code;
    123      1.1  lukem     ac_uint2 size;
    124      1.1  lukem     ac_uint2 used;
    125      1.1  lukem     ac_uint4 *decomp;
    126      1.1  lukem } _decomp_t;
    127      1.1  lukem 
    128      1.1  lukem /*
    129      1.1  lukem  * List of decomposition.  Created and expanded in order as the characters are
    130      1.1  lukem  * encountered. First list contains canonical mappings, second also includes
    131      1.1  lukem  * compatibility mappings.
    132      1.1  lukem  */
    133      1.1  lukem static _decomp_t *decomps;
    134      1.1  lukem static ac_uint4 decomps_used;
    135      1.1  lukem static ac_uint4 decomps_size;
    136      1.1  lukem 
    137      1.1  lukem static _decomp_t *kdecomps;
    138      1.1  lukem static ac_uint4 kdecomps_used;
    139      1.1  lukem static ac_uint4 kdecomps_size;
    140      1.1  lukem 
    141      1.1  lukem /*
    142      1.1  lukem  * Composition exclusion table stuff.
    143      1.1  lukem  */
    144      1.1  lukem #define COMPEX_SET(c) (compexs[(c) >> 5] |= (1 << ((c) & 31)))
    145      1.1  lukem #define COMPEX_TEST(c) (compexs[(c) >> 5] & (1 << ((c) & 31)))
    146      1.1  lukem static ac_uint4 compexs[8192];
    147      1.1  lukem 
    148      1.1  lukem /*
    149      1.1  lukem  * Struct for holding a composition pair, and array of composition pairs
    150      1.1  lukem  */
    151      1.1  lukem typedef struct {
    152      1.1  lukem     ac_uint4 comp;
    153      1.1  lukem     ac_uint4 count;
    154      1.1  lukem     ac_uint4 code1;
    155      1.1  lukem     ac_uint4 code2;
    156      1.1  lukem } _comp_t;
    157      1.1  lukem 
    158      1.1  lukem static _comp_t *comps;
    159      1.1  lukem static ac_uint4 comps_used;
    160      1.1  lukem 
    161      1.1  lukem /*
    162      1.1  lukem  * Types and lists for handling lists of case mappings.
    163      1.1  lukem  */
    164      1.1  lukem typedef struct {
    165      1.1  lukem     ac_uint4 key;
    166      1.1  lukem     ac_uint4 other1;
    167      1.1  lukem     ac_uint4 other2;
    168      1.1  lukem } _case_t;
    169      1.1  lukem 
    170      1.1  lukem static _case_t *upper;
    171      1.1  lukem static _case_t *lower;
    172      1.1  lukem static _case_t *title;
    173      1.1  lukem static ac_uint4 upper_used;
    174      1.1  lukem static ac_uint4 upper_size;
    175      1.1  lukem static ac_uint4 lower_used;
    176      1.1  lukem static ac_uint4 lower_size;
    177      1.1  lukem static ac_uint4 title_used;
    178      1.1  lukem static ac_uint4 title_size;
    179      1.1  lukem 
    180      1.1  lukem /*
    181      1.1  lukem  * Array used to collect case mappings before adding them to a list.
    182      1.1  lukem  */
    183      1.1  lukem static ac_uint4 cases[3];
    184      1.1  lukem 
    185      1.1  lukem /*
    186      1.1  lukem  * An array to hold ranges for combining classes.
    187      1.1  lukem  */
    188      1.1  lukem static ac_uint4 *ccl;
    189      1.1  lukem static ac_uint4 ccl_used;
    190      1.1  lukem static ac_uint4 ccl_size;
    191      1.1  lukem 
    192      1.1  lukem /*
    193      1.1  lukem  * Structures for handling numbers.
    194      1.1  lukem  */
    195      1.1  lukem typedef struct {
    196      1.1  lukem     ac_uint4 code;
    197      1.1  lukem     ac_uint4 idx;
    198      1.1  lukem } _codeidx_t;
    199      1.1  lukem 
    200      1.1  lukem typedef struct {
    201      1.1  lukem     short numerator;
    202      1.1  lukem     short denominator;
    203      1.1  lukem } _num_t;
    204      1.1  lukem 
    205      1.1  lukem /*
    206      1.1  lukem  * Arrays to hold the mapping of codes to numbers.
    207      1.1  lukem  */
    208      1.1  lukem static _codeidx_t *ncodes;
    209      1.1  lukem static ac_uint4 ncodes_used;
    210      1.1  lukem static ac_uint4 ncodes_size;
    211      1.1  lukem 
    212      1.1  lukem static _num_t *nums;
    213      1.1  lukem static ac_uint4 nums_used;
    214      1.1  lukem static ac_uint4 nums_size;
    215      1.1  lukem 
    216      1.1  lukem /*
    217      1.1  lukem  * Array for holding numbers.
    218      1.1  lukem  */
    219      1.1  lukem static _num_t *nums;
    220      1.1  lukem static ac_uint4 nums_used;
    221      1.1  lukem static ac_uint4 nums_size;
    222      1.1  lukem 
    223      1.1  lukem static void
    224      1.1  lukem add_range(ac_uint4 start, ac_uint4 end, char *p1, char *p2)
    225      1.1  lukem {
    226      1.1  lukem     int i, j, k, len;
    227      1.1  lukem     _ranges_t *rlp;
    228      1.1  lukem     char *name;
    229      1.1  lukem 
    230      1.1  lukem     for (k = 0; k < 2; k++) {
    231      1.1  lukem         if (k == 0) {
    232      1.1  lukem             name = p1;
    233      1.1  lukem             len = 2;
    234      1.1  lukem         } else {
    235      1.1  lukem             if (p2 == 0)
    236      1.1  lukem               break;
    237      1.1  lukem 
    238      1.1  lukem             name = p2;
    239      1.1  lukem             len = 1;
    240      1.1  lukem         }
    241      1.1  lukem 
    242      1.1  lukem         for (i = 0; i < NUMPROPS; i++) {
    243      1.1  lukem             if (props[i].len == len && memcmp(props[i].name, name, len) == 0)
    244      1.1  lukem               break;
    245      1.1  lukem         }
    246      1.1  lukem 
    247      1.1  lukem         if (i == NUMPROPS)
    248      1.1  lukem           continue;
    249      1.1  lukem 
    250      1.1  lukem         rlp = &proptbl[i];
    251      1.1  lukem 
    252      1.1  lukem         /*
    253      1.1  lukem          * Resize the range list if necessary.
    254      1.1  lukem          */
    255      1.1  lukem         if (rlp->used == rlp->size) {
    256      1.1  lukem             if (rlp->size == 0)
    257      1.1  lukem               rlp->ranges = (ac_uint4 *)
    258      1.1  lukem                   malloc(sizeof(ac_uint4) << 3);
    259      1.1  lukem             else
    260      1.1  lukem               rlp->ranges = (ac_uint4 *)
    261      1.1  lukem                   realloc((char *) rlp->ranges,
    262      1.1  lukem                           sizeof(ac_uint4) * (rlp->size + 8));
    263      1.1  lukem             rlp->size += 8;
    264      1.1  lukem         }
    265      1.1  lukem 
    266      1.1  lukem         /*
    267      1.1  lukem          * If this is the first code for this property list, just add it
    268      1.1  lukem          * and return.
    269      1.1  lukem          */
    270      1.1  lukem         if (rlp->used == 0) {
    271      1.1  lukem             rlp->ranges[0] = start;
    272      1.1  lukem             rlp->ranges[1] = end;
    273      1.1  lukem             rlp->used += 2;
    274      1.1  lukem             continue;
    275      1.1  lukem         }
    276      1.1  lukem 
    277      1.1  lukem         /*
    278      1.1  lukem          * Optimize the case of adding the range to the end.
    279      1.1  lukem          */
    280      1.1  lukem         j = rlp->used - 1;
    281      1.1  lukem         if (start > rlp->ranges[j]) {
    282      1.1  lukem             j = rlp->used;
    283      1.1  lukem             rlp->ranges[j++] = start;
    284      1.1  lukem             rlp->ranges[j++] = end;
    285      1.1  lukem             rlp->used = j;
    286      1.1  lukem             continue;
    287      1.1  lukem         }
    288      1.1  lukem 
    289      1.1  lukem         /*
    290      1.1  lukem          * Need to locate the insertion point.
    291      1.1  lukem          */
    292      1.1  lukem         for (i = 0;
    293      1.1  lukem              i < rlp->used && start > rlp->ranges[i + 1] + 1; i += 2) ;
    294      1.1  lukem 
    295      1.1  lukem         /*
    296      1.1  lukem          * If the start value lies in the current range, then simply set the
    297      1.1  lukem          * new end point of the range to the end value passed as a parameter.
    298      1.1  lukem          */
    299      1.1  lukem         if (rlp->ranges[i] <= start && start <= rlp->ranges[i + 1] + 1) {
    300      1.1  lukem             rlp->ranges[i + 1] = end;
    301      1.1  lukem             return;
    302      1.1  lukem         }
    303      1.1  lukem 
    304      1.1  lukem         /*
    305      1.1  lukem          * Shift following values up by two.
    306      1.1  lukem          */
    307      1.1  lukem         for (j = rlp->used; j > i; j -= 2) {
    308      1.1  lukem             rlp->ranges[j] = rlp->ranges[j - 2];
    309      1.1  lukem             rlp->ranges[j + 1] = rlp->ranges[j - 1];
    310      1.1  lukem         }
    311      1.1  lukem 
    312      1.1  lukem         /*
    313      1.1  lukem          * Add the new range at the insertion point.
    314      1.1  lukem          */
    315      1.1  lukem         rlp->ranges[i] = start;
    316      1.1  lukem         rlp->ranges[i + 1] = end;
    317      1.1  lukem         rlp->used += 2;
    318      1.1  lukem     }
    319      1.1  lukem }
    320      1.1  lukem 
    321      1.1  lukem static void
    322      1.1  lukem ordered_range_insert(ac_uint4 c, char *name, int len)
    323      1.1  lukem {
    324      1.1  lukem     int i, j;
    325      1.1  lukem     ac_uint4 s, e;
    326      1.1  lukem     _ranges_t *rlp;
    327      1.1  lukem 
    328      1.1  lukem     if (len == 0)
    329      1.1  lukem       return;
    330      1.1  lukem 
    331      1.1  lukem     /*
    332      1.1  lukem      * Deal with directionality codes introduced in Unicode 3.0.
    333      1.1  lukem      */
    334      1.1  lukem     if ((len == 2 && memcmp(name, "BN", 2) == 0) ||
    335      1.1  lukem         (len == 3 &&
    336      1.1  lukem          (memcmp(name, "NSM", 3) == 0 || memcmp(name, "PDF", 3) == 0 ||
    337      1.1  lukem           memcmp(name, "LRE", 3) == 0 || memcmp(name, "LRO", 3) == 0 ||
    338      1.1  lukem           memcmp(name, "RLE", 3) == 0 || memcmp(name, "RLO", 3) == 0))) {
    339      1.1  lukem         /*
    340      1.1  lukem          * Mark all of these as Other Neutral to preserve compatibility with
    341      1.1  lukem          * older versions.
    342      1.1  lukem          */
    343      1.1  lukem         len = 2;
    344      1.1  lukem         name = "ON";
    345      1.1  lukem     }
    346      1.1  lukem 
    347      1.1  lukem     for (i = 0; i < NUMPROPS; i++) {
    348      1.1  lukem         if (props[i].len == len && memcmp(props[i].name, name, len) == 0)
    349      1.1  lukem           break;
    350      1.1  lukem     }
    351      1.1  lukem 
    352      1.1  lukem     if (i == NUMPROPS)
    353      1.1  lukem       return;
    354      1.1  lukem 
    355      1.1  lukem     /*
    356      1.1  lukem      * Have a match, so insert the code in order.
    357      1.1  lukem      */
    358      1.1  lukem     rlp = &proptbl[i];
    359      1.1  lukem 
    360      1.1  lukem     /*
    361      1.1  lukem      * Resize the range list if necessary.
    362      1.1  lukem      */
    363      1.1  lukem     if (rlp->used == rlp->size) {
    364      1.1  lukem         if (rlp->size == 0)
    365      1.1  lukem           rlp->ranges = (ac_uint4 *)
    366      1.1  lukem               malloc(sizeof(ac_uint4) << 3);
    367      1.1  lukem         else
    368      1.1  lukem           rlp->ranges = (ac_uint4 *)
    369      1.1  lukem               realloc((char *) rlp->ranges,
    370      1.1  lukem                       sizeof(ac_uint4) * (rlp->size + 8));
    371      1.1  lukem         rlp->size += 8;
    372      1.1  lukem     }
    373      1.1  lukem 
    374      1.1  lukem     /*
    375      1.1  lukem      * If this is the first code for this property list, just add it
    376      1.1  lukem      * and return.
    377      1.1  lukem      */
    378      1.1  lukem     if (rlp->used == 0) {
    379      1.1  lukem         rlp->ranges[0] = rlp->ranges[1] = c;
    380      1.1  lukem         rlp->used += 2;
    381      1.1  lukem         return;
    382      1.1  lukem     }
    383      1.1  lukem 
    384      1.1  lukem     /*
    385      1.1  lukem      * Optimize the cases of extending the last range and adding new ranges to
    386      1.1  lukem      * the end.
    387      1.1  lukem      */
    388      1.1  lukem     j = rlp->used - 1;
    389      1.1  lukem     e = rlp->ranges[j];
    390      1.1  lukem     s = rlp->ranges[j - 1];
    391      1.1  lukem 
    392      1.1  lukem     if (c == e + 1) {
    393      1.1  lukem         /*
    394      1.1  lukem          * Extend the last range.
    395      1.1  lukem          */
    396      1.1  lukem         rlp->ranges[j] = c;
    397      1.1  lukem         return;
    398      1.1  lukem     }
    399      1.1  lukem 
    400      1.1  lukem     if (c > e + 1) {
    401      1.1  lukem         /*
    402      1.1  lukem          * Start another range on the end.
    403      1.1  lukem          */
    404      1.1  lukem         j = rlp->used;
    405      1.1  lukem         rlp->ranges[j] = rlp->ranges[j + 1] = c;
    406      1.1  lukem         rlp->used += 2;
    407      1.1  lukem         return;
    408      1.1  lukem     }
    409      1.1  lukem 
    410      1.1  lukem     if (c >= s)
    411      1.1  lukem       /*
    412      1.1  lukem        * The code is a duplicate of a code in the last range, so just return.
    413      1.1  lukem        */
    414      1.1  lukem       return;
    415      1.1  lukem 
    416      1.1  lukem     /*
    417      1.1  lukem      * The code should be inserted somewhere before the last range in the
    418      1.1  lukem      * list.  Locate the insertion point.
    419      1.1  lukem      */
    420      1.1  lukem     for (i = 0;
    421      1.1  lukem          i < rlp->used && c > rlp->ranges[i + 1] + 1; i += 2) ;
    422      1.1  lukem 
    423      1.1  lukem     s = rlp->ranges[i];
    424      1.1  lukem     e = rlp->ranges[i + 1];
    425      1.1  lukem 
    426      1.1  lukem     if (c == e + 1)
    427      1.1  lukem       /*
    428      1.1  lukem        * Simply extend the current range.
    429      1.1  lukem        */
    430      1.1  lukem       rlp->ranges[i + 1] = c;
    431      1.1  lukem     else if (c < s) {
    432      1.1  lukem         /*
    433      1.1  lukem          * Add a new entry before the current location.  Shift all entries
    434      1.1  lukem          * before the current one up by one to make room.
    435      1.1  lukem          */
    436      1.1  lukem         for (j = rlp->used; j > i; j -= 2) {
    437      1.1  lukem             rlp->ranges[j] = rlp->ranges[j - 2];
    438      1.1  lukem             rlp->ranges[j + 1] = rlp->ranges[j - 1];
    439      1.1  lukem         }
    440      1.1  lukem         rlp->ranges[i] = rlp->ranges[i + 1] = c;
    441      1.1  lukem 
    442      1.1  lukem         rlp->used += 2;
    443      1.1  lukem     }
    444      1.1  lukem }
    445      1.1  lukem 
    446      1.1  lukem static void
    447      1.1  lukem add_decomp(ac_uint4 code, short compat)
    448      1.1  lukem {
    449      1.1  lukem     ac_uint4 i, j, size;
    450      1.1  lukem     _decomp_t **pdecomps;
    451      1.1  lukem     ac_uint4 *pdecomps_used;
    452      1.1  lukem     ac_uint4 *pdecomps_size;
    453      1.1  lukem 
    454      1.1  lukem     if (compat) {
    455      1.1  lukem 	pdecomps = &kdecomps;
    456      1.1  lukem 	pdecomps_used = &kdecomps_used;
    457      1.1  lukem 	pdecomps_size = &kdecomps_size;
    458      1.1  lukem     } else {
    459      1.1  lukem 	pdecomps = &decomps;
    460      1.1  lukem 	pdecomps_used = &decomps_used;
    461      1.1  lukem 	pdecomps_size = &decomps_size;
    462      1.1  lukem     }
    463      1.1  lukem 
    464      1.1  lukem     /*
    465      1.1  lukem      * Add the code to the composite property.
    466      1.1  lukem      */
    467      1.1  lukem     if (!compat) {
    468      1.1  lukem 	ordered_range_insert(code, "Cm", 2);
    469      1.1  lukem     }
    470      1.1  lukem 
    471      1.1  lukem     /*
    472      1.1  lukem      * Locate the insertion point for the code.
    473      1.1  lukem      */
    474      1.1  lukem     for (i = 0; i < *pdecomps_used && code > (*pdecomps)[i].code; i++) ;
    475      1.1  lukem 
    476      1.1  lukem     /*
    477      1.1  lukem      * Allocate space for a new decomposition.
    478      1.1  lukem      */
    479      1.1  lukem     if (*pdecomps_used == *pdecomps_size) {
    480      1.1  lukem         if (*pdecomps_size == 0)
    481      1.1  lukem           *pdecomps = (_decomp_t *) malloc(sizeof(_decomp_t) << 3);
    482      1.1  lukem         else
    483      1.1  lukem           *pdecomps = (_decomp_t *)
    484      1.1  lukem               realloc((char *) *pdecomps,
    485      1.1  lukem                       sizeof(_decomp_t) * (*pdecomps_size + 8));
    486      1.1  lukem         (void) memset((char *) (*pdecomps + *pdecomps_size), '\0',
    487      1.1  lukem                       sizeof(_decomp_t) << 3);
    488      1.1  lukem         *pdecomps_size += 8;
    489      1.1  lukem     }
    490      1.1  lukem 
    491      1.1  lukem     if (i < *pdecomps_used && code != (*pdecomps)[i].code) {
    492      1.1  lukem         /*
    493      1.1  lukem          * Shift the decomps up by one if the codes don't match.
    494      1.1  lukem          */
    495      1.1  lukem         for (j = *pdecomps_used; j > i; j--)
    496      1.1  lukem           (void) AC_MEMCPY((char *) &(*pdecomps)[j], (char *) &(*pdecomps)[j - 1],
    497      1.1  lukem                         sizeof(_decomp_t));
    498      1.1  lukem     }
    499      1.1  lukem 
    500      1.1  lukem     /*
    501      1.1  lukem      * Insert or replace a decomposition.
    502      1.1  lukem      */
    503      1.1  lukem     size = dectmp_size + (4 - (dectmp_size & 3));
    504      1.1  lukem     if ((*pdecomps)[i].size < size) {
    505      1.1  lukem         if ((*pdecomps)[i].size == 0)
    506      1.1  lukem           (*pdecomps)[i].decomp = (ac_uint4 *)
    507      1.1  lukem               malloc(sizeof(ac_uint4) * size);
    508      1.1  lukem         else
    509      1.1  lukem           (*pdecomps)[i].decomp = (ac_uint4 *)
    510      1.1  lukem               realloc((char *) (*pdecomps)[i].decomp,
    511      1.1  lukem                       sizeof(ac_uint4) * size);
    512      1.1  lukem         (*pdecomps)[i].size = size;
    513      1.1  lukem     }
    514      1.1  lukem 
    515      1.1  lukem     if ((*pdecomps)[i].code != code)
    516      1.1  lukem       (*pdecomps_used)++;
    517      1.1  lukem 
    518      1.1  lukem     (*pdecomps)[i].code = code;
    519      1.1  lukem     (*pdecomps)[i].used = dectmp_size;
    520      1.1  lukem     (void) AC_MEMCPY((char *) (*pdecomps)[i].decomp, (char *) dectmp,
    521      1.1  lukem                   sizeof(ac_uint4) * dectmp_size);
    522      1.1  lukem 
    523      1.1  lukem     /*
    524      1.1  lukem      * NOTICE: This needs changing later so it is more general than simply
    525      1.1  lukem      * pairs.  This calculation is done here to simplify allocation elsewhere.
    526      1.1  lukem      */
    527      1.1  lukem     if (!compat && dectmp_size == 2)
    528      1.1  lukem       comps_used++;
    529      1.1  lukem }
    530      1.1  lukem 
    531      1.1  lukem static void
    532      1.1  lukem add_title(ac_uint4 code)
    533      1.1  lukem {
    534      1.1  lukem     ac_uint4 i, j;
    535      1.1  lukem 
    536      1.1  lukem     /*
    537      1.1  lukem      * Always map the code to itself.
    538      1.1  lukem      */
    539      1.1  lukem     cases[2] = code;
    540      1.1  lukem 
    541      1.1  lukem     if (title_used == title_size) {
    542      1.1  lukem         if (title_size == 0)
    543      1.1  lukem           title = (_case_t *) malloc(sizeof(_case_t) << 3);
    544      1.1  lukem         else
    545      1.1  lukem           title = (_case_t *) realloc((char *) title,
    546      1.1  lukem                                       sizeof(_case_t) * (title_size + 8));
    547      1.1  lukem         title_size += 8;
    548      1.1  lukem     }
    549      1.1  lukem 
    550      1.1  lukem     /*
    551      1.1  lukem      * Locate the insertion point.
    552      1.1  lukem      */
    553      1.1  lukem     for (i = 0; i < title_used && code > title[i].key; i++) ;
    554      1.1  lukem 
    555      1.1  lukem     if (i < title_used) {
    556      1.1  lukem         /*
    557      1.1  lukem          * Shift the array up by one.
    558      1.1  lukem          */
    559      1.1  lukem         for (j = title_used; j > i; j--)
    560      1.1  lukem           (void) AC_MEMCPY((char *) &title[j], (char *) &title[j - 1],
    561      1.1  lukem                         sizeof(_case_t));
    562      1.1  lukem     }
    563      1.1  lukem 
    564      1.1  lukem     title[i].key = cases[2];    /* Title */
    565      1.1  lukem     title[i].other1 = cases[0]; /* Upper */
    566      1.1  lukem     title[i].other2 = cases[1]; /* Lower */
    567      1.1  lukem 
    568      1.1  lukem     title_used++;
    569      1.1  lukem }
    570      1.1  lukem 
    571      1.1  lukem static void
    572      1.1  lukem add_upper(ac_uint4 code)
    573      1.1  lukem {
    574      1.1  lukem     ac_uint4 i, j;
    575      1.1  lukem 
    576      1.1  lukem     /*
    577      1.1  lukem      * Always map the code to itself.
    578      1.1  lukem      */
    579      1.1  lukem     cases[0] = code;
    580      1.1  lukem 
    581      1.1  lukem     /*
    582      1.1  lukem      * If the title case character is not present, then make it the same as
    583      1.1  lukem      * the upper case.
    584      1.1  lukem      */
    585      1.1  lukem     if (cases[2] == 0)
    586      1.1  lukem       cases[2] = code;
    587      1.1  lukem 
    588      1.1  lukem     if (upper_used == upper_size) {
    589      1.1  lukem         if (upper_size == 0)
    590      1.1  lukem           upper = (_case_t *) malloc(sizeof(_case_t) << 3);
    591      1.1  lukem         else
    592      1.1  lukem           upper = (_case_t *) realloc((char *) upper,
    593      1.1  lukem                                       sizeof(_case_t) * (upper_size + 8));
    594      1.1  lukem         upper_size += 8;
    595      1.1  lukem     }
    596      1.1  lukem 
    597      1.1  lukem     /*
    598      1.1  lukem      * Locate the insertion point.
    599      1.1  lukem      */
    600      1.1  lukem     for (i = 0; i < upper_used && code > upper[i].key; i++) ;
    601      1.1  lukem 
    602      1.1  lukem     if (i < upper_used) {
    603      1.1  lukem         /*
    604      1.1  lukem          * Shift the array up by one.
    605      1.1  lukem          */
    606      1.1  lukem         for (j = upper_used; j > i; j--)
    607      1.1  lukem           (void) AC_MEMCPY((char *) &upper[j], (char *) &upper[j - 1],
    608      1.1  lukem                         sizeof(_case_t));
    609      1.1  lukem     }
    610      1.1  lukem 
    611      1.1  lukem     upper[i].key = cases[0];    /* Upper */
    612      1.1  lukem     upper[i].other1 = cases[1]; /* Lower */
    613      1.1  lukem     upper[i].other2 = cases[2]; /* Title */
    614      1.1  lukem 
    615      1.1  lukem     upper_used++;
    616      1.1  lukem }
    617      1.1  lukem 
    618      1.1  lukem static void
    619      1.1  lukem add_lower(ac_uint4 code)
    620      1.1  lukem {
    621      1.1  lukem     ac_uint4 i, j;
    622      1.1  lukem 
    623      1.1  lukem     /*
    624      1.1  lukem      * Always map the code to itself.
    625      1.1  lukem      */
    626      1.1  lukem     cases[1] = code;
    627      1.1  lukem 
    628      1.1  lukem     /*
    629      1.1  lukem      * If the title case character is empty, then make it the same as the
    630      1.1  lukem      * upper case.
    631      1.1  lukem      */
    632      1.1  lukem     if (cases[2] == 0)
    633      1.1  lukem       cases[2] = cases[0];
    634      1.1  lukem 
    635      1.1  lukem     if (lower_used == lower_size) {
    636      1.1  lukem         if (lower_size == 0)
    637      1.1  lukem           lower = (_case_t *) malloc(sizeof(_case_t) << 3);
    638      1.1  lukem         else
    639      1.1  lukem           lower = (_case_t *) realloc((char *) lower,
    640      1.1  lukem                                       sizeof(_case_t) * (lower_size + 8));
    641      1.1  lukem         lower_size += 8;
    642      1.1  lukem     }
    643      1.1  lukem 
    644      1.1  lukem     /*
    645      1.1  lukem      * Locate the insertion point.
    646      1.1  lukem      */
    647      1.1  lukem     for (i = 0; i < lower_used && code > lower[i].key; i++) ;
    648      1.1  lukem 
    649      1.1  lukem     if (i < lower_used) {
    650      1.1  lukem         /*
    651      1.1  lukem          * Shift the array up by one.
    652      1.1  lukem          */
    653      1.1  lukem         for (j = lower_used; j > i; j--)
    654      1.1  lukem           (void) AC_MEMCPY((char *) &lower[j], (char *) &lower[j - 1],
    655      1.1  lukem                         sizeof(_case_t));
    656      1.1  lukem     }
    657      1.1  lukem 
    658      1.1  lukem     lower[i].key = cases[1];    /* Lower */
    659      1.1  lukem     lower[i].other1 = cases[0]; /* Upper */
    660      1.1  lukem     lower[i].other2 = cases[2]; /* Title */
    661      1.1  lukem 
    662      1.1  lukem     lower_used++;
    663      1.1  lukem }
    664      1.1  lukem 
    665      1.1  lukem static void
    666      1.1  lukem ordered_ccl_insert(ac_uint4 c, ac_uint4 ccl_code)
    667      1.1  lukem {
    668      1.1  lukem     ac_uint4 i, j;
    669      1.1  lukem 
    670      1.1  lukem     if (ccl_used == ccl_size) {
    671      1.1  lukem         if (ccl_size == 0)
    672      1.1  lukem           ccl = (ac_uint4 *) malloc(sizeof(ac_uint4) * 24);
    673      1.1  lukem         else
    674      1.1  lukem           ccl = (ac_uint4 *)
    675      1.1  lukem               realloc((char *) ccl, sizeof(ac_uint4) * (ccl_size + 24));
    676      1.1  lukem         ccl_size += 24;
    677      1.1  lukem     }
    678      1.1  lukem 
    679      1.1  lukem     /*
    680      1.1  lukem      * Optimize adding the first item.
    681      1.1  lukem      */
    682      1.1  lukem     if (ccl_used == 0) {
    683      1.1  lukem         ccl[0] = ccl[1] = c;
    684      1.1  lukem         ccl[2] = ccl_code;
    685      1.1  lukem         ccl_used += 3;
    686      1.1  lukem         return;
    687      1.1  lukem     }
    688      1.1  lukem 
    689      1.1  lukem     /*
    690      1.1  lukem      * Handle the special case of extending the range on the end.  This
    691      1.1  lukem      * requires that the combining class codes are the same.
    692      1.1  lukem      */
    693      1.1  lukem     if (ccl_code == ccl[ccl_used - 1] && c == ccl[ccl_used - 2] + 1) {
    694      1.1  lukem         ccl[ccl_used - 2] = c;
    695      1.1  lukem         return;
    696      1.1  lukem     }
    697      1.1  lukem 
    698      1.1  lukem     /*
    699      1.1  lukem      * Handle the special case of adding another range on the end.
    700      1.1  lukem      */
    701      1.1  lukem     if (c > ccl[ccl_used - 2] + 1 ||
    702      1.1  lukem         (c == ccl[ccl_used - 2] + 1 && ccl_code != ccl[ccl_used - 1])) {
    703      1.1  lukem         ccl[ccl_used++] = c;
    704      1.1  lukem         ccl[ccl_used++] = c;
    705      1.1  lukem         ccl[ccl_used++] = ccl_code;
    706      1.1  lukem         return;
    707      1.1  lukem     }
    708      1.1  lukem 
    709      1.1  lukem     /*
    710      1.1  lukem      * Locate either the insertion point or range for the code.
    711      1.1  lukem      */
    712      1.1  lukem     for (i = 0; i < ccl_used && c > ccl[i + 1] + 1; i += 3) ;
    713      1.1  lukem 
    714      1.1  lukem     if (ccl_code == ccl[i + 2] && c == ccl[i + 1] + 1) {
    715      1.1  lukem         /*
    716      1.1  lukem          * Extend an existing range.
    717      1.1  lukem          */
    718      1.1  lukem         ccl[i + 1] = c;
    719      1.1  lukem         return;
    720      1.1  lukem     } else if (c < ccl[i]) {
    721      1.1  lukem         /*
    722      1.1  lukem          * Start a new range before the current location.
    723      1.1  lukem          */
    724      1.1  lukem         for (j = ccl_used; j > i; j -= 3) {
    725      1.1  lukem             ccl[j] = ccl[j - 3];
    726      1.1  lukem             ccl[j - 1] = ccl[j - 4];
    727      1.1  lukem             ccl[j - 2] = ccl[j - 5];
    728      1.1  lukem         }
    729      1.1  lukem         ccl[i] = ccl[i + 1] = c;
    730      1.1  lukem         ccl[i + 2] = ccl_code;
    731      1.1  lukem     }
    732      1.1  lukem }
    733      1.1  lukem 
    734      1.1  lukem /*
    735      1.1  lukem  * Adds a number if it does not already exist and returns an index value
    736      1.1  lukem  * multiplied by 2.
    737      1.1  lukem  */
    738      1.1  lukem static ac_uint4
    739      1.1  lukem make_number(short num, short denom)
    740      1.1  lukem {
    741      1.1  lukem     ac_uint4 n;
    742      1.1  lukem 
    743      1.1  lukem     /*
    744      1.1  lukem      * Determine if the number already exists.
    745      1.1  lukem      */
    746      1.1  lukem     for (n = 0; n < nums_used; n++) {
    747      1.1  lukem         if (nums[n].numerator == num && nums[n].denominator == denom)
    748      1.1  lukem           return n << 1;
    749      1.1  lukem     }
    750      1.1  lukem 
    751      1.1  lukem     if (nums_used == nums_size) {
    752      1.1  lukem         if (nums_size == 0)
    753      1.1  lukem           nums = (_num_t *) malloc(sizeof(_num_t) << 3);
    754      1.1  lukem         else
    755      1.1  lukem           nums = (_num_t *) realloc((char *) nums,
    756      1.1  lukem                                     sizeof(_num_t) * (nums_size + 8));
    757      1.1  lukem         nums_size += 8;
    758      1.1  lukem     }
    759      1.1  lukem 
    760      1.1  lukem     n = nums_used++;
    761      1.1  lukem     nums[n].numerator = num;
    762      1.1  lukem     nums[n].denominator = denom;
    763      1.1  lukem 
    764      1.1  lukem     return n << 1;
    765      1.1  lukem }
    766      1.1  lukem 
    767      1.1  lukem static void
    768      1.1  lukem add_number(ac_uint4 code, short num, short denom)
    769      1.1  lukem {
    770      1.1  lukem     ac_uint4 i, j;
    771      1.1  lukem 
    772      1.1  lukem     /*
    773      1.1  lukem      * Insert the code in order.
    774      1.1  lukem      */
    775      1.1  lukem     for (i = 0; i < ncodes_used && code > ncodes[i].code; i++) ;
    776      1.1  lukem 
    777      1.1  lukem     /*
    778      1.1  lukem      * Handle the case of the codes matching and simply replace the number
    779      1.1  lukem      * that was there before.
    780      1.1  lukem      */
    781      1.1  lukem     if (i < ncodes_used && code == ncodes[i].code) {
    782      1.1  lukem         ncodes[i].idx = make_number(num, denom);
    783      1.1  lukem         return;
    784      1.1  lukem     }
    785      1.1  lukem 
    786      1.1  lukem     /*
    787      1.1  lukem      * Resize the array if necessary.
    788      1.1  lukem      */
    789      1.1  lukem     if (ncodes_used == ncodes_size) {
    790      1.1  lukem         if (ncodes_size == 0)
    791      1.1  lukem           ncodes = (_codeidx_t *) malloc(sizeof(_codeidx_t) << 3);
    792      1.1  lukem         else
    793      1.1  lukem           ncodes = (_codeidx_t *)
    794      1.1  lukem               realloc((char *) ncodes, sizeof(_codeidx_t) * (ncodes_size + 8));
    795      1.1  lukem 
    796      1.1  lukem         ncodes_size += 8;
    797      1.1  lukem     }
    798      1.1  lukem 
    799      1.1  lukem     /*
    800      1.1  lukem      * Shift things around to insert the code if necessary.
    801      1.1  lukem      */
    802      1.1  lukem     if (i < ncodes_used) {
    803      1.1  lukem         for (j = ncodes_used; j > i; j--) {
    804      1.1  lukem             ncodes[j].code = ncodes[j - 1].code;
    805      1.1  lukem             ncodes[j].idx = ncodes[j - 1].idx;
    806      1.1  lukem         }
    807      1.1  lukem     }
    808      1.1  lukem     ncodes[i].code = code;
    809      1.1  lukem     ncodes[i].idx = make_number(num, denom);
    810      1.1  lukem 
    811      1.1  lukem     ncodes_used++;
    812      1.1  lukem }
    813      1.1  lukem 
    814      1.1  lukem /*
    815      1.1  lukem  * This routine assumes that the line is a valid Unicode Character Database
    816      1.1  lukem  * entry.
    817      1.1  lukem  */
    818      1.1  lukem static void
    819      1.1  lukem read_cdata(FILE *in)
    820      1.1  lukem {
    821      1.1  lukem     ac_uint4 i, lineno, skip, code, ccl_code;
    822      1.1  lukem     short wnum, neg, number[2], compat;
    823      1.1  lukem     char line[512], *s, *e;
    824      1.1  lukem 
    825      1.1  lukem     lineno = skip = 0;
    826      1.1  lukem     while (fgets(line, sizeof(line), in)) {
    827      1.1  lukem 	if( (s=strchr(line, '\n')) ) *s = '\0';
    828      1.1  lukem         lineno++;
    829      1.1  lukem 
    830      1.1  lukem         /*
    831      1.1  lukem          * Skip blank lines and lines that start with a '#'.
    832      1.1  lukem          */
    833      1.1  lukem         if (line[0] == 0 || line[0] == '#')
    834      1.1  lukem           continue;
    835      1.1  lukem 
    836      1.1  lukem         /*
    837      1.1  lukem          * If lines need to be skipped, do it here.
    838      1.1  lukem          */
    839      1.1  lukem         if (skip) {
    840      1.1  lukem             skip--;
    841      1.1  lukem             continue;
    842      1.1  lukem         }
    843      1.1  lukem 
    844      1.1  lukem         /*
    845      1.1  lukem          * Collect the code.  The code can be up to 6 hex digits in length to
    846      1.1  lukem          * allow surrogates to be specified.
    847      1.1  lukem          */
    848      1.1  lukem         for (s = line, i = code = 0; *s != ';' && i < 6; i++, s++) {
    849      1.1  lukem             code <<= 4;
    850      1.1  lukem             if (*s >= '0' && *s <= '9')
    851      1.1  lukem               code += *s - '0';
    852      1.1  lukem             else if (*s >= 'A' && *s <= 'F')
    853      1.1  lukem               code += (*s - 'A') + 10;
    854      1.1  lukem             else if (*s >= 'a' && *s <= 'f')
    855      1.1  lukem               code += (*s - 'a') + 10;
    856      1.1  lukem         }
    857      1.1  lukem 
    858      1.1  lukem         /*
    859      1.1  lukem          * Handle the following special cases:
    860      1.1  lukem          * 1. 4E00-9FA5 CJK Ideographs.
    861      1.1  lukem          * 2. AC00-D7A3 Hangul Syllables.
    862      1.1  lukem          * 3. D800-DFFF Surrogates.
    863      1.1  lukem          * 4. E000-F8FF Private Use Area.
    864      1.1  lukem          * 5. F900-FA2D Han compatibility.
    865      1.1  lukem 	 * ...Plus additional ranges in newer Unicode versions...
    866      1.1  lukem          */
    867      1.1  lukem         switch (code) {
    868      1.1  lukem 	  case 0x3400:
    869      1.1  lukem 	    /* CJK Ideograph Extension A */
    870      1.1  lukem             add_range(0x3400, 0x4db5, "Lo", "L");
    871      1.1  lukem 
    872      1.1  lukem             add_range(0x3400, 0x4db5, "Cp", 0);
    873      1.1  lukem 
    874      1.1  lukem 	    skip = 1;
    875      1.1  lukem 	    break;
    876      1.1  lukem           case 0x4e00:
    877      1.1  lukem             /*
    878      1.1  lukem              * The Han ideographs.
    879      1.1  lukem              */
    880      1.1  lukem             add_range(0x4e00, 0x9fff, "Lo", "L");
    881      1.1  lukem 
    882      1.1  lukem             /*
    883      1.1  lukem              * Add the characters to the defined category.
    884      1.1  lukem              */
    885      1.1  lukem             add_range(0x4e00, 0x9fa5, "Cp", 0);
    886      1.1  lukem 
    887      1.1  lukem             skip = 1;
    888      1.1  lukem             break;
    889      1.1  lukem           case 0xac00:
    890      1.1  lukem             /*
    891      1.1  lukem              * The Hangul syllables.
    892      1.1  lukem              */
    893      1.1  lukem             add_range(0xac00, 0xd7a3, "Lo", "L");
    894      1.1  lukem 
    895      1.1  lukem             /*
    896      1.1  lukem              * Add the characters to the defined category.
    897      1.1  lukem              */
    898      1.1  lukem             add_range(0xac00, 0xd7a3, "Cp", 0);
    899      1.1  lukem 
    900      1.1  lukem             skip = 1;
    901      1.1  lukem             break;
    902      1.1  lukem           case 0xd800:
    903      1.1  lukem             /*
    904      1.1  lukem              * Make a range of all surrogates and assume some default
    905      1.1  lukem              * properties.
    906      1.1  lukem              */
    907      1.1  lukem             add_range(0x010000, 0x10ffff, "Cs", "L");
    908      1.1  lukem             skip = 5;
    909      1.1  lukem             break;
    910      1.1  lukem           case 0xe000:
    911      1.1  lukem             /*
    912      1.1  lukem              * The Private Use area.  Add with a default set of properties.
    913      1.1  lukem              */
    914      1.1  lukem             add_range(0xe000, 0xf8ff, "Co", "L");
    915      1.1  lukem             skip = 1;
    916      1.1  lukem             break;
    917      1.1  lukem           case 0xf900:
    918      1.1  lukem             /*
    919      1.1  lukem              * The CJK compatibility area.
    920      1.1  lukem              */
    921      1.1  lukem             add_range(0xf900, 0xfaff, "Lo", "L");
    922      1.1  lukem 
    923      1.1  lukem             /*
    924      1.1  lukem              * Add the characters to the defined category.
    925      1.1  lukem              */
    926      1.1  lukem             add_range(0xf900, 0xfaff, "Cp", 0);
    927      1.1  lukem 
    928      1.1  lukem             skip = 1;
    929      1.1  lukem 	    break;
    930      1.1  lukem 	  case 0x20000:
    931      1.1  lukem 	    /* CJK Ideograph Extension B */
    932      1.1  lukem             add_range(0x20000, 0x2a6d6, "Lo", "L");
    933      1.1  lukem 
    934      1.1  lukem             add_range(0x20000, 0x2a6d6, "Cp", 0);
    935      1.1  lukem 
    936      1.1  lukem 	    skip = 1;
    937      1.1  lukem 	    break;
    938      1.1  lukem 	  case 0xf0000:
    939      1.1  lukem 	    /* Plane 15 private use */
    940      1.1  lukem 	    add_range(0xf0000, 0xffffd, "Co", "L");
    941      1.1  lukem 	    skip = 1;
    942      1.1  lukem 	    break;
    943      1.1  lukem 
    944      1.1  lukem 	  case 0x100000:
    945      1.1  lukem 	    /* Plane 16 private use */
    946      1.1  lukem 	    add_range(0x100000, 0x10fffd, "Co", "L");
    947      1.1  lukem 	    skip = 1;
    948      1.1  lukem 	    break;
    949      1.1  lukem         }
    950      1.1  lukem 
    951      1.1  lukem         if (skip)
    952      1.1  lukem           continue;
    953      1.1  lukem 
    954      1.1  lukem         /*
    955      1.1  lukem          * Add the code to the defined category.
    956      1.1  lukem          */
    957      1.1  lukem         ordered_range_insert(code, "Cp", 2);
    958      1.1  lukem 
    959      1.1  lukem         /*
    960      1.1  lukem          * Locate the first character property field.
    961      1.1  lukem          */
    962      1.1  lukem         for (i = 0; *s != 0 && i < 2; s++) {
    963      1.1  lukem             if (*s == ';')
    964      1.1  lukem               i++;
    965      1.1  lukem         }
    966      1.1  lukem         for (e = s; *e && *e != ';'; e++) ;
    967      1.1  lukem 
    968      1.1  lukem         ordered_range_insert(code, s, e - s);
    969      1.1  lukem 
    970      1.1  lukem         /*
    971      1.1  lukem          * Locate the combining class code.
    972      1.1  lukem          */
    973      1.1  lukem         for (s = e; *s != 0 && i < 3; s++) {
    974      1.1  lukem             if (*s == ';')
    975      1.1  lukem               i++;
    976      1.1  lukem         }
    977      1.1  lukem 
    978      1.1  lukem         /*
    979      1.1  lukem          * Convert the combining class code from decimal.
    980      1.1  lukem          */
    981      1.1  lukem         for (ccl_code = 0, e = s; *e && *e != ';'; e++)
    982      1.1  lukem           ccl_code = (ccl_code * 10) + (*e - '0');
    983      1.1  lukem 
    984      1.1  lukem         /*
    985      1.1  lukem          * Add the code if it not 0.
    986      1.1  lukem          */
    987      1.1  lukem         if (ccl_code != 0)
    988      1.1  lukem           ordered_ccl_insert(code, ccl_code);
    989      1.1  lukem 
    990      1.1  lukem         /*
    991      1.1  lukem          * Locate the second character property field.
    992      1.1  lukem          */
    993      1.1  lukem         for (s = e; *s != 0 && i < 4; s++) {
    994      1.1  lukem             if (*s == ';')
    995      1.1  lukem               i++;
    996      1.1  lukem         }
    997      1.1  lukem         for (e = s; *e && *e != ';'; e++) ;
    998      1.1  lukem 
    999      1.1  lukem         ordered_range_insert(code, s, e - s);
   1000      1.1  lukem 
   1001      1.1  lukem         /*
   1002      1.1  lukem          * Check for a decomposition.
   1003      1.1  lukem          */
   1004      1.1  lukem         s = ++e;
   1005      1.1  lukem         if (*s != ';') {
   1006      1.1  lukem 	    compat = *s == '<';
   1007      1.1  lukem 	    if (compat) {
   1008      1.1  lukem 		/*
   1009      1.1  lukem 		 * Skip compatibility formatting tag.
   1010      1.1  lukem 		 */
   1011      1.1  lukem 		while (*s++ != '>');
   1012      1.1  lukem 	    }
   1013      1.1  lukem             /*
   1014      1.1  lukem              * Collect the codes of the decomposition.
   1015      1.1  lukem              */
   1016      1.1  lukem             for (dectmp_size = 0; *s != ';'; ) {
   1017      1.1  lukem                 /*
   1018      1.1  lukem                  * Skip all leading non-hex digits.
   1019      1.1  lukem                  */
   1020      1.1  lukem                 while (!ishdigit(*s))
   1021      1.1  lukem  		  s++;
   1022      1.1  lukem 
   1023      1.1  lukem                 for (dectmp[dectmp_size] = 0; ishdigit(*s); s++) {
   1024      1.1  lukem                     dectmp[dectmp_size] <<= 4;
   1025      1.1  lukem                     if (*s >= '0' && *s <= '9')
   1026      1.1  lukem                       dectmp[dectmp_size] += *s - '0';
   1027      1.1  lukem                     else if (*s >= 'A' && *s <= 'F')
   1028      1.1  lukem                       dectmp[dectmp_size] += (*s - 'A') + 10;
   1029      1.1  lukem                     else if (*s >= 'a' && *s <= 'f')
   1030      1.1  lukem                       dectmp[dectmp_size] += (*s - 'a') + 10;
   1031      1.1  lukem                 }
   1032      1.1  lukem                 dectmp_size++;
   1033      1.1  lukem             }
   1034      1.1  lukem 
   1035      1.1  lukem             /*
   1036      1.1  lukem              * If there are any codes in the temporary decomposition array,
   1037      1.1  lukem              * then add the character with its decomposition.
   1038      1.1  lukem              */
   1039      1.1  lukem             if (dectmp_size > 0) {
   1040      1.1  lukem 		if (!compat) {
   1041      1.1  lukem 		    add_decomp(code, 0);
   1042      1.1  lukem 		}
   1043      1.1  lukem 		add_decomp(code, 1);
   1044      1.1  lukem 	    }
   1045      1.1  lukem         }
   1046      1.1  lukem 
   1047      1.1  lukem         /*
   1048      1.1  lukem          * Skip to the number field.
   1049      1.1  lukem          */
   1050      1.1  lukem         for (i = 0; i < 3 && *s; s++) {
   1051      1.1  lukem             if (*s == ';')
   1052      1.1  lukem               i++;
   1053      1.1  lukem         }
   1054      1.1  lukem 
   1055      1.1  lukem         /*
   1056      1.1  lukem          * Scan the number in.
   1057      1.1  lukem          */
   1058      1.1  lukem         number[0] = number[1] = 0;
   1059      1.1  lukem         for (e = s, neg = wnum = 0; *e && *e != ';'; e++) {
   1060      1.1  lukem             if (*e == '-') {
   1061      1.1  lukem                 neg = 1;
   1062      1.1  lukem                 continue;
   1063      1.1  lukem             }
   1064      1.1  lukem 
   1065      1.1  lukem             if (*e == '/') {
   1066      1.1  lukem                 /*
   1067      1.1  lukem                  * Move the the denominator of the fraction.
   1068      1.1  lukem                  */
   1069      1.1  lukem                 if (neg)
   1070      1.1  lukem                   number[wnum] *= -1;
   1071      1.1  lukem                 neg = 0;
   1072      1.1  lukem                 e++;
   1073      1.1  lukem                 wnum++;
   1074      1.1  lukem             }
   1075      1.1  lukem             number[wnum] = (number[wnum] * 10) + (*e - '0');
   1076      1.1  lukem         }
   1077      1.1  lukem 
   1078      1.1  lukem         if (e > s) {
   1079      1.1  lukem             /*
   1080      1.1  lukem              * Adjust the denominator in case of integers and add the number.
   1081      1.1  lukem              */
   1082      1.1  lukem             if (wnum == 0)
   1083      1.1  lukem               number[1] = 1;
   1084      1.1  lukem 
   1085      1.1  lukem             add_number(code, number[0], number[1]);
   1086      1.1  lukem         }
   1087      1.1  lukem 
   1088      1.1  lukem         /*
   1089      1.1  lukem          * Skip to the start of the possible case mappings.
   1090      1.1  lukem          */
   1091      1.1  lukem         for (s = e, i = 0; i < 4 && *s; s++) {
   1092      1.1  lukem             if (*s == ';')
   1093      1.1  lukem               i++;
   1094      1.1  lukem         }
   1095      1.1  lukem 
   1096      1.1  lukem         /*
   1097      1.1  lukem          * Collect the case mappings.
   1098      1.1  lukem          */
   1099      1.1  lukem         cases[0] = cases[1] = cases[2] = 0;
   1100      1.1  lukem         for (i = 0; i < 3; i++) {
   1101      1.1  lukem             while (ishdigit(*s)) {
   1102      1.1  lukem                 cases[i] <<= 4;
   1103      1.1  lukem                 if (*s >= '0' && *s <= '9')
   1104      1.1  lukem                   cases[i] += *s - '0';
   1105      1.1  lukem                 else if (*s >= 'A' && *s <= 'F')
   1106      1.1  lukem                   cases[i] += (*s - 'A') + 10;
   1107      1.1  lukem                 else if (*s >= 'a' && *s <= 'f')
   1108      1.1  lukem                   cases[i] += (*s - 'a') + 10;
   1109      1.1  lukem                 s++;
   1110      1.1  lukem             }
   1111      1.1  lukem             if (*s == ';')
   1112      1.1  lukem               s++;
   1113      1.1  lukem         }
   1114      1.1  lukem         if (cases[0] && cases[1])
   1115      1.1  lukem           /*
   1116      1.1  lukem            * Add the upper and lower mappings for a title case character.
   1117      1.1  lukem            */
   1118      1.1  lukem           add_title(code);
   1119      1.1  lukem         else if (cases[1])
   1120      1.1  lukem           /*
   1121      1.1  lukem            * Add the lower and title case mappings for the upper case
   1122      1.1  lukem            * character.
   1123      1.1  lukem            */
   1124      1.1  lukem           add_upper(code);
   1125      1.1  lukem         else if (cases[0])
   1126      1.1  lukem           /*
   1127      1.1  lukem            * Add the upper and title case mappings for the lower case
   1128      1.1  lukem            * character.
   1129      1.1  lukem            */
   1130      1.1  lukem           add_lower(code);
   1131      1.1  lukem     }
   1132      1.1  lukem }
   1133      1.1  lukem 
   1134      1.1  lukem static _decomp_t *
   1135      1.1  lukem find_decomp(ac_uint4 code, short compat)
   1136      1.1  lukem {
   1137      1.1  lukem     long l, r, m;
   1138      1.1  lukem     _decomp_t *decs;
   1139      1.1  lukem 
   1140      1.1  lukem     l = 0;
   1141      1.1  lukem     r = (compat ? kdecomps_used : decomps_used) - 1;
   1142      1.1  lukem     decs = compat ? kdecomps : decomps;
   1143      1.1  lukem     while (l <= r) {
   1144      1.1  lukem         m = (l + r) >> 1;
   1145      1.1  lukem         if (code > decs[m].code)
   1146      1.1  lukem           l = m + 1;
   1147      1.1  lukem         else if (code < decs[m].code)
   1148      1.1  lukem           r = m - 1;
   1149      1.1  lukem         else
   1150      1.1  lukem           return &decs[m];
   1151      1.1  lukem     }
   1152      1.1  lukem     return 0;
   1153      1.1  lukem }
   1154      1.1  lukem 
   1155      1.1  lukem static void
   1156      1.1  lukem decomp_it(_decomp_t *d, short compat)
   1157      1.1  lukem {
   1158      1.1  lukem     ac_uint4 i;
   1159      1.1  lukem     _decomp_t *dp;
   1160      1.1  lukem 
   1161      1.1  lukem     for (i = 0; i < d->used; i++) {
   1162      1.1  lukem         if ((dp = find_decomp(d->decomp[i], compat)) != 0)
   1163      1.1  lukem           decomp_it(dp, compat);
   1164      1.1  lukem         else
   1165      1.1  lukem           dectmp[dectmp_size++] = d->decomp[i];
   1166      1.1  lukem     }
   1167      1.1  lukem }
   1168      1.1  lukem 
   1169      1.1  lukem /*
   1170      1.1  lukem  * Expand all decompositions by recursively decomposing each character
   1171      1.1  lukem  * in the decomposition.
   1172      1.1  lukem  */
   1173      1.1  lukem static void
   1174      1.1  lukem expand_decomp(void)
   1175      1.1  lukem {
   1176      1.1  lukem     ac_uint4 i;
   1177      1.1  lukem 
   1178      1.1  lukem     for (i = 0; i < decomps_used; i++) {
   1179      1.1  lukem         dectmp_size = 0;
   1180      1.1  lukem         decomp_it(&decomps[i], 0);
   1181      1.1  lukem         if (dectmp_size > 0)
   1182      1.1  lukem           add_decomp(decomps[i].code, 0);
   1183      1.1  lukem     }
   1184      1.1  lukem 
   1185      1.1  lukem     for (i = 0; i < kdecomps_used; i++) {
   1186      1.1  lukem         dectmp_size = 0;
   1187      1.1  lukem         decomp_it(&kdecomps[i], 1);
   1188      1.1  lukem         if (dectmp_size > 0)
   1189      1.1  lukem           add_decomp(kdecomps[i].code, 1);
   1190      1.1  lukem     }
   1191      1.1  lukem }
   1192      1.1  lukem 
   1193      1.1  lukem static int
   1194      1.1  lukem cmpcomps(const void *v_comp1, const void *v_comp2)
   1195      1.1  lukem {
   1196      1.1  lukem 	const _comp_t *comp1 = v_comp1, *comp2 = v_comp2;
   1197      1.1  lukem     long diff = comp1->code1 - comp2->code1;
   1198      1.1  lukem 
   1199      1.1  lukem     if (!diff)
   1200      1.1  lukem 	diff = comp1->code2 - comp2->code2;
   1201      1.1  lukem     return (int) diff;
   1202      1.1  lukem }
   1203      1.1  lukem 
   1204      1.1  lukem /*
   1205      1.1  lukem  * Load composition exclusion data
   1206      1.1  lukem  */
   1207      1.1  lukem static void
   1208      1.1  lukem read_compexdata(FILE *in)
   1209      1.1  lukem {
   1210      1.1  lukem     ac_uint2 i;
   1211      1.1  lukem     ac_uint4 code;
   1212      1.1  lukem     char line[512], *s;
   1213      1.1  lukem 
   1214      1.1  lukem     (void) memset((char *) compexs, 0, sizeof(compexs));
   1215      1.1  lukem 
   1216      1.1  lukem     while (fgets(line, sizeof(line), in)) {
   1217      1.1  lukem 	if( (s=strchr(line, '\n')) ) *s = '\0';
   1218      1.1  lukem         /*
   1219      1.1  lukem          * Skip blank lines and lines that start with a '#'.
   1220      1.1  lukem          */
   1221      1.1  lukem         if (line[0] == 0 || line[0] == '#')
   1222      1.1  lukem 	    continue;
   1223      1.1  lukem 
   1224      1.1  lukem 	/*
   1225      1.1  lukem          * Collect the code.  Assume max 6 digits
   1226      1.1  lukem          */
   1227      1.1  lukem 
   1228      1.1  lukem 	for (s = line, i = code = 0; *s != '#' && i < 6; i++, s++) {
   1229      1.1  lukem 	    if (isspace((unsigned char)*s)) break;
   1230      1.1  lukem             code <<= 4;
   1231      1.1  lukem             if (*s >= '0' && *s <= '9')
   1232      1.1  lukem 		code += *s - '0';
   1233      1.1  lukem             else if (*s >= 'A' && *s <= 'F')
   1234      1.1  lukem 		code += (*s - 'A') + 10;
   1235      1.1  lukem             else if (*s >= 'a' && *s <= 'f')
   1236      1.1  lukem 		code += (*s - 'a') + 10;
   1237      1.1  lukem         }
   1238      1.1  lukem         COMPEX_SET(code);
   1239      1.1  lukem     }
   1240      1.1  lukem }
   1241      1.1  lukem 
   1242      1.1  lukem /*
   1243      1.1  lukem  * Creates array of compositions from decomposition array
   1244      1.1  lukem  */
   1245      1.1  lukem static void
   1246      1.1  lukem create_comps(void)
   1247      1.1  lukem {
   1248      1.1  lukem     ac_uint4 i, cu;
   1249      1.1  lukem 
   1250      1.1  lukem     comps = (_comp_t *) malloc(comps_used * sizeof(_comp_t));
   1251      1.1  lukem 
   1252      1.1  lukem     for (i = cu = 0; i < decomps_used; i++) {
   1253      1.1  lukem 	if (decomps[i].used != 2 || COMPEX_TEST(decomps[i].code))
   1254      1.1  lukem 	    continue;
   1255      1.1  lukem 	comps[cu].comp = decomps[i].code;
   1256      1.1  lukem 	comps[cu].count = 2;
   1257      1.1  lukem 	comps[cu].code1 = decomps[i].decomp[0];
   1258      1.1  lukem 	comps[cu].code2 = decomps[i].decomp[1];
   1259      1.1  lukem 	cu++;
   1260      1.1  lukem     }
   1261      1.1  lukem     comps_used = cu;
   1262      1.1  lukem     qsort(comps, comps_used, sizeof(_comp_t), cmpcomps);
   1263      1.1  lukem }
   1264      1.1  lukem 
   1265      1.1  lukem #if HARDCODE_DATA
   1266      1.1  lukem static void
   1267      1.1  lukem write_case(FILE *out, _case_t *tab, int num, int first)
   1268      1.1  lukem {
   1269      1.1  lukem     int i;
   1270      1.1  lukem 
   1271      1.1  lukem     for (i=0; i<num; i++) {
   1272      1.1  lukem 	if (first) first = 0;
   1273      1.1  lukem 	else fprintf(out, ",");
   1274      1.1  lukem 	fprintf(out, "\n\t0x%08lx, 0x%08lx, 0x%08lx",
   1275      1.1  lukem 		(unsigned long) tab[i].key, (unsigned long) tab[i].other1,
   1276      1.1  lukem 		(unsigned long) tab[i].other2);
   1277      1.1  lukem     }
   1278      1.1  lukem }
   1279      1.1  lukem 
   1280      1.1  lukem #define PREF "static const "
   1281      1.1  lukem 
   1282      1.1  lukem #endif
   1283      1.1  lukem 
   1284      1.1  lukem static void
   1285      1.1  lukem write_cdata(char *opath)
   1286      1.1  lukem {
   1287      1.1  lukem     FILE *out;
   1288      1.1  lukem 	ac_uint4 bytes;
   1289      1.1  lukem     ac_uint4 i, idx, nprops;
   1290      1.1  lukem #if !(HARDCODE_DATA)
   1291      1.1  lukem     ac_uint2 casecnt[2];
   1292      1.1  lukem #endif
   1293      1.1  lukem     char path[BUFSIZ];
   1294      1.1  lukem #if HARDCODE_DATA
   1295      1.1  lukem     int j, k;
   1296      1.1  lukem 
   1297      1.1  lukem     /*****************************************************************
   1298      1.1  lukem      *
   1299      1.1  lukem      * Generate the ctype data.
   1300      1.1  lukem      *
   1301      1.1  lukem      *****************************************************************/
   1302      1.1  lukem 
   1303      1.1  lukem     /*
   1304      1.1  lukem      * Open the output file.
   1305      1.1  lukem      */
   1306      1.1  lukem     snprintf(path, sizeof path, "%s" LDAP_DIRSEP "uctable.h", opath);
   1307      1.1  lukem     if ((out = fopen(path, "w")) == 0)
   1308      1.1  lukem       return;
   1309      1.1  lukem #else
   1310      1.1  lukem     /*
   1311      1.1  lukem      * Open the ctype.dat file.
   1312      1.1  lukem      */
   1313      1.1  lukem     snprintf(path, sizeof path, "%s" LDAP_DIRSEP "ctype.dat", opath);
   1314      1.1  lukem     if ((out = fopen(path, "wb")) == 0)
   1315      1.1  lukem       return;
   1316      1.1  lukem #endif
   1317      1.1  lukem 
   1318      1.1  lukem     /*
   1319      1.1  lukem      * Collect the offsets for the properties.  The offsets array is
   1320      1.1  lukem      * on a 4-byte boundary to keep things efficient for architectures
   1321      1.1  lukem      * that need such a thing.
   1322      1.1  lukem      */
   1323      1.1  lukem     for (i = idx = 0; i < NUMPROPS; i++) {
   1324      1.1  lukem         propcnt[i] = (proptbl[i].used != 0) ? idx : 0xffff;
   1325      1.1  lukem         idx += proptbl[i].used;
   1326      1.1  lukem     }
   1327      1.1  lukem 
   1328      1.1  lukem     /*
   1329      1.1  lukem      * Add the sentinel index which is used by the binary search as the upper
   1330      1.1  lukem      * bound for a search.
   1331      1.1  lukem      */
   1332      1.1  lukem     propcnt[i] = idx;
   1333      1.1  lukem 
   1334      1.1  lukem     /*
   1335      1.1  lukem      * Record the actual number of property lists.  This may be different than
   1336      1.1  lukem      * the number of offsets actually written because of aligning on a 4-byte
   1337      1.1  lukem      * boundary.
   1338      1.1  lukem      */
   1339      1.1  lukem     hdr[1] = NUMPROPS;
   1340      1.1  lukem 
   1341      1.1  lukem     /*
   1342      1.1  lukem      * Calculate the byte count needed and pad the property counts array to a
   1343      1.1  lukem      * 4-byte boundary.
   1344      1.1  lukem      */
   1345      1.1  lukem     if ((bytes = sizeof(ac_uint2) * (NUMPROPS + 1)) & 3)
   1346      1.1  lukem       bytes += 4 - (bytes & 3);
   1347      1.1  lukem     nprops = bytes / sizeof(ac_uint2);
   1348      1.1  lukem     bytes += sizeof(ac_uint4) * idx;
   1349      1.1  lukem 
   1350      1.1  lukem #if HARDCODE_DATA
   1351      1.1  lukem     fprintf(out, PREF "ac_uint4 _ucprop_size = %d;\n\n", NUMPROPS);
   1352      1.1  lukem 
   1353      1.1  lukem     fprintf(out, PREF "ac_uint2 _ucprop_offsets[] = {");
   1354      1.1  lukem 
   1355      1.1  lukem     for (i = 0; i<nprops; i++) {
   1356      1.1  lukem        if (i) fprintf(out, ",");
   1357      1.1  lukem        if (!(i&7)) fprintf(out, "\n\t");
   1358      1.1  lukem        else fprintf(out, " ");
   1359      1.1  lukem        fprintf(out, "0x%04x", propcnt[i]);
   1360      1.1  lukem     }
   1361      1.1  lukem     fprintf(out, "\n};\n\n");
   1362      1.1  lukem 
   1363      1.1  lukem     fprintf(out, PREF "ac_uint4 _ucprop_ranges[] = {");
   1364      1.1  lukem 
   1365      1.1  lukem     k = 0;
   1366      1.1  lukem     for (i = 0; i < NUMPROPS; i++) {
   1367      1.1  lukem 	if (proptbl[i].used > 0) {
   1368      1.1  lukem 	  for (j=0; j<proptbl[i].used; j++) {
   1369      1.1  lukem 	    if (k) fprintf(out, ",");
   1370      1.1  lukem 	    if (!(k&3)) fprintf(out,"\n\t");
   1371      1.1  lukem 	    else fprintf(out, " ");
   1372      1.1  lukem 	    k++;
   1373      1.1  lukem 	    fprintf(out, "0x%08lx", (unsigned long) proptbl[i].ranges[j]);
   1374      1.1  lukem 	  }
   1375      1.1  lukem 	}
   1376      1.1  lukem     }
   1377      1.1  lukem     fprintf(out, "\n};\n\n");
   1378      1.1  lukem #else
   1379      1.1  lukem     /*
   1380      1.1  lukem      * Write the header.
   1381      1.1  lukem      */
   1382      1.1  lukem     fwrite((char *) hdr, sizeof(ac_uint2), 2, out);
   1383      1.1  lukem 
   1384      1.1  lukem     /*
   1385      1.1  lukem      * Write the byte count.
   1386      1.1  lukem      */
   1387      1.1  lukem     fwrite((char *) &bytes, sizeof(ac_uint4), 1, out);
   1388      1.1  lukem 
   1389      1.1  lukem     /*
   1390      1.1  lukem      * Write the property list counts.
   1391      1.1  lukem      */
   1392      1.1  lukem     fwrite((char *) propcnt, sizeof(ac_uint2), nprops, out);
   1393      1.1  lukem 
   1394      1.1  lukem     /*
   1395      1.1  lukem      * Write the property lists.
   1396      1.1  lukem      */
   1397      1.1  lukem     for (i = 0; i < NUMPROPS; i++) {
   1398      1.1  lukem         if (proptbl[i].used > 0)
   1399      1.1  lukem           fwrite((char *) proptbl[i].ranges, sizeof(ac_uint4),
   1400      1.1  lukem                  proptbl[i].used, out);
   1401      1.1  lukem     }
   1402      1.1  lukem 
   1403      1.1  lukem     fclose(out);
   1404      1.1  lukem #endif
   1405      1.1  lukem 
   1406      1.1  lukem     /*****************************************************************
   1407      1.1  lukem      *
   1408      1.1  lukem      * Generate the case mapping data.
   1409      1.1  lukem      *
   1410      1.1  lukem      *****************************************************************/
   1411      1.1  lukem 
   1412      1.1  lukem #if HARDCODE_DATA
   1413      1.1  lukem     fprintf(out, PREF "ac_uint4 _uccase_size = %ld;\n\n",
   1414      1.1  lukem         (long) (upper_used + lower_used + title_used));
   1415      1.1  lukem 
   1416      1.1  lukem     fprintf(out, PREF "ac_uint2 _uccase_len[2] = {%ld, %ld};\n\n",
   1417      1.1  lukem         (long) upper_used, (long) lower_used);
   1418      1.1  lukem     fprintf(out, PREF "ac_uint4 _uccase_map[] = {");
   1419      1.1  lukem 
   1420      1.1  lukem     if (upper_used > 0)
   1421      1.1  lukem       /*
   1422      1.1  lukem        * Write the upper case table.
   1423      1.1  lukem        */
   1424      1.1  lukem       write_case(out, upper, upper_used, 1);
   1425      1.1  lukem 
   1426      1.1  lukem     if (lower_used > 0)
   1427      1.1  lukem       /*
   1428      1.1  lukem        * Write the lower case table.
   1429      1.1  lukem        */
   1430      1.1  lukem       write_case(out, lower, lower_used, !upper_used);
   1431      1.1  lukem 
   1432      1.1  lukem     if (title_used > 0)
   1433      1.1  lukem       /*
   1434      1.1  lukem        * Write the title case table.
   1435      1.1  lukem        */
   1436      1.1  lukem       write_case(out, title, title_used, !(upper_used||lower_used));
   1437      1.1  lukem 
   1438      1.1  lukem     if (!(upper_used || lower_used || title_used))
   1439      1.1  lukem 	fprintf(out, "\t0");
   1440      1.1  lukem 
   1441      1.1  lukem     fprintf(out, "\n};\n\n");
   1442      1.1  lukem #else
   1443      1.1  lukem     /*
   1444      1.1  lukem      * Open the case.dat file.
   1445      1.1  lukem      */
   1446      1.1  lukem     snprintf(path, sizeof path, "%s" LDAP_DIRSEP "case.dat", opath);
   1447      1.1  lukem     if ((out = fopen(path, "wb")) == 0)
   1448      1.1  lukem       return;
   1449      1.1  lukem 
   1450      1.1  lukem     /*
   1451      1.1  lukem      * Write the case mapping tables.
   1452      1.1  lukem      */
   1453      1.1  lukem     hdr[1] = upper_used + lower_used + title_used;
   1454      1.1  lukem     casecnt[0] = upper_used;
   1455      1.1  lukem     casecnt[1] = lower_used;
   1456      1.1  lukem 
   1457      1.1  lukem     /*
   1458      1.1  lukem      * Write the header.
   1459      1.1  lukem      */
   1460      1.1  lukem     fwrite((char *) hdr, sizeof(ac_uint2), 2, out);
   1461      1.1  lukem 
   1462      1.1  lukem     /*
   1463      1.1  lukem      * Write the upper and lower case table sizes.
   1464      1.1  lukem      */
   1465      1.1  lukem     fwrite((char *) casecnt, sizeof(ac_uint2), 2, out);
   1466      1.1  lukem 
   1467      1.1  lukem     if (upper_used > 0)
   1468      1.1  lukem       /*
   1469      1.1  lukem        * Write the upper case table.
   1470      1.1  lukem        */
   1471      1.1  lukem       fwrite((char *) upper, sizeof(_case_t), upper_used, out);
   1472      1.1  lukem 
   1473      1.1  lukem     if (lower_used > 0)
   1474      1.1  lukem       /*
   1475      1.1  lukem        * Write the lower case table.
   1476      1.1  lukem        */
   1477      1.1  lukem       fwrite((char *) lower, sizeof(_case_t), lower_used, out);
   1478      1.1  lukem 
   1479      1.1  lukem     if (title_used > 0)
   1480      1.1  lukem       /*
   1481      1.1  lukem        * Write the title case table.
   1482      1.1  lukem        */
   1483      1.1  lukem       fwrite((char *) title, sizeof(_case_t), title_used, out);
   1484      1.1  lukem 
   1485      1.1  lukem     fclose(out);
   1486      1.1  lukem #endif
   1487      1.1  lukem 
   1488      1.1  lukem     /*****************************************************************
   1489      1.1  lukem      *
   1490      1.1  lukem      * Generate the composition data.
   1491      1.1  lukem      *
   1492      1.1  lukem      *****************************************************************/
   1493      1.1  lukem 
   1494      1.1  lukem     /*
   1495      1.1  lukem      * Create compositions from decomposition data
   1496      1.1  lukem      */
   1497      1.1  lukem     create_comps();
   1498      1.1  lukem 
   1499      1.1  lukem #if HARDCODE_DATA
   1500      1.1  lukem     fprintf(out, PREF "ac_uint4 _uccomp_size = %ld;\n\n",
   1501      1.1  lukem         comps_used * 4L);
   1502      1.1  lukem 
   1503      1.1  lukem     fprintf(out, PREF "ac_uint4 _uccomp_data[] = {");
   1504      1.1  lukem 
   1505      1.1  lukem      /*
   1506      1.1  lukem       * Now, if comps exist, write them out.
   1507      1.1  lukem       */
   1508      1.1  lukem     if (comps_used > 0) {
   1509      1.1  lukem 	for (i=0; i<comps_used; i++) {
   1510      1.1  lukem 	    if (i) fprintf(out, ",");
   1511      1.1  lukem 	    fprintf(out, "\n\t0x%08lx, 0x%08lx, 0x%08lx, 0x%08lx",
   1512      1.1  lukem 	        (unsigned long) comps[i].comp, (unsigned long) comps[i].count,
   1513      1.1  lukem 	        (unsigned long) comps[i].code1, (unsigned long) comps[i].code2);
   1514      1.1  lukem 	}
   1515      1.1  lukem     } else {
   1516      1.1  lukem 	fprintf(out, "\t0");
   1517      1.1  lukem     }
   1518      1.1  lukem     fprintf(out, "\n};\n\n");
   1519      1.1  lukem #else
   1520      1.1  lukem     /*
   1521      1.1  lukem      * Open the comp.dat file.
   1522      1.1  lukem      */
   1523      1.1  lukem     snprintf(path, sizeof path, "%s" LDAP_DIRSEP "comp.dat", opath);
   1524      1.1  lukem     if ((out = fopen(path, "wb")) == 0)
   1525      1.1  lukem 	return;
   1526      1.1  lukem 
   1527      1.1  lukem     /*
   1528      1.1  lukem      * Write the header.
   1529      1.1  lukem      */
   1530      1.1  lukem     hdr[1] = (ac_uint2) comps_used * 4;
   1531      1.1  lukem     fwrite((char *) hdr, sizeof(ac_uint2), 2, out);
   1532      1.1  lukem 
   1533      1.1  lukem     /*
   1534      1.1  lukem      * Write out the byte count to maintain header size.
   1535      1.1  lukem      */
   1536      1.1  lukem     bytes = comps_used * sizeof(_comp_t);
   1537      1.1  lukem     fwrite((char *) &bytes, sizeof(ac_uint4), 1, out);
   1538      1.1  lukem 
   1539      1.1  lukem     /*
   1540      1.1  lukem      * Now, if comps exist, write them out.
   1541      1.1  lukem      */
   1542      1.1  lukem     if (comps_used > 0)
   1543      1.1  lukem         fwrite((char *) comps, sizeof(_comp_t), comps_used, out);
   1544      1.1  lukem 
   1545      1.1  lukem     fclose(out);
   1546      1.1  lukem #endif
   1547      1.1  lukem 
   1548      1.1  lukem     /*****************************************************************
   1549      1.1  lukem      *
   1550      1.1  lukem      * Generate the decomposition data.
   1551      1.1  lukem      *
   1552      1.1  lukem      *****************************************************************/
   1553      1.1  lukem 
   1554      1.1  lukem     /*
   1555      1.1  lukem      * Fully expand all decompositions before generating the output file.
   1556      1.1  lukem      */
   1557      1.1  lukem     expand_decomp();
   1558      1.1  lukem 
   1559      1.1  lukem #if HARDCODE_DATA
   1560      1.1  lukem     fprintf(out, PREF "ac_uint4 _ucdcmp_size = %ld;\n\n",
   1561      1.1  lukem         decomps_used * 2L);
   1562      1.1  lukem 
   1563      1.1  lukem     fprintf(out, PREF "ac_uint4 _ucdcmp_nodes[] = {");
   1564      1.1  lukem 
   1565      1.1  lukem     if (decomps_used) {
   1566      1.1  lukem 	/*
   1567      1.1  lukem 	 * Write the list of decomp nodes.
   1568      1.1  lukem 	 */
   1569      1.1  lukem 	for (i = idx = 0; i < decomps_used; i++) {
   1570      1.1  lukem 	    fprintf(out, "\n\t0x%08lx, 0x%08lx,",
   1571      1.1  lukem 	        (unsigned long) decomps[i].code, (unsigned long) idx);
   1572      1.1  lukem 	    idx += decomps[i].used;
   1573      1.1  lukem 	}
   1574      1.1  lukem 
   1575      1.1  lukem 	/*
   1576      1.1  lukem 	 * Write the sentinel index as the last decomp node.
   1577      1.1  lukem 	 */
   1578      1.1  lukem 	fprintf(out, "\n\t0x%08lx\n};\n\n", (unsigned long) idx);
   1579      1.1  lukem 
   1580      1.1  lukem 	fprintf(out, PREF "ac_uint4 _ucdcmp_decomp[] = {");
   1581      1.1  lukem 	/*
   1582      1.1  lukem 	 * Write the decompositions themselves.
   1583      1.1  lukem 	 */
   1584      1.1  lukem 	k = 0;
   1585      1.1  lukem 	for (i = 0; i < decomps_used; i++)
   1586      1.1  lukem 	  for (j=0; j<decomps[i].used; j++) {
   1587      1.1  lukem 	    if (k) fprintf(out, ",");
   1588      1.1  lukem 	    if (!(k&3)) fprintf(out,"\n\t");
   1589      1.1  lukem 	    else fprintf(out, " ");
   1590      1.1  lukem 	    k++;
   1591      1.1  lukem 	    fprintf(out, "0x%08lx", (unsigned long) decomps[i].decomp[j]);
   1592      1.1  lukem 	  }
   1593      1.1  lukem 	fprintf(out, "\n};\n\n");
   1594      1.1  lukem     }
   1595      1.1  lukem #else
   1596      1.1  lukem     /*
   1597      1.1  lukem      * Open the decomp.dat file.
   1598      1.1  lukem      */
   1599      1.1  lukem     snprintf(path, sizeof path, "%s" LDAP_DIRSEP "decomp.dat", opath);
   1600      1.1  lukem     if ((out = fopen(path, "wb")) == 0)
   1601      1.1  lukem       return;
   1602      1.1  lukem 
   1603      1.1  lukem     hdr[1] = decomps_used;
   1604      1.1  lukem 
   1605      1.1  lukem     /*
   1606      1.1  lukem      * Write the header.
   1607      1.1  lukem      */
   1608      1.1  lukem     fwrite((char *) hdr, sizeof(ac_uint2), 2, out);
   1609      1.1  lukem 
   1610      1.1  lukem     /*
   1611      1.1  lukem      * Write a temporary byte count which will be calculated as the
   1612      1.1  lukem      * decompositions are written out.
   1613      1.1  lukem      */
   1614      1.1  lukem     bytes = 0;
   1615      1.1  lukem     fwrite((char *) &bytes, sizeof(ac_uint4), 1, out);
   1616      1.1  lukem 
   1617      1.1  lukem     if (decomps_used) {
   1618      1.1  lukem         /*
   1619      1.1  lukem          * Write the list of decomp nodes.
   1620      1.1  lukem          */
   1621      1.1  lukem         for (i = idx = 0; i < decomps_used; i++) {
   1622      1.1  lukem             fwrite((char *) &decomps[i].code, sizeof(ac_uint4), 1, out);
   1623      1.1  lukem             fwrite((char *) &idx, sizeof(ac_uint4), 1, out);
   1624      1.1  lukem             idx += decomps[i].used;
   1625      1.1  lukem         }
   1626      1.1  lukem 
   1627      1.1  lukem         /*
   1628      1.1  lukem          * Write the sentinel index as the last decomp node.
   1629      1.1  lukem          */
   1630      1.1  lukem         fwrite((char *) &idx, sizeof(ac_uint4), 1, out);
   1631      1.1  lukem 
   1632      1.1  lukem         /*
   1633      1.1  lukem          * Write the decompositions themselves.
   1634      1.1  lukem          */
   1635      1.1  lukem         for (i = 0; i < decomps_used; i++)
   1636      1.1  lukem           fwrite((char *) decomps[i].decomp, sizeof(ac_uint4),
   1637      1.1  lukem                  decomps[i].used, out);
   1638      1.1  lukem 
   1639      1.1  lukem         /*
   1640      1.1  lukem          * Seek back to the beginning and write the byte count.
   1641      1.1  lukem          */
   1642      1.1  lukem         bytes = (sizeof(ac_uint4) * idx) +
   1643      1.1  lukem             (sizeof(ac_uint4) * ((hdr[1] << 1) + 1));
   1644      1.1  lukem         fseek(out, sizeof(ac_uint2) << 1, 0L);
   1645      1.1  lukem         fwrite((char *) &bytes, sizeof(ac_uint4), 1, out);
   1646      1.1  lukem 
   1647      1.1  lukem         fclose(out);
   1648      1.1  lukem     }
   1649      1.1  lukem #endif
   1650      1.1  lukem 
   1651      1.1  lukem #ifdef HARDCODE_DATA
   1652      1.1  lukem     fprintf(out, PREF "ac_uint4 _uckdcmp_size = %ld;\n\n",
   1653      1.1  lukem         kdecomps_used * 2L);
   1654      1.1  lukem 
   1655      1.1  lukem     fprintf(out, PREF "ac_uint4 _uckdcmp_nodes[] = {");
   1656      1.1  lukem 
   1657      1.1  lukem     if (kdecomps_used) {
   1658      1.1  lukem 	/*
   1659      1.1  lukem 	 * Write the list of kdecomp nodes.
   1660      1.1  lukem 	 */
   1661      1.1  lukem 	for (i = idx = 0; i < kdecomps_used; i++) {
   1662      1.1  lukem 	    fprintf(out, "\n\t0x%08lx, 0x%08lx,",
   1663      1.1  lukem 	        (unsigned long) kdecomps[i].code, (unsigned long) idx);
   1664      1.1  lukem 	    idx += kdecomps[i].used;
   1665      1.1  lukem 	}
   1666      1.1  lukem 
   1667      1.1  lukem 	/*
   1668      1.1  lukem 	 * Write the sentinel index as the last decomp node.
   1669      1.1  lukem 	 */
   1670      1.1  lukem 	fprintf(out, "\n\t0x%08lx\n};\n\n", (unsigned long) idx);
   1671      1.1  lukem 
   1672      1.1  lukem 	fprintf(out, PREF "ac_uint4 _uckdcmp_decomp[] = {");
   1673      1.1  lukem 
   1674      1.1  lukem 	/*
   1675      1.1  lukem 	 * Write the decompositions themselves.
   1676      1.1  lukem 	 */
   1677      1.1  lukem 	k = 0;
   1678      1.1  lukem 	for (i = 0; i < kdecomps_used; i++)
   1679      1.1  lukem 	  for (j=0; j<kdecomps[i].used; j++) {
   1680      1.1  lukem 	    if (k) fprintf(out, ",");
   1681      1.1  lukem 	    if (!(k&3)) fprintf(out,"\n\t");
   1682      1.1  lukem 	    else fprintf(out, " ");
   1683      1.1  lukem 	    k++;
   1684      1.1  lukem 	    fprintf(out, "0x%08lx", (unsigned long) kdecomps[i].decomp[j]);
   1685      1.1  lukem 	  }
   1686      1.1  lukem 	fprintf(out, "\n};\n\n");
   1687      1.1  lukem     }
   1688      1.1  lukem #else
   1689      1.1  lukem     /*
   1690      1.1  lukem      * Open the kdecomp.dat file.
   1691      1.1  lukem      */
   1692      1.1  lukem     snprintf(path, sizeof path, "%s" LDAP_DIRSEP "kdecomp.dat", opath);
   1693      1.1  lukem     if ((out = fopen(path, "wb")) == 0)
   1694      1.1  lukem       return;
   1695      1.1  lukem 
   1696      1.1  lukem     hdr[1] = kdecomps_used;
   1697      1.1  lukem 
   1698      1.1  lukem     /*
   1699      1.1  lukem      * Write the header.
   1700      1.1  lukem      */
   1701      1.1  lukem     fwrite((char *) hdr, sizeof(ac_uint2), 2, out);
   1702      1.1  lukem 
   1703      1.1  lukem     /*
   1704      1.1  lukem      * Write a temporary byte count which will be calculated as the
   1705      1.1  lukem      * decompositions are written out.
   1706      1.1  lukem      */
   1707      1.1  lukem     bytes = 0;
   1708      1.1  lukem     fwrite((char *) &bytes, sizeof(ac_uint4), 1, out);
   1709      1.1  lukem 
   1710      1.1  lukem     if (kdecomps_used) {
   1711      1.1  lukem         /*
   1712      1.1  lukem          * Write the list of kdecomp nodes.
   1713      1.1  lukem          */
   1714      1.1  lukem         for (i = idx = 0; i < kdecomps_used; i++) {
   1715      1.1  lukem             fwrite((char *) &kdecomps[i].code, sizeof(ac_uint4), 1, out);
   1716      1.1  lukem             fwrite((char *) &idx, sizeof(ac_uint4), 1, out);
   1717      1.1  lukem             idx += kdecomps[i].used;
   1718      1.1  lukem         }
   1719      1.1  lukem 
   1720      1.1  lukem         /*
   1721      1.1  lukem          * Write the sentinel index as the last decomp node.
   1722      1.1  lukem          */
   1723      1.1  lukem         fwrite((char *) &idx, sizeof(ac_uint4), 1, out);
   1724      1.1  lukem 
   1725      1.1  lukem         /*
   1726      1.1  lukem          * Write the decompositions themselves.
   1727      1.1  lukem          */
   1728      1.1  lukem         for (i = 0; i < kdecomps_used; i++)
   1729      1.1  lukem           fwrite((char *) kdecomps[i].decomp, sizeof(ac_uint4),
   1730      1.1  lukem                  kdecomps[i].used, out);
   1731      1.1  lukem 
   1732      1.1  lukem         /*
   1733      1.1  lukem          * Seek back to the beginning and write the byte count.
   1734      1.1  lukem          */
   1735      1.1  lukem         bytes = (sizeof(ac_uint4) * idx) +
   1736      1.1  lukem             (sizeof(ac_uint4) * ((hdr[1] << 1) + 1));
   1737      1.1  lukem         fseek(out, sizeof(ac_uint2) << 1, 0L);
   1738      1.1  lukem         fwrite((char *) &bytes, sizeof(ac_uint4), 1, out);
   1739      1.1  lukem 
   1740      1.1  lukem         fclose(out);
   1741      1.1  lukem     }
   1742      1.1  lukem #endif
   1743      1.1  lukem 
   1744      1.1  lukem     /*****************************************************************
   1745      1.1  lukem      *
   1746      1.1  lukem      * Generate the combining class data.
   1747      1.1  lukem      *
   1748      1.1  lukem      *****************************************************************/
   1749      1.1  lukem #ifdef HARDCODE_DATA
   1750      1.1  lukem     fprintf(out, PREF "ac_uint4 _uccmcl_size = %ld;\n\n", (long) ccl_used);
   1751      1.1  lukem 
   1752      1.1  lukem     fprintf(out, PREF "ac_uint4 _uccmcl_nodes[] = {");
   1753      1.1  lukem 
   1754      1.1  lukem     if (ccl_used > 0) {
   1755      1.1  lukem 	/*
   1756      1.1  lukem 	 * Write the combining class ranges out.
   1757      1.1  lukem 	 */
   1758      1.1  lukem 	for (i = 0; i<ccl_used; i++) {
   1759      1.1  lukem 	    if (i) fprintf(out, ",");
   1760      1.1  lukem 	    if (!(i&3)) fprintf(out, "\n\t");
   1761      1.1  lukem 	    else fprintf(out, " ");
   1762      1.1  lukem 	    fprintf(out, "0x%08lx", (unsigned long) ccl[i]);
   1763      1.1  lukem 	}
   1764      1.1  lukem     } else {
   1765      1.1  lukem 	fprintf(out, "\t0");
   1766      1.1  lukem     }
   1767      1.1  lukem     fprintf(out, "\n};\n\n");
   1768      1.1  lukem #else
   1769      1.1  lukem     /*
   1770      1.1  lukem      * Open the cmbcl.dat file.
   1771      1.1  lukem      */
   1772      1.1  lukem     snprintf(path, sizeof path, "%s" LDAP_DIRSEP "cmbcl.dat", opath);
   1773      1.1  lukem     if ((out = fopen(path, "wb")) == 0)
   1774      1.1  lukem       return;
   1775      1.1  lukem 
   1776      1.1  lukem     /*
   1777      1.1  lukem      * Set the number of ranges used.  Each range has a combining class which
   1778      1.1  lukem      * means each entry is a 3-tuple.
   1779      1.1  lukem      */
   1780      1.1  lukem     hdr[1] = ccl_used / 3;
   1781      1.1  lukem 
   1782      1.1  lukem     /*
   1783      1.1  lukem      * Write the header.
   1784      1.1  lukem      */
   1785      1.1  lukem     fwrite((char *) hdr, sizeof(ac_uint2), 2, out);
   1786      1.1  lukem 
   1787      1.1  lukem     /*
   1788      1.1  lukem      * Write out the byte count to maintain header size.
   1789      1.1  lukem      */
   1790      1.1  lukem     bytes = ccl_used * sizeof(ac_uint4);
   1791      1.1  lukem     fwrite((char *) &bytes, sizeof(ac_uint4), 1, out);
   1792      1.1  lukem 
   1793      1.1  lukem     if (ccl_used > 0)
   1794      1.1  lukem       /*
   1795      1.1  lukem        * Write the combining class ranges out.
   1796      1.1  lukem        */
   1797      1.1  lukem       fwrite((char *) ccl, sizeof(ac_uint4), ccl_used, out);
   1798      1.1  lukem 
   1799      1.1  lukem     fclose(out);
   1800      1.1  lukem #endif
   1801      1.1  lukem 
   1802      1.1  lukem     /*****************************************************************
   1803      1.1  lukem      *
   1804      1.1  lukem      * Generate the number data.
   1805      1.1  lukem      *
   1806      1.1  lukem      *****************************************************************/
   1807      1.1  lukem 
   1808      1.1  lukem #if HARDCODE_DATA
   1809      1.1  lukem     fprintf(out, PREF "ac_uint4 _ucnum_size = %lu;\n\n",
   1810      1.1  lukem         (unsigned long)ncodes_used<<1);
   1811      1.1  lukem 
   1812      1.1  lukem     fprintf(out, PREF "ac_uint4 _ucnum_nodes[] = {");
   1813      1.1  lukem 
   1814      1.1  lukem     /*
   1815      1.1  lukem      * Now, if number mappings exist, write them out.
   1816      1.1  lukem      */
   1817      1.1  lukem     if (ncodes_used > 0) {
   1818      1.1  lukem 	for (i = 0; i<ncodes_used; i++) {
   1819      1.1  lukem 	    if (i) fprintf(out, ",");
   1820      1.1  lukem 	    if (!(i&1)) fprintf(out, "\n\t");
   1821      1.1  lukem 	    else fprintf(out, " ");
   1822      1.1  lukem 	    fprintf(out, "0x%08lx, 0x%08lx",
   1823      1.1  lukem 	        (unsigned long) ncodes[i].code, (unsigned long) ncodes[i].idx);
   1824      1.1  lukem 	}
   1825      1.1  lukem 	fprintf(out, "\n};\n\n");
   1826      1.1  lukem 
   1827      1.1  lukem 	fprintf(out, PREF "short _ucnum_vals[] = {");
   1828      1.1  lukem 	for (i = 0; i<nums_used; i++) {
   1829      1.1  lukem 	    if (i) fprintf(out, ",");
   1830      1.1  lukem 	    if (!(i&3)) fprintf(out, "\n\t");
   1831      1.1  lukem 	    else fprintf(out, " ");
   1832      1.1  lukem 	    if (nums[i].numerator < 0) {
   1833      1.1  lukem 		fprintf(out, "%6d, 0x%04x",
   1834      1.1  lukem 		  nums[i].numerator, nums[i].denominator);
   1835      1.1  lukem 	    } else {
   1836      1.1  lukem 		fprintf(out, "0x%04x, 0x%04x",
   1837      1.1  lukem 		  nums[i].numerator, nums[i].denominator);
   1838      1.1  lukem 	    }
   1839      1.1  lukem 	}
   1840      1.1  lukem 	fprintf(out, "\n};\n\n");
   1841      1.1  lukem     }
   1842      1.1  lukem #else
   1843      1.1  lukem     /*
   1844      1.1  lukem      * Open the num.dat file.
   1845      1.1  lukem      */
   1846      1.1  lukem     snprintf(path, sizeof path, "%s" LDAP_DIRSEP "num.dat", opath);
   1847      1.1  lukem     if ((out = fopen(path, "wb")) == 0)
   1848      1.1  lukem       return;
   1849      1.1  lukem 
   1850      1.1  lukem     /*
   1851      1.1  lukem      * The count part of the header will be the total number of codes that
   1852      1.1  lukem      * have numbers.
   1853      1.1  lukem      */
   1854      1.1  lukem     hdr[1] = (ac_uint2) (ncodes_used << 1);
   1855      1.1  lukem     bytes = (ncodes_used * sizeof(_codeidx_t)) + (nums_used * sizeof(_num_t));
   1856      1.1  lukem 
   1857      1.1  lukem     /*
   1858      1.1  lukem      * Write the header.
   1859      1.1  lukem      */
   1860      1.1  lukem     fwrite((char *) hdr, sizeof(ac_uint2), 2, out);
   1861      1.1  lukem 
   1862      1.1  lukem     /*
   1863      1.1  lukem      * Write out the byte count to maintain header size.
   1864      1.1  lukem      */
   1865      1.1  lukem     fwrite((char *) &bytes, sizeof(ac_uint4), 1, out);
   1866      1.1  lukem 
   1867      1.1  lukem     /*
   1868      1.1  lukem      * Now, if number mappings exist, write them out.
   1869      1.1  lukem      */
   1870      1.1  lukem     if (ncodes_used > 0) {
   1871      1.1  lukem         fwrite((char *) ncodes, sizeof(_codeidx_t), ncodes_used, out);
   1872      1.1  lukem         fwrite((char *) nums, sizeof(_num_t), nums_used, out);
   1873      1.1  lukem     }
   1874      1.1  lukem #endif
   1875      1.1  lukem 
   1876      1.1  lukem     fclose(out);
   1877      1.1  lukem }
   1878      1.1  lukem 
   1879      1.1  lukem static void
   1880      1.1  lukem usage(char *prog)
   1881      1.1  lukem {
   1882      1.1  lukem     fprintf(stderr,
   1883      1.1  lukem             "Usage: %s [-o output-directory|-x composition-exclusions]", prog);
   1884      1.1  lukem     fprintf(stderr, " datafile1 datafile2 ...\n\n");
   1885      1.1  lukem     fprintf(stderr,
   1886      1.1  lukem             "-o output-directory\n\t\tWrite the output files to a different");
   1887      1.1  lukem     fprintf(stderr, " directory (default: .).\n");
   1888      1.1  lukem     fprintf(stderr,
   1889      1.1  lukem             "-x composition-exclusion\n\t\tFile of composition codes");
   1890      1.1  lukem     fprintf(stderr, " that should be excluded.\n");
   1891      1.1  lukem     exit(1);
   1892      1.1  lukem }
   1893      1.1  lukem 
   1894      1.1  lukem int
   1895      1.1  lukem main(int argc, char *argv[])
   1896      1.1  lukem {
   1897      1.1  lukem     FILE *in;
   1898      1.1  lukem     char *prog, *opath;
   1899      1.1  lukem 
   1900      1.1  lukem     prog = lutil_progname( "ucgendat", argc, argv );
   1901      1.1  lukem 
   1902      1.1  lukem     opath = 0;
   1903      1.1  lukem     in = stdin;
   1904      1.1  lukem 
   1905      1.1  lukem     argc--;
   1906      1.1  lukem     argv++;
   1907      1.1  lukem 
   1908      1.1  lukem     while (argc > 0) {
   1909      1.1  lukem         if (argv[0][0] == '-') {
   1910      1.1  lukem             switch (argv[0][1]) {
   1911      1.1  lukem               case 'o':
   1912      1.1  lukem                 argc--;
   1913      1.1  lukem                 argv++;
   1914      1.1  lukem                 opath = argv[0];
   1915      1.1  lukem                 break;
   1916      1.1  lukem               case 'x':
   1917      1.1  lukem                 argc--;
   1918      1.1  lukem                 argv++;
   1919      1.1  lukem                 if ((in = fopen(argv[0], "r")) == 0)
   1920      1.1  lukem                   fprintf(stderr,
   1921      1.1  lukem                           "%s: unable to open composition exclusion file %s\n",
   1922      1.1  lukem                           prog, argv[0]);
   1923      1.1  lukem                 else {
   1924      1.1  lukem                     read_compexdata(in);
   1925      1.1  lukem                     fclose(in);
   1926      1.1  lukem                     in = 0;
   1927      1.1  lukem                 }
   1928      1.1  lukem                 break;
   1929      1.1  lukem               default:
   1930      1.1  lukem                 usage(prog);
   1931      1.1  lukem             }
   1932      1.1  lukem         } else {
   1933      1.1  lukem             if (in != stdin && in != NULL)
   1934      1.1  lukem               fclose(in);
   1935      1.1  lukem             if ((in = fopen(argv[0], "r")) == 0)
   1936      1.1  lukem               fprintf(stderr, "%s: unable to open ctype file %s\n",
   1937      1.1  lukem                       prog, argv[0]);
   1938      1.1  lukem             else {
   1939      1.1  lukem                 read_cdata(in);
   1940      1.1  lukem                 fclose(in);
   1941      1.1  lukem                 in = 0;
   1942      1.1  lukem 	    }
   1943      1.1  lukem         }
   1944      1.1  lukem         argc--;
   1945      1.1  lukem         argv++;
   1946      1.1  lukem     }
   1947      1.1  lukem 
   1948      1.1  lukem     if (opath == 0)
   1949      1.1  lukem       opath = ".";
   1950      1.1  lukem     write_cdata(opath);
   1951      1.1  lukem 
   1952      1.1  lukem     return 0;
   1953      1.1  lukem }
   1954