b078150958fc975fa372d515df0b6442647d4ca0
[yaz-moved-to-github.git] / src / matchstr.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2008 Index Data
3  * See the file LICENSE for details.
4  */
5
6 /**
7  * \file matchstr.c
8  * \brief Implements loose string matching 
9  */
10
11 #if HAVE_CONFIG_H
12 #include <config.h>
13 #endif
14
15 #include <stdio.h>
16 #include <assert.h>
17 #include <ctype.h>
18 #include <string.h>
19 #include <yaz/yaz-util.h>
20
21 int yaz_matchstr(const char *s1, const char *s2)
22 {
23     while (*s1 && *s2)
24     {
25         unsigned char c1 = *s1;
26         unsigned char c2 = *s2;
27
28         if (c2 == '?')
29             return 0;
30         if (c1 == '-')
31             c1 = *++s1;
32         if (c2 == '-')
33             c2 = *++s2;
34         if (!c1 || !c2)
35             break;
36         if (c2 != '.')
37         {
38             if (isupper(c1))
39                 c1 = tolower(c1);
40             if (isupper(c2))
41                 c2 = tolower(c2);
42             if (c1 != c2)
43                 break;
44         }
45         s1++;
46         s2++;
47     }
48     return *s1 || *s2;
49 }
50
51 int yaz_strcmp_del(const char *a, const char *b, const char *b_del)
52 {
53     while (*a && *b)
54     {
55         if (*a != *b)
56             return *a - *b;
57         a++;
58         b++;
59     }
60     if (b_del && strchr(b_del, *b))
61         return *a;
62     return *a - *b;
63 }
64
65 #ifdef __GNUC__
66 #ifdef __CHECKER__
67 void __assert_fail (const char *assertion, const char *file, 
68                     unsigned int line, const char *function)
69 {
70     fprintf (stderr, "%s in file %s line %d func %s\n",
71              assertion, file, line, function);
72     abort ();
73 }
74 #endif
75 #endif
76 /*
77  * Local variables:
78  * c-basic-offset: 4
79  * indent-tabs-mode: nil
80  * End:
81  * vim: shiftwidth=4 tabstop=8 expandtab
82  */
83