]> jfr.im git - irc/quakenet/newserv.git/blame - helpmod2/hgen.c
Merge pull request #1 from meeb/meeb
[irc/quakenet/newserv.git] / helpmod2 / hgen.c
CommitLineData
c86edd1d
Q
1#include <ctype.h>
2
3#include <stdlib.h>
4#include <stdio.h>
5#include <string.h>
6
7#include "hgen.h"
8
9int 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 */
22static 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
32int 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)\
75if (elem)\
76{\
0fe4b2b0 77 buf+=sprintf(buf, " %d%s", elem, marker);\
c86edd1d
Q
78}\
79
e908ecfa 80/* This implementation might look a little evil but it does work */
c86edd1d
Q
81const char *helpmod_strtime(int total_seconds)
82{
e908ecfa 83 static int buffer_index = 0;
84 static char buffers[3][64];
85
86 char *buf = buffers[buffer_index];
87 char *buffer = buf;
c86edd1d 88
0fe4b2b0 89 int years, months, days, hours, minutes, seconds;
c86edd1d 90
e908ecfa 91 buffer_index = (buffer_index+1) % 3;
92
c86edd1d
Q
93 /* trivial case */
94 if (!total_seconds)
95 return "0s";
96
97 if (total_seconds < 0)
98 {
e908ecfa 99 *buf = '-';
100 buf++;
c86edd1d
Q
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;
e908ecfa 117 total_seconds %= HDEF_m;
c86edd1d
Q
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
e908ecfa 128 if (*buffer != '-')
129 return buffer+1;
130 else
131 return buffer;
c86edd1d
Q
132}
133
134int helpmod_read_strtime(const char *str)
135{
9af95c3d 136 int sum = 0, tmp_sum, tmp;
c86edd1d
Q
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
181int 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
198int 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;
3a839281 203 else
204 return 0;
c86edd1d
Q
205}
206
207int strislower(const char *str)
208{
209 for (;*str;str++)
210 if (isupper(*str))
211 return 0;
212 return 1;
213}
214
215int strisupper(const char *str)
216{
217 for (;*str;str++)
218 if (islower(*str))
219 return 0;
220 return 1;
221}
222
223int strisalpha(const char *str)
224{
225 for (;*str;str++)
226 if (!isalpha(*str))
227 return 0;
228 return 1;
229}
230
231int strnumcount(const char *str)
232{
233 int count = 0;
234 for (;*str;str++)
235 if (isdigit(*str))
236 count++;
237 return count;
238}
3a839281 239
240float 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}
9af95c3d 247
248int 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}