File: util.c

package info (click to toggle)
c-icap 1:0.5.3-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 3,600 kB
  • sloc: ansic: 27,220; sh: 4,415; makefile: 242; perl: 95; awk: 10
file content (192 lines) | stat: -rw-r--r-- 4,455 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
 *  Copyright (C) 2004-2011 Christos Tsantilas
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *  MA  02110-1301  USA.
 */

#include "common.h"
#include "c-icap.h"
#include "array.h"
#include "util.h"
#include <ctype.h>
#include <errno.h>
#include <limits.h>

const char *ci_strnstr(const char *s, const char *find, size_t slen)
{
    size_t len = strlen(find);

    if (len == 0)
        return NULL;

    while (len <= slen) {
        if (*s == *find && strncmp(s, find, len) == 0 )
            return s;
        s++,slen--;
    }
    return NULL;
}

const char *ci_strcasestr(const char *str, const char *find)
{
    const char *s, *c, *f;
    for (s = str; *s != '\0'; ++s) {
        for (f = find, c = s; ; ++f, ++c) {
            if (*f == '\0') /*find matched s*/
                return s;
            if (*c == '\0') /*find is longer than the remaining string */
                return NULL;
            if (tolower(*c) != tolower(*f))
                break;
        }
    }
    return NULL;
}

const char *ci_strncasestr(const char *s, const char *find, size_t slen)
{
    size_t len = strlen(find);

    if (len == 0)
        return NULL;

    while (len <= slen) {
        if (tolower(*s) == tolower(*find) && strncasecmp(s, find, len) == 0 )
            return s;
        s++,slen--;
    }
    return NULL;
}

static const char *atol_err_erange = "ERANGE";
static const char *atol_err_conversion = "CONVERSION_ERROR";
static const char *atol_err_nonumber = "NO_DIGITS_ERROR";

long int ci_atol_ext(const char *str, const char **error)
{
    char *e;

    long int val;
    errno = 0;
    val = strtol(str, &e, 10);

    if (error) {
        *error = NULL;

        if (errno == ERANGE && (val == LONG_MAX || val == LONG_MIN))
            *error = atol_err_erange;
        else if (errno != 0 && val == 0)
            *error = atol_err_conversion;
        else if (e == str)
            *error = atol_err_nonumber;

        if (*error)
            return 0;
    }

    if (val) {
        if (*e == 'k' || * e == 'K')
            val = val * 1024;
        else if (*e == 'm' || * e == 'M')
            val = val * 1024 * 1024;
    }
    return val;
}

void ci_str_trim(char *str)
{
    char *s, *e;

    if (!str)
        return;

    s = str;
    e = NULL;
    while (isspace(*s)) {
        e = s;
        while (*e != '\0') {
            *e = *(e+1);
            e++;
        }
    }

    /*if (e) e--;  else */
    e = str+strlen(str);
    e--;
    while (isspace(*e) && e >= str) {*e = '\0'; --e;};
}

char *ci_str_trim2(char *s)
{
    char *e;

    if (!s)
        return NULL;

    while (isspace(*s)) ++s;
    e = s + strlen(s);
    e--;
    while (isspace(*e) && e >= s) {*e = '\0'; --e;};
    return s;
}

char * ci_strerror(int error, char *buf, size_t buflen)
{
#if defined(STRERROR_R_CHAR_P)
    return strerror_r(error, buf, buflen);
#elif defined(HAVE_STRERROR_R)
    if (strerror_r(error,  buf, buflen) == 0)
        return buf;
#else
    snprintf(buf, buflen, "%d", error);
    buf[buflen - 1] = '\0';
    return buf;

#endif
}

/*
  TODO: support escaped chars,
*/
ci_dyn_array_t *ci_parse_key_value_list(const char *str, char sep)
{
    char *s, *e, *k, *v;
    ci_dyn_array_t *args_array;
    s = strdup(str);
    if (!s)
        return NULL;

    args_array = ci_dyn_array_new(1024);
    k = s;
    while (k) {
        if ((e = strchr(k , sep))) {
            *e = '\0';
            e++;
        }
        if ((v = strchr(k, '='))) {
            *v = '\0';
            ++v;
        }
        k = ci_str_trim2(k);
        if (v)
            v = ci_str_trim2(v);
        if (*k) {
            ci_dyn_array_add(args_array, k, v ? v : "", v ? strlen(v) + 1 : 1);
        }
        k = (e && *e) ? e : NULL;
    }
    return args_array;
}