Commit | Line | Data |
---|---|---|
79072805 | 1 | /* $RCSfile: str.c,v $$Revision: 4.1 $$Date: 92/08/07 18:29:26 $ |
a687059c | 2 | * |
bc89e66f | 3 | * Copyright (c) 1991-2001, Larry Wall |
a687059c | 4 | * |
d48672a2 LW |
5 | * You may distribute under the terms of either the GNU General Public |
6 | * License or the Artistic License, as specified in the README file. | |
8d063cd8 LW |
7 | * |
8 | * $Log: str.c,v $ | |
8d063cd8 LW |
9 | */ |
10 | ||
8d063cd8 | 11 | #include "EXTERN.h" |
8d063cd8 | 12 | #include "a2p.h" |
9c8d0b29 | 13 | #include "util.h" |
8d063cd8 | 14 | |
9c8d0b29 | 15 | void |
f0f333f4 | 16 | str_numset(register STR *str, double num) |
8d063cd8 LW |
17 | { |
18 | str->str_nval = num; | |
19 | str->str_pok = 0; /* invalidate pointer */ | |
20 | str->str_nok = 1; /* validate number */ | |
21 | } | |
22 | ||
23 | char * | |
f0f333f4 | 24 | str_2ptr(register STR *str) |
8d063cd8 LW |
25 | { |
26 | register char *s; | |
27 | ||
28 | if (!str) | |
29 | return ""; | |
30 | GROWSTR(&(str->str_ptr), &(str->str_len), 24); | |
31 | s = str->str_ptr; | |
32 | if (str->str_nok) { | |
33 | sprintf(s,"%.20g",str->str_nval); | |
34 | while (*s) s++; | |
35 | } | |
36 | *s = '\0'; | |
37 | str->str_cur = s - str->str_ptr; | |
38 | str->str_pok = 1; | |
39 | #ifdef DEBUGGING | |
40 | if (debug & 32) | |
68dc0745 | 41 | fprintf(stderr,"0x%lx ptr(%s)\n",(unsigned long)str,str->str_ptr); |
8d063cd8 LW |
42 | #endif |
43 | return str->str_ptr; | |
44 | } | |
45 | ||
46 | double | |
f0f333f4 | 47 | str_2num(register STR *str) |
8d063cd8 LW |
48 | { |
49 | if (!str) | |
50 | return 0.0; | |
51 | if (str->str_len && str->str_pok) | |
52 | str->str_nval = atof(str->str_ptr); | |
53 | else | |
54 | str->str_nval = 0.0; | |
55 | str->str_nok = 1; | |
56 | #ifdef DEBUGGING | |
57 | if (debug & 32) | |
20d72259 | 58 | fprintf(stderr,"0x%lx num(%g)\n",(unsigned long)str,str->str_nval); |
8d063cd8 LW |
59 | #endif |
60 | return str->str_nval; | |
61 | } | |
62 | ||
9c8d0b29 | 63 | void |
f0f333f4 | 64 | str_sset(STR *dstr, register STR *sstr) |
8d063cd8 LW |
65 | { |
66 | if (!sstr) | |
67 | str_nset(dstr,No,0); | |
68 | else if (sstr->str_nok) | |
69 | str_numset(dstr,sstr->str_nval); | |
70 | else if (sstr->str_pok) | |
71 | str_nset(dstr,sstr->str_ptr,sstr->str_cur); | |
72 | else | |
73 | str_nset(dstr,"",0); | |
74 | } | |
75 | ||
9c8d0b29 | 76 | void |
f0f333f4 | 77 | str_nset(register STR *str, register char *ptr, register int len) |
8d063cd8 LW |
78 | { |
79 | GROWSTR(&(str->str_ptr), &(str->str_len), len + 1); | |
2ef53570 | 80 | memcpy(str->str_ptr,ptr,len); |
8d063cd8 LW |
81 | str->str_cur = len; |
82 | *(str->str_ptr+str->str_cur) = '\0'; | |
83 | str->str_nok = 0; /* invalidate number */ | |
84 | str->str_pok = 1; /* validate pointer */ | |
85 | } | |
86 | ||
9c8d0b29 | 87 | void |
f0f333f4 | 88 | str_set(register STR *str, register char *ptr) |
8d063cd8 LW |
89 | { |
90 | register int len; | |
91 | ||
92 | if (!ptr) | |
93 | ptr = ""; | |
94 | len = strlen(ptr); | |
95 | GROWSTR(&(str->str_ptr), &(str->str_len), len + 1); | |
2ef53570 | 96 | memcpy(str->str_ptr,ptr,len+1); |
8d063cd8 LW |
97 | str->str_cur = len; |
98 | str->str_nok = 0; /* invalidate number */ | |
99 | str->str_pok = 1; /* validate pointer */ | |
100 | } | |
101 | ||
9c8d0b29 | 102 | void |
f0f333f4 NIS |
103 | str_chop(register STR *str, register char *ptr) /* like set but assuming ptr is in str */ |
104 | ||
105 | ||
8d063cd8 LW |
106 | { |
107 | if (!(str->str_pok)) | |
108 | str_2ptr(str); | |
109 | str->str_cur -= (ptr - str->str_ptr); | |
2ef53570 | 110 | memcpy(str->str_ptr, ptr, str->str_cur + 1); |
8d063cd8 LW |
111 | str->str_nok = 0; /* invalidate number */ |
112 | str->str_pok = 1; /* validate pointer */ | |
113 | } | |
114 | ||
9c8d0b29 | 115 | void |
f0f333f4 | 116 | str_ncat(register STR *str, register char *ptr, register int len) |
8d063cd8 LW |
117 | { |
118 | if (!(str->str_pok)) | |
119 | str_2ptr(str); | |
120 | GROWSTR(&(str->str_ptr), &(str->str_len), str->str_cur + len + 1); | |
2ef53570 | 121 | memcpy(str->str_ptr+str->str_cur, ptr, len); |
8d063cd8 LW |
122 | str->str_cur += len; |
123 | *(str->str_ptr+str->str_cur) = '\0'; | |
124 | str->str_nok = 0; /* invalidate number */ | |
125 | str->str_pok = 1; /* validate pointer */ | |
126 | } | |
127 | ||
9c8d0b29 | 128 | void |
f0f333f4 | 129 | str_scat(STR *dstr, register STR *sstr) |
8d063cd8 LW |
130 | { |
131 | if (!(sstr->str_pok)) | |
132 | str_2ptr(sstr); | |
133 | if (sstr) | |
134 | str_ncat(dstr,sstr->str_ptr,sstr->str_cur); | |
135 | } | |
136 | ||
9c8d0b29 | 137 | void |
f0f333f4 | 138 | str_cat(register STR *str, register char *ptr) |
8d063cd8 LW |
139 | { |
140 | register int len; | |
141 | ||
142 | if (!ptr) | |
143 | return; | |
144 | if (!(str->str_pok)) | |
145 | str_2ptr(str); | |
146 | len = strlen(ptr); | |
147 | GROWSTR(&(str->str_ptr), &(str->str_len), str->str_cur + len + 1); | |
2ef53570 | 148 | memcpy(str->str_ptr+str->str_cur, ptr, len+1); |
8d063cd8 LW |
149 | str->str_cur += len; |
150 | str->str_nok = 0; /* invalidate number */ | |
151 | str->str_pok = 1; /* validate pointer */ | |
152 | } | |
153 | ||
154 | char * | |
f0f333f4 | 155 | str_append_till(register STR *str, register char *from, register int delim, char *keeplist) |
8d063cd8 LW |
156 | { |
157 | register char *to; | |
158 | register int len; | |
159 | ||
160 | if (!from) | |
161 | return Nullch; | |
162 | len = strlen(from); | |
163 | GROWSTR(&(str->str_ptr), &(str->str_len), str->str_cur + len + 1); | |
164 | str->str_nok = 0; /* invalidate number */ | |
165 | str->str_pok = 1; /* validate pointer */ | |
166 | to = str->str_ptr+str->str_cur; | |
167 | for (; *from; from++,to++) { | |
168 | if (*from == '\\' && from[1] && delim != '\\') { | |
169 | if (!keeplist) { | |
170 | if (from[1] == delim || from[1] == '\\') | |
171 | from++; | |
172 | else | |
173 | *to++ = *from++; | |
174 | } | |
a0d0e21e | 175 | else if (strchr(keeplist,from[1])) |
8d063cd8 LW |
176 | *to++ = *from++; |
177 | else | |
178 | from++; | |
179 | } | |
180 | else if (*from == delim) | |
181 | break; | |
182 | *to = *from; | |
183 | } | |
184 | *to = '\0'; | |
185 | str->str_cur = to - str->str_ptr; | |
186 | return from; | |
187 | } | |
188 | ||
189 | STR * | |
f0f333f4 | 190 | str_new(int len) |
8d063cd8 LW |
191 | { |
192 | register STR *str; | |
193 | ||
194 | if (freestrroot) { | |
195 | str = freestrroot; | |
196 | freestrroot = str->str_link.str_next; | |
197 | } | |
198 | else { | |
199 | str = (STR *) safemalloc(sizeof(STR)); | |
2ef53570 | 200 | memset((char*)str,0,sizeof(STR)); |
8d063cd8 LW |
201 | } |
202 | if (len) | |
203 | GROWSTR(&(str->str_ptr), &(str->str_len), len + 1); | |
204 | return str; | |
205 | } | |
206 | ||
207 | void | |
f0f333f4 | 208 | str_grow(register STR *str, int len) |
8d063cd8 LW |
209 | { |
210 | if (len && str) | |
211 | GROWSTR(&(str->str_ptr), &(str->str_len), len + 1); | |
212 | } | |
213 | ||
214 | /* make str point to what nstr did */ | |
215 | ||
216 | void | |
f0f333f4 | 217 | str_replace(register STR *str, register STR *nstr) |
8d063cd8 | 218 | { |
5e212cb5 | 219 | safefree(str->str_ptr); |
8d063cd8 LW |
220 | str->str_ptr = nstr->str_ptr; |
221 | str->str_len = nstr->str_len; | |
222 | str->str_cur = nstr->str_cur; | |
223 | str->str_pok = nstr->str_pok; | |
b7953727 | 224 | if ((str->str_nok = nstr->str_nok)) |
8d063cd8 LW |
225 | str->str_nval = nstr->str_nval; |
226 | safefree((char*)nstr); | |
227 | } | |
228 | ||
229 | void | |
f0f333f4 | 230 | str_free(register STR *str) |
8d063cd8 LW |
231 | { |
232 | if (!str) | |
233 | return; | |
234 | if (str->str_len) | |
235 | str->str_ptr[0] = '\0'; | |
236 | str->str_cur = 0; | |
237 | str->str_nok = 0; | |
238 | str->str_pok = 0; | |
239 | str->str_link.str_next = freestrroot; | |
240 | freestrroot = str; | |
241 | } | |
242 | ||
9c8d0b29 | 243 | int |
f0f333f4 | 244 | str_len(register STR *str) |
8d063cd8 LW |
245 | { |
246 | if (!str) | |
247 | return 0; | |
248 | if (!(str->str_pok)) | |
249 | str_2ptr(str); | |
250 | if (str->str_len) | |
251 | return str->str_cur; | |
252 | else | |
253 | return 0; | |
254 | } | |
255 | ||
256 | char * | |
f0f333f4 | 257 | str_gets(register STR *str, register FILE *fp) |
8d063cd8 | 258 | { |
c2960299 AD |
259 | #if defined(USE_STDIO_PTR) && defined(STDIO_PTR_LVALUE) && defined(STDIO_CNT_LVALUE) |
260 | /* Here is some breathtakingly efficient cheating */ | |
8d063cd8 LW |
261 | |
262 | register char *bp; /* we're going to steal some values */ | |
263 | register int cnt; /* from the stdio struct and put EVERYTHING */ | |
378cc40b | 264 | register STDCHAR *ptr; /* in the innermost loop into registers */ |
8d063cd8 LW |
265 | register char newline = '\n'; /* (assuming at least 6 registers) */ |
266 | int i; | |
267 | int bpx; | |
268 | ||
44a8e56a | 269 | #if defined(VMS) |
270 | /* An ungetc()d char is handled separately from the regular | |
271 | * buffer, so we getc() it back out and stuff it in the buffer. | |
272 | */ | |
273 | i = getc(fp); | |
274 | if (i == EOF) return Nullch; | |
275 | *(--((*fp)->_ptr)) = (unsigned char) i; | |
276 | (*fp)->_cnt++; | |
277 | #endif | |
278 | ||
16d20bd9 | 279 | cnt = FILE_cnt(fp); /* get count into register */ |
8d063cd8 LW |
280 | str->str_nok = 0; /* invalidate number */ |
281 | str->str_pok = 1; /* validate pointer */ | |
282 | if (str->str_len <= cnt) /* make sure we have the room */ | |
283 | GROWSTR(&(str->str_ptr), &(str->str_len), cnt+1); | |
284 | bp = str->str_ptr; /* move these two too to registers */ | |
cc00df79 | 285 | ptr = (STDCHAR*)FILE_ptr(fp); |
8d063cd8 | 286 | for (;;) { |
378cc40b | 287 | while (--cnt >= 0) { |
3f939f22 | 288 | if ((*bp++ = *ptr++) == newline) { |
378cc40b LW |
289 | if (bp <= str->str_ptr || bp[-2] != '\\') |
290 | goto thats_all_folks; | |
291 | else { | |
292 | line++; | |
293 | bp -= 2; | |
294 | } | |
3f939f22 | 295 | } |
8d063cd8 LW |
296 | } |
297 | ||
16d20bd9 | 298 | FILE_cnt(fp) = cnt; /* deregisterize cnt and ptr */ |
cc00df79 | 299 | FILE_ptr(fp) = (void*)ptr; /* LHS STDCHAR* cast non-portable */ |
44a8e56a | 300 | i = getc(fp); /* get more characters */ |
16d20bd9 AD |
301 | cnt = FILE_cnt(fp); |
302 | ptr = FILE_ptr(fp); /* reregisterize cnt and ptr */ | |
8d063cd8 LW |
303 | |
304 | bpx = bp - str->str_ptr; /* prepare for possible relocation */ | |
305 | GROWSTR(&(str->str_ptr), &(str->str_len), str->str_cur + cnt + 1); | |
306 | bp = str->str_ptr + bpx; /* reconstitute our pointer */ | |
307 | ||
308 | if (i == newline) { /* all done for now? */ | |
309 | *bp++ = i; | |
310 | goto thats_all_folks; | |
311 | } | |
312 | else if (i == EOF) /* all done for ever? */ | |
313 | goto thats_all_folks; | |
314 | *bp++ = i; /* now go back to screaming loop */ | |
315 | } | |
316 | ||
317 | thats_all_folks: | |
16d20bd9 | 318 | FILE_cnt(fp) = cnt; /* put these back or we're in trouble */ |
cc00df79 | 319 | FILE_ptr(fp) = (STDCHAR*)ptr; |
8d063cd8 LW |
320 | *bp = '\0'; |
321 | str->str_cur = bp - str->str_ptr; /* set length */ | |
322 | ||
c2960299 AD |
323 | #else /* USE_STDIO_PTR && STDIO_PTR_LVALUE && STDIO_CNT_LVALUE */ |
324 | /* The big, slow, and stupid way */ | |
8d063cd8 LW |
325 | |
326 | static char buf[4192]; | |
327 | ||
328 | if (fgets(buf, sizeof buf, fp) != Nullch) | |
329 | str_set(str, buf); | |
330 | else | |
331 | str_set(str, No); | |
332 | ||
c2960299 | 333 | #endif /* USE_STDIO_PTR && STDIO_PTR_LVALUE && STDIO_CNT_LVALUE */ |
8d063cd8 LW |
334 | |
335 | return str->str_cur ? str->str_ptr : Nullch; | |
336 | } | |
337 | ||
338 | void | |
f0f333f4 | 339 | str_inc(register STR *str) |
8d063cd8 LW |
340 | { |
341 | register char *d; | |
342 | ||
343 | if (!str) | |
344 | return; | |
345 | if (str->str_nok) { | |
346 | str->str_nval += 1.0; | |
347 | str->str_pok = 0; | |
348 | return; | |
349 | } | |
350 | if (!str->str_pok) { | |
351 | str->str_nval = 1.0; | |
352 | str->str_nok = 1; | |
353 | return; | |
354 | } | |
355 | for (d = str->str_ptr; *d && *d != '.'; d++) ; | |
356 | d--; | |
3f939f22 | 357 | if (!isDIGIT(*str->str_ptr) || !isDIGIT(*d) ) { |
8d063cd8 LW |
358 | str_numset(str,atof(str->str_ptr) + 1.0); /* punt */ |
359 | return; | |
360 | } | |
361 | while (d >= str->str_ptr) { | |
362 | if (++*d <= '9') | |
363 | return; | |
364 | *(d--) = '0'; | |
365 | } | |
366 | /* oh,oh, the number grew */ | |
367 | GROWSTR(&(str->str_ptr), &(str->str_len), str->str_cur + 2); | |
368 | str->str_cur++; | |
369 | for (d = str->str_ptr + str->str_cur; d > str->str_ptr; d--) | |
370 | *d = d[-1]; | |
371 | *d = '1'; | |
372 | } | |
373 | ||
374 | void | |
f0f333f4 | 375 | str_dec(register STR *str) |
8d063cd8 LW |
376 | { |
377 | register char *d; | |
378 | ||
379 | if (!str) | |
380 | return; | |
381 | if (str->str_nok) { | |
382 | str->str_nval -= 1.0; | |
383 | str->str_pok = 0; | |
384 | return; | |
385 | } | |
386 | if (!str->str_pok) { | |
387 | str->str_nval = -1.0; | |
388 | str->str_nok = 1; | |
389 | return; | |
390 | } | |
391 | for (d = str->str_ptr; *d && *d != '.'; d++) ; | |
392 | d--; | |
3f939f22 | 393 | if (!isDIGIT(*str->str_ptr) || !isDIGIT(*d) || (*d == '0' && d == str->str_ptr)) { |
8d063cd8 LW |
394 | str_numset(str,atof(str->str_ptr) - 1.0); /* punt */ |
395 | return; | |
396 | } | |
397 | while (d >= str->str_ptr) { | |
398 | if (--*d >= '0') | |
399 | return; | |
400 | *(d--) = '9'; | |
401 | } | |
402 | } | |
403 | ||
404 | /* make a string that will exist for the duration of the expression eval */ | |
405 | ||
406 | STR * | |
f0f333f4 | 407 | str_mortal(STR *oldstr) |
8d063cd8 LW |
408 | { |
409 | register STR *str = str_new(0); | |
410 | static long tmps_size = -1; | |
411 | ||
412 | str_sset(str,oldstr); | |
413 | if (++tmps_max > tmps_size) { | |
414 | tmps_size = tmps_max; | |
415 | if (!(tmps_size & 127)) { | |
416 | if (tmps_size) | |
417 | tmps_list = (STR**)saferealloc((char*)tmps_list, | |
418 | (tmps_size + 128) * sizeof(STR*) ); | |
419 | else | |
420 | tmps_list = (STR**)safemalloc(128 * sizeof(char*)); | |
421 | } | |
422 | } | |
423 | tmps_list[tmps_max] = str; | |
424 | return str; | |
425 | } | |
426 | ||
427 | STR * | |
f0f333f4 | 428 | str_make(char *s) |
8d063cd8 LW |
429 | { |
430 | register STR *str = str_new(0); | |
431 | ||
432 | str_set(str,s); | |
433 | return str; | |
434 | } | |
435 | ||
436 | STR * | |
f0f333f4 | 437 | str_nmake(double n) |
8d063cd8 LW |
438 | { |
439 | register STR *str = str_new(0); | |
440 | ||
441 | str_numset(str,n); | |
442 | return str; | |
443 | } |