1 /* MDB Tools - A library for reading MS Access database file
2  * Copyright (C) 2000 Brian Bruns
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA  02110-1301, USA.
18  */
19 
20 #include <stdio.h>
21 #include <string.h>
22 #include <mdbtools.h>
23 
24 #ifdef DMALLOC
25 #include "dmalloc.h"
26 #endif
27 
28 /**
29  * mdb_like_cmp
30  * @s: String to search within.
31  * @r: Search pattern.
32  *
33  * Tests the string @s to see if it matches the search pattern @r.  In the
34  * search pattern, a percent sign indicates matching on any number of
35  * characters, and an underscore indicates matching any single character.
36  *
37  * Returns: 1 if the string matches, 0 if the string does not match.
38  */
39 int mdb_like_cmp(char *s, char *r)
40 {
41 	unsigned int i;
42 	int ret;
43 
44 	mdb_debug(MDB_DEBUG_LIKE, "comparing %s and %s", s, r);
45 	switch (r[0]) {
46 		case '\0':
47 			if (s[0]=='\0') {
48 				return 1;
49 			} else {
50 				return 0;
51 			}
52 		case '_':
53 			/* skip one character */
54 			return mdb_like_cmp(&s[1],&r[1]);
55 		case '%':
56 			/* skip any number of characters */
57 			/* the strlen(s)+1 is important so the next call can */
58 			/* if there are trailing characters */
59 			for(i=0;i<strlen(s)+1;i++) {
60 				if (mdb_like_cmp(&s[i],&r[1])) {
61 					return 1;
62 				}
63 			}
64 			return 0;
65 		default:
mdb_read_indices(MdbTableDef * table)66 			for(i=0;i<strlen(r);i++) {
67 				if (r[i]=='_' || r[i]=='%') break;
68 			}
69 			if (strncmp(s,r,i)) {
70 				return 0;
71 			} else {
72 				mdb_debug(MDB_DEBUG_LIKE, "at pos %d comparing %s and %s", i, &s[i], &r[i]);
73 				ret = mdb_like_cmp(&s[i],&r[i]);
74 				mdb_debug(MDB_DEBUG_LIKE, "returning %d (%s and %s)", ret, &s[i], &r[i]);
75 				return ret;
76 			}
77 	}
78 }
79