Character '.' matches any single character.
[yaz-moved-to-github.git] / util / matchstr.c
1 /*
2  * Copyright (c) 1995-1997, Index Data.
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: matchstr.c,v $
7  * Revision 1.2  1999-10-15 11:35:41  adam
8  * Character '.' matches any single character.
9  *
10  * Revision 1.1  1999/06/08 10:10:16  adam
11  * New sub directory zutil. Moved YAZ Compiler to be part of YAZ tree.
12  *
13  * Revision 1.7  1997/09/30 11:47:47  adam
14  * Added function 'cause checkergcc doesn't include assert handler.
15  *
16  * Revision 1.6  1997/09/04 07:54:34  adam
17  * Right hande side operand of yaz_matchstr may include a ? in
18  * which case it returns "match ok".
19  *
20  * Revision 1.5  1997/07/21 12:48:11  adam
21  * Removed windows DLL stubs.
22  *
23  * Revision 1.4  1997/05/01 15:07:55  adam
24  * Added DLL entry point routines.
25  *
26  * Revision 1.3  1996/10/29 13:36:28  adam
27  * Added header.
28  *
29  * Revision 1.2  1996/02/20 17:58:42  adam
30  * Added const to yaz_matchstr.
31  *
32  * Revision 1.1  1996/02/20  16:33:06  quinn
33  * Moved matchstr to global util
34  *
35  * Revision 1.1  1995/11/01  11:56:08  quinn
36  * Added Retrieval (data management) functions en masse.
37  *
38  *
39  */
40 #include <stdio.h>
41 #include <assert.h>
42 #include <ctype.h>
43 #include <yaz-util.h>
44
45 /*
46  * Match strings, independently of case and occurences of '-'.
47  * fairly inefficient - will be replaced with an indexing scheme for
48  * the various subsystems if we get a bottleneck here.
49  */
50
51 int yaz_matchstr(const char *s1, const char *s2)
52 {
53     while (*s1 && *s2)
54     {
55         char c1 = *s1;
56         char c2 = *s2;
57
58         if (c2 == '?')
59             return 0;
60         if (c1 == '-')
61             s1++;
62         if (c2 == '-')
63             s2++;
64         if (!c1 || !c2)
65             break;
66         if (c2 != '.')
67         {
68             if (isupper(c1))
69                 c1 = tolower(c1);
70             if (isupper(c2))
71                 c2 = tolower(c2);
72             if (c1 != c2)
73                 break;
74         }
75         s1++;
76         s2++;
77     }
78     return *s1 || *s2;
79 }
80
81 #ifdef __GNUC__
82 #ifdef __CHECKER__
83 void __assert_fail (const char *assertion, const char *file, 
84                     unsigned int line, const char *function)
85 {
86     fprintf (stderr, "%s in file %s line %d func %s\n",
87              assertion, file, line, function);
88     abort ();
89 }
90 #endif
91 #endif