/src/mdbtools/src/libmdb/like.c
Line | Count | Source (jump to first uncovered line) |
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 Free Software |
16 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
17 | | */ |
18 | | |
19 | | #include <stdio.h> |
20 | | #include <string.h> |
21 | | #include "mdbtools.h" |
22 | | |
23 | | /** |
24 | | * |
25 | | * @param s: String to search within. |
26 | | * @param r: Search pattern. |
27 | | * |
28 | | * Tests the string @s to see if it matches the search pattern @r. In the |
29 | | * search pattern, a percent sign indicates matching on any number of |
30 | | * characters, and an underscore indicates matching any single character. |
31 | | * |
32 | | * @Returns: 1 if the string matches, 0 if the string does not match. |
33 | | */ |
34 | | int mdb_like_cmp(char *s, char *r) |
35 | 0 | { |
36 | 0 | unsigned int i; |
37 | 0 | int ret; |
38 | |
|
39 | 0 | mdb_debug(MDB_DEBUG_LIKE, "comparing %s and %s", s, r); |
40 | 0 | switch (r[0]) { |
41 | 0 | case '\0': |
42 | 0 | return (s[0]=='\0'); |
43 | 0 | case '_': |
44 | | /* skip one character */ |
45 | 0 | return mdb_like_cmp(&s[1],&r[1]); |
46 | 0 | case '%': |
47 | | /* skip any number of characters */ |
48 | | /* the strlen(s)+1 is important so the next call can */ |
49 | | /* if there are trailing characters */ |
50 | 0 | for(i=0;i<strlen(s)+1;i++) { |
51 | 0 | if (mdb_like_cmp(&s[i],&r[1])) { |
52 | 0 | return 1; |
53 | 0 | } |
54 | 0 | } |
55 | 0 | return 0; |
56 | 0 | default: |
57 | 0 | for(i=0;i<strlen(r);i++) { |
58 | 0 | if (r[i]=='_' || r[i]=='%') break; |
59 | 0 | } |
60 | 0 | if (strncmp(s,r,i)) { |
61 | 0 | return 0; |
62 | 0 | } else { |
63 | 0 | mdb_debug(MDB_DEBUG_LIKE, "at pos %d comparing %s and %s", i, &s[i], &r[i]); |
64 | 0 | ret = mdb_like_cmp(&s[i],&r[i]); |
65 | 0 | mdb_debug(MDB_DEBUG_LIKE, "returning %d (%s and %s)", ret, &s[i], &r[i]); |
66 | 0 | return ret; |
67 | 0 | } |
68 | 0 | } |
69 | 0 | } |
70 | | |
71 | | /** |
72 | | * |
73 | | * @param s: String to search within. |
74 | | * @param r: Case-insensitive search pattern. |
75 | | * |
76 | | * Tests the string @s to see if it matches the search pattern @r without |
77 | | * regard to case; this mimics the behavior of the Access LIKE operator. In the |
78 | | * search pattern, a percent sign indicates matching on any number of |
79 | | * characters, and an underscore indicates matching any single character. |
80 | | * |
81 | | * @Returns: 1 if the string matches, 0 if the string does not match. |
82 | | */ |
83 | 0 | int mdb_ilike_cmp(char *s, char *r) { |
84 | 0 | char *s1 = g_utf8_casefold(s, -1); |
85 | 0 | char *r1 = g_utf8_casefold(r, -1); |
86 | 0 | int result = mdb_like_cmp(s1, r1); |
87 | 0 | g_free(s1); |
88 | 0 | g_free(r1); |
89 | 0 | return result; |
90 | 0 | } |
91 | | |