1 /* $RCSfile: str.c,v $$Revision: 4.1 $$Date: 92/08/07 18:29:26 $
3 * Copyright (c) 1991-1997, Larry Wall
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.
16 str_numset(register STR *str, double num)
19 str->str_pok = 0; /* invalidate pointer */
20 str->str_nok = 1; /* validate number */
24 str_2ptr(register STR *str)
30 GROWSTR(&(str->str_ptr), &(str->str_len), 24);
33 sprintf(s,"%.20g",str->str_nval);
37 str->str_cur = s - str->str_ptr;
41 fprintf(stderr,"0x%lx ptr(%s)\n",(unsigned long)str,str->str_ptr);
47 str_2num(register STR *str)
51 if (str->str_len && str->str_pok)
52 str->str_nval = atof(str->str_ptr);
58 fprintf(stderr,"0x%lx num(%g)\n",(unsigned long)str,str->str_nval);
64 str_sset(STR *dstr, register STR *sstr)
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);
77 str_nset(register STR *str, register char *ptr, register int len)
79 GROWSTR(&(str->str_ptr), &(str->str_len), len + 1);
80 bcopy(ptr,str->str_ptr,len);
82 *(str->str_ptr+str->str_cur) = '\0';
83 str->str_nok = 0; /* invalidate number */
84 str->str_pok = 1; /* validate pointer */
88 str_set(register STR *str, register char *ptr)
95 GROWSTR(&(str->str_ptr), &(str->str_len), len + 1);
96 bcopy(ptr,str->str_ptr,len+1);
98 str->str_nok = 0; /* invalidate number */
99 str->str_pok = 1; /* validate pointer */
103 str_chop(register STR *str, register char *ptr) /* like set but assuming ptr is in str */
109 str->str_cur -= (ptr - str->str_ptr);
110 bcopy(ptr,str->str_ptr, str->str_cur + 1);
111 str->str_nok = 0; /* invalidate number */
112 str->str_pok = 1; /* validate pointer */
116 str_ncat(register STR *str, register char *ptr, register int len)
120 GROWSTR(&(str->str_ptr), &(str->str_len), str->str_cur + len + 1);
121 bcopy(ptr,str->str_ptr+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 */
129 str_scat(STR *dstr, register STR *sstr)
131 if (!(sstr->str_pok))
134 str_ncat(dstr,sstr->str_ptr,sstr->str_cur);
138 str_cat(register STR *str, register char *ptr)
147 GROWSTR(&(str->str_ptr), &(str->str_len), str->str_cur + len + 1);
148 bcopy(ptr,str->str_ptr+str->str_cur,len+1);
150 str->str_nok = 0; /* invalidate number */
151 str->str_pok = 1; /* validate pointer */
155 str_append_till(register STR *str, register char *from, register int delim, char *keeplist)
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 != '\\') {
170 if (from[1] == delim || from[1] == '\\')
175 else if (strchr(keeplist,from[1]))
180 else if (*from == delim)
185 str->str_cur = to - str->str_ptr;
196 freestrroot = str->str_link.str_next;
199 str = (STR *) safemalloc(sizeof(STR));
200 bzero((char*)str,sizeof(STR));
203 GROWSTR(&(str->str_ptr), &(str->str_len), len + 1);
208 str_grow(register STR *str, int len)
211 GROWSTR(&(str->str_ptr), &(str->str_len), len + 1);
214 /* make str point to what nstr did */
217 str_replace(register STR *str, register STR *nstr)
219 safefree(str->str_ptr);
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;
224 if (str->str_nok = nstr->str_nok)
225 str->str_nval = nstr->str_nval;
226 safefree((char*)nstr);
230 str_free(register STR *str)
235 str->str_ptr[0] = '\0';
239 str->str_link.str_next = freestrroot;
244 str_len(register STR *str)
257 str_gets(register STR *str, register FILE *fp)
259 #if defined(USE_STDIO_PTR) && defined(STDIO_PTR_LVALUE) && defined(STDIO_CNT_LVALUE)
260 /* Here is some breathtakingly efficient cheating */
262 register char *bp; /* we're going to steal some values */
263 register int cnt; /* from the stdio struct and put EVERYTHING */
264 register STDCHAR *ptr; /* in the innermost loop into registers */
265 register char newline = '\n'; /* (assuming at least 6 registers) */
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.
274 if (i == EOF) return Nullch;
275 *(--((*fp)->_ptr)) = (unsigned char) i;
279 cnt = FILE_cnt(fp); /* get count into register */
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 */
288 if ((*bp++ = *ptr++) == newline)
289 if (bp <= str->str_ptr || bp[-2] != '\\')
290 goto thats_all_folks;
297 FILE_cnt(fp) = cnt; /* deregisterize cnt and ptr */
299 i = getc(fp); /* get more characters */
301 ptr = FILE_ptr(fp); /* reregisterize cnt and ptr */
303 bpx = bp - str->str_ptr; /* prepare for possible relocation */
304 GROWSTR(&(str->str_ptr), &(str->str_len), str->str_cur + cnt + 1);
305 bp = str->str_ptr + bpx; /* reconstitute our pointer */
307 if (i == newline) { /* all done for now? */
309 goto thats_all_folks;
311 else if (i == EOF) /* all done for ever? */
312 goto thats_all_folks;
313 *bp++ = i; /* now go back to screaming loop */
317 FILE_cnt(fp) = cnt; /* put these back or we're in trouble */
320 str->str_cur = bp - str->str_ptr; /* set length */
322 #else /* USE_STDIO_PTR && STDIO_PTR_LVALUE && STDIO_CNT_LVALUE */
323 /* The big, slow, and stupid way */
325 static char buf[4192];
327 if (fgets(buf, sizeof buf, fp) != Nullch)
332 #endif /* USE_STDIO_PTR && STDIO_PTR_LVALUE && STDIO_CNT_LVALUE */
334 return str->str_cur ? str->str_ptr : Nullch;
338 str_inc(register STR *str)
345 str->str_nval += 1.0;
354 for (d = str->str_ptr; *d && *d != '.'; d++) ;
356 if (!isdigit(*str->str_ptr) || !isdigit(*d) ) {
357 str_numset(str,atof(str->str_ptr) + 1.0); /* punt */
360 while (d >= str->str_ptr) {
365 /* oh,oh, the number grew */
366 GROWSTR(&(str->str_ptr), &(str->str_len), str->str_cur + 2);
368 for (d = str->str_ptr + str->str_cur; d > str->str_ptr; d--)
374 str_dec(register STR *str)
381 str->str_nval -= 1.0;
386 str->str_nval = -1.0;
390 for (d = str->str_ptr; *d && *d != '.'; d++) ;
392 if (!isdigit(*str->str_ptr) || !isdigit(*d) || (*d == '0' && d == str->str_ptr)) {
393 str_numset(str,atof(str->str_ptr) - 1.0); /* punt */
396 while (d >= str->str_ptr) {
403 /* make a string that will exist for the duration of the expression eval */
406 str_mortal(STR *oldstr)
408 register STR *str = str_new(0);
409 static long tmps_size = -1;
411 str_sset(str,oldstr);
412 if (++tmps_max > tmps_size) {
413 tmps_size = tmps_max;
414 if (!(tmps_size & 127)) {
416 tmps_list = (STR**)saferealloc((char*)tmps_list,
417 (tmps_size + 128) * sizeof(STR*) );
419 tmps_list = (STR**)safemalloc(128 * sizeof(char*));
422 tmps_list[tmps_max] = str;
429 register STR *str = str_new(0);
438 register STR *str = str_new(0);