ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/ermyth/libermyth/util/string.C
Revision: 1.1
Committed: Sat Sep 22 14:27:26 2007 UTC (16 years, 8 months ago) by pippijn
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Log Message:
split up ermyth into ermyth-modules, libermyth (currently just ermyth-util) and ermyth-core

File Contents

# Content
1 #include <util/string.h>
2 #include <util/memory.h>
3
4 char *
5 sstrdup (char const * const s)
6 {
7 char *t;
8 size_t len;
9
10 if (s == NULL)
11 return NULL;
12
13 t = salloc<char> (len = strlen (s) + 1);
14
15 strlcpy (t, s, len);
16 return t;
17 }
18
19 char *
20 sstrndup (char const *s, size_t len)
21 {
22 char *t;
23
24 if (s == NULL)
25 return NULL;
26
27 t = salloc<char> (len + 1);
28
29 strlcpy (t, s, len);
30 return t;
31 }
32
33 void
34 assign (char *dst, char const *src, size_t maxlen)
35 {
36 if (!src)
37 src = "";
38
39 size_t len = strlen (src);
40
41 if (len >= maxlen - 1)
42 {
43 if (maxlen <= 4)
44 {
45 memset (dst, '.', maxlen - 1);
46 dst [maxlen - 1] = 0;
47 }
48 else
49 {
50 memcpy (dst, src, maxlen - 4);
51 memcpy (dst + maxlen - 4, "...", 4);
52 }
53 }
54 else
55 memcpy (dst, src, len + 1);
56 }
57
58 #ifndef HAVE_STRLCAT
59 /* These functions are taken from Linux. */
60 size_t
61 strlcat (char *dest, char const *src, size_t count)
62 {
63 size_t dsize = strlen (dest);
64 size_t len = strlen (src);
65 size_t res = dsize + len;
66
67 dest += dsize;
68 count -= dsize;
69 if (len >= count)
70 len = count - 1;
71 memcpy (dest, src, len);
72 dest[len] = 0;
73 return res;
74 }
75 #endif
76
77 #ifndef HAVE_STRLCPY
78 size_t
79 strlcpy (char *dest, char const *src, size_t size)
80 {
81 size_t ret = strlen (src);
82
83 if (size)
84 {
85 size_t len = (ret >= size) ? size - 1 : ret;
86 memcpy (dest, src, len);
87 dest[len] = '\0';
88 }
89 return ret;
90 }
91 #endif
92
93 /* removes unwanted chars from a line */
94 char const *
95 strip (char *line)
96 {
97 char *c = 0;
98
99 if (line)
100 {
101 if ((c = strchr (line, '\n')))
102 *c = '\0';
103 if ((c = strchr (line, '\r')))
104 *c = '\0';
105 if ((c = strchr (line, '\1')))
106 *c = '\0';
107 }
108
109 return c;
110 }
111
112 match_rule match_mapping = MATCH_RFC1459;
113
114 const unsigned char ToLowerTab[] = {
115 0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa,
116 0xb, 0xc, 0xd, 0xe, 0xf, 0x10, 0x11, 0x12, 0x13, 0x14,
117 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d,
118 0x1e, 0x1f,
119 ' ', '!', '"', '#', '$', '%', '&', 0x27, '(', ')',
120 '*', '+', ',', '-', '.', '/',
121 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
122 ':', ';', '<', '=', '>', '?',
123 '@', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
124 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
125 't', 'u', 'v', 'w', 'x', 'y', 'z', '{', '|', '}', '~',
126 '_',
127 '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
128 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
129 't', 'u', 'v', 'w', 'x', 'y', 'z', '{', '|', '}', '~',
130 0x7f,
131 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
132 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
133 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99,
134 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
135 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9,
136 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
137 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9,
138 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
139 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9,
140 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,
141 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9,
142 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf,
143 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
144 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
145 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9,
146 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff
147 };
148
149 const unsigned char ToUpperTab[] = {
150 0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa,
151 0xb, 0xc, 0xd, 0xe, 0xf, 0x10, 0x11, 0x12, 0x13, 0x14,
152 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d,
153 0x1e, 0x1f,
154 ' ', '!', '"', '#', '$', '%', '&', 0x27, '(', ')',
155 '*', '+', ',', '-', '.', '/',
156 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
157 ':', ';', '<', '=', '>', '?',
158 '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
159 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
160 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '[', '\\', ']', '^',
161 0x5f,
162 '`', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
163 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
164 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '[', '\\', ']', '^',
165 0x7f,
166 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
167 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
168 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99,
169 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
170 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9,
171 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
172 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9,
173 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
174 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9,
175 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,
176 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9,
177 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf,
178 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
179 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
180 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9,
181 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff
182 };
183
184 int
185 ToLower (int c)
186 {
187 if (match_mapping == MATCH_ASCII)
188 return tolower (c);
189 /* else */
190 return (ToLowerTab[(unsigned char) (c)]);
191 }
192
193 int
194 ToUpper (int c)
195 {
196 if (match_mapping == MATCH_ASCII)
197 return toupper (c);
198 /* else */
199 return (ToUpperTab[(unsigned char) (c)]);
200 }
201
202 void
203 set_match_mapping (match_rule type)
204 {
205 match_mapping = type;
206 }
207
208 /*
209 ** Case insensitive comparison of two null terminated strings.
210 **
211 ** returns 0, if s1 equal to s2
212 ** <0, if s1 lexicographically less than s2
213 ** >0, if s1 lexicographically greater than s2
214 */
215 int
216 irccasecmp (char const * const s1, char const * const s2)
217 {
218 const unsigned char *str1 = (const unsigned char *) s1;
219 const unsigned char *str2 = (const unsigned char *) s2;
220 int res;
221
222 if (!s1 || !s2)
223 return -1;
224
225 if (match_mapping == MATCH_ASCII)
226 return strcasecmp (s1, s2);
227
228 while ((res = ToUpper (*str1) - ToUpper (*str2)) == 0)
229 {
230 if (*str1 == '\0')
231 return 0;
232 str1++;
233 str2++;
234 }
235 return (res);
236 }
237
238 int
239 ircncasecmp (char const * const str1, char const * const str2, int n)
240 {
241 const unsigned char *s1 = (const unsigned char *) str1;
242 const unsigned char *s2 = (const unsigned char *) str2;
243 int res;
244
245 if (match_mapping == MATCH_ASCII)
246 return strncasecmp (str1, str2, n);
247
248 while ((res = ToUpper (*s1) - ToUpper (*s2)) == 0)
249 {
250 s1++;
251 s2++;
252 n--;
253 if (n == 0 || (*s1 == '\0' && *s2 == '\0'))
254 return 0;
255 }
256 return (res);
257 }
258
259 const unsigned int charattrs[] = {
260 /* 0 */ 0,
261 /* 1 */ 0,
262 /* 2 */ 0,
263 /* 3 */ 0,
264 /* 4 */ 0,
265 /* 5 */ 0,
266 /* 6 */ 0,
267 /* 7 BEL */ 0,
268 /* 8 \b */ 0,
269 /* 9 \t */ 0,
270 /* 10 \n */ 0,
271 /* 11 \v */ 0,
272 /* 12 \f */ 0,
273 /* 13 \r */ 0,
274 /* 14 */ 0,
275 /* 15 */ 0,
276 /* 16 */ 0,
277 /* 17 */ 0,
278 /* 18 */ 0,
279 /* 19 */ 0,
280 /* 20 */ 0,
281 /* 21 */ 0,
282 /* 22 */ 0,
283 /* 23 */ 0,
284 /* 24 */ 0,
285 /* 25 */ 0,
286 /* 26 */ 0,
287 /* 27 */ 0,
288 /* 28 */ 0,
289 /* 29 */ 0,
290 /* 30 */ 0,
291 /* 31 */ 0,
292 /* SP */ 0,
293 /* ! */ 0,
294 /* " */ 0,
295 /* # */ 0,
296 /* $ */ 0,
297 /* % */ 0,
298 /* & */ 0,
299 /* ' */ 0,
300 /* ( */ 0,
301 /* ) */ 0,
302 /* * */ 0,
303 /* + */ 0,
304 /* , */ 0,
305 /* - */ 0,
306 /* . */ 0,
307 /* / */ 0,
308 /* 0 */ C_DIGIT,
309 /* 1 */ C_DIGIT,
310 /* 2 */ C_DIGIT,
311 /* 3 */ C_DIGIT,
312 /* 4 */ C_DIGIT,
313 /* 5 */ C_DIGIT,
314 /* 6 */ C_DIGIT,
315 /* 7 */ C_DIGIT,
316 /* 8 */ C_DIGIT,
317 /* 9 */ C_DIGIT,
318 /* : */ 0,
319 /* ; */ 0,
320 /* < */ 0,
321 /* = */ 0,
322 /* > */ 0,
323 /* ? */ 0,
324 /* @ */ 0,
325 /* A */ C_ALPHA,
326 /* B */ C_ALPHA,
327 /* C */ C_ALPHA,
328 /* D */ C_ALPHA,
329 /* E */ C_ALPHA,
330 /* F */ C_ALPHA,
331 /* G */ C_ALPHA,
332 /* H */ C_ALPHA,
333 /* I */ C_ALPHA,
334 /* J */ C_ALPHA,
335 /* K */ C_ALPHA,
336 /* L */ C_ALPHA,
337 /* M */ C_ALPHA,
338 /* N */ C_ALPHA,
339 /* O */ C_ALPHA,
340 /* P */ C_ALPHA,
341 /* Q */ C_ALPHA,
342 /* R */ C_ALPHA,
343 /* S */ C_ALPHA,
344 /* T */ C_ALPHA,
345 /* U */ C_ALPHA,
346 /* V */ C_ALPHA,
347 /* W */ C_ALPHA,
348 /* X */ C_ALPHA,
349 /* Y */ C_ALPHA,
350 /* Z */ C_ALPHA,
351 /* [ */ 0,
352 /* \ */ 0,
353 /* ] */ 0,
354 /* ^ */ 0,
355 /* _ */ 0,
356 /* ` */ 0,
357 /* a */ C_ALPHA,
358 /* b */ C_ALPHA,
359 /* c */ C_ALPHA,
360 /* d */ C_ALPHA,
361 /* e */ C_ALPHA,
362 /* f */ C_ALPHA,
363 /* g */ C_ALPHA,
364 /* h */ C_ALPHA,
365 /* i */ C_ALPHA,
366 /* j */ C_ALPHA,
367 /* k */ C_ALPHA,
368 /* l */ C_ALPHA,
369 /* m */ C_ALPHA,
370 /* n */ C_ALPHA,
371 /* o */ C_ALPHA,
372 /* p */ C_ALPHA,
373 /* q */ C_ALPHA,
374 /* r */ C_ALPHA,
375 /* s */ C_ALPHA,
376 /* t */ C_ALPHA,
377 /* u */ C_ALPHA,
378 /* v */ C_ALPHA,
379 /* w */ C_ALPHA,
380 /* x */ C_ALPHA,
381 /* y */ C_ALPHA,
382 /* z */ C_ALPHA,
383 /* { */ 0,
384 /* | */ 0,
385 /* } */ 0,
386 /* ~ */ 0,
387 /* del */ 0,
388 /* 0x80 */ 0,
389 /* 0x81 */ 0,
390 /* 0x82 */ 0,
391 /* 0x83 */ 0,
392 /* 0x84 */ 0,
393 /* 0x85 */ 0,
394 /* 0x86 */ 0,
395 /* 0x87 */ 0,
396 /* 0x88 */ 0,
397 /* 0x89 */ 0,
398 /* 0x8A */ 0,
399 /* 0x8B */ 0,
400 /* 0x8C */ 0,
401 /* 0x8D */ 0,
402 /* 0x8E */ 0,
403 /* 0x8F */ 0,
404 /* 0x90 */ 0,
405 /* 0x91 */ 0,
406 /* 0x92 */ 0,
407 /* 0x93 */ 0,
408 /* 0x94 */ 0,
409 /* 0x95 */ 0,
410 /* 0x96 */ 0,
411 /* 0x97 */ 0,
412 /* 0x98 */ 0,
413 /* 0x99 */ 0,
414 /* 0x9A */ 0,
415 /* 0x9B */ 0,
416 /* 0x9C */ 0,
417 /* 0x9D */ 0,
418 /* 0x9E */ 0,
419 /* 0x9F */ 0,
420 /* 0xA0 */ 0,
421 /* 0xA1 */ 0,
422 /* 0xA2 */ 0,
423 /* 0xA3 */ 0,
424 /* 0xA4 */ 0,
425 /* 0xA5 */ 0,
426 /* 0xA6 */ 0,
427 /* 0xA7 */ 0,
428 /* 0xA8 */ 0,
429 /* 0xA9 */ 0,
430 /* 0xAA */ 0,
431 /* 0xAB */ 0,
432 /* 0xAC */ 0,
433 /* 0xAD */ 0,
434 /* 0xAE */ 0,
435 /* 0xAF */ 0,
436 /* 0xB0 */ 0,
437 /* 0xB1 */ 0,
438 /* 0xB2 */ 0,
439 /* 0xB3 */ 0,
440 /* 0xB4 */ 0,
441 /* 0xB5 */ 0,
442 /* 0xB6 */ 0,
443 /* 0xB7 */ 0,
444 /* 0xB8 */ 0,
445 /* 0xB9 */ 0,
446 /* 0xBA */ 0,
447 /* 0xBB */ 0,
448 /* 0xBC */ 0,
449 /* 0xBD */ 0,
450 /* 0xBE */ 0,
451 /* 0xBF */ 0,
452 /* 0xC0 */ 0,
453 /* 0xC1 */ 0,
454 /* 0xC2 */ 0,
455 /* 0xC3 */ 0,
456 /* 0xC4 */ 0,
457 /* 0xC5 */ 0,
458 /* 0xC6 */ 0,
459 /* 0xC7 */ 0,
460 /* 0xC8 */ 0,
461 /* 0xC9 */ 0,
462 /* 0xCA */ 0,
463 /* 0xCB */ 0,
464 /* 0xCC */ 0,
465 /* 0xCD */ 0,
466 /* 0xCE */ 0,
467 /* 0xCF */ 0,
468 /* 0xD0 */ 0,
469 /* 0xD1 */ 0,
470 /* 0xD2 */ 0,
471 /* 0xD3 */ 0,
472 /* 0xD4 */ 0,
473 /* 0xD5 */ 0,
474 /* 0xD6 */ 0,
475 /* 0xD7 */ 0,
476 /* 0xD8 */ 0,
477 /* 0xD9 */ 0,
478 /* 0xDA */ 0,
479 /* 0xDB */ 0,
480 /* 0xDC */ 0,
481 /* 0xDD */ 0,
482 /* 0xDE */ 0,
483 /* 0xDF */ 0,
484 /* 0xE0 */ 0,
485 /* 0xE1 */ 0,
486 /* 0xE2 */ 0,
487 /* 0xE3 */ 0,
488 /* 0xE4 */ 0,
489 /* 0xE5 */ 0,
490 /* 0xE6 */ 0,
491 /* 0xE7 */ 0,
492 /* 0xE8 */ 0,
493 /* 0xE9 */ 0,
494 /* 0xEA */ 0,
495 /* 0xEB */ 0,
496 /* 0xEC */ 0,
497 /* 0xED */ 0,
498 /* 0xEE */ 0,
499 /* 0xEF */ 0,
500 /* 0xF0 */ 0,
501 /* 0xF1 */ 0,
502 /* 0xF2 */ 0,
503 /* 0xF3 */ 0,
504 /* 0xF4 */ 0,
505 /* 0xF5 */ 0,
506 /* 0xF6 */ 0,
507 /* 0xF7 */ 0,
508 /* 0xF8 */ 0,
509 /* 0xF9 */ 0,
510 /* 0xFA */ 0,
511 /* 0xFB */ 0,
512 /* 0xFC */ 0,
513 /* 0xFD */ 0,
514 /* 0xFE */ 0,
515 /* 0xFF */ 0,
516 };
517