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