home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / math / pac / display.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-05  |  11.9 KB  |  405 lines

  1. /* display.c */
  2. /**********************************************************************
  3. *    File Name     : display.c
  4. *    Function      : calculator, stack, status window displays of pac
  5. *    Author        : Istvan Mohos, 1987
  6. ***********************************************************************/
  7.  
  8. #include "defs.h"
  9.  
  10. /* display Mainbuf after wait_main_pipe:
  11.    trim Mainbuf; reformat Mainbuf data in Rebuf, Tmpbuf */
  12. display_accum(showflag)
  13. int showflag;
  14. {
  15.     int zerofill;
  16.     int dp_at = -1;
  17.     register int ri;
  18.     register char *dpptr;
  19.     char *tpt;
  20.     int tmpsiz, readsiz;
  21.     int noint = 0;
  22.     struct stk_cell *sr = &Stk[0];
  23.     static char *fid = "display_accum";
  24.  
  25.     _TR
  26.     Bc_error = 0;
  27.     Negative = Too_big = Has_dp = FALSE;
  28.     e_syntax();
  29.     e_divby0();
  30.     e_exponent();
  31.     e_bcexec();
  32.  
  33.     if (*Mainbuf == '-')
  34.         Negative = TRUE;
  35.  
  36.     dpptr = Mainbuf;
  37.     if (*dpptr == '.') 
  38.         noint = 1;
  39.     ri = 0;
  40.     while (*dpptr != '\0') { /* wait_pipe changed last newline to \0 */
  41.         ++ri;
  42.         if (*dpptr++ == '.') {
  43.             tpt = dpptr + Precision +1;
  44.             *tpt = '\0';  /* cut overenthusiastic bc digits */
  45.             if (tpt == dpptr + strlen(dpptr)) {
  46.                 if (round(Mainbuf, Mainbuf+strlen(Mainbuf))) {
  47.                     ++dpptr; /* decimal point shifted 1 byte to right */
  48.                     ++ri;
  49.                 }
  50.             }
  51.             Has_dp = TRUE;
  52.             dp_at = ri;
  53.         }
  54.     }
  55.     if (Has_dp) {               /* trailing zero suppression */
  56.         while (*--dpptr == '0') {
  57.             *dpptr = '\0';
  58.             --ri;
  59.         }
  60.         if (*dpptr == '.') {
  61.             if (noint) {
  62.                 *dpptr = '0';
  63.                 pac_err("underflow");
  64.             }
  65.             else {
  66.                 *dpptr = '\0';
  67.                 --ri;
  68.             }
  69.             Has_dp = FALSE;
  70.         }
  71.     }
  72.     readsiz = ri;
  73.  
  74.     if ((strlen(Mainbuf) - Negative - Has_dp) > DIGMAX) {
  75.         Too_big = TRUE;
  76.         if (dp_at >= ACCUMAX)
  77.             Has_dp = FALSE;
  78.         e_overflow();  /* this error leaves oversize number in buffer */
  79.         readsiz = DIGMAX + Negative + Has_dp;
  80.         Mainbuf[readsiz] = '\0';
  81.     }
  82.  
  83.     sprintf(sr->cell, "%c %-*.*s", *(Base_str + Obase),
  84.         STACKMAX-2, STACKMAX-2, Mainbuf);
  85.  
  86.     if(Justify == JL) {
  87.         strcpy(Rebuf, Mainbuf);
  88.         strcat(Rebuf, Sp44);
  89.         Rebuf[ACCUMAX] = '\0';
  90.         display(Rebuf);
  91.     }
  92.     else if (Justify == JR) {
  93.         strcpy(Rebuf, Sp44);
  94.         strcpy(&Rebuf[ACCUMAX - readsiz], Mainbuf);
  95.         Rebuf[ACCUMAX] = '\0';
  96.         display(Rebuf);
  97.     }
  98.     else {   /* JF */
  99.         strcpy(Tmpbuf, Mainbuf);
  100.         if (!Too_big) {
  101.             if (Has_dp) {
  102.                 strcat(Tmpbuf, Fix32);
  103.                 zerofill = Precision - (readsiz - dp_at);
  104.                 Tmpbuf[readsiz + zerofill] = '\0';
  105.             }
  106.             else
  107.                 sprintf(&Tmpbuf[readsiz], ".%.*s", Precision, Fix32);
  108.         }
  109.         Tmpbuf[DIGMAX + Negative + Has_dp] = '\0';
  110.         tmpsiz = strlen(Tmpbuf);
  111.         strcpy(Rebuf, Sp44);
  112.         strcpy(&Rebuf[ACCUMAX - tmpsiz], Tmpbuf);
  113.         display(Rebuf);
  114.     }
  115.  
  116.     if ((Stack == ENA) && Painted) {
  117.         pushstack(1);
  118.         stack_reg(1, 0);
  119.     }
  120.     move(CY=UTOP, CX=ULEFT);
  121.     if (showflag)
  122.         pfresh();
  123.     TR_
  124. }
  125.  
  126. /* break up long digit string with spaces, for formatted display */
  127. display(source)
  128. char *source;
  129. {
  130.     char backward[MYBUF];
  131.     register ri;
  132.     register char *from, *to;
  133.     char *numstart, *frommark, *tomark;
  134.     int inlen, tail, group, int_digs;
  135.     static char *fid = "display";
  136.     
  137.     _TR
  138.     if (Format == DISA) {
  139.         mvaddstr(ACCUM,ULEFT,source);
  140.         if (Hc != -1 && !Hide) {
  141.             if ((write(Hc, source, strlen(source))) !=
  142.                 strlen(source))
  143.                 fatal("hardcopy accumulator write");
  144.             if ((write(Hc, "\n", 1)) != 1)
  145.                 fatal("hardcopy accumulator write");
  146.         }
  147.     }
  148.     else {
  149.         switch(Obase) {
  150.         case 2:
  151.             group = 8;
  152.             break;
  153.         case 8:
  154.         case 10:
  155.             group = 3;
  156.             break;
  157.         case 16:
  158.         default:
  159.             group = 4;
  160.             break;
  161.         }
  162.         inlen = strlen(source);
  163.         from = numstart = source;
  164.         while ((*numstart == ' ') || (*numstart == '-')) {
  165.                 ++numstart;
  166.                 ++from;
  167.         }
  168.         if (*numstart == '.')
  169.             numstart = ZERO;        /* bc does not prepend 0 before . */
  170.  
  171.         while (*from != '.' && *from != ' ' && *from != '\0')
  172.             ++from;                 /* stop on dot, space or null */
  173.  
  174.         if (from == source + inlen)        /* no fraction */
  175.             to = &backward[MYBUF - 1];     /* end of source: '\0' */
  176.         else {
  177.             tail = source - from + inlen;  /* on (include) dec. point */
  178.             to = &backward[MYBUF - 1 - tail - (tail-2)/group];
  179.  
  180.             frommark = from;
  181.             tomark = to;
  182.             *to++ = *from++;
  183.             for (ri = 0; ++ri < tail;) {
  184.                 *to++ = *from++;
  185.                 if ((ri % group == 0) && (*from != ' '))
  186.                     *to++ = Separator;
  187.             }
  188.             from = frommark;
  189.             to = tomark;
  190.         }
  191.         backward[MYBUF - 1] = '\0';
  192.  
  193.         if (numstart != ZERO) {
  194.             --to;
  195.             --from;                        /* back to last int digit */
  196.             int_digs = from - numstart;    /* one more, really */
  197.             for (ri = 0; ++ri <= int_digs;) {
  198.                 *to-- = *from--;
  199.                 if (ri % group == 0)
  200.                     *to-- = Separator;
  201.             }
  202.             *to = *from;                   /* first integer digit */
  203.         }
  204.         if (Negative)
  205.             *--to = *--from;
  206.  
  207.         if (Justify == JL) {
  208.             strcpy(Tmpbuf, to);
  209.             strcat(Tmpbuf, Sp44);
  210.             Tmpbuf[ACCUMAX] = '\0';
  211.             mvaddstr(ACCUM,ULEFT, Tmpbuf);
  212.             if (Hc != -1 && !Hide) {
  213.             if ((write(Hc, Tmpbuf, strlen(Tmpbuf))) !=
  214.                 strlen(Tmpbuf))
  215.                 fatal("hardcopy justified left write");
  216.             if ((write(Hc, "\n", 1)) != 1)
  217.                 fatal("hardcopy justified left write");
  218.             }
  219.         }
  220.         else {
  221.             for (ri = from - source + 1; --ri;)
  222.                 *to-- = *from--;
  223.             *to = *from;          /* to avoid indexing neg. address */
  224.             to = &backward[MYBUF - 1 - ACCUMAX];
  225.             mvaddstr(ACCUM,ULEFT, to);
  226.             if (Hc != -1 && !Hide) {
  227.                 if ((write(Hc, to, strlen(to))) != strlen(to))
  228.                     fatal("justified right write");
  229.                 if ((write(Hc, "\n", 1)) != 1)
  230.                     fatal("justified right write");
  231.             }
  232.         }
  233.     }
  234.     TR_
  235. }
  236.  
  237. /* flag values:
  238.    2)     write to Main pipe, clear_accwin, display_accum.  The user
  239.           input string is assumed to have completed processing.
  240.    1)     write to Main pipe, clear_accwin, display_accum only if
  241.           Show is ENA.  (Some functions may withold the current
  242.           result from the stack, by temporarily disabling Stack.)
  243.           The operation always results in a new value from bc, and
  244.           in the clearing of the input string up to the currently
  245.           scanned token.
  246.    0)     write control info to main pipe, redraw Status window.
  247.           This is a one-way  write to bc, no data is returned in
  248.           the pipe; the accumulator and Lastob contents stay intact.
  249.           Error bar under accum does get cleared.
  250. */
  251.  
  252. show_result(flag)
  253. int flag;
  254. {
  255.     int Ubuflen, Controlbuflen;
  256.     static char *fid = "show_result";
  257.  
  258.     _TR
  259.     if (flag) {
  260.         if ((Ubuflen = strlen(Ubuf)) != 0) {
  261.             if (!(Ubuflen == 2 && Ubuf[0] == ';')) {
  262.                 if (Ubuf[Ubuflen - 1] != '\n')
  263.                     Ubuf[Ubuflen++] = '\n';
  264.  
  265. #ifdef DEBUG
  266. fprintf(Dfp, "Ubuf to A_write: %*.*s<<<\n",Ubuflen, Ubuflen, Ubuf);
  267. #endif
  268.  
  269.                 if (write(A_write[1], Ubuf, Ubuflen) == -1)
  270.                     fatal("ubuf to main pipe write");
  271.                 clear_accwin();
  272.                 wait_main_pipe();
  273.  
  274. #ifdef DEBUG
  275. fprintf(Dfp, "Mainbuf read: %s<<<\n", Mainbuf);
  276. #endif
  277.  
  278.                 Lastob = Obase;
  279.  
  280.                 if (Do_conv) {
  281.                     Titlq[TALYREQ] = ZERO;
  282.                     show_uconv();
  283.                     conv_usr();
  284.                 }
  285.  
  286.                 if (Show == ENA || flag > 1)
  287.                     display_accum(1);
  288.                 else
  289.                     display_accum(0);
  290.             }
  291.             Ubuf[0] = '\0';
  292.         }
  293.     }
  294.     else {
  295.         if ((Controlbuflen = strlen(Controlbuf)) != 0) {
  296.             if (Controlbuf[Controlbuflen - 1] != '\n')
  297.                 Controlbuf[Controlbuflen++] = '\n';
  298.  
  299. #ifdef DEBUG
  300. fprintf(Dfp, "Control to A_write: %*.*s<<<\n", Controlbuflen,
  301.     Controlbuflen, Controlbuf);
  302. #endif
  303.  
  304.             if (write(A_write[1], Controlbuf, Controlbuflen) == -1)
  305.                 fatal("controlbuf to main pipe write");
  306.         }
  307.         show_stat();
  308.         Controlbuf[0] = '\0';
  309.         move(CY=UTOP, CX=ULEFT);
  310.         pfresh();
  311.     }
  312.     TR_
  313. }
  314.  
  315. show_stat()
  316. {
  317.     static char *statstr = "  23456789ABCDEFX";
  318.     register int cur_y = STATY;
  319.     int pyp, pxp;
  320.     static char *fid = "show_stat";
  321.  
  322.     _TR
  323.     CYX;
  324.     standout();
  325.     mvaddstr(STATY - 1, STATMSG - 1, "    GLOBALS     ");
  326.     standend();
  327.  
  328.     move(cur_y++, STATMSG);
  329.     printw("    ibase %c   " , statstr[Ibase]);
  330.  
  331.     move(cur_y++, STATMSG);
  332.     printw("    obase %c   " , statstr[Obase]);
  333.  
  334.     mvaddstr(cur_y++, STATMSG, (Staybase == ENA) ?  " staybase on  " 
  335.                                                 :  " staybase off " );
  336.  
  337.  
  338.     move(cur_y++, STATMSG);
  339.     printw("precision %d  " , Precision);
  340.  
  341.     mvaddstr(cur_y++, STATMSG, (Autoconv == ENA) ?  " autoconv on  " 
  342.                                                 :  " autoconv off " );
  343.  
  344.     if (Format == COMMA_)
  345.         mvaddstr(cur_y++, STATMSG,  "   format ',' " );
  346.     else if (Format == DISA)
  347.         mvaddstr(cur_y++, STATMSG,  "   format off " );
  348.     else /* SPACE_ */
  349.         mvaddstr(cur_y++, STATMSG,  "   format ' ' " );
  350.  
  351.     if (Hf == FVER)
  352.         mvaddstr(cur_y++, STATMSG,  " hardform ver " );
  353.     else if (Hf == FTER)
  354.         mvaddstr(cur_y++, STATMSG,  " hardform te  " );
  355.     else
  356.         mvaddstr(cur_y++, STATMSG,  " hardform xt  " );
  357.  
  358.     if (Justify == JF)
  359.         mvaddstr(cur_y++, STATMSG,  "  justify fix " );
  360.     else if (Justify == JR)
  361.         mvaddstr(cur_y++, STATMSG,  "  justify ri  " );
  362.     else
  363.         mvaddstr(cur_y++, STATMSG,  "  justify le  " );
  364.  
  365.     mvaddstr(cur_y++, STATMSG, (Stack == ENA) ?  "    stack on  " 
  366.                                              :  "    stack off " );
  367.     mvaddstr(cur_y++, STATMSG, (Autotime == ENA) ?  " autotime on  " 
  368.                                              :  " autotime off " );
  369.     Statopts = 0;
  370.  
  371.     PYX;
  372.     pfresh();
  373.     TR_
  374. }
  375.  
  376. show_param()
  377. {
  378.     register int cur_y = STATY;
  379.     int pyp, pxp;
  380.     static char *fid = "show_param";
  381.  
  382.     _TR
  383.     CYX;
  384.     standout();
  385.     mvaddstr(STATY - 1, STATMSG - 1, " STAT  OPTIONS  ");
  386.     standend();
  387.  
  388.     mvaddstr(cur_y++, STATMSG, "ib   2 --- 16 ");
  389.     mvaddstr(cur_y++, STATMSG, "ob   2 --- 16 ");
  390.     mvaddstr(cur_y++, STATMSG, "sb     off|on ");
  391.     mvaddstr(cur_y++, STATMSG, "pr   0 --- 32 ");
  392.     mvaddstr(cur_y++, STATMSG, "au     off|on ");
  393.     mvaddstr(cur_y++, STATMSG, "fo  sp|off|cm ");
  394.     mvaddstr(cur_y++, STATMSG, "hf  te|ver|xt ");
  395.     mvaddstr(cur_y++, STATMSG, "ju  le|fix|ri ");
  396.     mvaddstr(cur_y++, STATMSG, "st     off|on ");
  397.     mvaddstr(cur_y++, STATMSG, "at     off|on ");
  398.  
  399.     Statopts = 1;
  400.     PYX;
  401.     pfresh();
  402.     TR_
  403. }
  404.  
  405.