5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 2002 - 2007 Pekka Riikonen
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; version 2 of the License.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
22 #include "silcstrutil.h"
24 /* Concatenates the `src' into `dest'. If `src_len' is more than the
25 size of the `dest' (minus NULL at the end) the `src' will be
28 char *silc_strncat(char *dest, SilcUInt32 dest_size,
29 const char *src, SilcUInt32 src_len)
33 dest[dest_size - 1] = '\0';
35 len = dest_size - 1 - strlen(dest);
38 strncat(dest, src, len);
40 strncat(dest, src, src_len);
46 /* Compares two strings. Strings may include wildcards '*' and '?'.
47 Returns TRUE if strings match. */
49 int silc_string_compare(char *string1, char *string2)
54 char *tmpstr1, *tmpstr2;
56 if (!string1 || !string2)
59 slen1 = strlen(string1);
60 slen2 = strlen(string2);
62 /* See if they are same already */
63 if (!strncmp(string1, string2, slen2) && slen2 == slen1)
67 if (!strchr(string1, '*'))
70 /* Take copies of the original strings as we will change them */
71 tmpstr1 = silc_calloc(slen1 + 1, sizeof(char));
72 memcpy(tmpstr1, string1, slen1);
73 tmpstr2 = silc_calloc(slen2 + 1, sizeof(char));
74 memcpy(tmpstr2, string2, slen2);
76 for (i = 0; i < slen1; i++) {
78 /* * wildcard. Only one * wildcard is possible. */
79 if (tmpstr1[i] == '*')
80 if (!strncmp(tmpstr1, tmpstr2, i)) {
81 memset(tmpstr2, 0, slen2);
82 strncpy(tmpstr2, tmpstr1, i);
87 if (tmpstr1[i] == '?') {
88 if (!strncmp(tmpstr1, tmpstr2, i)) {
90 if (tmpstr1[i + 1] != '?' &&
91 tmpstr1[i + 1] != tmpstr2[i + 1])
100 /* if using *, remove it */
101 if (strchr(tmpstr1, '*'))
102 *strchr(tmpstr1, '*') = 0;
104 if (!strcmp(tmpstr1, tmpstr2)) {
105 memset(tmpstr1, 0, slen1);
106 memset(tmpstr2, 0, slen2);
112 memset(tmpstr1, 0, slen1);
113 memset(tmpstr2, 0, slen2);
119 /* Splits a string containing separator `ch' and returns an array of the
122 char **silc_string_split(const char *string, char ch, int *ret_count)
124 char **splitted = NULL, sep[1], *item, *cp;
132 splitted = silc_calloc(1, sizeof(*splitted));
136 if (!strchr(string, ch)) {
137 splitted[0] = silc_memdup(string, strlen(string));
145 len = strcspn(cp, sep);
146 item = silc_memdup(cp, len);
158 splitted = silc_realloc(splitted, (i + 1) * sizeof(*splitted));
161 splitted[i++] = item;
168 /* Inspects the `string' for wildcards and returns regex string that can
169 be used by the GNU regex library. A comma (`,') in the `string' means
170 that the string is list. */
172 char *silc_string_regexify(const char *string)
180 len = strlen(string);
182 for (i = 0; i < len; i++) {
183 if (string[i] == '*' || string[i] == '?')
184 count++; /* Will add '.' */
185 if (string[i] == ',')
186 count += 2; /* Will add '|' and '^' */
189 regex = silc_calloc(len + count + 1, sizeof(*regex));
194 regex[count++] = '(';
195 regex[count++] = '^';
197 for (i = 0; i < len; i++) {
198 if (string[i] == '*' || string[i] == '?') {
201 } else if (string[i] == ',') {
204 regex[count++] = '|';
205 regex[count++] = '^';
209 regex[count++] = string[i];
212 regex[count++] = ')';
218 /* Combines two regex strings into one regex string so that they can be
219 used as one by the GNU regex library. The `string2' is combine into
222 char *silc_string_regex_combine(const char *string1, const char *string2)
227 if (!string1 || !string2)
230 len1 = strlen(string1);
231 len2 = strlen(string2);
233 tmp = silc_calloc(2 + len1 + len2, sizeof(*tmp));
234 strncat(tmp, string1, len1 - 2);
235 strncat(tmp, "|", 1);
236 strncat(tmp, string2 + 1, len2 - 1);
241 /* Matches the two strings and returns TRUE if the strings match. */
243 int silc_string_regex_match(const char *regex, const char *string)
248 if (regcomp(&preg, regex, REG_NOSUB | REG_EXTENDED) != 0)
251 if (regexec(&preg, string, 0, NULL, 0) == 0)
259 /* Do regex match to the two strings `string1' and `string2'. If the
260 `string2' matches the `string1' this returns TRUE. */
262 int silc_string_match(const char *string1, const char *string2)
267 if (!string1 || !string2)
270 s1 = silc_string_regexify(string1);
271 ret = silc_string_regex_match(s1, string2);