Use good m_ functions, because it smell like a flower, version 2.
[apps/madmutt.git] / lib-lib / str.c
1 /*
2  *  This program is free software; you can redistribute it and/or modify
3  *  it under the terms of the GNU General Public License as published by
4  *  the Free Software Foundation; either version 2 of the License, or (at
5  *  your option) any later version.
6  *
7  *  This program is distributed in the hope that it will be useful, but
8  *  WITHOUT ANY WARRANTY; without even the implied warranty of
9  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10  *  General Public License for more details.
11  *
12  *  You should have received a copy of the GNU General Public License
13  *  along with this program; if not, write to the Free Software
14  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
15  *  MA 02110-1301, USA.
16  *
17  *  Copyright © 2006 Pierre Habouzit
18  */
19
20 #include "lib-lib.h"
21
22 #define XX 255
23 unsigned char const __m_strdigits[128] = {
24     XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX,
25     XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX,
26     XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX,
27      0,  1,  2,  3,  4,  5,  6,  7,  8,  9, XX, XX, XX, XX, XX, XX,
28     XX, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
29     25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, XX, XX, XX, XX, XX,
30     XX, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
31     25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, XX, XX, XX, XX, XX,
32 };
33 #undef XX
34
35 #define XX -1
36 signed char const __m_b64digits[128] = {
37     XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX,
38     XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX,
39     XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, 62, XX, XX, XX, 63,
40     52, 53, 54, 55, 56, 57, 58, 59, 60, 61, XX, XX, XX, XX, XX, XX,
41     XX,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
42     15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, XX, XX, XX, XX, XX,
43     XX, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
44     41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, XX, XX, XX, XX, XX
45 };
46 #undef XX
47
48 char const __m_b64chars[64] = {
49     'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
50     'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
51     'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
52     't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7',
53     '8', '9', '+', '/'
54 };
55
56 char const __m_b36chars_lower[36] = {
57     '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
58     'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
59     'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
60     'u', 'v', 'w', 'x', 'y', 'z'
61 };
62
63 char const __m_b36chars_upper[36] = {
64     '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
65     'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
66     'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
67     'U', 'V', 'W', 'X', 'Y', 'Z'
68 };
69
70
71 ssize_t m_strcpy(char *dst, ssize_t n, const char *src)
72 {
73     ssize_t len = m_strlen(src);
74
75     if (dst && n > 0) {
76         ssize_t dlen = MIN(n - 1, len);
77         memcpy(dst, src, dlen);
78         dst[dlen] = '\0';
79     }
80
81     return len;
82 }
83
84 ssize_t m_strncpy(char *dst, ssize_t n, const char *src, ssize_t l)
85 {
86     ssize_t len = MIN(m_strlen(src), l);
87
88     if (dst && n > 0) {
89         ssize_t dlen = MIN(n - 1, len);
90         memcpy(dst, src, dlen);
91         dst[dlen] = '\0';
92     }
93
94     return len;
95 }
96
97 char *m_strrtrim(char *s)
98 {
99     if (s) {
100         char *p = s + m_strlen(s);
101
102         while (p > s && ISSPACE(p[-1])) {
103             *--p = '\0';
104         }
105         return p;
106     }
107
108     return NULL;
109 }
110
111 const char *m_stristrn(const char *haystack, const char *needle, ssize_t nlen)
112 {
113     int nc;
114
115     if (!nlen)
116         return haystack;
117
118     nc = tolower(*needle);
119     for (;;) {
120         int c = tolower(*haystack);
121
122         if (c != nc) {
123             if (c == '\0')
124                 return NULL;
125         } else {
126             ssize_t i;
127
128             /* compare the rest of needle */
129             for (i = 1;; i++) {
130                 if (i == nlen)
131                     return haystack;
132                 if (c == '\0')
133                     return NULL;
134                 c = tolower(haystack[i]);
135                 if (c != tolower(needle[i]))
136                     break;
137             }
138         }
139
140         haystack++;
141     }
142 }
143
144 int ascii_strcasecmp(const char *a, const char *b)
145 {
146     int i;
147
148     if (a == b)
149         return 0;
150     if (a == NULL && b)
151         return -1;
152     if (b == NULL && a)
153         return 1;
154
155     for (; *a || *b; a++, b++) {
156         if ((i = ascii_tolower(*a) - ascii_tolower(*b)))
157             return i;
158     }
159
160     return 0;
161 }
162
163 int ascii_strncasecmp (const char *a, const char *b, ssize_t n)
164 {
165     int i, j;
166
167     if (a == b)
168         return 0;
169     if (a == NULL && b)
170         return -1;
171     if (b == NULL && a)
172         return 1;
173
174     for (j = 0; (*a || *b) && j < n; a++, b++, j++) {
175         if ((i = ascii_tolower(*a) - ascii_tolower(*b)))
176             return i;
177     }
178
179     return 0;
180 }