Added SILC Server library.
[silc.git] / lib / silcutil / beos / silcbeosutil.c
1 /*
2
3   silcbeosutil.c 
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 2002 Pekka Riikonen
8
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.
12
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.
17
18 */
19 /* $Id$ */
20
21 #include "silc.h"
22
23 /* Inspects the `string' for wildcards and returns regex string that can
24    be used by the GNU regex library. A comma (`,') in the `string' means
25    that the string is list. */
26
27 char *silc_string_regexify(const char *string)
28 {
29   int i, len, count;
30   char *regex;
31
32   len = strlen(string);
33   count = 4;
34   for (i = 0; i < len; i++)
35     if (string[i] == '*' || string[i] == '?')
36       count++;
37
38   regex = silc_calloc(len + count, sizeof(*regex));
39
40   count = 0;
41   regex[count] = '(';
42   count++;
43
44   for (i = 0; i < len; i++) {
45     if (string[i] == '*' || string[i] == '?') {
46       regex[count] = '.';
47       count++;
48     } else if (string[i] == ',') {
49       if (i + 1 == len)
50         continue;
51       regex[count] = '|';
52       count++;
53       continue;
54     }
55
56     regex[count] = string[i];
57     count++;
58   }
59
60   regex[count++] = ')';
61   regex[count] = '$';
62
63   return regex;
64 }
65
66 /* Combines two regex strings into one regex string so that they can be
67    used as one by the GNU regex library. The `string2' is combine into
68    the `string1'. */
69
70 char *silc_string_regex_combine(const char *string1, const char *string2)
71 {
72   char *tmp;
73   int len1, len2;
74
75   len1 = strlen(string1);
76   len2 = strlen(string2);
77
78   tmp = silc_calloc(2 + len1 + len2, sizeof(*tmp));
79   strncat(tmp, string1, len1 - 2);
80   strncat(tmp, "|", 1);
81   strncat(tmp, string2 + 1, len2 - 1);
82
83   return tmp;
84 }
85
86 /* Matches the two strings and returns TRUE if the strings match. */
87
88 int silc_string_regex_match(const char *regex, const char *string)
89 {
90   regex_t preg;
91   int ret = FALSE;
92   
93   if (regcomp(&preg, regex, REG_NOSUB | REG_EXTENDED) < 0)
94     return FALSE;
95
96   if (regexec(&preg, string, 0, NULL, 0) == 0)
97     ret = TRUE;
98
99   regfree(&preg);
100
101   return ret;
102 }
103
104 /* Do regex match to the two strings `string1' and `string2'. If the
105    `string2' matches the `string1' this returns TRUE. */
106
107 int silc_string_match(const char *string1, const char *string2)
108 {
109   char *s1;
110   int ret = FALSE;
111
112   if (!string1 || !string2)
113     return ret;
114
115   s1 = silc_string_regexify(string1);
116   ret = silc_string_regex_match(s1, string2);
117   silc_free(s1);
118
119   return ret;
120 }
121
122 /* Return current time to struct timeval. */
123
124 int silc_gettimeofday(struct timeval *p)
125 {
126   return gettimeofday(p, NULL);
127 }