home *** CD-ROM | disk | FTP | other *** search
/ World of Shareware - Software Farm 2 / wosw_2.zip / wosw_2 / CPROG / KSLIB.ZIP / DEMO1.C < prev    next >
Text File  |  1991-08-01  |  12KB  |  743 lines

  1. /*****************************************************************************
  2. *
  3. *    Library Demo
  4. *
  5. *    By Kevin Spencer, DigiServ        (c) 1991
  6. *
  7. *    Edit History
  8. *    ------------
  9. *
  10. *****************************************************************************/
  11.  
  12. /*
  13. *    Defines
  14. */
  15.  
  16. #define title    "Library Demo Program"
  17. #define version "1.0"
  18. #define verdate "31-Jul-91"
  19.  
  20. #define SHOWTIME 200
  21.  
  22. /*
  23. *    System includes
  24. */
  25.  
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <ctype.h>
  29. #include <stdarg.h>
  30. #include <string.h>
  31. #include <dos.h>
  32.  
  33. /*
  34. *    Project includes
  35. */
  36.  
  37. #include "stdinc.h"
  38. #include "misc.h"
  39. #include "wn.h"
  40. #include "mn.h"
  41. #include "kb.h"
  42. #include "arg.h"
  43. #include "cf.h"
  44. #include "pr.h"
  45. #include "sc.h"
  46. #include "db.h"
  47.  
  48. /*
  49. *    Structures
  50. */
  51.  
  52. struct rec_st {
  53.     uchar    sn[21];
  54.     uchar    fn[21];
  55.     uchar    addr1[31];
  56.     uchar    addr2[31];
  57.     uchar    phon[11];
  58.     uchar    paid;
  59. };
  60.  
  61. typedef struct rec_st rec_st;
  62.  
  63. /*
  64. *    Function prototypes
  65. */
  66.  
  67. int        main();
  68. int        initsystem();
  69. int        seekfunc(rec_st *,rec_st *);
  70. int        findfunc(rec_st *,rec_st *);
  71. int        search();
  72. int        checksave();
  73. int        save();
  74. int        scandata(rec_st);
  75. int        selectrec(int);
  76. int        message(char *,...);
  77. int        help();
  78. int        popup(char *);
  79. int        getans(char *);
  80. int        scan();
  81. int        pack();
  82. int        add();
  83. int        delete();
  84. int        back();
  85. int        next();
  86. int        clear();
  87. int        reports();
  88. int        rbrief();
  89. void    prerr(char *);
  90. int        rfull();
  91. int        rpaid();
  92. int        header(char *);
  93.  
  94. /*
  95. *    Configuration parameters
  96. */
  97.  
  98. struct conf_st {
  99.     char    datafile[65];
  100.     char    indexfile[65];
  101.     char    helpfile[65];
  102.     char    tempfile[65];
  103.     char    tempidx[65];
  104.     uint    norm;
  105.     uint    high;
  106. }    conf = {
  107.     "patient.dat",
  108.     "patient.idx",
  109.     "prs.hlp",
  110.     "temp.dat",
  111.     "temp.idx",
  112.     WHITE,
  113.     BLACK|BKGR(RED),
  114. };
  115.  
  116. CFdata    config[] = {
  117.     'S',"datafile",&conf.datafile,
  118.     'S',"indexfile",&conf.indexfile,
  119.     'S',"helpfile",&conf.helpfile,
  120.     'S',"tempfile",&conf.tempfile,
  121.     'S',"tempidx",&conf.tempidx,
  122.     'C',"norm",&conf.norm,
  123.     'C',"high",&conf.high,
  124.     NULL,NULL,NULL
  125. };
  126.  
  127. /*
  128. *    Global variables
  129. */
  130.  
  131. static    rec_st    rec;
  132. static    rec_st    frec;
  133. static    int        recn[20];
  134. static    char    recs[20][81];
  135.  
  136. static    SCdata    screen[] = {
  137.     'S',20,0,17, 1,&rec.sn,
  138.     'S',20,0,17, 2,&rec.fn,
  139.     'S',30,0,17, 4,&rec.addr1,
  140.     'S',30,0,17, 5,&rec.addr2,
  141.     'S',10,0,17, 7,&rec.phon,
  142.     'L', 1,0,17, 9,&rec.paid,
  143.     '0',0,0,0,0,NULL
  144. };
  145.  
  146. MNdata    reportmenu =    {    4,40,0,0,CENTRE,
  147.                             " Report Menu ",
  148.                             "Brief Listing",NULL,&rbrief,
  149.                             "Full Listing",NULL,&rfull,
  150.                             "Unpaid patients",NULL,&rpaid,
  151.                             NULL,NULL,NULL
  152.                         };
  153.  
  154. MNdata    cmdmenu =        {    4,40,0,0,CENTRE,
  155.                             " Command Menu ",
  156.                             "Help",NULL,&help,
  157.                             "Search for patient",NULL,&search,
  158.                             "Add patient",NULL,&add,
  159.                             "Save patient",NULL,&save,
  160.                             "Delete patient",NULL,&delete,
  161.                             "List patients",NULL,&scan,
  162.                             "Report menu",&reportmenu,NULL,
  163.                             "Pack database",NULL,&pack,
  164.                             "Previous patient",NULL,&back,
  165.                             "Next patient",NULL,&next,
  166.                             "Clear screen",NULL,&clear,
  167.                             NULL,NULL,NULL
  168.                         };
  169.  
  170. char    confarg[81];
  171. DBfile    *curr;            /* Pointer to current database file                    */
  172. char    pline[] = "--------------------------------------------------------------------------------";
  173. int        cf;
  174.  
  175. /*
  176. *    Start of code
  177. */
  178.  
  179. int
  180. main()
  181. {
  182.     int        exitloop;
  183.  
  184.     initsystem();
  185.  
  186.     if ((curr = DBopen(conf.datafile,sizeof(rec_st))) == NULL) {
  187.         WNdisplay(20,10,"Database inaccessible - Press any key : ");
  188.         KBgetch();
  189.         exit(1);
  190.     }
  191.  
  192.     if (DBindex(conf.indexfile,findfunc) < 0) {
  193.         WNdisplay(20,10,"Index file inaccessible - Press any key : ");
  194.         KBgetch();
  195.         exit(1);
  196.     }
  197.  
  198.     WNdisplay(2,1,"Surname      :");
  199.     WNdisplay(2,2,"First name   :");
  200.     WNdisplay(2,4,"Address 1    :");
  201.     WNdisplay(2,5,"Address 2    :");
  202.     WNdisplay(2,7,"Phone number :");
  203.     WNdisplay(2,9,"Paid :");
  204.  
  205.     DBgo(0);
  206.     SCclear(screen);
  207.     cf = 0;
  208.     exitloop = FALSE;
  209.     while (!exitloop) {
  210.         switch(SCgetscreen(screen,&cf)) {
  211.             case ESCAPE :
  212.                 checksave();
  213.                 exitloop = TRUE;
  214.                 break;
  215.             case F1 :
  216.                 help();
  217.                 break;
  218.             case F2 :
  219.                 search();
  220.                 break;
  221.             case F3 :
  222.                 add();
  223.                 break;
  224.             case F4 :
  225.                 save();
  226.                 break;
  227.             case F5 :
  228.                 delete();
  229.                 break;
  230.             case F6 :
  231.                 scan();
  232.                 break;
  233.             case F7 :
  234.                 MNmenu(&reportmenu);
  235.                 break;
  236.             case F8 :
  237.                 pack();
  238.                 break;
  239.             case F9 :
  240.                 MNmenu(&cmdmenu);
  241.                 break;
  242.             case F0 :
  243.                 clear();
  244.                 break;
  245.             case CPGUP :
  246.                 back();
  247.                 break;
  248.             case CPGDN :
  249.                 next();
  250.                 break;
  251.         }
  252.     }
  253. }
  254.  
  255. int
  256. initsystem()
  257. {
  258.     getopt("c:",NULL,confarg);
  259.     CFconf(confarg,config);
  260.     MNcolor(conf.norm,conf.high);
  261.  
  262.     WNopen(1,1,80,3,conf.norm,SINGLE);
  263.     WNgotoxy(39-(strlen(title)+strlen(version)+4)/2,1);
  264.     WNprintf("%s (v%s)",title,version);
  265.     WNopen(1,4,80,25,conf.norm,DOUBLE);
  266. }
  267.  
  268. int
  269. pack()
  270. {
  271.     int        r;
  272.     DBfile    *temp;
  273.  
  274.     if (!getans("Packing database - Are you sure (Y/N) ? "))
  275.         return(-1);
  276.  
  277.     if ((temp = DBopen(conf.tempfile,sizeof(rec_st))) == NULL) {
  278.         message("Can't open temporary file - Press any key");
  279.         return(-1);
  280.     }
  281.     if (DBindex(conf.tempidx,findfunc) < 0) {
  282.         message("Can't open temporary index - Press any key");
  283.         DBclose();
  284.         remove(conf.tempfile);
  285.         return(-1);
  286.     }
  287.  
  288.     WNopen(1,1,80,3,conf.norm,DOUBLE|POPUP);
  289.     r = 1;
  290.     DBselect(curr);
  291.     DBgo(DBtop());
  292.     while (DBrecno() != 0) {
  293.         DBread(&rec);
  294.         WNprintf("\r\nCopying record %d (%s)",r,rec.sn);
  295.         DBselect(temp);
  296.         DBadd(&rec);
  297.         DBselect(curr);
  298.         DBskip(1);
  299.         r++;
  300.     }
  301.  
  302.     DBselect(temp);
  303.     DBclose();        /* Temp database */
  304.     DBclose();        /* Real database */
  305.     remove(conf.datafile);
  306.     remove(conf.indexfile);
  307.     rename(conf.tempfile,conf.datafile);
  308.     rename(conf.tempidx,conf.indexfile);
  309.     curr = DBopen(conf.datafile,sizeof(rec_st));
  310.     DBindex(conf.indexfile,findfunc);
  311.     WNclose();
  312.     DBgo(0);
  313.     SCclear(screen);
  314.  
  315.     return(--r);
  316. }
  317.  
  318. int
  319. add()
  320. {
  321.     if ((DBfindrec(&rec) == 0) ||
  322.         (getans("Are you sure you want to add this patient (Y/N) ? "))) {
  323.         popup("Adding");
  324.         DBadd(&rec);
  325.         WNclose();
  326.     }
  327. }
  328.  
  329. int
  330. delete()
  331. {
  332.     if (DBrecno() == 0) {
  333.         message("No patient selected - Press any key");
  334.     }
  335.     else {
  336.         if (getans("Are you sure you want to delete this patient (Y/N) ? ")) {
  337.             popup("Deleting");
  338.             DBdelete(&rec);
  339.             WNclose();
  340.         }
  341.     }
  342. }
  343.  
  344. int
  345. back()
  346. {
  347.     if (DBrecno() != DBtop()) {
  348.         checksave();
  349.         DBskip(-1);
  350.         DBread(&rec);
  351.         SCshow(screen);
  352.     }
  353. }
  354.  
  355. int
  356. next()
  357. {
  358.     if (DBrecno() != DBbot()) {
  359.         checksave();
  360.         DBskip(1);
  361.         DBread(&rec);
  362.         SCshow(screen);
  363.     }
  364. }
  365.  
  366. int
  367. clear()
  368. {
  369.     checksave();
  370.     DBgo(0);
  371.     SCclear(screen);
  372.     cf = 0;
  373. }
  374.  
  375. int
  376. scan()
  377. {
  378.     int        exitloop;
  379.     rec_st    t;
  380.     int        i;
  381.     int        orn;
  382.     int        tmprn;
  383.  
  384.     WNopen(1,4,80,25,conf.norm,DOUBLE|POPUP);
  385.     tmprn = DBrecno();
  386.     orn = -1;
  387.     exitloop = FALSE;
  388.     while (!exitloop) {
  389.         if (DBrecno() == 0)
  390.             DBgo(DBtop());
  391.         if (DBrecno() != orn) {
  392.             orn = DBrecno();
  393.             WNclear();
  394.             for (i=1; (i<=20) && (DBrecno() != 0); i++) {
  395.                 DBread(&t);
  396.                 WNgotoxy(2,i);
  397.                 WNprintf("%-20s %-20s %-34.34s",t.sn,t.fn,t.addr1);
  398.                 DBskip(1);
  399.             }
  400.             DBskip(-20);
  401.         }
  402.         switch(KBgetch()) {
  403.             case ESCAPE :
  404.                 exitloop = TRUE;
  405.                 break;
  406.             case PGUP :
  407.                 DBskip(-20);
  408.                 if (DBrecno() == 0)
  409.                     DBgo(DBtop());
  410.                 break;
  411.             case PGDN :
  412.                 DBskip(40);
  413.                 DBskip(-20);
  414.                 break;
  415.         }
  416.     }
  417.     WNclose();
  418.     DBgo(tmprn);
  419. }
  420.  
  421. int
  422. save()
  423. {
  424.     if (DBrecno() == 0)
  425.         return(0);
  426.  
  427.     DBread(&frec);
  428.     if (findfunc(&rec,&frec)) {
  429.         if (!getans("Patient surname changed - Add new patient (Y/N) ? ")) {
  430.             popup("Saving");
  431.             DBdelete();
  432.             DBadd(&rec);
  433.             WNclose();
  434.         }
  435.         else {
  436.             popup("Adding");
  437.             DBadd(&rec);
  438.             WNclose();
  439.         }
  440.     }
  441.     else {
  442.         popup("Saving");
  443.         DBwrite(&rec);
  444.         WNclose();
  445.     }
  446. }
  447.  
  448. int
  449. seekfunc(r1,r2)
  450.     rec_st    *r1;
  451.     rec_st    *r2;
  452. {
  453.     return(r1->sn[0] == '\0' ? 0 : strnicmp(r1->sn,r2->sn,strlen(r1->sn)));
  454. }
  455.  
  456. int
  457. findfunc(r1,r2)
  458.     rec_st    *r1;
  459.     rec_st    *r2;
  460. {
  461.     return(stricmp(r1->sn,r2->sn));
  462. }
  463.  
  464. int
  465. help()
  466. {
  467.     FILE    *fp;
  468.     char    s[81];
  469.  
  470.     WNopen(1,4,80,25,conf.norm,DOUBLE|POPUP);
  471.     if ((fp = fopen(conf.helpfile,"rt")) == NULL) {
  472.         WNdisplay(20,10,"Help not available - Press any key : ");
  473.     }
  474.     else {
  475.         while (fgets(s,80,fp)) {
  476.             WNputs(s);
  477.             WNputc('\r');
  478.         }
  479.         WNdisplay(1,20,"Press any key : ");
  480.     }
  481.     KBgetch();
  482.     fclose(fp);
  483.     WNclose();
  484.  
  485.     return(0);
  486. }
  487.  
  488. int
  489. checksave()
  490. {
  491.     if (DBrecno() == 0)
  492.         return(TRUE);
  493.  
  494.     DBread(&frec);
  495.     if (!DBcmp(&rec,&frec))
  496.         return(TRUE);
  497.  
  498.     if (getans("Current patient has been updated - Save (Y/N) ? "))
  499.         save();
  500.  
  501.     return(TRUE);
  502. }
  503.  
  504. int
  505. search()
  506. {
  507.     int        n;
  508.     uchar    ch;
  509.     int        r;
  510.  
  511.     n = scandata(rec);
  512.     if (n == 1) {
  513.         checksave();
  514.         DBreadn(recn[0],&rec);
  515.         SCshow(screen);
  516.     }
  517.     else if (n == 0) {
  518.         if (getans("Patient not found - Add (Y/N) ? ")) {
  519.             popup("Adding");
  520.             DBadd(&rec);
  521.             WNclose();
  522.         }
  523.     }
  524.     else {
  525.         if ((r = selectrec(n)) > 0) {
  526.             checksave();
  527.             DBreadn(r,&rec);
  528.             SCshow(screen);
  529.         }
  530.     }
  531. }
  532.  
  533. int
  534. scandata(r)
  535.     rec_st    r;
  536. {
  537.     int        n = 0;
  538.     int        rn;
  539.     rec_st    t;
  540.     int        currn;
  541.  
  542.     currn = DBrecno();
  543.     for (rn = DBseekfirst(&r,seekfunc); (n<20) && (rn>0);
  544.         rn = DBseeknext(&r,seekfunc)) {
  545.         DBreadn(rn,&t);
  546.         sprintf(recs[n]," %-20s %-20s %-32.32s ",t.sn,t.fn,t.addr1);
  547.         recn[n++] = rn;
  548.     }
  549.     DBgo(currn);
  550.  
  551.     return(n);
  552. }
  553.  
  554. int
  555. selectrec(n)
  556.     int        n;
  557. {
  558.     int        i;
  559.     int        l;
  560.     int        ol;
  561.     int        exitloop;
  562.     int        retval;
  563.  
  564.     WNopen(1,4,80,25,conf.norm,DOUBLE|POPUP);
  565.     for (i=0; i<n; i++) {
  566.         WNdisplay(2,i+1,recs[i]);
  567.     }
  568.  
  569.     l = 0;
  570.     ol = -1;
  571.     retval = 0;
  572.     exitloop = FALSE;
  573.     while (!exitloop) {
  574.         if (l != ol) {
  575.             WNcolor(conf.norm);
  576.             if (ol >= 0)
  577.                 WNdisplay(2,ol+1,recs[ol]);
  578.             WNcolor(conf.high);
  579.             WNdisplay(2,l+1,recs[l]);
  580.             ol = l;
  581.         }
  582.         switch (KBgetch()) {
  583.             case ESCAPE :
  584.                 exitloop = TRUE;
  585.                 break;
  586.             case RETURN :
  587.                 exitloop = TRUE;
  588.                 retval = recn[l];
  589.                 break;
  590.             case CURUP :
  591.                 l = (l == 0 ? n-1 : l-1);
  592.                 break;
  593.             case CURDN :
  594.                 l = (l == n-1 ? 0 : l+1);
  595.                 break;
  596.         }
  597.     }
  598.  
  599.     WNcolor(conf.norm);
  600.     WNclose();
  601.     return(retval);
  602. }
  603.  
  604. int
  605. message(s)
  606.     char    *s;
  607. {
  608.     va_list    ap;
  609.     char    t[81];
  610.  
  611.     WNopen(2,2,79,2,conf.norm,POPUP);
  612.     va_start(ap,s);
  613.     vsprintf(t,s,ap);
  614.     va_end(ap);
  615.     strcat(t," : ");
  616.     WNputs(t);
  617.     KBgetch();
  618.     WNclose();
  619. }
  620.  
  621. int
  622. popup(s)
  623.     char    *s;
  624. {
  625.     int        l;
  626.  
  627.     l = 39-(strlen(s)/2);
  628.     WNopen(l,4,l+strlen(s)+1,4,conf.high,POPUP);
  629.     WNprintf(" %s",s);
  630.     delay(SHOWTIME);
  631. }
  632.  
  633. int
  634. getans(s)
  635.     char    *s;
  636. {
  637.     uchar    ch;
  638.  
  639.     WNopen(2,2,79,2,conf.norm,POPUP);
  640.     WNputc(' ');
  641.     WNputs(s);
  642.     ch = toupper(KBgetch());
  643.     WNclose();
  644.  
  645.     return(ch == 'Y' ? TRUE : FALSE);
  646. }
  647.  
  648. int
  649. rbrief()
  650. {
  651.     int        tmprn;
  652.     rec_st    t;
  653.     int        l;
  654.  
  655.     tmprn = DBrecno();
  656.     PRseterr(prerr);
  657.     l = 0;
  658.     DBgo(DBtop());
  659.     while (DBrecno() != 0) {
  660.         if (l++ > 59) {
  661.             PRputs("\014");
  662.             l = 1;
  663.         }
  664.         if (l == 1)
  665.             header("Brief Patient Listing");
  666.         DBread(&t);
  667.         PRprintf("%-20s %-20s %-36.36s\r\n",t.sn,t.fn,t.addr1);
  668.         DBskip(1);
  669.     }
  670.     DBgo(tmprn);
  671. }
  672.  
  673. void
  674. prerr(s)
  675.     char    *s;
  676. {
  677.     message(s);
  678.     DBgo(DBbot());
  679. }
  680.  
  681. int
  682. rfull()
  683. {
  684.     int        tmprn;
  685.     rec_st    t;
  686.     int        l;
  687.  
  688.     tmprn = DBrecno();
  689.     PRseterr(prerr);
  690.     l = 0;
  691.     DBgo(DBtop());
  692.     while (DBrecno() != 0) {
  693.         if (l++ > 9) {
  694.             PRputs("\014");
  695.             l = 1;
  696.         }
  697.         if (l == 1)
  698.             header("Full Patient Listing");
  699.         DBread(&t);
  700.         PRprintf("%-20s %-20s %s\r\n%-60s %s\r\n",t.sn,t.fn,
  701.             (t.paid == 'Y' ? "Paid" : "Not Paid"),t.addr1,t.phon);
  702.         PRputs(pline);
  703.         DBskip(1);
  704.     }
  705.     DBgo(tmprn);
  706. }
  707.  
  708. int
  709. header(s)
  710.     char    *s;
  711. {
  712.     PRprintf("\016%6sPatient Record System (v%s)\r\n","",version);
  713.     PRprintf("%*s%s\r\n",40-(strlen(s)/2),"",s);
  714.     PRputs(pline);
  715. }
  716.  
  717. int
  718. rpaid()
  719. {
  720.     int        tmprn;
  721.     rec_st    t;
  722.     int        l;
  723.  
  724.     tmprn = DBrecno();
  725.     PRseterr(prerr);
  726.     l = 0;
  727.     DBgo(DBtop());
  728.     while (DBrecno() != 0) {
  729.         DBread(&t);
  730.         if (t.paid != 'Y') {
  731.             if (l++ > 59) {
  732.                 PRputs("\014");
  733.                 l = 1;
  734.             }
  735.             if (l == 1)
  736.                 header("Unpaid Patient Report");
  737.             PRprintf("%-20s %-20s %s\r\n",t.sn,t.fn,t.addr1);
  738.             DBskip(1);
  739.         }
  740.     }
  741.     DBgo(tmprn);
  742. }
  743.