]> jfr.im git - irc/quakenet/newserv.git/blob - helpmod2/hgen.c
CHANSERV: authtracker now keeps 240 days history
[irc/quakenet/newserv.git] / helpmod2 / hgen.c
1 #include <ctype.h>
2
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <string.h>
6
7 #include "hgen.h"
8
9 int ci_strcmp(const char *str1, const char *str2)
10 {
11 while (*str1)
12 {
13 if (tolower(*str1) != tolower(*str2))
14 return tolower(*str1) - tolower(*str2);
15 str1++;
16 str2++;
17 }
18 return tolower(*str1) - tolower(*str2);
19 }
20
21 /* can't even remember what this does, but it works */
22 static char *rstrchr(const char * str, char chr)
23 {
24 while (*str)
25 if (tolower(*str) == tolower(chr))
26 return (char*)str;
27 else
28 str++;
29 return NULL;
30 }
31
32 int strregexp(const char * str, const char * pattern)
33 {
34 if (!pattern)
35 return 1;
36 while (*pattern && *str)
37 {
38 if (*pattern == '?')
39 pattern++, str++;
40 else if (*pattern == '*')
41 {
42 while (*(++pattern) == '*');
43 if (!*pattern)
44 return 1;
45 if (*pattern == '?')
46 {
47 while (*str)
48 if (strregexp(str++, pattern))
49 return 1;
50 }
51 else
52 {
53 while ((str = rstrchr(str, *pattern)))
54 if (strregexp(str++, pattern))
55 return 1;
56 }
57 return 0;
58 }
59 else if (tolower(*pattern) != tolower(*str))
60 return 0;
61 else
62 pattern++, str++;
63 }
64
65 while(*pattern == '*')
66 pattern++;
67
68 if (!*pattern && !*str)
69 return 1;
70 else
71 return 0;
72 }
73
74 #define TIME_PRINT(elem,marker)\
75 if (elem)\
76 {\
77 buf+=sprintf(buf, " %d%s", elem, marker);\
78 }\
79
80 /* This implementation might look a little evil but it does work */
81 const char *helpmod_strtime(int total_seconds)
82 {
83 static int buffer_index = 0;
84 static char buffers[3][64];
85
86 char *buf = buffers[buffer_index];
87 char *buffer = buf;
88
89 int years, months, days, hours, minutes, seconds;
90
91 buffer_index = (buffer_index+1) % 3;
92
93 /* trivial case */
94 if (!total_seconds)
95 return "0s";
96
97 if (total_seconds < 0)
98 {
99 *buf = '-';
100 buf++;
101 total_seconds = -total_seconds;
102 }
103
104 years = total_seconds / HDEF_y;
105 total_seconds %= HDEF_y;
106
107 months = total_seconds / HDEF_M;
108 total_seconds %= HDEF_M;
109
110 days = total_seconds / HDEF_d;
111 total_seconds %= HDEF_d;
112
113 hours = total_seconds / HDEF_h;
114 total_seconds %= HDEF_h;
115
116 minutes = total_seconds / HDEF_m;
117 total_seconds %= HDEF_m;
118
119 seconds = total_seconds;
120
121 TIME_PRINT(years, "y");
122 TIME_PRINT(months, "M");
123 TIME_PRINT(days, "d");
124 TIME_PRINT(hours, "h");
125 TIME_PRINT(minutes, "m");
126 TIME_PRINT(seconds, "s");
127
128 if (*buffer != '-')
129 return buffer+1;
130 else
131 return buffer;
132 }
133
134 int helpmod_read_strtime(const char *str)
135 {
136 int sum = 0, tmp_sum, tmp;
137
138 while (*str)
139 {
140 if (!sscanf(str, "%d%n", &tmp_sum, &tmp))
141 return -1;
142 str+=tmp;
143
144 switch (*str)
145 {
146 case 's':
147 break;
148 case 'm':
149 tmp_sum*=HDEF_m;
150 break;
151 case 'h':
152 tmp_sum*=HDEF_h;
153 break;
154 case 'd':
155 tmp_sum*=HDEF_d;
156 break;
157 case 'w':
158 tmp_sum*=HDEF_w;
159 break;
160 case 'M':
161 tmp_sum*=HDEF_M;
162 break;
163 case 'y':
164 tmp_sum*=HDEF_y;
165 break;
166 default: /* includes '\0' */
167 return -1;
168 }
169
170 str++;
171 /* sanity checks */
172 if (tmp_sum > 10 * HDEF_y)
173 return -1;
174 sum+=tmp_sum;
175 if (sum > 10 * HDEF_y)
176 return -1;
177 }
178 return sum;
179 }
180
181 int hword_count(const char *ptr)
182 {
183 int wordc = 0;
184
185 while (*ptr)
186 {
187 while (isspace(*ptr) && *ptr)
188 ptr++;
189 if (*ptr)
190 wordc++;
191 while (!isspace(*ptr) && *ptr)
192 ptr++;
193 }
194
195 return wordc;
196 }
197
198 int helpmod_is_lame_line(const char *line)
199 {
200 const char lamechars[] = {(char)2, (char)3, (char)22, (char)31, (char)0};
201 if (strpbrk(line, lamechars) != NULL)
202 return 1;
203 else
204 return 0;
205 }
206
207 int strislower(const char *str)
208 {
209 for (;*str;str++)
210 if (isupper(*str))
211 return 0;
212 return 1;
213 }
214
215 int strisupper(const char *str)
216 {
217 for (;*str;str++)
218 if (islower(*str))
219 return 0;
220 return 1;
221 }
222
223 int strisalpha(const char *str)
224 {
225 for (;*str;str++)
226 if (!isalpha(*str))
227 return 0;
228 return 1;
229 }
230
231 int strnumcount(const char *str)
232 {
233 int count = 0;
234 for (;*str;str++)
235 if (isdigit(*str))
236 count++;
237 return count;
238 }
239
240 float helpmod_percentage(int larger, int smaller)
241 {
242 if (larger == 0)
243 return 0.0;
244
245 return 100.0 * ((float)smaller / (float)larger);
246 }
247
248 int helpmod_select(const char *str, const char **strs, int *enums, int count)
249 {
250 int i;
251
252 if (count == 0)
253 return -1;
254
255 for (i = 0;i < count;i++)
256 if (!ci_strcmp(strs[i], str))
257 return enums[i];
258
259 return -1;
260 }