diff options
author | Carsten Haitzler <raster@rasterman.com> | 2004-03-29 11:58:57 +0000 |
---|---|---|
committer | Carsten Haitzler <raster@rasterman.com> | 2004-03-29 11:58:57 +0000 |
commit | 811cd94fbab432b59866aed3b9a724fe66227a4a (patch) | |
tree | 5271e95060d2acd529857b5d6e6d06b1b5dd5522 /legacy/embryo/src/lib/embryo_str.c | |
parent | 419bd778771ae100be0402de0795b71052cbd58e (diff) |
more meaty api...
SVN revision: 9518
Diffstat (limited to '')
-rw-r--r-- | legacy/embryo/src/lib/embryo_str.c | 448 |
1 files changed, 448 insertions, 0 deletions
diff --git a/legacy/embryo/src/lib/embryo_str.c b/legacy/embryo/src/lib/embryo_str.c new file mode 100644 index 0000000000..51250a24c6 --- /dev/null +++ b/legacy/embryo/src/lib/embryo_str.c | |||
@@ -0,0 +1,448 @@ | |||
1 | #include "embryo_private.h" | ||
2 | #include <fnmatch.h> | ||
3 | |||
4 | #define STRGET(ep, str, par) { \ | ||
5 | Embryo_Cell *___cptr; \ | ||
6 | if ((___cptr = embryo_data_address_get(ep, par))) { \ | ||
7 | int ___l; \ | ||
8 | ___l = embryo_data_string_length_get(ep, ___cptr); \ | ||
9 | (str) = alloca(___l + 1); \ | ||
10 | if (str) embryo_data_string_get(ep, ___cptr, str); \ | ||
11 | } } | ||
12 | #define STRSET(ep, par, str) { \ | ||
13 | Embryo_Cell *___cptr; \ | ||
14 | if ((___cptr = embryo_data_address_get(ep, par))) { \ | ||
15 | embryo_data_string_set(ep, str, ___cptr); \ | ||
16 | } } | ||
17 | |||
18 | /* exported string api */ | ||
19 | |||
20 | static Embryo_Cell | ||
21 | _embryo_str_atoi(Embryo_Program *ep, Embryo_Cell *params) | ||
22 | { | ||
23 | char *s1; | ||
24 | |||
25 | /* params[1] = str */ | ||
26 | if (params[0] != (2 * sizeof(Embryo_Cell))) return 0; | ||
27 | STRGET(ep, s1, params[1]); | ||
28 | if (!s1) return 0; | ||
29 | return (Embryo_Cell)atoi(s1); | ||
30 | } | ||
31 | |||
32 | static Embryo_Cell | ||
33 | _embryo_str_fnmatch(Embryo_Program *ep, Embryo_Cell *params) | ||
34 | { | ||
35 | char *s1, *s2; | ||
36 | |||
37 | /* params[1] = glob */ | ||
38 | /* params[2] = str */ | ||
39 | if (params[0] != (2 * sizeof(Embryo_Cell))) return 0; | ||
40 | STRGET(ep, s1, params[1]); | ||
41 | STRGET(ep, s2, params[2]); | ||
42 | if ((!s1) || (!s2)) return -1; | ||
43 | return (Embryo_Cell)fnmatch(s1, s2, 0); | ||
44 | } | ||
45 | |||
46 | static Embryo_Cell | ||
47 | _embryo_str_strcmp(Embryo_Program *ep, Embryo_Cell *params) | ||
48 | { | ||
49 | char *s1, *s2; | ||
50 | |||
51 | /* params[1] = str1 */ | ||
52 | /* params[2] = str2 */ | ||
53 | if (params[0] != (2 * sizeof(Embryo_Cell))) return -1; | ||
54 | STRGET(ep, s1, params[1]); | ||
55 | STRGET(ep, s2, params[2]); | ||
56 | if ((!s1) || (!s2)) return -1; | ||
57 | return (Embryo_Cell)strcmp(s1, s2); | ||
58 | } | ||
59 | |||
60 | static Embryo_Cell | ||
61 | _embryo_str_strncmp(Embryo_Program *ep, Embryo_Cell *params) | ||
62 | { | ||
63 | char *s1, *s2; | ||
64 | |||
65 | /* params[1] = str1 */ | ||
66 | /* params[2] = str2 */ | ||
67 | /* params[3] = n */ | ||
68 | if (params[0] != (3 * sizeof(Embryo_Cell))) return 0; | ||
69 | if (params[3] < 0) params[3] = 0; | ||
70 | STRGET(ep, s1, params[1]); | ||
71 | STRGET(ep, s2, params[2]); | ||
72 | if ((!s1) || (!s2)) return -1; | ||
73 | return (Embryo_Cell)strncmp(s1, s2, (size_t)params[3]); | ||
74 | } | ||
75 | |||
76 | static Embryo_Cell | ||
77 | _embryo_str_strcpy(Embryo_Program *ep, Embryo_Cell *params) | ||
78 | { | ||
79 | char *s1; | ||
80 | |||
81 | /* params[1] = dst */ | ||
82 | /* params[2] = str */ | ||
83 | if (params[0] != (2 * sizeof(Embryo_Cell))) return 0; | ||
84 | STRGET(ep, s1, params[2]); | ||
85 | if (!s1) return 0; | ||
86 | STRSET(ep, params[1], s1); | ||
87 | return 0; | ||
88 | } | ||
89 | |||
90 | static Embryo_Cell | ||
91 | _embryo_str_strncpy(Embryo_Program *ep, Embryo_Cell *params) | ||
92 | { | ||
93 | char *s1; | ||
94 | int l; | ||
95 | |||
96 | /* params[1] = dst */ | ||
97 | /* params[2] = str */ | ||
98 | /* params[3] = n */ | ||
99 | if (params[0] != (3 * sizeof(Embryo_Cell))) return 0; | ||
100 | if (params[3] < 0) params[3] = 0; | ||
101 | STRGET(ep, s1, params[2]); | ||
102 | if (!s1) return 0; | ||
103 | l = strlen(s1); | ||
104 | if (l > params[3]) s1[params[3]] = 0; | ||
105 | STRSET(ep, params[1], s1); | ||
106 | return 0; | ||
107 | } | ||
108 | |||
109 | static Embryo_Cell | ||
110 | _embryo_str_strlen(Embryo_Program *ep, Embryo_Cell *params) | ||
111 | { | ||
112 | char *s1; | ||
113 | |||
114 | /* params[1] = str */ | ||
115 | if (params[0] != (1 * sizeof(Embryo_Cell))) return 0; | ||
116 | STRGET(ep, s1, params[1]); | ||
117 | if (!s1) return 0; | ||
118 | return (Embryo_Cell)strlen(s1); | ||
119 | } | ||
120 | |||
121 | static Embryo_Cell | ||
122 | _embryo_str_strcat(Embryo_Program *ep, Embryo_Cell *params) | ||
123 | { | ||
124 | char *s1, *s2, *s3; | ||
125 | |||
126 | /* params[1] = dsr */ | ||
127 | /* params[2] = str */ | ||
128 | if (params[0] != (2 * sizeof(Embryo_Cell))) return 0; | ||
129 | STRGET(ep, s1, params[1]); | ||
130 | STRGET(ep, s2, params[2]); | ||
131 | if ((!s1) || (!s2)) return 0; | ||
132 | s3 = alloca(strlen(s1) + strlen(s2) + 1); | ||
133 | if (!s3) return 0; | ||
134 | strcpy(s3, s1); | ||
135 | strcat(s3, s2); | ||
136 | STRSET(ep, params[1], s3); | ||
137 | return 0; | ||
138 | } | ||
139 | |||
140 | static Embryo_Cell | ||
141 | _embryo_str_strncat(Embryo_Program *ep, Embryo_Cell *params) | ||
142 | { | ||
143 | char *s1, *s2, *s3; | ||
144 | int l1, l2; | ||
145 | |||
146 | /* params[1] = dst */ | ||
147 | /* params[2] = str */ | ||
148 | /* params[3] = n */ | ||
149 | if (params[0] != (3 * sizeof(Embryo_Cell))) return 0; | ||
150 | if (params[3] < 0) params[3] = 0; | ||
151 | STRGET(ep, s1, params[1]); | ||
152 | STRGET(ep, s2, params[2]); | ||
153 | if ((!s1) || (!s2)) return 0; | ||
154 | l1 = strlen(s1); | ||
155 | l2 = strlen(s2); | ||
156 | s3 = alloca(l1 + l2 + 1); | ||
157 | if (!s3) return 0; | ||
158 | strcpy(s3, s1); | ||
159 | strncat(s3, s2, params[3]); | ||
160 | if (l2 >= params[3]) s3[l1 + params[3]] = 0; | ||
161 | STRSET(ep, params[1], s3); | ||
162 | return 0; | ||
163 | } | ||
164 | |||
165 | static Embryo_Cell | ||
166 | _embryo_str_strprep(Embryo_Program *ep, Embryo_Cell *params) | ||
167 | { | ||
168 | char *s1, *s2, *s3; | ||
169 | |||
170 | /* params[1] = dst */ | ||
171 | /* params[2] = str */ | ||
172 | if (params[0] != (2 * sizeof(Embryo_Cell))) return 0; | ||
173 | STRGET(ep, s1, params[1]); | ||
174 | STRGET(ep, s2, params[2]); | ||
175 | if ((!s1) || (!s2)) return 0; | ||
176 | s3 = alloca(strlen(s1) + strlen(s2) + 1); | ||
177 | if (!s3) return 0; | ||
178 | strcpy(s3, s2); | ||
179 | strcat(s3, s1); | ||
180 | STRSET(ep, params[1], s3); | ||
181 | return 0; | ||
182 | } | ||
183 | |||
184 | static Embryo_Cell | ||
185 | _embryo_str_strnprep(Embryo_Program *ep, Embryo_Cell *params) | ||
186 | { | ||
187 | char *s1, *s2, *s3; | ||
188 | int l1, l2; | ||
189 | |||
190 | /* params[1] = dst */ | ||
191 | /* params[2] = str */ | ||
192 | /* params[3] = n */ | ||
193 | if (params[0] != (3 * sizeof(Embryo_Cell))) return 0; | ||
194 | if (params[3] < 0) params[3] = 0; | ||
195 | STRGET(ep, s1, params[1]); | ||
196 | STRGET(ep, s2, params[2]); | ||
197 | if ((!s1) || (!s2)) return 0; | ||
198 | l1 = strlen(s1); | ||
199 | l2 = strlen(s2); | ||
200 | s3 = alloca(l1 + l2 + 1); | ||
201 | if (!s3) return 0; | ||
202 | strncpy(s3, s2, params[3]); | ||
203 | if (params[3] <= l2) s3[params[3]] = 0; | ||
204 | strcat(s3, s1); | ||
205 | STRSET(ep, params[1], s3); | ||
206 | return 0; | ||
207 | } | ||
208 | |||
209 | static Embryo_Cell | ||
210 | _embryo_str_strcut(Embryo_Program *ep, Embryo_Cell *params) | ||
211 | { | ||
212 | char *s1, *s2; | ||
213 | int l1; | ||
214 | |||
215 | /* params[1] = dst */ | ||
216 | /* params[2] = str */ | ||
217 | /* params[3] = n */ | ||
218 | /* params[4] = n2 */ | ||
219 | if (params[0] != (4 * sizeof(Embryo_Cell))) return 0; | ||
220 | if (params[3] < 0) params[3] = 0; | ||
221 | if (params[4] < params[3]) params[4] = params[3]; | ||
222 | STRGET(ep, s1, params[2]); | ||
223 | if (!s1) return 0; | ||
224 | l1 = strlen(s1); | ||
225 | if (params[3] >= l1) params[3] = l1; | ||
226 | if (params[4] >= l1) params[4] = l1; | ||
227 | if (params[4] == params[3]) | ||
228 | { | ||
229 | STRSET(ep, params[1], ""); | ||
230 | return 0; | ||
231 | } | ||
232 | s2 = alloca(params[4] - params[3] + 1); | ||
233 | strncpy(s2, s1 + params[3], params[4] - params[3]); | ||
234 | s2[params[4] - params[3]] = 0; | ||
235 | STRSET(ep, params[1], s2); | ||
236 | return 0; | ||
237 | } | ||
238 | |||
239 | static Embryo_Cell | ||
240 | _embryo_str_snprintf(Embryo_Program *ep, Embryo_Cell *params) | ||
241 | { | ||
242 | char *s1, *s2; | ||
243 | int i, o; | ||
244 | int inesc = 0; | ||
245 | int insub = 0; | ||
246 | int p, pnum; | ||
247 | |||
248 | /* params[1] = buf */ | ||
249 | /* params[2] = bufsize */ | ||
250 | /* params[3] = format_string */ | ||
251 | /* params[4] = first arg ... */ | ||
252 | if (params[0] < (3 * sizeof(Embryo_Cell))) return 0; | ||
253 | if (params[2] <= 0) return 0; | ||
254 | STRGET(ep, s1, params[3]); | ||
255 | if (!s1) return -1; | ||
256 | s2 = alloca(params[2] + 1); | ||
257 | if (!s2) return -1; | ||
258 | s2[0] = 0; | ||
259 | pnum = (params[0] / sizeof(Embryo_Cell)) - 3; | ||
260 | for (p = 0, o = 0; (s1[i]) && (o < (params[2] - 1)) && (p < pnum); i++) | ||
261 | { | ||
262 | if ((!inesc) && (!insub)) | ||
263 | { | ||
264 | if (s1[i] == '\\') inesc = 1; | ||
265 | else if (s1[i] == '%') insub = 1; | ||
266 | if ((!inesc) && (!insub)) | ||
267 | { | ||
268 | s2[o] = s1[i]; | ||
269 | o++; | ||
270 | } | ||
271 | } | ||
272 | else | ||
273 | { | ||
274 | if (inesc) | ||
275 | { | ||
276 | switch (s1[i]) | ||
277 | { | ||
278 | case 't': | ||
279 | s2[o] = '\t'; | ||
280 | o++; | ||
281 | break; | ||
282 | case 'n': | ||
283 | s2[o] = '\n'; | ||
284 | o++; | ||
285 | break; | ||
286 | default: | ||
287 | s2[o] = s1[i]; | ||
288 | o++; | ||
289 | break; | ||
290 | } | ||
291 | inesc = 0; | ||
292 | } | ||
293 | if (insub) | ||
294 | { | ||
295 | switch (s1[i]) | ||
296 | { | ||
297 | case '%': | ||
298 | s2[o] = '%'; | ||
299 | o++; | ||
300 | break; | ||
301 | case 'c': | ||
302 | s2[o] = params[4 + p]; | ||
303 | p++; | ||
304 | o++; | ||
305 | break; | ||
306 | case 'i': | ||
307 | case 'd': | ||
308 | case 'x': | ||
309 | case 'X': | ||
310 | { | ||
311 | char fmt[10] = ""; | ||
312 | char tmp[256] = ""; | ||
313 | int l; | ||
314 | |||
315 | if (s1[i] == 'i') strcpy(fmt, "%i"); | ||
316 | else if (s1[i] == 'd') strcpy(fmt, "%d"); | ||
317 | else if (s1[i] == 'x') strcpy(fmt, "%x"); | ||
318 | else if (s1[i] == 'X') strcpy(fmt, "%08x"); | ||
319 | snprintf(tmp, sizeof(tmp), fmt, params[4 + p]); | ||
320 | l = strlen(tmp); | ||
321 | if ((o + l) > (params[2] - 1)) | ||
322 | { | ||
323 | l = params[2] - 1 - o; | ||
324 | if (l < 0) l = 0; | ||
325 | tmp[l] = 0; | ||
326 | } | ||
327 | strcat(s2, tmp); | ||
328 | o += l; | ||
329 | p++; | ||
330 | } | ||
331 | break; | ||
332 | case 'f': | ||
333 | { | ||
334 | char tmp[256] = ""; | ||
335 | int l; | ||
336 | |||
337 | snprintf(tmp, sizeof(tmp), "%f", EMBRYO_CELL_TO_FLOAT(params[4 + p])); | ||
338 | l = strlen(tmp); | ||
339 | if ((o + l) > (params[2] - 1)) | ||
340 | { | ||
341 | l = params[2] - 1 - o; | ||
342 | if (l < 0) l = 0; | ||
343 | tmp[l] = 0; | ||
344 | } | ||
345 | strcat(s2, tmp); | ||
346 | o += l; | ||
347 | p++; | ||
348 | } | ||
349 | break; | ||
350 | case 's': | ||
351 | { | ||
352 | char *tmp; | ||
353 | int l; | ||
354 | |||
355 | STRGET(ep, tmp, params[4 + p]); | ||
356 | l = strlen(tmp); | ||
357 | if ((o + l) > (params[2] - 1)) | ||
358 | { | ||
359 | l = params[2] - 1 - o; | ||
360 | if (l < 0) l = 0; | ||
361 | tmp[l] = 0; | ||
362 | } | ||
363 | strcat(s2, tmp); | ||
364 | o += l; | ||
365 | p++; | ||
366 | } | ||
367 | break; | ||
368 | default: | ||
369 | break; | ||
370 | } | ||
371 | insub = 0; | ||
372 | } | ||
373 | } | ||
374 | } | ||
375 | s2[o] = 0; | ||
376 | |||
377 | STRSET(ep, params[1], s2); | ||
378 | return 0; | ||
379 | } | ||
380 | |||
381 | static Embryo_Cell | ||
382 | _embryo_str_strstr(Embryo_Program *ep, Embryo_Cell *params) | ||
383 | { | ||
384 | char *s1, *s2, *p; | ||
385 | |||
386 | /* params[1] = str */ | ||
387 | /* params[2] = ndl */ | ||
388 | if (params[0] != (2 * sizeof(Embryo_Cell))) return 0; | ||
389 | STRGET(ep, s1, params[1]); | ||
390 | STRGET(ep, s2, params[2]); | ||
391 | if ((!s1) || (!s2)) return -1; | ||
392 | p = strstr(s1, s2); | ||
393 | if (p == NULL) return -1; | ||
394 | return (Embryo_Cell)(p - s1); | ||
395 | } | ||
396 | |||
397 | static Embryo_Cell | ||
398 | _embryo_str_strchr(Embryo_Program *ep, Embryo_Cell *params) | ||
399 | { | ||
400 | char *s1, *s2, *p; | ||
401 | |||
402 | /* params[1] = str */ | ||
403 | /* params[2] = ch */ | ||
404 | if (params[0] != (2 * sizeof(Embryo_Cell))) return 0; | ||
405 | STRGET(ep, s1, params[1]); | ||
406 | STRGET(ep, s2, params[2]); | ||
407 | p = strchr(s1, s2[0]); | ||
408 | if (p == NULL) return -1; | ||
409 | return (Embryo_Cell)(p - s1); | ||
410 | } | ||
411 | |||
412 | static Embryo_Cell | ||
413 | _embryo_str_strrchr(Embryo_Program *ep, Embryo_Cell *params) | ||
414 | { | ||
415 | char *s1, *s2, *p; | ||
416 | |||
417 | /* params[1] = str */ | ||
418 | /* params[2] = ch */ | ||
419 | if (params[0] != (2 * sizeof(Embryo_Cell))) return 0; | ||
420 | STRGET(ep, s1, params[1]); | ||
421 | STRGET(ep, s2, params[2]); | ||
422 | p = strrchr(s1, s2[0]); | ||
423 | if (p == NULL) return -1; | ||
424 | return (Embryo_Cell)(p - s1); | ||
425 | } | ||
426 | |||
427 | /* functions used by the rest of embryo */ | ||
428 | |||
429 | void | ||
430 | _embryo_str_init(Embryo_Program *ep) | ||
431 | { | ||
432 | embryo_program_native_call_add(ep, "atoi", _embryo_str_atoi); | ||
433 | embryo_program_native_call_add(ep, "fnmtach", _embryo_str_fnmatch); | ||
434 | embryo_program_native_call_add(ep, "strcmp", _embryo_str_strcmp); | ||
435 | embryo_program_native_call_add(ep, "strncmp", _embryo_str_strncmp); | ||
436 | embryo_program_native_call_add(ep, "strcpy", _embryo_str_strcpy); | ||
437 | embryo_program_native_call_add(ep, "strncpy", _embryo_str_strncpy); | ||
438 | embryo_program_native_call_add(ep, "strlen", _embryo_str_strlen); | ||
439 | embryo_program_native_call_add(ep, "strcat", _embryo_str_strcat); | ||
440 | embryo_program_native_call_add(ep, "strncat", _embryo_str_strncat); | ||
441 | embryo_program_native_call_add(ep, "strprep", _embryo_str_strprep); | ||
442 | embryo_program_native_call_add(ep, "strnprep", _embryo_str_strnprep); | ||
443 | embryo_program_native_call_add(ep, "strcut", _embryo_str_strcut); | ||
444 | embryo_program_native_call_add(ep, "snprintf", _embryo_str_snprintf); | ||
445 | embryo_program_native_call_add(ep, "strstr", _embryo_str_strstr); | ||
446 | embryo_program_native_call_add(ep, "strchr", _embryo_str_strchr); | ||
447 | embryo_program_native_call_add(ep, "strrchr", _embryo_str_strrchr); | ||
448 | } | ||