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