intl/unicharutil/src/ucdata.c

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

     1 /*
     2  * Copyright 1996, 1997, 1998 Computing Research Labs,
     3  * New Mexico State University
     4  *
     5  * Permission is hereby granted, free of charge, to any person obtaining a
     6  * copy of this software and associated documentation files (the "Software"),
     7  * to deal in the Software without restriction, including without limitation
     8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     9  * and/or sell copies of the Software, and to permit persons to whom the
    10  * Software is furnished to do so, subject to the following conditions:
    11  *
    12  * The above copyright notice and this permission notice shall be included in
    13  * all copies or substantial portions of the Software.
    14  *
    15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
    18  * THE COMPUTING RESEARCH LAB OR NEW MEXICO STATE UNIVERSITY BE LIABLE FOR ANY
    19  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
    20  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
    21  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    22  */
    23 #ifndef lint
    24 #ifdef __GNUC__
    25 static char rcsid[] __attribute__ ((unused)) = "$Id: ucdata.c,v 1.1 1999/01/08 00:19:11 ftang%netscape.com Exp $";
    26 #else
    27 static char rcsid[] = "$Id: ucdata.c,v 1.1 1999/01/08 00:19:11 ftang%netscape.com Exp $";
    28 #endif
    29 #endif
    31 #include <stdio.h>
    32 #include <stdlib.h>
    33 #include <string.h>
    34 #ifndef WIN32
    35 #include <unistd.h>
    36 #endif
    38 #include "ucdata.h"
    40 /**************************************************************************
    41  *
    42  * Miscellaneous types, data, and support functions.
    43  *
    44  **************************************************************************/
    46 typedef struct {
    47     unsigned short bom;
    48     unsigned short cnt;
    49     union {
    50         unsigned long bytes;
    51         unsigned short len[2];
    52     } size;
    53 } _ucheader_t;
    55 /*
    56  * A simple array of 32-bit masks for lookup.
    57  */
    58 static unsigned long masks32[32] = {
    59     0x00000001, 0x00000002, 0x00000004, 0x00000008, 0x00000010, 0x00000020,
    60     0x00000040, 0x00000080, 0x00000100, 0x00000200, 0x00000400, 0x00000800,
    61     0x00001000, 0x00002000, 0x00004000, 0x00008000, 0x00010000, 0x00020000,
    62     0x00040000, 0x00080000, 0x00100000, 0x00200000, 0x00400000, 0x00800000,
    63     0x01000000, 0x02000000, 0x04000000, 0x08000000, 0x10000000, 0x20000000,
    64     0x40000000, 0x80000000
    65 };
    67 #define endian_short(cc) (((cc) >> 8) | (((cc) & 0xff) << 8))
    68 #define endian_long(cc) ((((cc) & 0xff) << 24)|((((cc) >> 8) & 0xff) << 16)|\
    69                         ((((cc) >> 16) & 0xff) << 8)|((cc) >> 24))
    71 static FILE *
    72 #ifdef __STDC__
    73 _ucopenfile(char *paths, char *filename, char *mode)
    74 #else
    75 _ucopenfile(paths, filename, mode)
    76 char *paths, *filename, *mode;
    77 #endif
    78 {
    79     FILE *f;
    80     char *fp, *dp, *pp, path[BUFSIZ];
    82     if (filename == 0 || *filename == 0)
    83       return 0;
    85     dp = paths;
    86     while (dp && *dp) {
    87         pp = path;
    88         while (*dp && *dp != ':')
    89           *pp++ = *dp++;
    90         *pp++ = '/';
    92         fp = filename;
    93         while (*fp)
    94           *pp++ = *fp++;
    95         *pp = 0;
    97         if ((f = fopen(path, mode)) != 0)
    98           return f;
   100         if (*dp == ':')
   101           dp++;
   102     }
   104     return 0;
   105 }
   107 /**************************************************************************
   108  *
   109  * Support for the character properties.
   110  *
   111  **************************************************************************/
   113 static unsigned long  _ucprop_size;
   114 static unsigned short *_ucprop_offsets;
   115 static unsigned long  *_ucprop_ranges;
   117 static void
   118 #ifdef __STDC__
   119 _ucprop_load(char *paths, int reload)
   120 #else
   121 _ucprop_load(paths, reload)
   122 char *paths;
   123 int reload;
   124 #endif
   125 {
   126     FILE *in;
   127     unsigned long size, i;
   128     _ucheader_t hdr;
   130     if (_ucprop_size > 0) {
   131         if (!reload)
   132           /*
   133            * The character properties have already been loaded.
   134            */
   135           return;
   137         /*
   138          * Unload the current character property data in preparation for
   139          * loading a new copy.  Only the first array has to be deallocated
   140          * because all the memory for the arrays is allocated as a single
   141          * block.
   142          */
   143         free((char *) _ucprop_offsets);
   144         _ucprop_size = 0;
   145     }
   147     if ((in = _ucopenfile(paths, "ctype.dat", "rb")) == 0)
   148       return;
   150     /*
   151      * Load the header.
   152      */
   153     fread((char *) &hdr, sizeof(_ucheader_t), 1, in);
   155     if (hdr.bom == 0xfffe) {
   156         hdr.cnt = endian_short(hdr.cnt);
   157         hdr.size.bytes = endian_long(hdr.size.bytes);
   158     }
   160     if ((_ucprop_size = hdr.cnt) == 0) {
   161         fclose(in);
   162         return;
   163     }
   165     /*
   166      * Allocate all the storage needed for the lookup table.
   167      */
   168     _ucprop_offsets = (unsigned short *) malloc(hdr.size.bytes);
   170     /*
   171      * Calculate the offset into the storage for the ranges.  The offsets
   172      * array is on a 4-byte boundary and one larger than the value provided in
   173      * the header count field.  This means the offset to the ranges must be
   174      * calculated after aligning the count to a 4-byte boundary.
   175      */
   176     if ((size = ((hdr.cnt + 1) * sizeof(unsigned short))) & 3)
   177       size += 4 - (size & 3);
   178     size >>= 1;
   179     _ucprop_ranges = (unsigned long *) (_ucprop_offsets + size);
   181     /*
   182      * Load the offset array.
   183      */
   184     fread((char *) _ucprop_offsets, sizeof(unsigned short), size, in);
   186     /*
   187      * Do an endian swap if necessary.  Don't forget there is an extra node on
   188      * the end with the final index.
   189      */
   190     if (hdr.bom == 0xfffe) {
   191         for (i = 0; i <= _ucprop_size; i++)
   192           _ucprop_offsets[i] = endian_short(_ucprop_offsets[i]);
   193     }
   195     /*
   196      * Load the ranges.  The number of elements is in the last array position
   197      * of the offsets.
   198      */
   199     fread((char *) _ucprop_ranges, sizeof(unsigned long),
   200           _ucprop_offsets[_ucprop_size], in);
   202     fclose(in);
   204     /*
   205      * Do an endian swap if necessary.
   206      */
   207     if (hdr.bom == 0xfffe) {
   208         for (i = 0; i < _ucprop_offsets[_ucprop_size]; i++)
   209           _ucprop_ranges[i] = endian_long(_ucprop_ranges[i]);
   210     }
   211 }
   213 static void
   214 #ifdef __STDC__
   215 _ucprop_unload(void)
   216 #else
   217 _ucprop_unload()
   218 #endif
   219 {
   220     if (_ucprop_size == 0)
   221       return;
   223     /*
   224      * Only need to free the offsets because the memory is allocated as a
   225      * single block.
   226      */
   227     free((char *) _ucprop_offsets);
   228     _ucprop_size = 0;
   229 }
   231 static int
   232 #ifdef __STDC__
   233 _ucprop_lookup(unsigned long code, unsigned long n)
   234 #else
   235 _ucprop_lookup(code, n)
   236 unsigned long code, n;
   237 #endif
   238 {
   239     long l, r, m;
   241     /*
   242      * There is an extra node on the end of the offsets to allow this routine
   243      * to work right.  If the index is 0xffff, then there are no nodes for the
   244      * property.
   245      */
   246     if ((l = _ucprop_offsets[n]) == 0xffff)
   247       return 0;
   249     /*
   250      * Locate the next offset that is not 0xffff.  The sentinel at the end of
   251      * the array is the max index value.
   252      */
   253     for (m = 1;
   254          n + m < _ucprop_size && _ucprop_offsets[n + m] == 0xffff; m++) ;
   256     r = _ucprop_offsets[n + m] - 1;
   258     while (l <= r) {
   259         /*
   260          * Determine a "mid" point and adjust to make sure the mid point is at
   261          * the beginning of a range pair.
   262          */
   263         m = (l + r) >> 1;
   264         m -= (m & 1);
   265         if (code > _ucprop_ranges[m + 1])
   266           l = m + 2;
   267         else if (code < _ucprop_ranges[m])
   268           r = m - 2;
   269         else if (code >= _ucprop_ranges[m] && code <= _ucprop_ranges[m + 1])
   270           return 1;
   271     }
   272     return 0;
   273 }
   275 int
   276 #ifdef __STDC__
   277 ucisprop(unsigned long code, unsigned long mask1, unsigned long mask2)
   278 #else
   279 ucisprop(code, mask1, mask2)
   280 unsigned long code, mask1, mask2;
   281 #endif
   282 {
   283     unsigned long i;
   285     if (mask1 == 0 && mask2 == 0)
   286       return 0;
   288     for (i = 0; mask1 && i < 32; i++) {
   289         if ((mask1 & masks32[i]) && _ucprop_lookup(code, i))
   290           return 1;
   291     }
   293     for (i = 32; mask2 && i < _ucprop_size; i++) {
   294         if ((mask2 & masks32[i & 31]) && _ucprop_lookup(code, i))
   295           return 1;
   296     }
   298     return 0;
   299 }
   301 /**************************************************************************
   302  *
   303  * Support for case mapping.
   304  *
   305  **************************************************************************/
   307 static unsigned long _uccase_size;
   308 static unsigned short _uccase_len[2];
   309 static unsigned long *_uccase_map;
   311 static void
   312 #ifdef __STDC__
   313 _uccase_load(char *paths, int reload)
   314 #else
   315 _uccase_load(paths, reload)
   316 char *paths;
   317 int reload;
   318 #endif
   319 {
   320     FILE *in;
   321     unsigned long i;
   322     _ucheader_t hdr;
   324     if (_uccase_size > 0) {
   325         if (!reload)
   326           /*
   327            * The case mappings have already been loaded.
   328            */
   329           return;
   331         free((char *) _uccase_map);
   332         _uccase_size = 0;
   333     }
   335     if ((in = _ucopenfile(paths, "case.dat", "rb")) == 0)
   336       return;
   338     /*
   339      * Load the header.
   340      */
   341     fread((char *) &hdr, sizeof(_ucheader_t), 1, in);
   343     if (hdr.bom == 0xfffe) {
   344         hdr.cnt = endian_short(hdr.cnt);
   345         hdr.size.len[0] = endian_short(hdr.size.len[0]);
   346         hdr.size.len[1] = endian_short(hdr.size.len[1]);
   347     }
   349     /*
   350      * Set the node count and lengths of the upper and lower case mapping
   351      * tables.
   352      */
   353     _uccase_size = hdr.cnt * 3;
   354     _uccase_len[0] = hdr.size.len[0] * 3;
   355     _uccase_len[1] = hdr.size.len[1] * 3;
   357     _uccase_map = (unsigned long *)
   358         malloc(_uccase_size * sizeof(unsigned long));
   360     /*
   361      * Load the case mapping table.
   362      */
   363     fread((char *) _uccase_map, sizeof(unsigned long), _uccase_size, in);
   365     /*
   366      * Do an endian swap if necessary.
   367      */
   368     if (hdr.bom == 0xfffe) {
   369         for (i = 0; i < _uccase_size; i++)
   370           _uccase_map[i] = endian_long(_uccase_map[i]);
   371     }
   372 }
   374 static void
   375 #ifdef __STDC__
   376 _uccase_unload(void)
   377 #else
   378 _uccase_unload()
   379 #endif
   380 {
   381     if (_uccase_size == 0)
   382       return;
   384     free((char *) _uccase_map);
   385     _uccase_size = 0;
   386 }
   388 static unsigned long
   389 #ifdef __STDC__
   390 _uccase_lookup(unsigned long code, long l, long r, int field)
   391 #else
   392 _uccase_lookup(code, l, r, field)
   393 unsigned long code;
   394 long l, r;
   395 int field;
   396 #endif
   397 {
   398     long m;
   400     /*
   401      * Do the binary search.
   402      */
   403     while (l <= r) {
   404         /*
   405          * Determine a "mid" point and adjust to make sure the mid point is at
   406          * the beginning of a case mapping triple.
   407          */
   408         m = (l + r) >> 1;
   409         m -= (m % 3);
   410         if (code > _uccase_map[m])
   411           l = m + 3;
   412         else if (code < _uccase_map[m])
   413           r = m - 3;
   414         else if (code == _uccase_map[m])
   415           return _uccase_map[m + field];
   416     }
   418     return code;
   419 }
   421 unsigned long
   422 #ifdef __STDC__
   423 uctoupper(unsigned long code)
   424 #else
   425 uctoupper(code)
   426 unsigned long code;
   427 #endif
   428 {
   429     int field;
   430     long l, r;
   432     if (ucisupper(code))
   433       return code;
   435     if (ucislower(code)) {
   436         /*
   437          * The character is lower case.
   438          */
   439         field = 1;
   440         l = _uccase_len[0];
   441         r = (l + _uccase_len[1]) - 1;
   442     } else {
   443         /*
   444          * The character is title case.
   445          */
   446         field = 2;
   447         l = _uccase_len[0] + _uccase_len[1];
   448         r = _uccase_size - 1;
   449     }
   450     return _uccase_lookup(code, l, r, field);
   451 }
   453 unsigned long
   454 #ifdef __STDC__
   455 uctolower(unsigned long code)
   456 #else
   457 uctolower(code)
   458 unsigned long code;
   459 #endif
   460 {
   461     int field;
   462     long l, r;
   464     if (ucislower(code))
   465       return code;
   467     if (ucisupper(code)) {
   468         /*
   469          * The character is upper case.
   470          */
   471         field = 1;
   472         l = 0;
   473         r = _uccase_len[0] - 1;
   474     } else {
   475         /*
   476          * The character is title case.
   477          */
   478         field = 2;
   479         l = _uccase_len[0] + _uccase_len[1];
   480         r = _uccase_size - 1;
   481     }
   482     return _uccase_lookup(code, l, r, field);
   483 }
   485 unsigned long
   486 #ifdef __STDC__
   487 uctotitle(unsigned long code)
   488 #else
   489 uctotitle(code)
   490 unsigned long code;
   491 #endif
   492 {
   493     int field;
   494     long l, r;
   496     if (ucistitle(code))
   497       return code;
   499     /*
   500      * The offset will always be the same for converting to title case.
   501      */
   502     field = 2;
   504     if (ucisupper(code)) {
   505         /*
   506          * The character is upper case.
   507          */
   508         l = 0;
   509         r = _uccase_len[0] - 1;
   510     } else {
   511         /*
   512          * The character is lower case.
   513          */
   514         l = _uccase_len[0];
   515         r = (l + _uccase_len[1]) - 1;
   516     }
   517     return _uccase_lookup(code, l, r, field);
   518 }
   520 /**************************************************************************
   521  *
   522  * Support for decompositions.
   523  *
   524  **************************************************************************/
   526 static unsigned long  _ucdcmp_size;
   527 static unsigned long *_ucdcmp_nodes;
   528 static unsigned long *_ucdcmp_decomp;
   530 static void
   531 #ifdef __STDC__
   532 _ucdcmp_load(char *paths, int reload)
   533 #else
   534 _ucdcmp_load(paths, reload)
   535 char *paths;
   536 int reload;
   537 #endif
   538 {
   539     FILE *in;
   540     unsigned long size, i;
   541     _ucheader_t hdr;
   543     if (_ucdcmp_size > 0) {
   544         if (!reload)
   545           /*
   546            * The decompositions have already been loaded.
   547            */
   548           return;
   550         free((char *) _ucdcmp_nodes);
   551         _ucdcmp_size = 0;
   552     }
   554     if ((in = _ucopenfile(paths, "decomp.dat", "rb")) == 0)
   555       return;
   557     /*
   558      * Load the header.
   559      */
   560     fread((char *) &hdr, sizeof(_ucheader_t), 1, in);
   562     if (hdr.bom == 0xfffe) {
   563         hdr.cnt = endian_short(hdr.cnt);
   564         hdr.size.bytes = endian_long(hdr.size.bytes);
   565     }
   567     _ucdcmp_size = hdr.cnt << 1;
   568     _ucdcmp_nodes = (unsigned long *) malloc(hdr.size.bytes);
   569     _ucdcmp_decomp = _ucdcmp_nodes + (_ucdcmp_size + 1);
   571     /*
   572      * Read the decomposition data in.
   573      */
   574     size = hdr.size.bytes / sizeof(unsigned long);
   575     fread((char *) _ucdcmp_nodes, sizeof(unsigned long), size, in);
   577     /*
   578      * Do an endian swap if necessary.
   579      */
   580     if (hdr.bom == 0xfffe) {
   581         for (i = 0; i < size; i++)
   582           _ucdcmp_nodes[i] = endian_long(_ucdcmp_nodes[i]);
   583     }        
   584 }
   586 static void
   587 #ifdef __STDC__
   588 _ucdcmp_unload(void)
   589 #else
   590 _ucdcmp_unload()
   591 #endif
   592 {
   593     if (_ucdcmp_size == 0)
   594       return;
   596     /*
   597      * Only need to free the offsets because the memory is allocated as a
   598      * single block.
   599      */
   600     free((char *) _ucdcmp_nodes);
   601     _ucdcmp_size = 0;
   602 }
   604 int
   605 #ifdef __STDC__
   606 ucdecomp(unsigned long code, unsigned long *num, unsigned long **decomp)
   607 #else
   608 ucdecomp(code, num, decomp)
   609 unsigned long code, *num, **decomp;
   610 #endif
   611 {
   612     long l, r, m;
   614     l = 0;
   615     r = _ucdcmp_nodes[_ucdcmp_size] - 1;
   617     while (l <= r) {
   618         /*
   619          * Determine a "mid" point and adjust to make sure the mid point is at
   620          * the beginning of a code+offset pair.
   621          */
   622         m = (l + r) >> 1;
   623         m -= (m & 1);
   624         if (code > _ucdcmp_nodes[m])
   625           l = m + 2;
   626         else if (code < _ucdcmp_nodes[m])
   627           r = m - 2;
   628         else if (code == _ucdcmp_nodes[m]) {
   629             *num = _ucdcmp_nodes[m + 3] - _ucdcmp_nodes[m + 1];
   630             *decomp = &_ucdcmp_decomp[_ucdcmp_nodes[m + 1]];
   631             return 1;
   632         }
   633     }
   634     return 0;
   635 }
   637 int
   638 #ifdef __STDC__
   639 ucdecomp_hangul(unsigned long code, unsigned long *num, unsigned long decomp[])
   640 #else
   641 ucdecomp_hangul(code, num, decomp)
   642 unsigned long code, *num, decomp[];
   643 #endif
   644 {
   645     if (!ucishangul(code))
   646       return 0;
   648     code -= 0xac00;
   649     decomp[0] = 0x1100 + (unsigned long) (code / 588);
   650     decomp[1] = 0x1161 + (unsigned long) ((code % 588) / 28);
   651     decomp[2] = 0x11a7 + (unsigned long) (code % 28);
   652     *num = (decomp[2] != 0x11a7) ? 3 : 2;
   654     return 1;
   655 }
   657 /**************************************************************************
   658  *
   659  * Support for combining classes.
   660  *
   661  **************************************************************************/
   663 static unsigned long  _uccmcl_size;
   664 static unsigned long *_uccmcl_nodes;
   666 static void
   667 #ifdef __STDC__
   668 _uccmcl_load(char *paths, int reload)
   669 #else
   670 _uccmcl_load(paths, reload)
   671 char *paths;
   672 int reload;
   673 #endif
   674 {
   675     FILE *in;
   676     unsigned long i;
   677     _ucheader_t hdr;
   679     if (_uccmcl_size > 0) {
   680         if (!reload)
   681           /*
   682            * The combining classes have already been loaded.
   683            */
   684           return;
   686         free((char *) _uccmcl_nodes);
   687         _uccmcl_size = 0;
   688     }
   690     if ((in = _ucopenfile(paths, "cmbcl.dat", "rb")) == 0)
   691       return;
   693     /*
   694      * Load the header.
   695      */
   696     fread((char *) &hdr, sizeof(_ucheader_t), 1, in);
   698     if (hdr.bom == 0xfffe) {
   699         hdr.cnt = endian_short(hdr.cnt);
   700         hdr.size.bytes = endian_long(hdr.size.bytes);
   701     }
   703     _uccmcl_size = hdr.cnt * 3;
   704     _uccmcl_nodes = (unsigned long *) malloc(hdr.size.bytes);
   706     /*
   707      * Read the combining classes in.
   708      */
   709     fread((char *) _uccmcl_nodes, sizeof(unsigned long), _uccmcl_size, in);
   711     /*
   712      * Do an endian swap if necessary.
   713      */
   714     if (hdr.bom == 0xfffe) {
   715         for (i = 0; i < _uccmcl_size; i++)
   716           _uccmcl_nodes[i] = endian_long(_uccmcl_nodes[i]);
   717     }        
   718 }
   720 static void
   721 #ifdef __STDC__
   722 _uccmcl_unload(void)
   723 #else
   724 _uccmcl_unload()
   725 #endif
   726 {
   727     if (_uccmcl_size == 0)
   728       return;
   730     free((char *) _uccmcl_nodes);
   731     _uccmcl_size = 0;
   732 }
   734 unsigned long
   735 #ifdef __STDC__
   736 uccombining_class(unsigned long code)
   737 #else
   738 uccombining_class(code)
   739 unsigned long code;
   740 #endif
   741 {
   742     long l, r, m;
   744     l = 0;
   745     r = _uccmcl_size - 1;
   747     while (l <= r) {
   748         m = (l + r) >> 1;
   749         m -= (m % 3);
   750         if (code > _uccmcl_nodes[m + 1])
   751           l = m + 3;
   752         else if (code < _uccmcl_nodes[m])
   753           r = m - 3;
   754         else if (code >= _uccmcl_nodes[m] && code <= _uccmcl_nodes[m + 1])
   755           return _uccmcl_nodes[m + 2];
   756     }
   757     return 0;
   758 }
   760 /**************************************************************************
   761  *
   762  * Support for numeric values.
   763  *
   764  **************************************************************************/
   766 static unsigned long *_ucnum_nodes;
   767 static unsigned long _ucnum_size;
   768 static short *_ucnum_vals;
   770 static void
   771 #ifdef __STDC__
   772 _ucnumb_load(char *paths, int reload)
   773 #else
   774 _ucnumb_load(paths, reload)
   775 char *paths;
   776 int reload;
   777 #endif
   778 {
   779     FILE *in;
   780     unsigned long size, i;
   781     _ucheader_t hdr;
   783     if (_ucnum_size > 0) {
   784         if (!reload)
   785           /*
   786            * The numbers have already been loaded.
   787            */
   788           return;
   790         free((char *) _ucnum_nodes);
   791         _ucnum_size = 0;
   792     }
   794     if ((in = _ucopenfile(paths, "num.dat", "rb")) == 0)
   795       return;
   797     /*
   798      * Load the header.
   799      */
   800     fread((char *) &hdr, sizeof(_ucheader_t), 1, in);
   802     if (hdr.bom == 0xfffe) {
   803         hdr.cnt = endian_short(hdr.cnt);
   804         hdr.size.bytes = endian_long(hdr.size.bytes);
   805     }
   807     _ucnum_size = hdr.cnt;
   808     _ucnum_nodes = (unsigned long *) malloc(hdr.size.bytes);
   809     _ucnum_vals = (short *) (_ucnum_nodes + _ucnum_size);
   811     /*
   812      * Read the combining classes in.
   813      */
   814     fread((char *) _ucnum_nodes, sizeof(unsigned char), hdr.size.bytes, in);
   816     /*
   817      * Do an endian swap if necessary.
   818      */
   819     if (hdr.bom == 0xfffe) {
   820         for (i = 0; i < _ucnum_size; i++)
   821           _ucnum_nodes[i] = endian_long(_ucnum_nodes[i]);
   823         /*
   824          * Determine the number of values that have to be adjusted.
   825          */
   826         size = (hdr.size.bytes -
   827                 (_ucnum_size * (sizeof(unsigned long) << 1))) /
   828             sizeof(short);
   830         for (i = 0; i < size; i++)
   831           _ucnum_vals[i] = endian_short(_ucnum_vals[i]);
   832     }        
   833 }
   835 static void
   836 #ifdef __STDC__
   837 _ucnumb_unload(void)
   838 #else
   839 _ucnumb_unload()
   840 #endif
   841 {
   842     if (_ucnum_size == 0)
   843       return;
   845     free((char *) _ucnum_nodes);
   846     _ucnum_size = 0;
   847 }
   849 int
   850 #ifdef __STDC__
   851 ucnumber_lookup(unsigned long code, struct ucnumber *num)
   852 #else
   853 ucnumber_lookup(code, num)
   854 unsigned long code;
   855 struct ucnumber *num;
   856 #endif
   857 {
   858     long l, r, m;
   859     short *vp;
   861     l = 0;
   862     r = _ucnum_size - 1;
   863     while (l <= r) {
   864         /*
   865          * Determine a "mid" point and adjust to make sure the mid point is at
   866          * the beginning of a code+offset pair.
   867          */
   868         m = (l + r) >> 1;
   869         m -= (m & 1);
   870         if (code > _ucnum_nodes[m])
   871           l = m + 2;
   872         else if (code < _ucnum_nodes[m])
   873           r = m - 2;
   874         else {
   875             vp = _ucnum_vals + _ucnum_nodes[m + 1];
   876             num->numerator = (int) *vp++;
   877             num->denominator = (int) *vp;
   878             return 1;
   879         }
   880     }
   881     return 0;
   882 }
   884 int
   885 #ifdef __STDC__
   886 ucdigit_lookup(unsigned long code, int *digit)
   887 #else
   888 ucdigit_lookup(code, digit)
   889 unsigned long code;
   890 int *digit;
   891 #endif
   892 {
   893     long l, r, m;
   894     short *vp;
   896     l = 0;
   897     r = _ucnum_size - 1;
   898     while (l <= r) {
   899         /*
   900          * Determine a "mid" point and adjust to make sure the mid point is at
   901          * the beginning of a code+offset pair.
   902          */
   903         m = (l + r) >> 1;
   904         m -= (m & 1);
   905         if (code > _ucnum_nodes[m])
   906           l = m + 2;
   907         else if (code < _ucnum_nodes[m])
   908           r = m - 2;
   909         else {
   910             vp = _ucnum_vals + _ucnum_nodes[m + 1];
   911             if (*vp == *(vp + 1)) {
   912               *digit = *vp;
   913               return 1;
   914             }
   915             return 0;
   916         }
   917     }
   918     return 0;
   919 }
   921 struct ucnumber
   922 #ifdef __STDC__
   923 ucgetnumber(unsigned long code)
   924 #else
   925 ucgetnumber(code)
   926 unsigned long code;
   927 #endif
   928 {
   929     struct ucnumber num;
   931     /*
   932      * Initialize with some arbitrary value, because the caller simply cannot
   933      * tell for sure if the code is a number without calling the ucisnumber()
   934      * macro before calling this function.
   935      */
   936     num.numerator = num.denominator = -111;
   938     (void) ucnumber_lookup(code, &num);
   940     return num;
   941 }
   943 int
   944 #ifdef __STDC__
   945 ucgetdigit(unsigned long code)
   946 #else
   947 ucgetdigit(code)
   948 unsigned long code;
   949 #endif
   950 {
   951     int dig;
   953     /*
   954      * Initialize with some arbitrary value, because the caller simply cannot
   955      * tell for sure if the code is a number without calling the ucisdigit()
   956      * macro before calling this function.
   957      */
   958     dig = -111;
   960     (void) ucdigit_lookup(code, &dig);
   962     return dig;
   963 }
   965 /**************************************************************************
   966  *
   967  * Setup and cleanup routines.
   968  *
   969  **************************************************************************/
   971 void
   972 #ifdef __STDC__
   973 ucdata_load(char *paths, int masks)
   974 #else
   975 ucdata_load(paths, masks)
   976 char *paths;
   977 int masks;
   978 #endif
   979 {
   980     if (masks & UCDATA_CTYPE)
   981       _ucprop_load(paths, 0);
   982     if (masks & UCDATA_CASE)
   983       _uccase_load(paths, 0);
   984     if (masks & UCDATA_DECOMP)
   985       _ucdcmp_load(paths, 0);
   986     if (masks & UCDATA_CMBCL)
   987       _uccmcl_load(paths, 0);
   988     if (masks & UCDATA_NUM)
   989       _ucnumb_load(paths, 0);
   990 }
   992 void
   993 #ifdef __STDC__
   994 ucdata_unload(int masks)
   995 #else
   996 ucdata_unload(masks)
   997 int masks;
   998 #endif
   999 {
  1000     if (masks & UCDATA_CTYPE)
  1001       _ucprop_unload();
  1002     if (masks & UCDATA_CASE)
  1003       _uccase_unload();
  1004     if (masks & UCDATA_DECOMP)
  1005       _ucdcmp_unload();
  1006     if (masks & UCDATA_CMBCL)
  1007       _uccmcl_unload();
  1008     if (masks & UCDATA_NUM)
  1009       _ucnumb_unload();
  1012 void
  1013 #ifdef __STDC__
  1014 ucdata_reload(char *paths, int masks)
  1015 #else
  1016 ucdata_reload(paths, masks)
  1017 char *paths;
  1018 int masks;
  1019 #endif
  1021     if (masks & UCDATA_CTYPE)
  1022       _ucprop_load(paths, 1);
  1023     if (masks & UCDATA_CASE)
  1024       _uccase_load(paths, 1);
  1025     if (masks & UCDATA_DECOMP)
  1026       _ucdcmp_load(paths, 1);
  1027     if (masks & UCDATA_CMBCL)
  1028       _uccmcl_load(paths, 1);
  1029     if (masks & UCDATA_NUM)
  1030       _ucnumb_load(paths, 1);
  1033 #ifdef TEST
  1035 void
  1036 #ifdef __STDC__
  1037 main(void)
  1038 #else
  1039 main()
  1040 #endif
  1042     int dig;
  1043     unsigned long i, lo, *dec;
  1044     struct ucnumber num;
  1046     ucdata_setup(".");
  1048     if (ucisweak(0x30))
  1049       printf("WEAK\n");
  1050     else
  1051       printf("NOT WEAK\n");
  1053     printf("LOWER 0x%04lX\n", uctolower(0xff3a));
  1054     printf("UPPER 0x%04lX\n", uctoupper(0xff5a));
  1056     if (ucisalpha(0x1d5))
  1057       printf("ALPHA\n");
  1058     else
  1059       printf("NOT ALPHA\n");
  1061     if (ucisupper(0x1d5)) {
  1062         printf("UPPER\n");
  1063         lo = uctolower(0x1d5);
  1064         printf("0x%04lx\n", lo);
  1065         lo = uctotitle(0x1d5);
  1066         printf("0x%04lx\n", lo);
  1067     } else
  1068       printf("NOT UPPER\n");
  1070     if (ucistitle(0x1d5))
  1071       printf("TITLE\n");
  1072     else
  1073       printf("NOT TITLE\n");
  1075     if (uciscomposite(0x1d5))
  1076       printf("COMPOSITE\n");
  1077     else
  1078       printf("NOT COMPOSITE\n");
  1080     if (ucdecomp(0x1d5, &lo, &dec)) {
  1081         for (i = 0; i < lo; i++)
  1082           printf("0x%04lx ", dec[i]);
  1083         putchar('\n');
  1086     if ((lo = uccombining_class(0x41)) != 0)
  1087       printf("0x41 CCL %ld\n", lo);
  1089     if (ucisxdigit(0xfeff))
  1090       printf("0xFEFF HEX DIGIT\n");
  1091     else
  1092       printf("0xFEFF NOT HEX DIGIT\n");
  1094     if (ucisdefined(0x10000))
  1095       printf("0x10000 DEFINED\n");
  1096     else
  1097       printf("0x10000 NOT DEFINED\n");
  1099     if (ucnumber_lookup(0x30, &num)) {
  1100         if (num.numerator != num.denominator)
  1101           printf("UCNUMBER: 0x30 = %d/%d\n", num.numerator, num.denominator);
  1102         else
  1103           printf("UCNUMBER: 0x30 = %d\n", num.numerator);
  1104     } else
  1105       printf("UCNUMBER: 0x30 NOT A NUMBER\n");
  1107     if (ucnumber_lookup(0xbc, &num)) {
  1108         if (num.numerator != num.denominator)
  1109           printf("UCNUMBER: 0xbc = %d/%d\n", num.numerator, num.denominator);
  1110         else
  1111           printf("UCNUMBER: 0xbc = %d\n", num.numerator);
  1112     } else
  1113       printf("UCNUMBER: 0xbc NOT A NUMBER\n");
  1116     if (ucnumber_lookup(0xff19, &num)) {
  1117         if (num.numerator != num.denominator)
  1118           printf("UCNUMBER: 0xff19 = %d/%d\n", num.numerator, num.denominator);
  1119         else
  1120           printf("UCNUMBER: 0xff19 = %d\n", num.numerator);
  1121     } else
  1122       printf("UCNUMBER: 0xff19 NOT A NUMBER\n");
  1124     if (ucnumber_lookup(0x4e00, &num)) {
  1125         if (num.numerator != num.denominator)
  1126           printf("UCNUMBER: 0x4e00 = %d/%d\n", num.numerator, num.denominator);
  1127         else
  1128           printf("UCNUMBER: 0x4e00 = %d\n", num.numerator);
  1129     } else
  1130       printf("UCNUMBER: 0x4e00 NOT A NUMBER\n");
  1132     if (ucdigit_lookup(0x06f9, &dig))
  1133       printf("UCDIGIT: 0x6f9 = %d\n", dig);
  1134     else
  1135       printf("UCDIGIT: 0x6f9 NOT A NUMBER\n");
  1137     dig = ucgetdigit(0x0969);
  1138     printf("UCGETDIGIT: 0x969 = %d\n", dig);
  1140     num = ucgetnumber(0x30);
  1141     if (num.numerator != num.denominator)
  1142       printf("UCGETNUMBER: 0x30 = %d/%d\n", num.numerator, num.denominator);
  1143     else
  1144       printf("UCGETNUMBER: 0x30 = %d\n", num.numerator);
  1146     num = ucgetnumber(0xbc);
  1147     if (num.numerator != num.denominator)
  1148       printf("UCGETNUMBER: 0xbc = %d/%d\n", num.numerator, num.denominator);
  1149     else
  1150       printf("UCGETNUMBER: 0xbc = %d\n", num.numerator);
  1152     num = ucgetnumber(0xff19);
  1153     if (num.numerator != num.denominator)
  1154       printf("UCGETNUMBER: 0xff19 = %d/%d\n", num.numerator, num.denominator);
  1155     else
  1156       printf("UCGETNUMBER: 0xff19 = %d\n", num.numerator);
  1158     ucdata_cleanup();
  1159     exit(0);
  1162 #endif /* TEST */

mercurial