Sync usage with man page.
[netbsd-mini2440.git] / dist / ntp / libopts / streqvcmp.c
blobcc962e46108f8aa2350b55909b838cf7548904da
1 /* $NetBSD$ */
4 /*
5 * Id: streqvcmp.c,v 4.10 2007/04/28 22:19:23 bkorb Exp
6 * Time-stamp: "2006-07-26 18:25:53 bkorb"
8 * String Equivalence Comparison
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).
17 * Automated Options copyright 1992-2007 Bruce Korb
19 * Automated Options is free software.
20 * You may redistribute it and/or modify it under the terms of the
21 * GNU General Public License, as published by the Free Software
22 * Foundation; either version 2, or (at your option) any later version.
24 * Automated Options is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with Automated Options. See the file "COPYING". If not,
31 * write to: The Free Software Foundation, Inc.,
32 * 51 Franklin Street, Fifth Floor,
33 * Boston, MA 02110-1301, USA.
35 * As a special exception, Bruce Korb gives permission for additional
36 * uses of the text contained in his release of AutoOpts.
38 * The exception is that, if you link the AutoOpts library with other
39 * files to produce an executable, this does not by itself cause the
40 * resulting executable to be covered by the GNU General Public License.
41 * Your use of that executable is in no way restricted on account of
42 * linking the AutoOpts library code into it.
44 * This exception does not however invalidate any other reasons why
45 * the executable file might be covered by the GNU General Public License.
47 * This exception applies only to the code released by Bruce Korb under
48 * the name AutoOpts. If you copy code from other sources under the
49 * General Public License into a copy of AutoOpts, as the General Public
50 * License permits, the exception does not apply to the code that you add
51 * in this way. To avoid misleading anyone as to the status of such
52 * modified files, you must delete this exception notice from them.
54 * If you write modifications of your own for AutoOpts, it is your choice
55 * whether to permit this exception to apply to your modifications.
56 * If you do not wish that, delete this exception notice.
60 * This array is designed for mapping upper and lower case letter
61 * together for a case independent comparison. The mappings are
62 * based upon ascii character sequences.
64 static unsigned char charmap[] = {
65 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, '\a',
66 '\b', '\t', '\n', '\v', '\f', '\r', 0x0E, 0x0F,
67 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
68 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F,
70 ' ', '!', '"', '#', '$', '%', '&', '\'',
71 '(', ')', '*', '+', ',', '-', '.', '/',
72 '0', '1', '2', '3', '4', '5', '6', '7',
73 '8', '9', ':', ';', '<', '=', '>', '?',
75 '@', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
76 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
77 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
78 'x', 'y', 'z', '[', '\\', ']', '^', '_',
79 '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
80 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
81 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
82 'x', 'y', 'z', '{', '|', '}', '~', 0x7f,
84 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
85 0x88, 0x89, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E, 0x8F,
86 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
87 0x98, 0x99, 0x9A, 0x9B, 0x9C, 0x9D, 0x9E, 0x9F,
88 0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7,
89 0xA8, 0xA9, 0xAA, 0xAB, 0xAC, 0xAD, 0xAE, 0xAF,
90 0xB0, 0xB1, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7,
91 0xB8, 0xB9, 0xBA, 0xBB, 0xBC, 0xBD, 0xBE, 0xBF,
93 0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7,
94 0xC8, 0xC9, 0xCA, 0xCB, 0xCC, 0xCD, 0xCE, 0xCF,
95 0xD0, 0xD1, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7,
96 0xD8, 0xD9, 0xDA, 0xDB, 0xDC, 0xDD, 0xDE, 0xDF,
97 0xE0, 0xE1, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7,
98 0xE8, 0xE9, 0xEA, 0xEB, 0xEC, 0xED, 0xEE, 0xEF,
99 0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7,
100 0xF8, 0xF9, 0xFA, 0xFB, 0xFC, 0xFD, 0xFE, 0xFF,
104 /*=export_func strneqvcmp
106 * what: compare two strings with an equivalence mapping
108 * arg: + char const* + str1 + first string +
109 * arg: + char const* + str2 + second string +
110 * arg: + int + ct + compare length +
112 * ret_type: int
113 * ret_desc: the difference between two differing characters
115 * doc:
117 * Using a character mapping, two strings are compared for "equivalence".
118 * Each input character is mapped to a comparison character and the
119 * mapped-to characters are compared for the two NUL terminated input strings.
120 * The comparison is limited to @code{ct} bytes.
121 * This function name is mapped to option_strneqvcmp so as to not conflict
122 * with the POSIX name space.
124 * err: none checked. Caller responsible for seg faults.
127 strneqvcmp( tCC* s1, tCC* s2, int ct )
129 for (; ct > 0; --ct) {
130 unsigned char u1 = (unsigned char) *s1++;
131 unsigned char u2 = (unsigned char) *s2++;
132 int dif = charmap[ u1 ] - charmap[ u2 ];
134 if (dif != 0)
135 return dif;
137 if (u1 == NUL)
138 return 0;
141 return 0;
145 /*=export_func streqvcmp
147 * what: compare two strings with an equivalence mapping
149 * arg: + char const* + str1 + first string +
150 * arg: + char const* + str2 + second string +
152 * ret_type: int
153 * ret_desc: the difference between two differing characters
155 * doc:
157 * Using a character mapping, two strings are compared for "equivalence".
158 * Each input character is mapped to a comparison character and the
159 * mapped-to characters are compared for the two NUL terminated input strings.
160 * This function name is mapped to option_streqvcmp so as to not conflict
161 * with the POSIX name space.
163 * err: none checked. Caller responsible for seg faults.
166 streqvcmp( tCC* s1, tCC* s2 )
168 for (;;) {
169 unsigned char u1 = (unsigned char) *s1++;
170 unsigned char u2 = (unsigned char) *s2++;
171 int dif = charmap[ u1 ] - charmap[ u2 ];
173 if (dif != 0)
174 return dif;
176 if (u1 == NUL)
177 return 0;
182 /*=export_func streqvmap
184 * what: Set the character mappings for the streqv functions
186 * arg: + char + From + Input character +
187 * arg: + char + To + Mapped-to character +
188 * arg: + int + ct + compare length +
190 * doc:
192 * Set the character mapping. If the count (@code{ct}) is set to zero, then
193 * the map is cleared by setting all entries in the map to their index
194 * value. Otherwise, the "@code{From}" character is mapped to the "@code{To}"
195 * character. If @code{ct} is greater than 1, then @code{From} and @code{To}
196 * are incremented and the process repeated until @code{ct} entries have been
197 * set. For example,
198 * @example
199 * streqvmap( 'a', 'A', 26 );
200 * @end example
201 * @noindent
202 * will alter the mapping so that all English lower case letters
203 * will map to upper case.
205 * This function name is mapped to option_streqvmap so as to not conflict
206 * with the POSIX name space.
208 * err: none.
210 void
211 streqvmap( char From, char To, int ct )
213 if (ct == 0) {
214 ct = sizeof( charmap ) - 1;
215 do {
216 charmap[ ct ] = ct;
217 } while (--ct >= 0);
220 else {
221 int chTo = (int)To & 0xFF;
222 int chFrom = (int)From & 0xFF;
224 do {
225 charmap[ chFrom ] = (unsigned)chTo;
226 chFrom++;
227 chTo++;
228 if ((chFrom >= sizeof( charmap )) || (chTo >= sizeof( charmap )))
229 break;
230 } while (--ct > 0);
235 /*=export_func strequate
237 * what: map a list of characters to the same value
239 * arg: + char const* + ch_list + characters to equivalence +
241 * doc:
243 * Each character in the input string get mapped to the first character
244 * in the string.
245 * This function name is mapped to option_strequate so as to not conflict
246 * with the POSIX name space.
248 * err: none.
250 void
251 strequate( char const* s )
253 if ((s != NULL) && (*s != NUL)) {
254 unsigned char equiv = (unsigned)*s;
255 while (*s != NUL)
256 charmap[ (unsigned)*(s++) ] = equiv;
261 /*=export_func strtransform
263 * what: convert a string into its mapped-to value
265 * arg: + char* + dest + output string +
266 * arg: + char const* + src + input string +
268 * doc:
270 * Each character in the input string is mapped and the mapped-to
271 * character is put into the output.
272 * This function name is mapped to option_strtransform so as to not conflict
273 * with the POSIX name space.
275 * err: none.
277 void
278 strtransform( char* d, char const* s )
280 do {
281 *(d++) = (char)charmap[ (unsigned)*s ];
282 } while (*(s++) != NUL);
286 * Local Variables:
287 * mode: C
288 * c-file-style: "stroustrup"
289 * indent-tabs-mode: nil
290 * End:
291 * end of autoopts/streqvcmp.c */