538b4356e4e48190c6ba6b7bb7c6e7aedfb66a9b
[yaz-moved-to-github.git] / util / matchstr.c
1 /*
2  * Copyright (c) 1995-2003, Index Data.
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Id: matchstr.c,v 1.6 2003-02-12 15:06:44 adam Exp $
7  */
8 #if HAVE_CONFIG_H
9 #include <config.h>
10 #endif
11
12 #include <stdio.h>
13 #include <assert.h>
14 #include <ctype.h>
15 #include <yaz/yaz-util.h>
16
17 /*
18  * Match strings, independently of case and occurences of '-'.
19  * fairly inefficient - will be replaced with an indexing scheme for
20  * the various subsystems if we get a bottleneck here.
21  */
22
23 int yaz_matchstr(const char *s1, const char *s2)
24 {
25     while (*s1 && *s2)
26     {
27         char c1 = *s1;
28         char c2 = *s2;
29
30         if (c2 == '?')
31             return 0;
32         if (c1 == '-')
33             c1 = *++s1;
34         if (c2 == '-')
35             c2 = *++s2;
36         if (!c1 || !c2)
37             break;
38         if (c2 != '.')
39         {
40             if (isupper(c1))
41                 c1 = tolower(c1);
42             if (isupper(c2))
43                 c2 = tolower(c2);
44             if (c1 != c2)
45                 break;
46         }
47         s1++;
48         s2++;
49     }
50     return *s1 || *s2;
51 }
52
53 int yaz_strcmp_del(const char *a, const char *b, const char *b_del)
54 {
55     while (*a && *b)
56     {
57         if (*a != *b)
58             return *a - *b;
59         a++;
60         b++;
61     }
62     if (b_del && strchr(b_del, *b))
63         return *a;
64     return *a - *b;
65 }
66
67 #ifdef __GNUC__
68 #ifdef __CHECKER__
69 void __assert_fail (const char *assertion, const char *file, 
70                     unsigned int line, const char *function)
71 {
72     fprintf (stderr, "%s in file %s line %d func %s\n",
73              assertion, file, line, function);
74     abort ();
75 }
76 #endif
77 #endif