rdfmt.c 8.7 KB

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