]>
Commit | Line | Data |
---|---|---|
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 | ||
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 | ||
e908ecfa | 81 | /* This implementation might look a little evil but it does work */ |
c86edd1d Q |
82 | const char *helpmod_strtime(int total_seconds) |
83 | { | |
e908ecfa | 84 | static int buffer_index = 0; |
85 | static char buffers[3][64]; | |
86 | ||
87 | char *buf = buffers[buffer_index]; | |
88 | char *buffer = buf; | |
c86edd1d Q |
89 | |
90 | int years, months, days, hours, minutes, seconds, tmp; | |
91 | ||
e908ecfa | 92 | buffer_index = (buffer_index+1) % 3; |
93 | ||
c86edd1d Q |
94 | /* trivial case */ |
95 | if (!total_seconds) | |
96 | return "0s"; | |
97 | ||
98 | if (total_seconds < 0) | |
99 | { | |
e908ecfa | 100 | *buf = '-'; |
101 | buf++; | |
c86edd1d Q |
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; | |
e908ecfa | 118 | total_seconds %= HDEF_m; |
c86edd1d Q |
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 | ||
e908ecfa | 129 | if (*buffer != '-') |
130 | return buffer+1; | |
131 | else | |
132 | return buffer; | |
c86edd1d Q |
133 | } |
134 | ||
135 | int helpmod_read_strtime(const char *str) | |
136 | { | |
9af95c3d | 137 | int sum = 0, tmp_sum, tmp; |
c86edd1d Q |
138 | |
139 | while (*str) | |
140 | { | |
141 | if (!sscanf(str, "%d%n", &tmp_sum, &tmp)) | |
142 | return -1; | |
143 | str+=tmp; | |
144 | ||
145 | switch (*str) | |
146 | { | |
147 | case 's': | |
148 | break; | |
149 | case 'm': | |
150 | tmp_sum*=HDEF_m; | |
151 | break; | |
152 | case 'h': | |
153 | tmp_sum*=HDEF_h; | |
154 | break; | |
155 | case 'd': | |
156 | tmp_sum*=HDEF_d; | |
157 | break; | |
158 | case 'w': | |
159 | tmp_sum*=HDEF_w; | |
160 | break; | |
161 | case 'M': | |
162 | tmp_sum*=HDEF_M; | |
163 | break; | |
164 | case 'y': | |
165 | tmp_sum*=HDEF_y; | |
166 | break; | |
167 | default: /* includes '\0' */ | |
168 | return -1; | |
169 | } | |
170 | ||
171 | str++; | |
172 | /* sanity checks */ | |
173 | if (tmp_sum > 10 * HDEF_y) | |
174 | return -1; | |
175 | sum+=tmp_sum; | |
176 | if (sum > 10 * HDEF_y) | |
177 | return -1; | |
178 | } | |
179 | return sum; | |
180 | } | |
181 | ||
182 | int hword_count(const char *ptr) | |
183 | { | |
184 | int wordc = 0; | |
185 | ||
186 | while (*ptr) | |
187 | { | |
188 | while (isspace(*ptr) && *ptr) | |
189 | ptr++; | |
190 | if (*ptr) | |
191 | wordc++; | |
192 | while (!isspace(*ptr) && *ptr) | |
193 | ptr++; | |
194 | } | |
195 | ||
196 | return wordc; | |
197 | } | |
198 | ||
199 | int helpmod_is_lame_line(const char *line) | |
200 | { | |
201 | const char lamechars[] = {(char)2, (char)3, (char)22, (char)31, (char)0}; | |
202 | if (strpbrk(line, lamechars) != NULL) | |
203 | return 1; | |
3a839281 | 204 | else |
205 | return 0; | |
c86edd1d Q |
206 | } |
207 | ||
208 | int strislower(const char *str) | |
209 | { | |
210 | for (;*str;str++) | |
211 | if (isupper(*str)) | |
212 | return 0; | |
213 | return 1; | |
214 | } | |
215 | ||
216 | int strisupper(const char *str) | |
217 | { | |
218 | for (;*str;str++) | |
219 | if (islower(*str)) | |
220 | return 0; | |
221 | return 1; | |
222 | } | |
223 | ||
224 | int strisalpha(const char *str) | |
225 | { | |
226 | for (;*str;str++) | |
227 | if (!isalpha(*str)) | |
228 | return 0; | |
229 | return 1; | |
230 | } | |
231 | ||
232 | int strnumcount(const char *str) | |
233 | { | |
234 | int count = 0; | |
235 | for (;*str;str++) | |
236 | if (isdigit(*str)) | |
237 | count++; | |
238 | return count; | |
239 | } | |
3a839281 | 240 | |
241 | float helpmod_percentage(int larger, int smaller) | |
242 | { | |
243 | if (larger == 0) | |
244 | return 0.0; | |
245 | ||
246 | return 100.0 * ((float)smaller / (float)larger); | |
247 | } | |
9af95c3d | 248 | |
249 | int helpmod_select(const char *str, const char **strs, int *enums, int count) | |
250 | { | |
251 | int i; | |
252 | ||
253 | if (count == 0) | |
254 | return -1; | |
255 | ||
256 | for (i = 0;i < count;i++) | |
257 | if (!ci_strcmp(strs[i], str)) | |
258 | return enums[i]; | |
259 | ||
260 | return -1; | |
261 | } |