Home | History | Annotate | Line # | Download | only in libopts
streqvcmp.c revision 1.1
      1  1.1  kardel /*	$NetBSD: streqvcmp.c,v 1.1 2009/12/13 16:57:22 kardel Exp $	*/
      2  1.1  kardel 
      3  1.1  kardel 
      4  1.1  kardel /*
      5  1.1  kardel  *  Id: cb437d22b0c48960c9e1c23501fba6e3291fecd8
      6  1.1  kardel  * Time-stamp:      "2008-12-26 10:15:46 bkorb"
      7  1.1  kardel  *
      8  1.1  kardel  *  String Equivalence Comparison
      9  1.1  kardel  *
     10  1.1  kardel  *  These routines allow any character to be mapped to any other
     11  1.1  kardel  *  character before comparison.  In processing long option names,
     12  1.1  kardel  *  the characters "-", "_" and "^" all need to be equivalent
     13  1.1  kardel  *  (because they are treated so by different development environments).
     14  1.1  kardel  *
     15  1.1  kardel  *  This file is part of AutoOpts, a companion to AutoGen.
     16  1.1  kardel  *  AutoOpts is free software.
     17  1.1  kardel  *  AutoOpts is copyright (c) 1992-2009 by Bruce Korb - all rights reserved
     18  1.1  kardel  *
     19  1.1  kardel  *  AutoOpts is available under any one of two licenses.  The license
     20  1.1  kardel  *  in use must be one of these two and the choice is under the control
     21  1.1  kardel  *  of the user of the license.
     22  1.1  kardel  *
     23  1.1  kardel  *   The GNU Lesser General Public License, version 3 or later
     24  1.1  kardel  *      See the files "COPYING.lgplv3" and "COPYING.gplv3"
     25  1.1  kardel  *
     26  1.1  kardel  *   The Modified Berkeley Software Distribution License
     27  1.1  kardel  *      See the file "COPYING.mbsd"
     28  1.1  kardel  *
     29  1.1  kardel  *  These files have the following md5sums:
     30  1.1  kardel  *
     31  1.1  kardel  *  43b91e8ca915626ed3818ffb1b71248b pkg/libopts/COPYING.gplv3
     32  1.1  kardel  *  06a1a2e4760c90ea5e1dad8dfaac4d39 pkg/libopts/COPYING.lgplv3
     33  1.1  kardel  *  66a5cedaf62c4b2637025f049f9b826f pkg/libopts/COPYING.mbsd
     34  1.1  kardel  *
     35  1.1  kardel  * This array is designed for mapping upper and lower case letter
     36  1.1  kardel  * together for a case independent comparison.  The mappings are
     37  1.1  kardel  * based upon ascii character sequences.
     38  1.1  kardel  */
     39  1.1  kardel static unsigned char charmap[] = {
     40  1.1  kardel     0x00, 0x01, 0x02, 0x03,  0x04, 0x05, 0x06, '\a',
     41  1.1  kardel     '\b', '\t', '\n', '\v',  '\f', '\r', 0x0E, 0x0F,
     42  1.1  kardel     0x10, 0x11, 0x12, 0x13,  0x14, 0x15, 0x16, 0x17,
     43  1.1  kardel     0x18, 0x19, 0x1A, 0x1B,  0x1C, 0x1D, 0x1E, 0x1F,
     44  1.1  kardel 
     45  1.1  kardel     ' ',  '!',  '"',  '#',   '$',  '%',  '&',  '\'',
     46  1.1  kardel     '(',  ')',  '*',  '+',   ',',  '-',  '.',  '/',
     47  1.1  kardel     '0',  '1',  '2',  '3',   '4',  '5',  '6',  '7',
     48  1.1  kardel     '8',  '9',  ':',  ';',   '<',  '=',  '>',  '?',
     49  1.1  kardel 
     50  1.1  kardel     '@',  'a',  'b',  'c',   'd',  'e',  'f',  'g',
     51  1.1  kardel     'h',  'i',  'j',  'k',   'l',  'm',  'n',  'o',
     52  1.1  kardel     'p',  'q',  'r',  's',   't',  'u',  'v',  'w',
     53  1.1  kardel     'x',  'y',  'z',  '[',   '\\', ']',  '^',  '_',
     54  1.1  kardel     '`',  'a',  'b',  'c',   'd',  'e',  'f',  'g',
     55  1.1  kardel     'h',  'i',  'j',  'k',   'l',  'm',  'n',  'o',
     56  1.1  kardel     'p',  'q',  'r',  's',   't',  'u',  'v',  'w',
     57  1.1  kardel     'x',  'y',  'z',  '{',   '|',  '}',  '~',  0x7f,
     58  1.1  kardel 
     59  1.1  kardel     0x80, 0x81, 0x82, 0x83,  0x84, 0x85, 0x86, 0x87,
     60  1.1  kardel     0x88, 0x89, 0x8A, 0x8B,  0x8C, 0x8D, 0x8E, 0x8F,
     61  1.1  kardel     0x90, 0x91, 0x92, 0x93,  0x94, 0x95, 0x96, 0x97,
     62  1.1  kardel     0x98, 0x99, 0x9A, 0x9B,  0x9C, 0x9D, 0x9E, 0x9F,
     63  1.1  kardel     0xA0, 0xA1, 0xA2, 0xA3,  0xA4, 0xA5, 0xA6, 0xA7,
     64  1.1  kardel     0xA8, 0xA9, 0xAA, 0xAB,  0xAC, 0xAD, 0xAE, 0xAF,
     65  1.1  kardel     0xB0, 0xB1, 0xB2, 0xB3,  0xB4, 0xB5, 0xB6, 0xB7,
     66  1.1  kardel     0xB8, 0xB9, 0xBA, 0xBB,  0xBC, 0xBD, 0xBE, 0xBF,
     67  1.1  kardel 
     68  1.1  kardel     0xC0, 0xC1, 0xC2, 0xC3,  0xC4, 0xC5, 0xC6, 0xC7,
     69  1.1  kardel     0xC8, 0xC9, 0xCA, 0xCB,  0xCC, 0xCD, 0xCE, 0xCF,
     70  1.1  kardel     0xD0, 0xD1, 0xD2, 0xD3,  0xD4, 0xD5, 0xD6, 0xD7,
     71  1.1  kardel     0xD8, 0xD9, 0xDA, 0xDB,  0xDC, 0xDD, 0xDE, 0xDF,
     72  1.1  kardel     0xE0, 0xE1, 0xE2, 0xE3,  0xE4, 0xE5, 0xE6, 0xE7,
     73  1.1  kardel     0xE8, 0xE9, 0xEA, 0xEB,  0xEC, 0xED, 0xEE, 0xEF,
     74  1.1  kardel     0xF0, 0xF1, 0xF2, 0xF3,  0xF4, 0xF5, 0xF6, 0xF7,
     75  1.1  kardel     0xF8, 0xF9, 0xFA, 0xFB,  0xFC, 0xFD, 0xFE, 0xFF,
     76  1.1  kardel };
     77  1.1  kardel 
     78  1.1  kardel 
     79  1.1  kardel /*=export_func strneqvcmp
     80  1.1  kardel  *
     81  1.1  kardel  * what: compare two strings with an equivalence mapping
     82  1.1  kardel  *
     83  1.1  kardel  * arg:  + char const* + str1 + first string +
     84  1.1  kardel  * arg:  + char const* + str2 + second string +
     85  1.1  kardel  * arg:  + int         + ct   + compare length +
     86  1.1  kardel  *
     87  1.1  kardel  * ret_type:  int
     88  1.1  kardel  * ret_desc:  the difference between two differing characters
     89  1.1  kardel  *
     90  1.1  kardel  * doc:
     91  1.1  kardel  *
     92  1.1  kardel  * Using a character mapping, two strings are compared for "equivalence".
     93  1.1  kardel  * Each input character is mapped to a comparison character and the
     94  1.1  kardel  * mapped-to characters are compared for the two NUL terminated input strings.
     95  1.1  kardel  * The comparison is limited to @code{ct} bytes.
     96  1.1  kardel  * This function name is mapped to option_strneqvcmp so as to not conflict
     97  1.1  kardel  * with the POSIX name space.
     98  1.1  kardel  *
     99  1.1  kardel  * err:  none checked.  Caller responsible for seg faults.
    100  1.1  kardel =*/
    101  1.1  kardel int
    102  1.1  kardel strneqvcmp( tCC* s1, tCC* s2, int ct )
    103  1.1  kardel {
    104  1.1  kardel     for (; ct > 0; --ct) {
    105  1.1  kardel         unsigned char u1 = (unsigned char) *s1++;
    106  1.1  kardel         unsigned char u2 = (unsigned char) *s2++;
    107  1.1  kardel         int dif = charmap[ u1 ] - charmap[ u2 ];
    108  1.1  kardel 
    109  1.1  kardel         if (dif != 0)
    110  1.1  kardel             return dif;
    111  1.1  kardel 
    112  1.1  kardel         if (u1 == NUL)
    113  1.1  kardel             return 0;
    114  1.1  kardel     }
    115  1.1  kardel 
    116  1.1  kardel     return 0;
    117  1.1  kardel }
    118  1.1  kardel 
    119  1.1  kardel 
    120  1.1  kardel /*=export_func streqvcmp
    121  1.1  kardel  *
    122  1.1  kardel  * what: compare two strings with an equivalence mapping
    123  1.1  kardel  *
    124  1.1  kardel  * arg:  + char const* + str1 + first string +
    125  1.1  kardel  * arg:  + char const* + str2 + second string +
    126  1.1  kardel  *
    127  1.1  kardel  * ret_type:  int
    128  1.1  kardel  * ret_desc:  the difference between two differing characters
    129  1.1  kardel  *
    130  1.1  kardel  * doc:
    131  1.1  kardel  *
    132  1.1  kardel  * Using a character mapping, two strings are compared for "equivalence".
    133  1.1  kardel  * Each input character is mapped to a comparison character and the
    134  1.1  kardel  * mapped-to characters are compared for the two NUL terminated input strings.
    135  1.1  kardel  * This function name is mapped to option_streqvcmp so as to not conflict
    136  1.1  kardel  * with the POSIX name space.
    137  1.1  kardel  *
    138  1.1  kardel  * err:  none checked.  Caller responsible for seg faults.
    139  1.1  kardel =*/
    140  1.1  kardel int
    141  1.1  kardel streqvcmp( tCC* s1, tCC* s2 )
    142  1.1  kardel {
    143  1.1  kardel     for (;;) {
    144  1.1  kardel         unsigned char u1 = (unsigned char) *s1++;
    145  1.1  kardel         unsigned char u2 = (unsigned char) *s2++;
    146  1.1  kardel         int dif = charmap[ u1 ] - charmap[ u2 ];
    147  1.1  kardel 
    148  1.1  kardel         if (dif != 0)
    149  1.1  kardel             return dif;
    150  1.1  kardel 
    151  1.1  kardel         if (u1 == NUL)
    152  1.1  kardel             return 0;
    153  1.1  kardel     }
    154  1.1  kardel }
    155  1.1  kardel 
    156  1.1  kardel 
    157  1.1  kardel /*=export_func streqvmap
    158  1.1  kardel  *
    159  1.1  kardel  * what: Set the character mappings for the streqv functions
    160  1.1  kardel  *
    161  1.1  kardel  * arg:  + char + From + Input character +
    162  1.1  kardel  * arg:  + char + To   + Mapped-to character +
    163  1.1  kardel  * arg:  + int  + ct   + compare length +
    164  1.1  kardel  *
    165  1.1  kardel  * doc:
    166  1.1  kardel  *
    167  1.1  kardel  * Set the character mapping.  If the count (@code{ct}) is set to zero, then
    168  1.1  kardel  * the map is cleared by setting all entries in the map to their index
    169  1.1  kardel  * value.  Otherwise, the "@code{From}" character is mapped to the "@code{To}"
    170  1.1  kardel  * character.  If @code{ct} is greater than 1, then @code{From} and @code{To}
    171  1.1  kardel  * are incremented and the process repeated until @code{ct} entries have been
    172  1.1  kardel  * set. For example,
    173  1.1  kardel  * @example
    174  1.1  kardel  *    streqvmap( 'a', 'A', 26 );
    175  1.1  kardel  * @end example
    176  1.1  kardel  * @noindent
    177  1.1  kardel  * will alter the mapping so that all English lower case letters
    178  1.1  kardel  * will map to upper case.
    179  1.1  kardel  *
    180  1.1  kardel  * This function name is mapped to option_streqvmap so as to not conflict
    181  1.1  kardel  * with the POSIX name space.
    182  1.1  kardel  *
    183  1.1  kardel  * err:  none.
    184  1.1  kardel =*/
    185  1.1  kardel void
    186  1.1  kardel streqvmap( char From, char To, int ct )
    187  1.1  kardel {
    188  1.1  kardel     if (ct == 0) {
    189  1.1  kardel         ct = sizeof( charmap ) - 1;
    190  1.1  kardel         do  {
    191  1.1  kardel             charmap[ ct ] = ct;
    192  1.1  kardel         } while (--ct >= 0);
    193  1.1  kardel     }
    194  1.1  kardel 
    195  1.1  kardel     else {
    196  1.1  kardel         int  chTo   = (int)To   & 0xFF;
    197  1.1  kardel         int  chFrom = (int)From & 0xFF;
    198  1.1  kardel 
    199  1.1  kardel         do  {
    200  1.1  kardel             charmap[ chFrom ] = (unsigned)chTo;
    201  1.1  kardel             chFrom++;
    202  1.1  kardel             chTo++;
    203  1.1  kardel             if ((chFrom >= sizeof( charmap )) || (chTo >= sizeof( charmap )))
    204  1.1  kardel                 break;
    205  1.1  kardel         } while (--ct > 0);
    206  1.1  kardel     }
    207  1.1  kardel }
    208  1.1  kardel 
    209  1.1  kardel 
    210  1.1  kardel /*=export_func strequate
    211  1.1  kardel  *
    212  1.1  kardel  * what: map a list of characters to the same value
    213  1.1  kardel  *
    214  1.1  kardel  * arg:  + char const* + ch_list + characters to equivalence +
    215  1.1  kardel  *
    216  1.1  kardel  * doc:
    217  1.1  kardel  *
    218  1.1  kardel  * Each character in the input string get mapped to the first character
    219  1.1  kardel  * in the string.
    220  1.1  kardel  * This function name is mapped to option_strequate so as to not conflict
    221  1.1  kardel  * with the POSIX name space.
    222  1.1  kardel  *
    223  1.1  kardel  * err:  none.
    224  1.1  kardel =*/
    225  1.1  kardel void
    226  1.1  kardel strequate( char const* s )
    227  1.1  kardel {
    228  1.1  kardel     if ((s != NULL) && (*s != NUL)) {
    229  1.1  kardel         unsigned char equiv = (unsigned)*s;
    230  1.1  kardel         while (*s != NUL)
    231  1.1  kardel             charmap[ (unsigned)*(s++) ] = equiv;
    232  1.1  kardel     }
    233  1.1  kardel }
    234  1.1  kardel 
    235  1.1  kardel 
    236  1.1  kardel /*=export_func strtransform
    237  1.1  kardel  *
    238  1.1  kardel  * what: convert a string into its mapped-to value
    239  1.1  kardel  *
    240  1.1  kardel  * arg:  + char*       + dest + output string +
    241  1.1  kardel  * arg:  + char const* + src  + input string +
    242  1.1  kardel  *
    243  1.1  kardel  * doc:
    244  1.1  kardel  *
    245  1.1  kardel  * Each character in the input string is mapped and the mapped-to
    246  1.1  kardel  * character is put into the output.
    247  1.1  kardel  * This function name is mapped to option_strtransform so as to not conflict
    248  1.1  kardel  * with the POSIX name space.
    249  1.1  kardel  *
    250  1.1  kardel  * The source and destination may be the same.
    251  1.1  kardel  *
    252  1.1  kardel  * err:  none.
    253  1.1  kardel =*/
    254  1.1  kardel void
    255  1.1  kardel strtransform( char* d, char const* s )
    256  1.1  kardel {
    257  1.1  kardel     do  {
    258  1.1  kardel         *(d++) = (char)charmap[ (unsigned)*s ];
    259  1.1  kardel     } while (*(s++) != NUL);
    260  1.1  kardel }
    261  1.1  kardel 
    262  1.1  kardel /*
    263  1.1  kardel  * Local Variables:
    264  1.1  kardel  * mode: C
    265  1.1  kardel  * c-file-style: "stroustrup"
    266  1.1  kardel  * indent-tabs-mode: nil
    267  1.1  kardel  * End:
    268  1.1  kardel  * end of autoopts/streqvcmp.c */
    269