3a319d33b0e3175f90deb993c59b4dce3b475d29
[yaz-moved-to-github.git] / src / matchstr.c
1 /*
2  * Copyright (C) 1995-2007, Index Data ApS
3  * See the file LICENSE for details.
4  *
5  * $Id: matchstr.c,v 1.7 2007-01-03 08:42:15 adam Exp $
6  */
7
8 /**
9  * \file matchstr.c
10  * \brief Implements loose string matching 
11  */
12
13 #if HAVE_CONFIG_H
14 #include <config.h>
15 #endif
16
17 #include <stdio.h>
18 #include <assert.h>
19 #include <ctype.h>
20 #include <string.h>
21 #include <yaz/yaz-util.h>
22
23 int yaz_matchstr(const char *s1, const char *s2)
24 {
25     while (*s1 && *s2)
26     {
27         unsigned char c1 = *s1;
28         unsigned 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
78 /*
79  * Local variables:
80  * c-basic-offset: 4
81  * indent-tabs-mode: nil
82  * End:
83  * vim: shiftwidth=4 tabstop=8 expandtab
84  */
85