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

  1. /* file.c */
  2. /**********************************************************************
  3. *    File Name     : file.c
  4. *    Function      : file i/o of pac: rc, hardcopies, debug files
  5. *    Author        : Istvan Mohos, 1987
  6. ***********************************************************************/
  7.  
  8. #include "defs.h"
  9. #define FILEMAP
  10. #include "maps.h"
  11. #undef FILEMAP
  12.  
  13. #define RCMIN  (STACKDEEP * (STACKMAX + 1) + 53) /* minimum count */
  14. read_rc()
  15. {
  16.     char rcbuf[PIPEMAX];
  17.     register char *rc = rcbuf;
  18.     static char *fid = "read_rc";
  19.  
  20.     _TR
  21.     if (read(Rcfd, rc, RCMIN) < RCMIN) {
  22.         mvaddstr(2,4,"Rcerr after RCMIN");
  23.         pfresh();
  24.         Rcerr = TRUE;
  25.         TR_
  26.         return;
  27.     }
  28.     if ((Format = *rc++ -48)!=COMMA_ && Format!=SPACE_ && Format!=DISA)
  29.         Format = FORM_DFLT;
  30.     (Format == COMMA_) ? (Separator = ',') : (Separator = ' ');
  31.  
  32.     if ((Hf = *rc++ -48) != FVER && Hf != FTER && Hf != FXTER)
  33.         Hf = HF_DFLT;
  34.     if ((Ibase = *rc++ -48) < 2 || Ibase > 16)
  35.         Ibase = IB_DFLT;
  36.     if ((Justify = *rc++ -48) != JL && Justify != JF && Justify != JR)
  37.         Justify = JUS_DFLT;
  38.     if ((Obase = *rc++ -48) < 2 || Obase > 16)
  39.         Obase = OB_DFLT;
  40.     if ((Precision = *rc++ -48) < 0 || Precision > 32)
  41.         Precision = PREC_DFLT;
  42.     if ((Autotime = *rc++ -48) != ENA && Autotime != DISA)
  43.         Autotime = DISA;
  44.     if ((Stack = *rc++ -48) != ENA && Stack != DISA)
  45.         Stack = STACK_DFLT;
  46.     if ((Staybase = *rc++ -48) != ENA && Staybase != DISA)
  47.         Staybase = SB_DFLT;
  48.     if ((Convcount = atoi(rc)) > 255 || Convcount < CONVCOUNT) {
  49.         mvaddstr(2,4,"Rcerr at Convcount");
  50.         pfresh();
  51.         Rcerr = TRUE;
  52.         TR_
  53.         return;
  54.     }
  55.     rc += 4;
  56.     if ((Convsel = atoi(rc)) < 0 || Convsel > Convcount -1)
  57.         Convsel = CONVSEL;
  58.     rc += 4;
  59.     if ((Convhsiz = atoi(rc)) < 1) {
  60.         mvaddstr(2,4,"Rcerr after Convhsiz");
  61.         pfresh();
  62.         Rcerr = TRUE;
  63.         TR_
  64.         return;
  65.     }
  66.     rc += 6;
  67.     if ((Amt = atof(rc)) < 0.)
  68.         Amt = 0.;
  69.     rc += 14;
  70.     if ((Years = atof(rc)) < 0.)
  71.         Years = 0.;
  72.     rc += 8;
  73.     if ((Rate = atof(rc)) < 0.)
  74.         Rate = 0.;
  75.     rc += 8;
  76.  
  77.     fill_stack(rc);
  78.  
  79.     if ((Convhom = malloc(Convhsiz)) == ZERO)
  80.         fatal("malloc error at read_rc");
  81.  
  82.     if (read(Rcfd, Convhom, (int)Convhsiz) != Convhsiz) {
  83.         mvaddstr(2,4,"Rcerr after Convhom");
  84.         pfresh();
  85.         Rcerr = TRUE;
  86.         TR_
  87.         return;
  88.     }
  89.  
  90.     for (rc = Convhom + Convhsiz; --rc > Convhom;)
  91.         if (*rc == '\n')
  92.             *rc = 0;
  93.  
  94.     realign_conv();
  95.     TR_
  96. }
  97.  
  98. write_rc()
  99. {
  100.     char rcbuf[PIPEMAX];
  101.     register char *rc = rcbuf;
  102.     static char *fid = "write_rc";
  103.  
  104.     _TR
  105.     if (Home != ZERO && !Dontsave) {
  106.         if ((Rcfd = open(Rcfile, O_WRONLY | O_CREAT, 0600)) != -1) {
  107.  
  108.             *rc++ = Format +48;
  109.             *rc++ = Hf +48;
  110.             *rc++ = Ibase +48;
  111.             *rc++ = Justify +48;
  112.             *rc++ = Obase +48;
  113.             *rc++ = Precision +48;
  114.             *rc++ = Autotime +48;
  115.             *rc++ = Stack +48;
  116.             *rc++ = Staybase +48;
  117.             sprintf(rc, "%3d;%3d;%5d\n", Convcount, Convsel, Convhsiz);
  118.             rc += 14;
  119.             sprintf(rc, "%13.2f", Amt);
  120.             rc += 13;
  121.             *rc++ = ';'; /* printf too generous on overflow*/
  122.             sprintf(rc, "%7.3f", Years);
  123.             rc += 7;
  124.             *rc++ = ';';
  125.             sprintf(rc, "%7.3f", Rate);
  126.             rc += 7;
  127.             *rc++ = '\n';
  128.  
  129.             save_stack(rc, 0);
  130.  
  131.             if ((write(Rcfd, rcbuf, RCMIN)) != RCMIN)
  132.                 fatal("pacrc stack write");
  133.  
  134.             for (rc = Convhom + Convhsiz; --rc > Convhom;)
  135.                 if (*rc == '\0')
  136.                     *rc = '\n';
  137.             if ((write(Rcfd, Convhom, (int)Convhsiz)) != (int)Convhsiz)
  138.                 fatal("pacrc conv write");
  139.  
  140.             close(Rcfd);
  141.        }
  142.     }
  143.     TR_
  144. }
  145.  
  146. #ifdef DESIGN
  147. read_scr(name)        /* read screen image from named file */
  148. char *name;
  149. {
  150.     FILE *fp;
  151.     register rx, ry;
  152.     static char *fid = "read_scr";
  153.  
  154.     _TR
  155.     if ((fp = fopen(name, "r")) != NULL) {
  156.         for (ry = 0; ry < LINES; ry++)
  157.             for (rx=0; rx < COLS-1; rx++)
  158.                 stdscr->_y[ry][rx] = fgetc(fp);
  159.         fclose(fp);
  160.     }
  161.     TR_
  162. }
  163.  
  164. write_scr(name, rf)
  165. char *name;
  166. int rf;
  167. {
  168.     FILE *fp;
  169.     register rx, ry;
  170.     static char *fid = "write_scr";
  171.  
  172.     _TR
  173.     if ((fp = fopen(name, "w")) == NULL)
  174.         Status = 1, fatal("write image");
  175.     if (rf)
  176.         for (ry = 0; ry < LINES; ry++)
  177.             for (rx=0; rx < COLS-1; rx++)
  178.                 putc(stdscr->_y[ry][rx] & 0377, fp);
  179.     else
  180.         for (ry = 0; ry < LINES; ry++) {
  181.             for (rx=0; rx < COLS-1; rx++)
  182.                 putc(stdscr->_y[ry][rx] & 127, fp);
  183.             putc('\n', fp);
  184.         }
  185.     fclose(fp);
  186.     TR_
  187. }
  188. #endif
  189.  
  190. hard(fnum)
  191. int fnum;
  192. {
  193.     int newlev;
  194.     int pyp, pxp;
  195.     int *whichfile, *whichfd;
  196.     char *np;
  197.     char *whichname;
  198.     char namebuf[TITSIZ + 1];
  199.     char spaceless[TITSIZ + 1];
  200.     static char *fid = "hard";
  201.  
  202.     _TR
  203.     if (fnum) {
  204.         whichname = Totname;
  205.         whichfd = &Tc;
  206.         if (*(whichfile = &Totcopy) == ENA)
  207.             newlev = TOTLREQ;
  208.         else if (*whichfile == AP)
  209.             newlev = TAPPREQ;
  210.         else {
  211.             newlev = TALYREQ;
  212.             Basq[TOTLREQ] = ZERO;
  213.             Basq[TAPPREQ] = ZERO;
  214.         }
  215.     }
  216.     else {
  217.         whichname = Hardname;
  218.         whichfd = &Hc;
  219.         if (*(whichfile = &Hardcopy) == ENA)
  220.             newlev = FILEREQ;
  221.         else if (*whichfile == AP)
  222.             newlev = POSTREQ;
  223.         else {
  224.             newlev = 0;
  225.             Basq[FILEREQ] = ZERO;
  226.             Basq[POSTREQ] = ZERO;
  227.         }
  228.     }
  229.     Basq[newlev] = Bb[newlev];
  230.     Basq[EDITREQ] = Bb[newlev];
  231.     CYX;  /* to save the caller's coordinates */
  232.     update();  /* this returns to the original coordinates,
  233.                   but does'nt pfresh */
  234.     
  235.     if (*whichfile == ENA || *whichfile == AP) {
  236.  
  237. redo:
  238.  
  239.         ledit(namebuf, f_ed_map, BOT, FBOUND, RBOUND, 1, 1, 0);
  240.  
  241.         if (strlen(namebuf) == 0) {
  242.             strcpy(spaceless, whichname);
  243.             for (np = spaceless; *np > 32; np++);
  244.             *np = '\0';
  245.             standout();
  246.             mvaddstr(BOT, FBOUND, whichname);
  247.             standend();
  248.             pfresh();
  249.         }
  250.         else {
  251.             strcpy(whichname, namebuf);
  252.             strcpy(spaceless, namebuf);
  253.         }
  254.  
  255.         if (*whichfile == ENA) {
  256.             if ((*whichfd = open(spaceless,
  257.             O_WRONLY | O_CREAT | O_TRUNC, 0644)) == -1) {
  258.                 standout();
  259.                 mvaddstr(BOT, ULEFT, "can't access:");
  260.                 standend();
  261.                 pfresh();
  262.                 goto redo;
  263.             }
  264.         }
  265.         else if ((*whichfd = open(spaceless,
  266.             O_WRONLY | O_APPEND | O_CREAT, 0644)) == -1) {
  267.                 standout();
  268.                 mvaddstr(BOT, ULEFT, "can't access:");
  269.                 standend();
  270.                 pfresh();
  271.                 goto redo;
  272.         }
  273.         /* make a copy of name in alternate buffer also */
  274.         if (*whichfile == AP) {
  275.             strcpy(Bb[newlev - 1] + BUFSTOP, whichname);
  276.             rev_clear(Bb[newlev - 1] + TITSIZ);
  277.         }
  278.         else {
  279.             strcpy(Bb[newlev + 1] + BUFSTOP, whichname);
  280.             rev_clear(Bb[newlev + 1] + TITSIZ);
  281.         }
  282.         strcpy(Bb[newlev] + BUFSTOP, whichname);
  283.         rev_clear(Bb[newlev] + TITSIZ);
  284.         Basq[EDITREQ] = ZERO;
  285.     }
  286.     PYX;
  287.     update();
  288.     pfresh();
  289.     TR_
  290. }
  291.