Debian Codegraph @

debian/external_deps/fetk/punc/src/vf2c/rdfmt.c

raw
1 #include "f2c.h"
2 #include "fio.h"
3
4 #ifdef KR_headers
5 extern double atof();
6 #else
7 #undef abs
8 #undef min
9 #undef max
10 #include "stdlib.h"
11 #endif
12
13 #include "fmt.h"
14 #include "fp.h"
15 #include "ctype.h"
16 #ifdef __cplusplus
17 extern "C" {
18 #endif
19
20 static int
21 #ifdef KR_headers
22 rd_Z(n,w,len) Uint *n; ftnlen len;
23 #else
24 rd_Z(Uint *n, int w, ftnlen len)
25 #endif
26 {
27 long x[9];
28 char *s, *s0, *s1, *se, *t;
29 int ch, i, w1, w2;
30 static char hex[256];
31 static int one = 1;
32 int bad = 0;
33
34 if (!hex['0']) {
35 s = "0123456789";
36 while(ch = *s++)
37 hex[ch] = ch - '0' + 1;
38 s = "ABCDEF";
39 while(ch = *s++)
40 hex[ch] = hex[ch + 'a' - 'A'] = ch - 'A' + 11;
41 }
42 s = s0 = (char *)x;
43 s1 = (char *)&x[4];
44 se = (char *)&x[8];
45 if (len > 4*sizeof(long))
46 return errno = 117;
47 while (w) {
48 GET(ch);
49 if (ch==',' || ch=='\n')
50 break;
51 w--;
52 if (ch > ' ') {
53 if (!hex[ch & 0xff])
54 bad++;
55 *s++ = ch;
56 if (s == se) {
57 /* discard excess characters */
58 for(t = s0, s = s1; t < s1;)
59 *t++ = *s++;
60 s = s1;
61 }
62 }
63 }
64 if (bad)
65 return errno = 115;
66 w = (int)len;
67 w1 = s - s0;
68 w2 = w1+1 >> 1;
69 t = (char *)n;
70 if (*(char *)&one) {
71 /* little endian */
72 t += w - 1;
73 i = -1;
74 }
75 else
76 i = 1;
77 for(; w > w2; t += i, --w)
78 *t = 0;
79 if (!w)
80 return 0;
81 if (w < w2)
82 s0 = s - (w << 1);
83 else if (w1 & 1) {
84 *t = hex[*s0++ & 0xff] - 1;
85 if (!--w)
86 return 0;
87 t += i;
88 }
89 do {
90 *t = hex[*s0 & 0xff]-1 << 4 | hex[s0[1] & 0xff]-1;
91 t += i;
92 s0 += 2;
93 }
94 while(--w);
95 return 0;
96 }
97
98 static int
99 #ifdef KR_headers
100 rd_I(n,w,len, base) Uint *n; int w; ftnlen len; register int base;
101 #else
102 rd_I(Uint *n, int w, ftnlen len, register int base)
103 #endif
104 {
105 int ch, sign;
106 longint x = 0;
107
108 if (w <= 0)
109 goto have_x;
110 for(;;) {
111 GET(ch);
112 if (ch != ' ')
113 break;
114 if (!--w)
115 goto have_x;
116 }
117 sign = 0;
118 switch(ch) {
119 case ',':
120 case '\n':
121 w = 0;
122 goto have_x;
123 case '-':
124 sign = 1;
125 case '+':
126 break;
127 default:
128 if (ch >= '0' && ch <= '9') {
129 x = ch - '0';
130 break;
131 }
132 goto have_x;
133 }
134 while(--w) {
135 GET(ch);
136 if (ch >= '0' && ch <= '9') {
137 x = x*base + ch - '0';
138 continue;
139 }
140 if (ch != ' ') {
141 if (ch == '\n' || ch == ',')
142 w = 0;
143 break;
144 }
145 if (f__cblank)
146 x *= base;
147 }
148 if (sign)
149 x = -x;
150 have_x:
151 if(len == sizeof(integer))
152 n->il=x;
153 else if(len == sizeof(char))
154 n->ic = (char)x;
155 #ifdef Allow_TYQUAD
156 else if (len == sizeof(longint))
157 n->ili = x;
158 #endif
159 else
160 n->is = (short)x;
161 if (w) {
162 while(--w)
163 GET(ch);
164 return errno = 115;
165 }
166 return 0;
167 }
168
169 static int
170 #ifdef KR_headers
171 rd_L(n,w,len) ftnint *n; ftnlen len;
172 #else
173 rd_L(ftnint *n, int w, ftnlen len)
174 #endif
175 { int ch, dot, lv;
176
177 if (w <= 0)
178 goto bad;
179 for(;;) {
180 GET(ch);
181 --w;
182 if (ch != ' ')
183 break;
184 if (!w)
185 goto bad;
186 }
187 dot = 0;
188 retry:
189 switch(ch) {
190 case '.':
191 if (dot++ || !w)
192 goto bad;
193 GET(ch);
194 --w;
195 goto retry;
196 case 't':
197 case 'T':
198 lv = 1;
199 break;
200 case 'f':
201 case 'F':
202 lv = 0;
203 break;
204 default:
205 bad:
206 for(; w > 0; --w)
207 GET(ch);
208 /* no break */
209 case ',':
210 case '\n':
211 return errno = 116;
212 }
213 switch(len) {
214 case sizeof(char): *(char *)n = (char)lv; break;
215 case sizeof(short): *(short *)n = (short)lv; break;
216 default: *n = lv;
217 }
218 while(w-- > 0) {
219 GET(ch);
220 if (ch == ',' || ch == '\n')
221 break;
222 }
223 return 0;
224 }
225
226 static int
227 #ifdef KR_headers
228 rd_F(p, w, d, len) ufloat *p; ftnlen len;
229 #else
230 rd_F(ufloat *p, int w, int d, ftnlen len)
231 #endif
232 {
233 char s[FMAX+EXPMAXDIGS+4];
234 register int ch;
235 register char *sp, *spe, *sp1;
236 double x;
237 int scale1, se;
238 long e, exp;
239
240 sp1 = sp = s;
241 spe = sp + FMAX;
242 exp = -d;
243 x = 0.;
244
245 do {
246 GET(ch);
247 w--;
248 } while (ch == ' ' && w);
249 switch(ch) {
250 case '-': *sp++ = ch; sp1++; spe++;
251 case '+':
252 if (!w) goto zero;
253 --w;
254 GET(ch);
255 }
256 while(ch == ' ') {
257 blankdrop:
258 if (!w--) goto zero; GET(ch); }
259 while(ch == '0')
260 { if (!w--) goto zero; GET(ch); }
261 if (ch == ' ' && f__cblank)
262 goto blankdrop;
263 scale1 = f__scale;
264 while(isdigit(ch)) {
265 digloop1:
266 if (sp < spe) *sp++ = ch;
267 else ++exp;
268 digloop1e:
269 if (!w--) goto done;
270 GET(ch);
271 }
272 if (ch == ' ') {
273 if (f__cblank)
274 { ch = '0'; goto digloop1; }
275 goto digloop1e;
276 }
277 if (ch == '.') {
278 exp += d;
279 if (!w--) goto done;
280 GET(ch);
281 if (sp == sp1) { /* no digits yet */
282 while(ch == '0') {
283 skip01:
284 --exp;
285 skip0:
286 if (!w--) goto done;
287 GET(ch);
288 }
289 if (ch == ' ') {
290 if (f__cblank) goto skip01;
291 goto skip0;
292 }
293 }
294 while(isdigit(ch)) {
295 digloop2:
296 if (sp < spe)
297 { *sp++ = ch; --exp; }
298 digloop2e:
299 if (!w--) goto done;
300 GET(ch);
301 }
302 if (ch == ' ') {
303 if (f__cblank)
304 { ch = '0'; goto digloop2; }
305 goto digloop2e;
306 }
307 }
308 switch(ch) {
309 default:
310 break;
311 case '-': se = 1; goto signonly;
312 case '+': se = 0; goto signonly;
313 case 'e':
314 case 'E':
315 case 'd':
316 case 'D':
317 if (!w--)
318 goto bad;
319 GET(ch);
320 while(ch == ' ') {
321 if (!w--)
322 goto bad;
323 GET(ch);
324 }
325 se = 0;
326 switch(ch) {
327 case '-': se = 1;
328 case '+':
329 signonly:
330 if (!w--)
331 goto bad;
332 GET(ch);
333 }
334 while(ch == ' ') {
335 if (!w--)
336 goto bad;
337 GET(ch);
338 }
339 if (!isdigit(ch))
340 goto bad;
341
342 e = ch - '0';
343 for(;;) {
344 if (!w--)
345 { ch = '\n'; break; }
346 GET(ch);
347 if (!isdigit(ch)) {
348 if (ch == ' ') {
349 if (f__cblank)
350 ch = '0';
351 else continue;
352 }
353 else
354 break;
355 }
356 e = 10*e + ch - '0';
357 if (e > EXPMAX && sp > sp1)
358 goto bad;
359 }
360 if (se)
361 exp -= e;
362 else
363 exp += e;
364 scale1 = 0;
365 }
366 switch(ch) {
367 case '\n':
368 case ',':
369 break;
370 default:
371 bad:
372 return (errno = 115);
373 }
374 done:
375 if (sp > sp1) {
376 while(*--sp == '0')
377 ++exp;
378 if (exp -= scale1)
379 sprintf(sp+1, "e%ld", exp);
380 else
381 sp[1] = 0;
382 x = atof(s);
383 }
384 zero:
385 if (len == sizeof(real))
386 p->pf = x;
387 else
388 p->pd = x;
389 return(0);
390 }
391
392
393 static int
394 #ifdef KR_headers
395 rd_A(p,len) char *p; ftnlen len;
396 #else
397 rd_A(char *p, ftnlen len)
398 #endif
399 { int i,ch;
400 for(i=0;i<len;i++)
401 { GET(ch);
402 *p++=VAL(ch);
403 }
404 return(0);
405 }
406 static int
407 #ifdef KR_headers
408 rd_AW(p,w,len) char *p; ftnlen len;
409 #else
410 rd_AW(char *p, int w, ftnlen len)
411 #endif
412 { int i,ch;
413 if(w>=len)
414 { for(i=0;i<w-len;i++)
415 GET(ch);
416 for(i=0;i<len;i++)
417 { GET(ch);
418 *p++=VAL(ch);
419 }
420 return(0);
421 }
422 for(i=0;i<w;i++)
423 { GET(ch);
424 *p++=VAL(ch);
425 }
426 for(i=0;i<len-w;i++) *p++=' ';
427 return(0);
428 }
429 static int
430 #ifdef KR_headers
431 rd_H(n,s) char *s;
432 #else
433 rd_H(int n, char *s)
434 #endif
435 { int i,ch;
436 for(i=0;i<n;i++)
437 if((ch=(*f__getn)())<0) return(ch);
438 else *s++ = ch=='\n'?' ':ch;
439 return(1);
440 }
441 static int
442 #ifdef KR_headers
443 rd_POS(s) char *s;
444 #else
445 rd_POS(char *s)
446 #endif
447 { char quote;
448 int ch;
449 quote= *s++;
450 for(;*s;s++)
451 if(*s==quote && *(s+1)!=quote) break;
452 else if((ch=(*f__getn)())<0) return(ch);
453 else *s = ch=='\n'?' ':ch;
454 return(1);
455 }
456
457 int
458 #ifdef KR_headers
459 rd_ed(p,ptr,len) struct syl *p; char *ptr; ftnlen len;
460 #else
461 rd_ed(struct syl *p, char *ptr, ftnlen len)
462 #endif
463 { int ch;
464 for(;f__cursor>0;f__cursor--) if((ch=(*f__getn)())<0) return(ch);
465 if(f__cursor<0)
466 { if(f__recpos+f__cursor < 0) /*err(elist->cierr,110,"fmt")*/
467 f__cursor = -f__recpos; /* is this in the standard? */
468 if(f__external == 0) {
469 extern char *f__icptr;
470 f__icptr += f__cursor;
471 }
472 else if(f__curunit && f__curunit->useek)
473 (void) FSEEK(f__cf, f__cursor,SEEK_CUR);
474 else
475 err(f__elist->cierr,106,"fmt");
476 f__recpos += f__cursor;
477 f__cursor=0;
478 }
479 switch(p->op)
480 {
481 default: fprintf(stderr,"rd_ed, unexpected code: %d\n", p->op);
482 sig_die(f__fmtbuf, 1);
483 case IM:
484 case I: ch = rd_I((Uint *)ptr,p->p1,len, 10);
485 break;
486
487 /* O and OM don't work right for character, double, realcomplex, */
488 /* or doublecomplex, and they differ from Fortran 90 in */
489 /* showing a minus sign for negative values. */
490
491 case OM:
492 case O: ch = rd_I((Uint *)ptr, p->p1, len, 8);
493 break;
494 case L: ch = rd_L((ftnint *)ptr,p->p1,len);
495 break;
496 case A: ch = rd_A(ptr,len);
497 break;
498 case AW:
499 ch = rd_AW(ptr,p->p1,len);
500 break;
501 case E: case EE:
502 case D:
503 case G:
504 case GE:
505 case F: ch = rd_F((ufloat *)ptr,p->p1,p->p2.i[0],len);
506 break;
507
508 /* Z and ZM assume 8-bit bytes. */
509
510 case ZM:
511 case Z:
512 ch = rd_Z((Uint *)ptr, p->p1, len);
513 break;
514 }
515 if(ch == 0) return(ch);
516 else if(ch == EOF) return(EOF);
517 if (f__cf)
518 clearerr(f__cf);
519 return(errno);
520 }
521
522 int
523 #ifdef KR_headers
524 rd_ned(p) struct syl *p;
525 #else
526 rd_ned(struct syl *p)
527 #endif
528 {
529 switch(p->op)
530 {
531 default: fprintf(stderr,"rd_ned, unexpected code: %d\n", p->op);
532 sig_die(f__fmtbuf, 1);
533 case APOS:
534 return(rd_POS(p->p2.s));
535 case H: return(rd_H(p->p1,p->p2.s));
536 case SLASH: return((*f__donewrec)());
537 case TR:
538 case X: f__cursor += p->p1;
539 return(1);
540 case T: f__cursor=p->p1-f__recpos - 1;
541 return(1);
542 case TL: f__cursor -= p->p1;
543 if(f__cursor < -f__recpos) /* TL1000, 1X */
544 f__cursor = -f__recpos;
545 return(1);
546 }
547 }
548 #ifdef __cplusplus
549 }
550 #endif