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