lwrite.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. #include "f2c.h"
  2. #include "fio.h"
  3. #include "fmt.h"
  4. #include "lio.h"
  5. #ifdef __cplusplus
  6. extern "C" {
  7. #endif
  8. ftnint L_len;
  9. int f__Aquote;
  10. static VOID
  11. donewrec(Void)
  12. {
  13. if (f__recpos)
  14. (*f__donewrec)();
  15. }
  16. static VOID
  17. #ifdef KR_headers
  18. lwrt_I(n) longint n;
  19. #else
  20. lwrt_I(longint n)
  21. #endif
  22. {
  23. char *p;
  24. int ndigit, sign;
  25. p = f__icvt(n, &ndigit, &sign, 10);
  26. if(f__recpos + ndigit >= L_len)
  27. donewrec();
  28. PUT(' ');
  29. if (sign)
  30. PUT('-');
  31. while(*p)
  32. PUT(*p++);
  33. }
  34. static VOID
  35. #ifdef KR_headers
  36. lwrt_L(n, len) ftnint n; ftnlen len;
  37. #else
  38. lwrt_L(ftnint n, ftnlen len)
  39. #endif
  40. {
  41. if(f__recpos+LLOGW>=L_len)
  42. donewrec();
  43. wrt_L((Uint *)&n,LLOGW, len);
  44. }
  45. static VOID
  46. #ifdef KR_headers
  47. lwrt_A(p,len) char *p; ftnlen len;
  48. #else
  49. lwrt_A(char *p, ftnlen len)
  50. #endif
  51. {
  52. int a;
  53. char *p1, *pe;
  54. a = 0;
  55. pe = p + len;
  56. if (f__Aquote) {
  57. a = 3;
  58. if (len > 1 && p[len-1] == ' ') {
  59. while(--len > 1 && p[len-1] == ' ');
  60. pe = p + len;
  61. }
  62. p1 = p;
  63. while(p1 < pe)
  64. if (*p1++ == '\'')
  65. a++;
  66. }
  67. if(f__recpos+len+a >= L_len)
  68. donewrec();
  69. if (a
  70. #ifndef OMIT_BLANK_CC
  71. || !f__recpos
  72. #endif
  73. )
  74. PUT(' ');
  75. if (a) {
  76. PUT('\'');
  77. while(p < pe) {
  78. if (*p == '\'')
  79. PUT('\'');
  80. PUT(*p++);
  81. }
  82. PUT('\'');
  83. }
  84. else
  85. while(p < pe)
  86. PUT(*p++);
  87. }
  88. static int
  89. #ifdef KR_headers
  90. l_g(buf, n) char *buf; double n;
  91. #else
  92. l_g(char *buf, double n)
  93. #endif
  94. {
  95. #ifdef Old_list_output
  96. doublereal absn;
  97. char *fmt;
  98. absn = n;
  99. if (absn < 0)
  100. absn = -absn;
  101. fmt = LLOW <= absn && absn < LHIGH ? LFFMT : LEFMT;
  102. #ifdef USE_STRLEN
  103. sprintf(buf, fmt, n);
  104. return strlen(buf);
  105. #else
  106. return sprintf(buf, fmt, n);
  107. #endif
  108. #else
  109. register char *b, c, c1;
  110. b = buf;
  111. *b++ = ' ';
  112. if (n < 0) {
  113. *b++ = '-';
  114. n = -n;
  115. }
  116. else
  117. *b++ = ' ';
  118. if (n == 0) {
  119. #ifdef SIGNED_ZEROS
  120. if (signbit_f2c(&n))
  121. *b++ = '-';
  122. #endif
  123. *b++ = '0';
  124. *b++ = '.';
  125. *b = 0;
  126. goto f__ret;
  127. }
  128. sprintf(b, LGFMT, n);
  129. switch(*b) {
  130. #ifndef WANT_LEAD_0
  131. case '0':
  132. while(b[0] = b[1])
  133. b++;
  134. break;
  135. #endif
  136. case 'i':
  137. case 'I':
  138. /* Infinity */
  139. case 'n':
  140. case 'N':
  141. /* NaN */
  142. while(*++b);
  143. break;
  144. default:
  145. /* Fortran 77 insists on having a decimal point... */
  146. for(;; b++)
  147. switch(*b) {
  148. case 0:
  149. *b++ = '.';
  150. *b = 0;
  151. goto f__ret;
  152. case '.':
  153. while(*++b);
  154. goto f__ret;
  155. case 'E':
  156. for(c1 = '.', c = 'E'; *b = c1;
  157. c1 = c, c = *++b);
  158. goto f__ret;
  159. }
  160. }
  161. f__ret:
  162. return b - buf;
  163. #endif
  164. }
  165. static VOID
  166. #ifdef KR_headers
  167. l_put(s) register char *s;
  168. #else
  169. l_put(register char *s)
  170. #endif
  171. {
  172. #ifdef KR_headers
  173. register void (*pn)() = f__putn;
  174. #else
  175. register void (*pn)(int) = f__putn;
  176. #endif
  177. register int c;
  178. while(c = *s++)
  179. (*pn)(c);
  180. }
  181. static VOID
  182. #ifdef KR_headers
  183. lwrt_F(n) double n;
  184. #else
  185. lwrt_F(double n)
  186. #endif
  187. {
  188. char buf[LEFBL];
  189. if(f__recpos + l_g(buf,n) >= L_len)
  190. donewrec();
  191. l_put(buf);
  192. }
  193. static VOID
  194. #ifdef KR_headers
  195. lwrt_C(a,b) double a,b;
  196. #else
  197. lwrt_C(double a, double b)
  198. #endif
  199. {
  200. char *ba, *bb, bufa[LEFBL], bufb[LEFBL];
  201. int al, bl;
  202. al = l_g(bufa, a);
  203. for(ba = bufa; *ba == ' '; ba++)
  204. --al;
  205. bl = l_g(bufb, b) + 1; /* intentionally high by 1 */
  206. for(bb = bufb; *bb == ' '; bb++)
  207. --bl;
  208. if(f__recpos + al + bl + 3 >= L_len)
  209. donewrec();
  210. #ifdef OMIT_BLANK_CC
  211. else
  212. #endif
  213. PUT(' ');
  214. PUT('(');
  215. l_put(ba);
  216. PUT(',');
  217. if (f__recpos + bl >= L_len) {
  218. (*f__donewrec)();
  219. #ifndef OMIT_BLANK_CC
  220. PUT(' ');
  221. #endif
  222. }
  223. l_put(bb);
  224. PUT(')');
  225. }
  226. int
  227. #ifdef KR_headers
  228. l_write(number,ptr,len,type) ftnint *number,type; char *ptr; ftnlen len;
  229. #else
  230. l_write(ftnint *number, char *ptr, ftnlen len, ftnint type)
  231. #endif
  232. {
  233. #define Ptr ((flex *)ptr)
  234. int i;
  235. longint x;
  236. double y,z;
  237. real *xx;
  238. doublereal *yy;
  239. for(i=0;i< *number; i++)
  240. {
  241. switch((int)type)
  242. {
  243. default: f__fatal(117,"unknown type in lio");
  244. case TYINT1:
  245. x = Ptr->flchar;
  246. goto xint;
  247. case TYSHORT:
  248. x=Ptr->flshort;
  249. goto xint;
  250. #ifdef Allow_TYQUAD
  251. case TYQUAD:
  252. x = Ptr->fllongint;
  253. goto xint;
  254. #endif
  255. case TYLONG:
  256. x=Ptr->flint;
  257. xint: lwrt_I(x);
  258. break;
  259. case TYREAL:
  260. y=Ptr->flreal;
  261. goto xfloat;
  262. case TYDREAL:
  263. y=Ptr->fldouble;
  264. xfloat: lwrt_F(y);
  265. break;
  266. case TYCOMPLEX:
  267. xx= &Ptr->flreal;
  268. y = *xx++;
  269. z = *xx;
  270. goto xcomplex;
  271. case TYDCOMPLEX:
  272. yy = &Ptr->fldouble;
  273. y= *yy++;
  274. z = *yy;
  275. xcomplex:
  276. lwrt_C(y,z);
  277. break;
  278. case TYLOGICAL1:
  279. x = Ptr->flchar;
  280. goto xlog;
  281. case TYLOGICAL2:
  282. x = Ptr->flshort;
  283. goto xlog;
  284. case TYLOGICAL:
  285. x = Ptr->flint;
  286. xlog: lwrt_L(Ptr->flint, len);
  287. break;
  288. case TYCHAR:
  289. lwrt_A(ptr,len);
  290. break;
  291. }
  292. ptr += len;
  293. }
  294. return(0);
  295. }
  296. #ifdef __cplusplus
  297. }
  298. #endif