]> jfr.im git - irc/quakenet/newserv.git/blob - helpmod2/hgen.c
2.09
[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 sprintf(buf, " %d%s%n", elem, marker, &tmp);\
78 buf+=tmp;\
79 }\
80
81 /* This implementation might look a little evil but it does work */
82 const char *helpmod_strtime(int total_seconds)
83 {
84 static int buffer_index = 0;
85 static char buffers[3][64];
86
87 char *buf = buffers[buffer_index];
88 char *buffer = buf;
89
90 int years, months, days, hours, minutes, seconds, tmp;
91
92 buffer_index = (buffer_index+1) % 3;
93
94 /* trivial case */
95 if (!total_seconds)
96 return "0s";
97
98 if (total_seconds < 0)
99 {
100 *buf = '-';
101 buf++;
102 total_seconds = -total_seconds;
103 }
104
105 years = total_seconds / HDEF_y;
106 total_seconds %= HDEF_y;
107
108 months = total_seconds / HDEF_M;
109 total_seconds %= HDEF_M;
110
111 days = total_seconds / HDEF_d;
112 total_seconds %= HDEF_d;
113
114 hours = total_seconds / HDEF_h;
115 total_seconds %= HDEF_h;
116
117 minutes = total_seconds / HDEF_m;
118 total_seconds %= HDEF_m;
119
120 seconds = total_seconds;
121
122 TIME_PRINT(years, "y");
123 TIME_PRINT(months, "M");
124 TIME_PRINT(days, "d");
125 TIME_PRINT(hours, "h");
126 TIME_PRINT(minutes, "m");
127 TIME_PRINT(seconds, "s");
128
129 if (*buffer != '-')
130 return buffer+1;
131 else
132 return buffer;
133 }
134
135 int helpmod_read_strtime(const char *str)
136 {
137 int sum = 0;
138 int tmp_sum;
139 int tmp;
140
141
142 while (*str)
143 {
144 if (!sscanf(str, "%d%n", &tmp_sum, &tmp))
145 return -1;
146 str+=tmp;
147
148 switch (*str)
149 {
150 case 's':
151 break;
152 case 'm':
153 tmp_sum*=HDEF_m;
154 break;
155 case 'h':
156 tmp_sum*=HDEF_h;
157 break;
158 case 'd':
159 tmp_sum*=HDEF_d;
160 break;
161 case 'w':
162 tmp_sum*=HDEF_w;
163 break;
164 case 'M':
165 tmp_sum*=HDEF_M;
166 break;
167 case 'y':
168 tmp_sum*=HDEF_y;
169 break;
170 default: /* includes '\0' */
171 return -1;
172 }
173
174 str++;
175 /* sanity checks */
176 if (tmp_sum > 10 * HDEF_y)
177 return -1;
178 sum+=tmp_sum;
179 if (sum > 10 * HDEF_y)
180 return -1;
181 }
182 return sum;
183 }
184
185 int hword_count(const char *ptr)
186 {
187 int wordc = 0;
188
189 while (*ptr)
190 {
191 while (isspace(*ptr) && *ptr)
192 ptr++;
193 if (*ptr)
194 wordc++;
195 while (!isspace(*ptr) && *ptr)
196 ptr++;
197 }
198
199 return wordc;
200 }
201
202 int helpmod_is_lame_line(const char *line)
203 {
204 const char lamechars[] = {(char)2, (char)3, (char)22, (char)31, (char)0};
205 if (strpbrk(line, lamechars) != NULL)
206 return 1;
207 /*
208 if (strchr(line, (char)2)) bold
209 return 1;
210 if (strchr(line, (char)3)) colour
211 return 1;
212 if (strchr(line, (char)22)) reverse
213 return 1;
214 if (strchr(line, (char)31)) underline
215 return 1;
216 */
217 return 0;
218 }
219
220 int strislower(const char *str)
221 {
222 for (;*str;str++)
223 if (isupper(*str))
224 return 0;
225 return 1;
226 }
227
228 int strisupper(const char *str)
229 {
230 for (;*str;str++)
231 if (islower(*str))
232 return 0;
233 return 1;
234 }
235
236 int strisalpha(const char *str)
237 {
238 for (;*str;str++)
239 if (!isalpha(*str))
240 return 0;
241 return 1;
242 }
243
244 int strnumcount(const char *str)
245 {
246 int count = 0;
247 for (;*str;str++)
248 if (isdigit(*str))
249 count++;
250 return count;
251 }