record_conv: change construct prototype
[yaz-moved-to-github.git] / src / matchstr.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2012 Index Data
3  * See the file LICENSE for details.
4  */
5
6 /**
7  * \file matchstr.c
8  * \brief a couple of string utilities
9  */
10
11 #if HAVE_CONFIG_H
12 #include <config.h>
13 #endif
14
15 #include <stdio.h>
16 #include <assert.h>
17 #include <string.h>
18 #include <yaz/yaz-iconv.h>
19 #include <yaz/matchstr.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 (yaz_isupper(c1))
39                 c1 = yaz_tolower(c1);
40             if (yaz_isupper(c2))
41                 c2 = yaz_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 int yaz_memcmp(const void *a, const void *b, size_t len_a, size_t len_b)
66 {
67     size_t m_len = len_a < len_b ? len_a : len_b;
68     int r = memcmp(a, b, m_len);
69     if (r)
70         return r;
71     return len_a - len_b;
72 }
73
74 /*
75  * Local variables:
76  * c-basic-offset: 4
77  * c-file-style: "Stroustrup"
78  * indent-tabs-mode: nil
79  * End:
80  * vim: shiftwidth=4 tabstop=8 expandtab
81  */
82