1 /* $RCSfile: str.c,v $$Revision: 4.1 $$Date: 92/08/07 18:29:26 $
3 * Copyright (c) 1991, 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.
21 str->str_pok = 0; /* invalidate pointer */
22 str->str_nok = 1; /* validate number */
33 GROWSTR(&(str->str_ptr), &(str->str_len), 24);
36 sprintf(s,"%.20g",str->str_nval);
40 str->str_cur = s - str->str_ptr;
44 fprintf(stderr,"0x%lx ptr(%s)\n",str,str->str_ptr);
55 if (str->str_len && str->str_pok)
56 str->str_nval = atof(str->str_ptr);
62 fprintf(stderr,"0x%lx num(%g)\n",str,str->str_nval);
74 else if (sstr->str_nok)
75 str_numset(dstr,sstr->str_nval);
76 else if (sstr->str_pok)
77 str_nset(dstr,sstr->str_ptr,sstr->str_cur);
88 GROWSTR(&(str->str_ptr), &(str->str_len), len + 1);
89 bcopy(ptr,str->str_ptr,len);
91 *(str->str_ptr+str->str_cur) = '\0';
92 str->str_nok = 0; /* invalidate number */
93 str->str_pok = 1; /* validate pointer */
106 GROWSTR(&(str->str_ptr), &(str->str_len), len + 1);
107 bcopy(ptr,str->str_ptr,len+1);
109 str->str_nok = 0; /* invalidate number */
110 str->str_pok = 1; /* validate pointer */
114 str_chop(str,ptr) /* like set but assuming ptr is in str */
120 str->str_cur -= (ptr - str->str_ptr);
121 bcopy(ptr,str->str_ptr, str->str_cur + 1);
122 str->str_nok = 0; /* invalidate number */
123 str->str_pok = 1; /* validate pointer */
127 str_ncat(str,ptr,len)
134 GROWSTR(&(str->str_ptr), &(str->str_len), str->str_cur + len + 1);
135 bcopy(ptr,str->str_ptr+str->str_cur,len);
137 *(str->str_ptr+str->str_cur) = '\0';
138 str->str_nok = 0; /* invalidate number */
139 str->str_pok = 1; /* validate pointer */
147 if (!(sstr->str_pok))
150 str_ncat(dstr,sstr->str_ptr,sstr->str_cur);
165 GROWSTR(&(str->str_ptr), &(str->str_len), str->str_cur + len + 1);
166 bcopy(ptr,str->str_ptr+str->str_cur,len+1);
168 str->str_nok = 0; /* invalidate number */
169 str->str_pok = 1; /* validate pointer */
173 str_append_till(str,from,delim,keeplist)
185 GROWSTR(&(str->str_ptr), &(str->str_len), str->str_cur + len + 1);
186 str->str_nok = 0; /* invalidate number */
187 str->str_pok = 1; /* validate pointer */
188 to = str->str_ptr+str->str_cur;
189 for (; *from; from++,to++) {
190 if (*from == '\\' && from[1] && delim != '\\') {
192 if (from[1] == delim || from[1] == '\\')
197 else if (strchr(keeplist,from[1]))
202 else if (*from == delim)
207 str->str_cur = to - str->str_ptr;
219 freestrroot = str->str_link.str_next;
222 str = (STR *) safemalloc(sizeof(STR));
223 bzero((char*)str,sizeof(STR));
226 GROWSTR(&(str->str_ptr), &(str->str_len), len + 1);
236 GROWSTR(&(str->str_ptr), &(str->str_len), len + 1);
239 /* make str point to what nstr did */
242 str_replace(str,nstr)
246 Safefree(str->str_ptr);
247 str->str_ptr = nstr->str_ptr;
248 str->str_len = nstr->str_len;
249 str->str_cur = nstr->str_cur;
250 str->str_pok = nstr->str_pok;
251 if (str->str_nok = nstr->str_nok)
252 str->str_nval = nstr->str_nval;
253 safefree((char*)nstr);
263 str->str_ptr[0] = '\0';
267 str->str_link.str_next = freestrroot;
290 #ifdef USE_STDIO_PTR /* Here is some breathtakingly efficient cheating */
292 register char *bp; /* we're going to steal some values */
293 register int cnt; /* from the stdio struct and put EVERYTHING */
294 register STDCHAR *ptr; /* in the innermost loop into registers */
295 register char newline = '\n'; /* (assuming at least 6 registers) */
299 cnt = FILE_cnt(fp); /* get count into register */
300 str->str_nok = 0; /* invalidate number */
301 str->str_pok = 1; /* validate pointer */
302 if (str->str_len <= cnt) /* make sure we have the room */
303 GROWSTR(&(str->str_ptr), &(str->str_len), cnt+1);
304 bp = str->str_ptr; /* move these two too to registers */
308 if ((*bp++ = *ptr++) == newline)
309 if (bp <= str->str_ptr || bp[-2] != '\\')
310 goto thats_all_folks;
317 FILE_cnt(fp) = cnt; /* deregisterize cnt and ptr */
319 i = _filbuf(fp); /* get more characters */
321 ptr = FILE_ptr(fp); /* reregisterize cnt and ptr */
323 bpx = bp - str->str_ptr; /* prepare for possible relocation */
324 GROWSTR(&(str->str_ptr), &(str->str_len), str->str_cur + cnt + 1);
325 bp = str->str_ptr + bpx; /* reconstitute our pointer */
327 if (i == newline) { /* all done for now? */
329 goto thats_all_folks;
331 else if (i == EOF) /* all done for ever? */
332 goto thats_all_folks;
333 *bp++ = i; /* now go back to screaming loop */
337 FILE_cnt(fp) = cnt; /* put these back or we're in trouble */
340 str->str_cur = bp - str->str_ptr; /* set length */
342 #else /* !USE_STDIO_PTR */ /* The big, slow, and stupid way */
344 static char buf[4192];
346 if (fgets(buf, sizeof buf, fp) != Nullch)
351 #endif /* USE_STDIO_PTR */
353 return str->str_cur ? str->str_ptr : Nullch;
365 str->str_nval += 1.0;
374 for (d = str->str_ptr; *d && *d != '.'; d++) ;
376 if (!isdigit(*str->str_ptr) || !isdigit(*d) ) {
377 str_numset(str,atof(str->str_ptr) + 1.0); /* punt */
380 while (d >= str->str_ptr) {
385 /* oh,oh, the number grew */
386 GROWSTR(&(str->str_ptr), &(str->str_len), str->str_cur + 2);
388 for (d = str->str_ptr + str->str_cur; d > str->str_ptr; d--)
402 str->str_nval -= 1.0;
407 str->str_nval = -1.0;
411 for (d = str->str_ptr; *d && *d != '.'; d++) ;
413 if (!isdigit(*str->str_ptr) || !isdigit(*d) || (*d == '0' && d == str->str_ptr)) {
414 str_numset(str,atof(str->str_ptr) - 1.0); /* punt */
417 while (d >= str->str_ptr) {
424 /* make a string that will exist for the duration of the expression eval */
430 register STR *str = str_new(0);
431 static long tmps_size = -1;
433 str_sset(str,oldstr);
434 if (++tmps_max > tmps_size) {
435 tmps_size = tmps_max;
436 if (!(tmps_size & 127)) {
438 tmps_list = (STR**)saferealloc((char*)tmps_list,
439 (tmps_size + 128) * sizeof(STR*) );
441 tmps_list = (STR**)safemalloc(128 * sizeof(char*));
444 tmps_list[tmps_max] = str;
452 register STR *str = str_new(0);
462 register STR *str = str_new(0);