1 /* $RCSfile: str.c,v $$Revision: 4.1 $$Date: 92/08/07 18:29:26 $
3 * Copyright (c) 1991-2001, 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(%"NVgf")\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 memcpy(str->str_ptr,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 memcpy(str->str_ptr,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 memcpy(str->str_ptr, 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 memcpy(str->str_ptr+str->str_cur, ptr, 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 memcpy(str->str_ptr+str->str_cur, ptr, 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 memset((char*)str,0,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 */
285 ptr = (STDCHAR*)FILE_ptr(fp);
288 if ((*bp++ = *ptr++) == newline) {
289 if (bp <= str->str_ptr || bp[-2] != '\\')
290 goto thats_all_folks;
298 FILE_cnt(fp) = cnt; /* deregisterize cnt and ptr */
299 FILE_ptr(fp) = (void*)ptr; /* LHS STDCHAR* cast non-portable */
300 i = getc(fp); /* get more characters */
302 ptr = FILE_ptr(fp); /* reregisterize cnt and ptr */
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 */
308 if (i == newline) { /* all done for now? */
310 goto thats_all_folks;
312 else if (i == EOF) /* all done for ever? */
313 goto thats_all_folks;
314 *bp++ = i; /* now go back to screaming loop */
318 FILE_cnt(fp) = cnt; /* put these back or we're in trouble */
319 FILE_ptr(fp) = (STDCHAR*)ptr;
321 str->str_cur = bp - str->str_ptr; /* set length */
323 #else /* USE_STDIO_PTR && STDIO_PTR_LVALUE && STDIO_CNT_LVALUE */
324 /* The big, slow, and stupid way */
326 static char buf[4192];
328 if (fgets(buf, sizeof buf, fp) != Nullch)
333 #endif /* USE_STDIO_PTR && STDIO_PTR_LVALUE && STDIO_CNT_LVALUE */
335 return str->str_cur ? str->str_ptr : Nullch;
339 str_inc(register STR *str)
346 str->str_nval += 1.0;
355 for (d = str->str_ptr; *d && *d != '.'; d++) ;
357 if (!isDIGIT(*str->str_ptr) || !isDIGIT(*d) ) {
358 str_numset(str,atof(str->str_ptr) + 1.0); /* punt */
361 while (d >= str->str_ptr) {
366 /* oh,oh, the number grew */
367 GROWSTR(&(str->str_ptr), &(str->str_len), str->str_cur + 2);
369 for (d = str->str_ptr + str->str_cur; d > str->str_ptr; d--)
375 str_dec(register STR *str)
382 str->str_nval -= 1.0;
387 str->str_nval = -1.0;
391 for (d = str->str_ptr; *d && *d != '.'; d++) ;
393 if (!isDIGIT(*str->str_ptr) || !isDIGIT(*d) || (*d == '0' && d == str->str_ptr)) {
394 str_numset(str,atof(str->str_ptr) - 1.0); /* punt */
397 while (d >= str->str_ptr) {
404 /* make a string that will exist for the duration of the expression eval */
407 str_mortal(STR *oldstr)
409 register STR *str = str_new(0);
410 static long tmps_size = -1;
412 str_sset(str,oldstr);
413 if (++tmps_max > tmps_size) {
414 tmps_size = tmps_max;
415 if (!(tmps_size & 127)) {
417 tmps_list = (STR**)saferealloc((char*)tmps_list,
418 (tmps_size + 128) * sizeof(STR*) );
420 tmps_list = (STR**)safemalloc(128 * sizeof(char*));
423 tmps_list[tmps_max] = str;
430 register STR *str = str_new(0);
439 register STR *str = str_new(0);