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