3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1999,
4 * 2001, 2002, 2005 by Larry Wall and others
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Artistic License, as specified in the README file.
15 str_numset(register STR *str, double num)
18 str->str_pok = 0; /* invalidate pointer */
19 str->str_nok = 1; /* validate number */
23 str_2ptr(register STR *str)
28 return (char *)""; /* probably safe - won't be written to */
29 GROWSTR(&(str->str_ptr), &(str->str_len), 24);
32 sprintf(s,"%.20g",str->str_nval);
36 str->str_cur = s - str->str_ptr;
40 fprintf(stderr,"0x%lx ptr(%s)\n",(unsigned long)str,str->str_ptr);
46 str_sset(STR *dstr, register STR *sstr)
50 else if (sstr->str_nok)
51 str_numset(dstr,sstr->str_nval);
52 else if (sstr->str_pok)
53 str_nset(dstr,sstr->str_ptr,sstr->str_cur);
59 str_nset(register STR *str, register const char *ptr, register int len)
61 GROWSTR(&(str->str_ptr), &(str->str_len), len + 1);
62 memcpy(str->str_ptr,ptr,len);
64 *(str->str_ptr+str->str_cur) = '\0';
65 str->str_nok = 0; /* invalidate number */
66 str->str_pok = 1; /* validate pointer */
70 str_set(register STR *str, register const char *ptr)
77 GROWSTR(&(str->str_ptr), &(str->str_len), len + 1);
78 memcpy(str->str_ptr,ptr,len+1);
80 str->str_nok = 0; /* invalidate number */
81 str->str_pok = 1; /* validate pointer */
85 str_ncat(register STR *str, register const char *ptr, register int len)
89 GROWSTR(&(str->str_ptr), &(str->str_len), str->str_cur + len + 1);
90 memcpy(str->str_ptr+str->str_cur, ptr, len);
92 *(str->str_ptr+str->str_cur) = '\0';
93 str->str_nok = 0; /* invalidate number */
94 str->str_pok = 1; /* validate pointer */
98 str_scat(STR *dstr, register STR *sstr)
100 if (!(sstr->str_pok))
103 str_ncat(dstr,sstr->str_ptr,sstr->str_cur);
107 str_cat(register STR *str, register const char *ptr)
116 GROWSTR(&(str->str_ptr), &(str->str_len), str->str_cur + len + 1);
117 memcpy(str->str_ptr+str->str_cur, ptr, len+1);
119 str->str_nok = 0; /* invalidate number */
120 str->str_pok = 1; /* validate pointer */
130 freestrroot = str->str_link.str_next;
133 str = (STR *) safemalloc(sizeof(STR));
134 memset((char*)str,0,sizeof(STR));
137 GROWSTR(&(str->str_ptr), &(str->str_len), len + 1);
141 /* make str point to what nstr did */
144 str_free(register STR *str)
149 str->str_ptr[0] = '\0';
153 str->str_link.str_next = freestrroot;
158 str_len(register STR *str)
171 str_gets(register STR *str, register FILE *fp)
173 #if defined(USE_STDIO_PTR) && defined(STDIO_PTR_LVALUE) && defined(STDIO_CNT_LVALUE)
174 /* Here is some breathtakingly efficient cheating */
176 register char *bp; /* we're going to steal some values */
177 register int cnt; /* from the stdio struct and put EVERYTHING */
178 register STDCHAR *ptr; /* in the innermost loop into registers */
179 register char newline = '\n'; /* (assuming at least 6 registers) */
184 /* An ungetc()d char is handled separately from the regular
185 * buffer, so we getc() it back out and stuff it in the buffer.
188 if (i == EOF) return NULL;
189 *(--((*fp)->_ptr)) = (unsigned char) i;
193 cnt = FILE_cnt(fp); /* get count into register */
194 str->str_nok = 0; /* invalidate number */
195 str->str_pok = 1; /* validate pointer */
196 if (str->str_len <= cnt) /* make sure we have the room */
197 GROWSTR(&(str->str_ptr), &(str->str_len), cnt+1);
198 bp = str->str_ptr; /* move these two too to registers */
199 ptr = (STDCHAR*)FILE_ptr(fp);
202 if ((*bp++ = *ptr++) == newline) {
203 if (bp <= str->str_ptr || bp[-2] != '\\')
204 goto thats_all_folks;
212 FILE_cnt(fp) = cnt; /* deregisterize cnt and ptr */
213 FILE_ptr(fp) = (void*)ptr; /* LHS STDCHAR* cast non-portable */
214 i = getc(fp); /* get more characters */
216 ptr = (STDCHAR*)FILE_ptr(fp); /* reregisterize cnt and ptr */
218 bpx = bp - str->str_ptr; /* prepare for possible relocation */
219 GROWSTR(&(str->str_ptr), &(str->str_len), str->str_cur + cnt + 1);
220 bp = str->str_ptr + bpx; /* reconstitute our pointer */
222 if (i == newline) { /* all done for now? */
224 goto thats_all_folks;
226 else if (i == EOF) /* all done for ever? */
227 goto thats_all_folks;
228 *bp++ = i; /* now go back to screaming loop */
232 FILE_cnt(fp) = cnt; /* put these back or we're in trouble */
233 FILE_ptr(fp) = (void*)ptr; /* LHS STDCHAR* cast non-portable */
235 str->str_cur = bp - str->str_ptr; /* set length */
237 #else /* USE_STDIO_PTR && STDIO_PTR_LVALUE && STDIO_CNT_LVALUE */
238 /* The big, slow, and stupid way */
240 static char buf[4192];
242 if (fgets(buf, sizeof buf, fp) != NULL)
247 #endif /* USE_STDIO_PTR && STDIO_PTR_LVALUE && STDIO_CNT_LVALUE */
249 return str->str_cur ? str->str_ptr : NULL;
253 str_make(const char *s)
255 register STR *str = str_new(0);