home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / astrnomy / ephem421.zip / LISTING.C < prev    next >
C/C++ Source or Header  |  1990-09-13  |  8KB  |  299 lines

  1. /* code to support the listing capabilities.
  2.  * idea is to let the operator name a listing file and mark some fields for
  3.  * logging. then after each screen update, the logged fields are written to
  4.  * the listing file in the same manner as they appeared on the screen.
  5.  * 
  6.  * format of the listing file is one line per screen update.
  7.  */
  8.  
  9. #include <stdio.h>
  10. #include <math.h>
  11. #include "screen.h"
  12.  
  13. extern char *strcpy();
  14. extern errno;
  15. extern char *sys_errlist[];
  16. #define    errsys    (sys_errlist[errno])
  17.  
  18. #define    TRACE(x)    {FILE *fp = fopen("trace","a"); fprintf x; fclose(fp);}
  19.  
  20. #define    MAXLSTFLDS    10    /* max number of fields we can track.
  21.                  * note we can't store more than NFLOGS fields
  22.                  * anyway (see flog.c).
  23.                  */
  24. #define    FNLEN        (14+1)    /* longest filename; plus 1 for \0 */
  25.  
  26. static char lst_filename[FNLEN] = "ephem.lst";    /* default plot file name */
  27. static FILE *lst_fp;        /* the plot file; == 0 means don't plot */
  28.  
  29. /* store rcfpack()s for each field to track, in l-to-r order */
  30. static int lstflds[MAXLSTFLDS];
  31. static int nlstflds;        /* number of lstflds[] in actual use */
  32.  
  33. static int lstsrchfld;        /* set when the Search field is to be listed */
  34.  
  35. /* picked the Listing label:
  36.  * if on, just turn it off.
  37.  * if off, turn on, define fields or select name of file to list to and do it.
  38.  * TODO: more flexibility, more relevance.
  39.  */
  40. listing_setup()
  41. {
  42.     if (lst_fp)
  43.         lst_turn_off();
  44.     else {
  45.         static char *chcs[] = {
  46.         "Select fields", "Display a listing file", "Begin listing"
  47.         };
  48.         static int fn;    /* start with 0, then remember for next time */
  49.     ask:
  50.         switch (popup(chcs, fn, nlstflds > 0 ? 3 : 2)) {
  51.         case 0: fn = 0; lst_select_fields(); goto ask;
  52.         case 1: fn = 1; lst_file(); goto ask;
  53.         case 2: fn = 2; lst_turn_on(); break;
  54.         default: break;
  55.         }
  56.     }
  57. }
  58.  
  59. /* write the active listing to the current listing file, if one is open. */
  60. listing()
  61. {
  62.     if (lst_fp) {
  63.         int n;
  64.         double flx;
  65.         char flstr[32];
  66.         if (!srch_ison() && lstsrchfld) {
  67.         /* if searching is not on but we are listing the search
  68.          * funtion we must evaluate and log it ourselves here and now.
  69.          * lst_turn_on() insured there is a good function to eval.
  70.          * N.B. if searching IS on, we rely on main() having called
  71.          * srch_eval() BEFORE plot() so it is already evaluated.
  72.          */
  73.         double e;
  74.         char errmsg[128];
  75.         if (execute_expr (&e, errmsg) < 0) {
  76.             f_msg (errmsg);
  77.             lst_turn_off();
  78.             return;
  79.         } else {
  80.             (void) sprintf (flstr, "%g", e);
  81.             (void) flog_log (R_SRCH, C_SRCH, e, flstr);
  82.         }
  83.         }
  84.  
  85.         /* list in order of original selection */
  86.         for (n = 0; n < nlstflds; n++)
  87.         if (flog_get (lstflds[n], &flx, flstr) == 0)
  88.             (void) fprintf (lst_fp, "%s  ", flstr);
  89.         (void) fprintf (lst_fp, "\n");
  90.     }
  91. }
  92.  
  93. listing_prstate (force)
  94. int force;
  95. {
  96.     static last;
  97.     int this = lst_fp != 0;
  98.  
  99.     if (force || this != last) {
  100.         f_string (R_LISTING, C_LISTINGV, this ? " on" : "off");
  101.         last = this;
  102.     }
  103. }
  104.  
  105. listing_ison()
  106. {
  107.     return (lst_fp != 0);
  108. }
  109.  
  110. static
  111. lst_reset()
  112. {
  113.     int *lp;
  114.  
  115.     for (lp = lstflds; lp < &lstflds[nlstflds]; lp++) {
  116.         (void) flog_delete (*lp);
  117.         *lp = 0;
  118.     }
  119.     nlstflds = 0;
  120.     lstsrchfld = 0;
  121. }
  122.  
  123. /* let operator select the fields he wants to have in his listing.
  124.  * register them with flog and keep rcfpack() in lstflds[] array.
  125.  * as a special case, set lstsrchfld if Search field is selected.
  126.  */
  127. static
  128. lst_select_fields()
  129. {
  130.     static char hlp[] = "move and RETURN to select a field, or q to quit";
  131.     static char sry[] = "Sorry; can not list any more fields.";
  132.     int r = R_UT, c = C_UTV; /* TODO: start where main was? */
  133.     int sf = rcfpack (R_SRCH, C_SRCH, 0);
  134.     char buf[64];
  135.     int rcp;
  136.     int i;
  137.  
  138.     lst_reset();
  139.     for (i = 0; i < MAXLSTFLDS; i++) {
  140.         (void) sprintf(buf,"select field for column %d or q to quit", i+1);
  141.         rcp = sel_fld (r, c, alt_menumask()|F_PLT, buf, hlp);
  142.         if (!rcp)
  143.         break;
  144.         if (flog_add (rcp) < 0) {
  145.         f_msg (sry);
  146.         break;
  147.         }
  148.         lstflds[i] = rcp;
  149.         if (rcp == sf)
  150.         lstsrchfld = 1;
  151.         r = unpackr(rcp);
  152.         c = unpackc(rcp);
  153.     }
  154.     if (i == MAXLSTFLDS)
  155.         f_msg (sry);
  156.     nlstflds = i;
  157. }
  158.  
  159. static
  160. lst_turn_off ()
  161. {
  162.     (void) fclose (lst_fp);
  163.     lst_fp = 0;
  164.     listing_prstate(0);
  165. }
  166.  
  167. /* turn on listing facility.
  168.  * establish a file to use (and thereby set lst_fp, the "listing-is-on" flag).
  169.  * also check that there is a srch function if it is being used.
  170.  */
  171. static
  172. lst_turn_on ()
  173. {
  174.     int sf = rcfpack(R_SRCH, C_SRCH, 0);
  175.     char fn[FNLEN], fnq[NC];
  176.     char *optype;
  177.     int n;
  178.  
  179.     /* insure there is a valid srch function if we are to list it */
  180.     for (n = 0; n < nlstflds; n++)
  181.         if (lstflds[n] == sf && !prog_isgood()) {
  182.         f_msg ("Listing search function but it is not defined.");
  183.         return;
  184.         }
  185.  
  186.     /* prompt for file name, giving current as default */
  187.     (void) sprintf (fnq, "file to write <%s>: ", lst_filename);
  188.     f_prompt (fnq);
  189.     n = read_line (fn, sizeof(fn)-1);
  190.  
  191.     /* leave plotting off if type END.
  192.      * reuse same fn if just type \n
  193.      */
  194.     if (n < 0)
  195.         return;
  196.     if (n > 0)
  197.         (void) strcpy (lst_filename, fn);
  198.  
  199.     /* give option to append if file already exists */
  200.     optype = "w";
  201.     if (access (lst_filename, 2) == 0) {
  202.         while (1) {
  203.         f_prompt ("files exists; append or overwrite (a/o)?: ");
  204.         n = read_char();
  205.         if (n == 'a') {
  206.             optype = "a";
  207.             break;
  208.         }
  209.         if (n == 'o')
  210.             break;
  211.         }
  212.     }
  213.  
  214.     /* listing is on if file opens ok */
  215.     lst_fp = fopen (lst_filename, optype);
  216.     if (!lst_fp) {
  217.         (void) sprintf (fnq, "can not open %s: %s", lst_filename, errsys);
  218.         f_msg (fnq);
  219.     } else {
  220.         /* add a title if desired */
  221.         static char tp[] = "Title (q to skip): ";
  222.         f_prompt (tp);
  223.         if (read_line (fnq, PW - sizeof(tp)) > 0)
  224.         (void) fprintf (lst_fp, "%s\n", fnq);
  225.     }
  226.  
  227.     listing_prstate (0);
  228. }
  229.  
  230. /* ask operator for a listing file to show. if it's ok, do it.
  231.  */
  232. static
  233. lst_file ()
  234. {
  235.     char fn[FNLEN], fnq[64];
  236.     FILE *lfp;
  237.     int n;
  238.  
  239.     /* prompt for file name, giving current as default */
  240.     (void) sprintf (fnq, "file to read <%s>: ", lst_filename);
  241.     f_prompt (fnq);
  242.     n = read_line (fn, sizeof(fn)-1);
  243.  
  244.     /* forget it if type END.
  245.      * reuse same fn if just type \n
  246.      */
  247.     if (n < 0)
  248.         return;
  249.     if (n > 0)
  250.         (void) strcpy (lst_filename, fn);
  251.  
  252.     /* show it if file opens ok */
  253.     lfp = fopen (lst_filename, "r");
  254.     if (lfp) {
  255.         display_listing_file (lfp);
  256.         (void) fclose (lfp);
  257.     } else {
  258.         char buf[NC];
  259.         (void) sprintf (buf, "can not open %s: %s", lst_filename, errsys);
  260.         f_prompt (buf);
  261.         (void)read_char();
  262.     }
  263. }
  264.  
  265. /* display the given listing file on the screen.
  266.  * allow for files longer than the screen.
  267.  * N.B. do whatever you like but redraw the screen when done.
  268.  */
  269. static
  270. display_listing_file (lfp)
  271. FILE *lfp;
  272. {
  273.     char buf[128];
  274.     int n;
  275.  
  276.     c_erase();
  277.     n = 0;
  278.     while (1) {
  279.         (void) fgets (buf, sizeof(buf), lfp);
  280.         if (feof(lfp) || (printf ("%.*s\r", NC, buf), ++n == NR-1)) {
  281.         static char p[] = "[Hit any key to continue or q to quit...]";
  282.         static char ep[] = "[End-of-file. Hit any key to resume...]";
  283.         if (feof(lfp)) {
  284.             f_string (NR, (NC-sizeof(ep))/2, ep);
  285.             (void) read_char();
  286.             break;
  287.         } else {
  288.             f_string (NR, (NC-sizeof(p))/2, p);
  289.             if (read_char() == END)
  290.             break;
  291.         }
  292.         c_erase();
  293.         n = 0;
  294.         }
  295.     }
  296.  
  297.     redraw_screen (2);    /* full redraw */
  298. }
  299.