PageRenderTime 231ms CodeModel.GetById 37ms RepoModel.GetById 0ms app.codeStats 0ms

/Sources/clapack/wrtfmt.c

https://code.google.com/p/photivo/
C | 365 lines | 347 code | 12 blank | 6 comment | 111 complexity | 2edaa01cb9edb94b8195af0f2a550eef MD5 | raw file
Possible License(s): GPL-3.0, LGPL-3.0, LGPL-2.0
  1. #include "f2c.h"
  2. #include "fio.h"
  3. #include "fmt.h"
  4. extern icilist *f__svic;
  5. extern char *f__icptr;
  6. static int
  7. mv_cur(Void) /* shouldn't use fseek because it insists on calling fflush */
  8. /* instead we know too much about stdio */
  9. {
  10. int cursor = f__cursor;
  11. f__cursor = 0;
  12. if(f__external == 0) {
  13. if(cursor < 0) {
  14. if(f__hiwater < f__recpos)
  15. f__hiwater = f__recpos;
  16. f__recpos += cursor;
  17. f__icptr += cursor;
  18. if(f__recpos < 0)
  19. err(f__elist->cierr, 110, "left off");
  20. }
  21. else if(cursor > 0) {
  22. if(f__recpos + cursor >= f__svic->icirlen)
  23. err(f__elist->cierr, 110, "recend");
  24. if(f__hiwater <= f__recpos)
  25. for(; cursor > 0; cursor--)
  26. (*f__putn)(' ');
  27. else if(f__hiwater <= f__recpos + cursor) {
  28. cursor -= f__hiwater - f__recpos;
  29. f__icptr += f__hiwater - f__recpos;
  30. f__recpos = f__hiwater;
  31. for(; cursor > 0; cursor--)
  32. (*f__putn)(' ');
  33. }
  34. else {
  35. f__icptr += cursor;
  36. f__recpos += cursor;
  37. }
  38. }
  39. return(0);
  40. }
  41. if (cursor > 0) {
  42. if(f__hiwater <= f__recpos)
  43. for(;cursor>0;cursor--) (*f__putn)(' ');
  44. else if(f__hiwater <= f__recpos + cursor) {
  45. cursor -= f__hiwater - f__recpos;
  46. f__recpos = f__hiwater;
  47. for(; cursor > 0; cursor--)
  48. (*f__putn)(' ');
  49. }
  50. else {
  51. f__recpos += cursor;
  52. }
  53. }
  54. else if (cursor < 0)
  55. {
  56. if(cursor + f__recpos < 0)
  57. err(f__elist->cierr,110,"left off");
  58. if(f__hiwater < f__recpos)
  59. f__hiwater = f__recpos;
  60. f__recpos += cursor;
  61. }
  62. return(0);
  63. }
  64. static int
  65. #ifdef KR_headers
  66. wrt_Z(n,w,minlen,len) Uint *n; int w, minlen; ftnlen len;
  67. #else
  68. wrt_Z(Uint *n, int w, int minlen, ftnlen len)
  69. #endif
  70. {
  71. register char *s, *se;
  72. register int i, w1;
  73. static int one = 1;
  74. static char hex[] = "0123456789ABCDEF";
  75. s = (char *)n;
  76. --len;
  77. if (*(char *)&one) {
  78. /* little endian */
  79. se = s;
  80. s += len;
  81. i = -1;
  82. }
  83. else {
  84. se = s + len;
  85. i = 1;
  86. }
  87. for(;; s += i)
  88. if (s == se || *s)
  89. break;
  90. w1 = (i*(se-s) << 1) + 1;
  91. if (*s & 0xf0)
  92. w1++;
  93. if (w1 > w)
  94. for(i = 0; i < w; i++)
  95. (*f__putn)('*');
  96. else {
  97. if ((minlen -= w1) > 0)
  98. w1 += minlen;
  99. while(--w >= w1)
  100. (*f__putn)(' ');
  101. while(--minlen >= 0)
  102. (*f__putn)('0');
  103. if (!(*s & 0xf0)) {
  104. (*f__putn)(hex[*s & 0xf]);
  105. if (s == se)
  106. return 0;
  107. s += i;
  108. }
  109. for(;; s += i) {
  110. (*f__putn)(hex[*s >> 4 & 0xf]);
  111. (*f__putn)(hex[*s & 0xf]);
  112. if (s == se)
  113. break;
  114. }
  115. }
  116. return 0;
  117. }
  118. static int
  119. #ifdef KR_headers
  120. wrt_I(n,w,len, base) Uint *n; ftnlen len; register int base;
  121. #else
  122. wrt_I(Uint *n, int w, ftnlen len, register int base)
  123. #endif
  124. { int ndigit,sign,spare,i;
  125. longint x;
  126. char *ans;
  127. if(len==sizeof(integer)) x=n->il;
  128. else if(len == sizeof(char)) x = n->ic;
  129. #ifdef Allow_TYQUAD
  130. else if (len == sizeof(longint)) x = n->ili;
  131. #endif
  132. else x=n->is;
  133. ans=f__icvt(x,&ndigit,&sign, base);
  134. spare=w-ndigit;
  135. if(sign || f__cplus) spare--;
  136. if(spare<0)
  137. for(i=0;i<w;i++) (*f__putn)('*');
  138. else
  139. { for(i=0;i<spare;i++) (*f__putn)(' ');
  140. if(sign) (*f__putn)('-');
  141. else if(f__cplus) (*f__putn)('+');
  142. for(i=0;i<ndigit;i++) (*f__putn)(*ans++);
  143. }
  144. return(0);
  145. }
  146. static int
  147. #ifdef KR_headers
  148. wrt_IM(n,w,m,len,base) Uint *n; ftnlen len; int base;
  149. #else
  150. wrt_IM(Uint *n, int w, int m, ftnlen len, int base)
  151. #endif
  152. { int ndigit,sign,spare,i,xsign;
  153. longint x;
  154. char *ans;
  155. if(sizeof(integer)==len) x=n->il;
  156. else if(len == sizeof(char)) x = n->ic;
  157. #ifdef Allow_TYQUAD
  158. else if (len == sizeof(longint)) x = n->ili;
  159. #endif
  160. else x=n->is;
  161. ans=f__icvt(x,&ndigit,&sign, base);
  162. if(sign || f__cplus) xsign=1;
  163. else xsign=0;
  164. if(ndigit+xsign>w || m+xsign>w)
  165. { for(i=0;i<w;i++) (*f__putn)('*');
  166. return(0);
  167. }
  168. if(x==0 && m==0)
  169. { for(i=0;i<w;i++) (*f__putn)(' ');
  170. return(0);
  171. }
  172. if(ndigit>=m)
  173. spare=w-ndigit-xsign;
  174. else
  175. spare=w-m-xsign;
  176. for(i=0;i<spare;i++) (*f__putn)(' ');
  177. if(sign) (*f__putn)('-');
  178. else if(f__cplus) (*f__putn)('+');
  179. for(i=0;i<m-ndigit;i++) (*f__putn)('0');
  180. for(i=0;i<ndigit;i++) (*f__putn)(*ans++);
  181. return(0);
  182. }
  183. static int
  184. #ifdef KR_headers
  185. wrt_AP(s) char *s;
  186. #else
  187. wrt_AP(char *s)
  188. #endif
  189. { char quote;
  190. int i;
  191. if(f__cursor && (i = mv_cur()))
  192. return i;
  193. quote = *s++;
  194. for(;*s;s++)
  195. { if(*s!=quote) (*f__putn)(*s);
  196. else if(*++s==quote) (*f__putn)(*s);
  197. else return(1);
  198. }
  199. return(1);
  200. }
  201. static int
  202. #ifdef KR_headers
  203. wrt_H(a,s) char *s;
  204. #else
  205. wrt_H(int a, char *s)
  206. #endif
  207. {
  208. int i;
  209. if(f__cursor && (i = mv_cur()))
  210. return i;
  211. while(a--) (*f__putn)(*s++);
  212. return(1);
  213. }
  214. #ifdef KR_headers
  215. int wrt_L(n,len, sz) Uint *n; ftnlen sz;
  216. #else
  217. int wrt_L(Uint *n, int len, ftnlen sz)
  218. #endif
  219. { int i;
  220. long x;
  221. if(sizeof(long)==sz) x=n->il;
  222. else if(sz == sizeof(char)) x = n->ic;
  223. else x=n->is;
  224. for(i=0;i<len-1;i++)
  225. (*f__putn)(' ');
  226. if(x) (*f__putn)('T');
  227. else (*f__putn)('F');
  228. return(0);
  229. }
  230. static int
  231. #ifdef KR_headers
  232. wrt_A(p,len) char *p; ftnlen len;
  233. #else
  234. wrt_A(char *p, ftnlen len)
  235. #endif
  236. {
  237. while(len-- > 0) (*f__putn)(*p++);
  238. return(0);
  239. }
  240. static int
  241. #ifdef KR_headers
  242. wrt_AW(p,w,len) char * p; ftnlen len;
  243. #else
  244. wrt_AW(char * p, int w, ftnlen len)
  245. #endif
  246. {
  247. while(w>len)
  248. { w--;
  249. (*f__putn)(' ');
  250. }
  251. while(w-- > 0)
  252. (*f__putn)(*p++);
  253. return(0);
  254. }
  255. static int
  256. #ifdef KR_headers
  257. wrt_G(p,w,d,e,len) ufloat *p; ftnlen len;
  258. #else
  259. wrt_G(ufloat *p, int w, int d, int e, ftnlen len)
  260. #endif
  261. { double up = 1,x;
  262. int i=0,oldscale,n,j;
  263. x = len==sizeof(real)?p->pf:p->pd;
  264. if(x < 0 ) x = -x;
  265. if(x<.1) {
  266. if (x != 0.)
  267. return(wrt_E(p,w,d,e,len));
  268. i = 1;
  269. goto have_i;
  270. }
  271. for(;i<=d;i++,up*=10)
  272. { if(x>=up) continue;
  273. have_i:
  274. oldscale = f__scale;
  275. f__scale = 0;
  276. if(e==0) n=4;
  277. else n=e+2;
  278. i=wrt_F(p,w-n,d-i,len);
  279. for(j=0;j<n;j++) (*f__putn)(' ');
  280. f__scale=oldscale;
  281. return(i);
  282. }
  283. return(wrt_E(p,w,d,e,len));
  284. }
  285. #ifdef KR_headers
  286. int w_ed(p,ptr,len) struct syl *p; char *ptr; ftnlen len;
  287. #else
  288. int w_ed(struct syl *p, char *ptr, ftnlen len)
  289. #endif
  290. {
  291. int i;
  292. if(f__cursor && (i = mv_cur()))
  293. return i;
  294. switch(p->op)
  295. {
  296. default:
  297. fprintf(stderr,"w_ed, unexpected code: %d\n", p->op);
  298. sig_die(f__fmtbuf, 1);
  299. case I: return(wrt_I((Uint *)ptr,p->p1,len, 10));
  300. case IM:
  301. return(wrt_IM((Uint *)ptr,p->p1,p->p2.i[0],len,10));
  302. /* O and OM don't work right for character, double, complex, */
  303. /* or doublecomplex, and they differ from Fortran 90 in */
  304. /* showing a minus sign for negative values. */
  305. case O: return(wrt_I((Uint *)ptr, p->p1, len, 8));
  306. case OM:
  307. return(wrt_IM((Uint *)ptr,p->p1,p->p2.i[0],len,8));
  308. case L: return(wrt_L((Uint *)ptr,p->p1, len));
  309. case A: return(wrt_A(ptr,len));
  310. case AW:
  311. return(wrt_AW(ptr,p->p1,len));
  312. case D:
  313. case E:
  314. case EE:
  315. return(wrt_E((ufloat *)ptr,p->p1,p->p2.i[0],p->p2.i[1],len));
  316. case G:
  317. case GE:
  318. return(wrt_G((ufloat *)ptr,p->p1,p->p2.i[0],p->p2.i[1],len));
  319. case F: return(wrt_F((ufloat *)ptr,p->p1,p->p2.i[0],len));
  320. /* Z and ZM assume 8-bit bytes. */
  321. case Z: return(wrt_Z((Uint *)ptr,p->p1,0,len));
  322. case ZM:
  323. return(wrt_Z((Uint *)ptr,p->p1,p->p2.i[0],len));
  324. }
  325. }
  326. #ifdef KR_headers
  327. int w_ned(p) struct syl *p;
  328. #else
  329. int w_ned(struct syl *p)
  330. #endif
  331. {
  332. switch(p->op)
  333. {
  334. default: fprintf(stderr,"w_ned, unexpected code: %d\n", p->op);
  335. sig_die(f__fmtbuf, 1);
  336. case SLASH:
  337. return((*f__donewrec)());
  338. case T: f__cursor = p->p1-f__recpos - 1;
  339. return(1);
  340. case TL: f__cursor -= p->p1;
  341. if(f__cursor < -f__recpos) /* TL1000, 1X */
  342. f__cursor = -f__recpos;
  343. return(1);
  344. case TR:
  345. case X:
  346. f__cursor += p->p1;
  347. return(1);
  348. case APOS:
  349. return(wrt_AP(p->p2.s));
  350. case H:
  351. return(wrt_H(p->p1,p->p2.s));
  352. }
  353. }