home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / c / kafsrt20.zip / KATEST.C < prev    next >
C/C++ Source or Header  |  1993-05-29  |  12KB  |  460 lines

  1. /***************************************************************************
  2.  * KATEST.C - Test for the key access System
  3.  *
  4.  * THIS PROGRAM REQUIRES THE USE OF THE SORT MODULE.
  5.  * ALL KAFS (KEYED ACCESS FILE SYSTEM) LINES ARE DENOTED BY
  6.  *   A COMMENT BEGINNING WITH: $$KAFS$$
  7.  *
  8.  ***************************************************************************/
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <alloc.h>
  12. #include <stdlib.h>
  13.  
  14. #include "KATEST.H"
  15.  
  16. #define SOF -1
  17.  
  18. extern long rec;
  19. extern char key[];
  20.  
  21.  
  22. struct courserec course;  /* Define the Course File Structure */
  23.  
  24. char password[]="password";
  25.  
  26. void main();
  27. void pgmxit();
  28. void mcourse();
  29. int rf_course();
  30. void courselist();
  31. void w_putp(int row, int col, char *str);
  32. void w_pc(int row, int col);
  33. int w_penter(int row, int col, char *str, int len);
  34. int w_enter(char str[], int len);
  35. void error(char *es);
  36.  
  37. void main()
  38. {
  39.    int sel;
  40.    int recsize;
  41.  
  42.    recsize = sizeof(course);   /* $$KAFS$$ Find out Record Sizes */
  43.  
  44.    if(ka_open(COURSEFILE,"COURSE")!=OK)  /* $$KAFS$$ Open the course file */
  45.       {
  46.       putchar(7); 
  47.       printf("\n$$$ CANNOT OPEN COURSE FILE $$$\n");
  48.       getch();
  49.       return;
  50.       }
  51.  
  52.    
  53. while(TRUE)
  54.    {
  55.  clrscr();
  56.  
  57.   printf("                           KAFS - Keyed Access File System\n\n");
  58.   printf("                                    TEST APPLICATION\n\n\n\n\n");
  59.   printf("                                1 - (C)ourse Maintenance\n\n");
  60.   printf("                                2 - (L)ist Courses\n\n");
  61.   printf("                                3 - (Q)uit to DOS\n");
  62.  
  63.   w_pc(24,2);
  64.   printf("Course Record Size is %d\n",recsize);
  65.  
  66.    sel=toupper(getch());
  67.    switch(sel)
  68.       {
  69.       case '1':
  70.       case 'C':
  71.         mcourse();
  72.         break;
  73.       case '2':
  74.       case 'L':
  75.          courselist();
  76.          break;
  77.       case '3':
  78.       case 'Q':
  79.       case 27:
  80.          pgmxit();
  81.          break;
  82.       }
  83.    }
  84. }
  85. /*********************************
  86.  * PGMXIT - End Progrm
  87.  *********************************/
  88. void pgmxit()
  89. {
  90.    ka_close_all();  /* $$KAFS$$ Close all Open KAFS Files */
  91.    clrscr();
  92.    exit(0);
  93. }
  94.  
  95. /****************************************************
  96.  * MCOURSE
  97.  ******************************************************/
  98. void mcourse()
  99. {
  100.    int wcc, eflag;
  101.    char resp[250];
  102.    char ans;
  103.    float tf;   /* Temp float */
  104.  
  105.  
  106. while(TRUE)
  107. {
  108. cedit:
  109. clrscr();
  110. memset(&course, 0, sizeof(course));
  111. gotoxy(1,2);
  112. puts(" Course Number:\n");
  113. puts("   Course Name:");
  114. puts("    Instructor:");
  115. puts("         Units:");
  116. puts(" Per Unit Cost:");
  117. puts("         Notes:");
  118. puts("    Commission:\n");
  119. puts("   Total Course Cost:            $$ to MJM:");
  120. puts(" Worksheet Only Cost:            $$ to MJM:");
  121.  
  122.  
  123. strcpy(course.cnum,"");
  124. if(w_penter(2,17,course.cnum,9)==ESC) break;
  125.  
  126. if(rf_course())  /* Read for Course, if found display Values */
  127.    {
  128.    w_putp(4,17,course.cname);
  129.    w_putp(5,17,course.inst);
  130.    w_putp(8,17,course.note);
  131.    sprintf(resp,"%5d",course.units);
  132.    w_putp(6,17,resp);
  133.    sprintf(resp,"%4.2f",course.pucost);
  134.    w_putp(7,17,resp);
  135.    w_pc(9,17);
  136.    putch(course.comm);
  137.  
  138.    sprintf(resp,"%5.2f",course.pucost * course.units);
  139.    w_putp(11,23,resp);
  140.    sprintf(resp,"%5.2f",course.mjmamt);
  141.    w_putp(11,45,resp);
  142.  
  143.    sprintf(resp,"%5.2f",course.wsonly);
  144.    w_putp(12,23,resp);
  145.    sprintf(resp,"%5.2f",course.mjmwso);
  146.    w_putp(12,45,resp);
  147.  
  148.    eflag=TRUE;
  149.    }
  150. else
  151.   {
  152.    if(strlen(course.cnum)==0) goto cedit;
  153.    putchar(7);
  154.    w_putp(15,4,"COURSE Not Found, Create COURSE (Y/N)?");
  155.    do{ans=tolower(getch());} while(ans != 'y' && ans !='n' && ans !=ESC);
  156.    w_pc(15,1);
  157.    printf("                                           ");
  158.    if(ans != 'y') goto cedit;
  159.    eflag = FALSE;
  160.    }
  161.  
  162. w_pc(4,17);
  163. if(w_enter(course.cname,30)==ESC) goto cedit;
  164. if(eflag && !strcmpi(course.cname,"delete"))
  165.   {
  166.    putchar(7);
  167.    w_putp(15,4,"ENTER PASSWORD TO DELETE:");
  168.    strcpy(resp,"");
  169.    w_enter(resp,10);
  170.    w_pc(15,1);
  171.    printf("                                      ");
  172.    if(strcmpi(resp,password))
  173.       {
  174.       error("INVALID PASSWORD");
  175.       goto cedit;
  176.       }
  177.    rdelete(COURSEFILE, rec); /* $$KAFS$$ DELETE This Record */
  178.    error("RECORD DELETED");
  179.    goto cedit;
  180.   }
  181.  
  182.  
  183. w_pc(5,17);
  184. if(w_enter(course.inst,20)==ESC) goto cedit;
  185.  
  186. sprintf(resp,"%5d",course.units);
  187. w_pc(6,17);
  188. if(w_enter(resp,33)==ESC) goto cedit;
  189. course.units = atoi(resp);
  190.  
  191. sprintf(resp,"%4.2f",course.pucost);
  192. w_pc(7,17);
  193. if(w_enter(resp,4)==ESC) goto cedit;
  194. course.pucost = atof(resp);
  195.  
  196. w_pc(8,17);
  197. if(w_enter(course.note,35)==ESC) goto cedit;
  198.  
  199. gcflg:
  200. if(!eflag) course.comm='N';
  201. sprintf(resp,"%c",course.comm);
  202. w_pc(9,17);
  203. printf("     ");
  204. if(w_enter(resp,1)==ESC) goto cedit;
  205. if(resp[0]!='Y' && resp[0]!='N')
  206. {
  207.    error(" Y or N only");
  208.    goto gcflg;
  209. }
  210. course.comm = resp[0];
  211.  
  212. sprintf(resp,"%5.2f",course.pucost * course.units);
  213. w_putp(11,23,resp);
  214.  
  215. /* Allow Editing the Course Amounts to MJM */
  216. w_pc(11,45);
  217. sprintf(resp,"%5.2f",course.mjmamt);
  218. if(w_enter(resp, 6)==ESC) goto cedit;  /* Edit the mjm Amt */
  219. course.mjmamt = atof(resp);
  220.  
  221. w_pc(12,23);
  222. if(!eflag) course.wsonly = (course.pucost * course.units) - 25;
  223. sprintf(resp,"%5.2f",course.wsonly);
  224. if(w_enter(resp,6)==ESC) goto cedit;
  225. course.wsonly = atof(resp);
  226.  
  227. w_pc(12,45);
  228. sprintf(resp,"%5.2f",course.mjmwso);
  229. if(w_enter(resp, 6)==ESC) goto cedit; /* Edit mjm wso amt */
  230. course.mjmwso = atof(resp);
  231.  
  232.  
  233. if(eflag)
  234.  {
  235.   if(write_upd(COURSEFILE, &course)!=OK)  /* $$KAFS$$ Update the Record */
  236.       error("ERROR UPDATING RECORD");  /* UPDATE */
  237.  }
  238.  else
  239.    {
  240.    if(write_key(COURSEFILE, course.cnum, &course)!=OK) /* $$KAFS$$ Create NEW Record */
  241.        error("ERROR WRITING RECORD"); /* NEW REC */
  242.    }
  243. }
  244. }
  245.  
  246. /*******************************************************************
  247.  * rf_course - record find a course
  248.  *    returns TRUE if found else FALSE
  249.  **************************************************************************/
  250. int rf_course()
  251. {
  252.   rec = SOF;  /* $$KAFS$$ Set for read to begin at beginning of file */
  253.   if(read_key(COURSEFILE, course.cnum, &course)==OK) /* $$KAFS$$ Read by key */
  254.        return(TRUE);
  255.  
  256.   else return(FALSE);
  257. }
  258. /***********************************************************************
  259.  * COURSELIST - Course List Sorted by coursenumber
  260.  ***********************************************************************/
  261. void courselist()
  262. {
  263.     int ccode, sortsize, sortcount;
  264.     int noteflag, invflag;
  265.     int soption;
  266.     char ans;
  267.     char ts[20]; /* Temp String */
  268.     char rptline[133];
  269.     float value;
  270.  
  271.       clrscr();
  272.     invflag=FALSE;
  273.     w_putp(5,2,"DO YOU WANT INVOICING INFORMATION (Y/N)? ");
  274.     do{ans=tolower(getch());} while(ans != 'y' && ans !='n' && ans !=ESC);
  275.     if(ans==ESC) goto crxit;
  276.     if(ans=='y') invflag = TRUE;
  277.     putch(toupper(ans));
  278.  
  279.     noteflag=FALSE;
  280.     if(invflag==FALSE)
  281.        {
  282.        w_putp(7,2,"DO YOU WANT COURSE NOTES PRINTED (Y/N)? ");
  283.        do{ans=tolower(getch());} while(ans != 'y' && ans !='n' && ans !=ESC);
  284.        if(ans==ESC) goto crxit;
  285.        if(ans=='y') noteflag = TRUE;
  286.        putch(toupper(ans));
  287.        }
  288.  
  289.      sortsize = sizeof(course.cnum);
  290.  
  291.      if(initsort(sortsize) != OK)
  292.        {
  293.        error("NO SORT SPACE");
  294.        goto crxit;
  295.        }
  296.  
  297.     w_putp(9,25,"SORT IN PROGRESS\n");
  298.  
  299.  
  300.  
  301.   rec=SOF;  /* $$KAFS$$ Set for read to begin at beginning of file */
  302.   while(read_seq(COURSEFILE, &course)==OK)  /* $$KAFS$$ Read Sequentially */
  303.     {
  304.        sort(course.cnum,rec);
  305.       }
  306.  
  307.    if(sortcount < 1)
  308.     {
  309.     error("NO RECORDS TO SORT");
  310.     goto crxit;
  311.      }
  312.  
  313.    if(merge()!=OK)
  314.     {
  315.     error("MERGE ERROR");
  316.     goto crxit;
  317.     }
  318.  
  319.     clrscr();
  320.     printf("COURSE LIST\n\n");
  321.     if(invflag)
  322.      printf("COURSE #   COURSE NAME                    UNITS COST   WSONLY  MJMAMT  WSOAMT\n");
  323.     else
  324.      printf("COURSE #   COURSE NAME                     INSTRUCTOR            UNITS  COST\n");
  325.     printf("-----------------------------------------------------------------------------\n");
  326.  
  327.       while(TRUE)
  328.        {
  329.        rec = fetch();  /* $$KAFS$$ Set Rec to value from fetch */
  330.        if(rec == SORTERR || rec == FETCHEND) break; /* $$KAFS$$ check rec value */
  331.        read_rec(COURSEFILE, &course);  /* $$KAFS$$ Read by value in rec */
  332.        if(invflag)
  333.     sprintf(rptline,"%-9s  %-31s%3d  %6.2f  %6.2f  %6.2f  %6.2f\n",
  334.            course.cnum,course.cname,course.units,course.pucost * course.units,
  335.              course.wsonly, course.mjmamt, course.mjmwso);
  336.        else
  337.          sprintf(rptline,"%-9s  %-31s %-21s %3d   %6.2f\n",
  338.             course.cnum,course.cname,course.inst,course.units,
  339.                course.pucost * course.units);
  340.        printf("%s",rptline);
  341.        if(noteflag)
  342.          {
  343.          if(strlen(course.note)> 0)
  344.           {
  345.          sprintf(rptline,"          NOTES: %s\n",course.note);
  346.          printf("%s",rptline);
  347.          }
  348.     printf("\n");
  349.         }
  350.      }
  351.    printf("************* REPORT END ************");
  352.    getch();
  353.  
  354.  crxit:
  355.  endsort();   /* Free the Sort buffer */
  356. }
  357.  
  358. /***************************************************************************
  359.  * Screen Postioning and keyboard entry routines
  360.  ***************************************************************************/
  361. void w_putp(int row, int col, char *str)
  362. {
  363.    gotoxy(col, row);
  364.    printf("%s",str);
  365. }
  366.  
  367. void w_pc(int row, int col)
  368. {
  369.    gotoxy(col, row);
  370. }
  371.  
  372. int w_penter(int row, int col, char *str, int len)
  373. {
  374.    w_pc(row, col);
  375.   return(w_enter(str, len));
  376. }
  377.  
  378. int w_enter(char str[], int count)
  379. {
  380.    int c, pos, x;
  381.  
  382.    if(str[0]!=0) pos = strlen(str);
  383.      else pos = 0;
  384.    str[pos] = '\0';  /* Terminate the string just in case */
  385.  
  386.    if(pos>0)
  387.       {
  388.       printf("%s",str);  /* we're in edit mode */
  389.       x=1;        /* Set Xout mode */
  390.       }
  391.     else x=0;   /* NO Xout if not editing */
  392.  
  393.    do {
  394.       c = getch();   /* get a character from stdin */
  395.  
  396.       if (c ==0)   /* Got a Screen Control indicator */
  397.      {
  398.       c = getch();    /* waste the control code */
  399.       if (c == DELETE)
  400.        {
  401.         while(pos) {putch(BKSP);putch(SPACE);putch(BKSP);pos--;} /* Delete The Field */
  402.          }
  403.           c=255;  /* Invalidate, it was a control code */
  404.      }
  405.  
  406.       if (c == ESC || c == ENTER) break;   /* We're done !! */
  407.  
  408.      if(x)
  409.       {
  410.       if(c>=SPACE) while(pos)
  411.      {
  412.      putch(BKSP);
  413.      putch(SPACE);
  414.      putch(BKSP);
  415.      pos--;
  416.      }
  417.       x=0; /* Cancel it now that we've done it */
  418.       }
  419.  /** See if the character is a backspace **/
  420.       if (c == BKSP)
  421.      {
  422.      if(pos == 0)
  423.         putch(BELL);  /* If nothing entered, can't backspace */
  424.         else
  425.            {
  426.            putch(BKSP);    /* Do a Backspace */
  427.            putch(SPACE);
  428.            putch(BKSP);
  429.            pos--;
  430.            }
  431.         }
  432.      else
  433.  /** See if we are at the end of the buffer.  If so, only accept an end **/
  434.       if (pos <= count-1)
  435.      {
  436.         if (c >= SPACE && c < ASCIITOP)
  437.            {
  438.         c=toupper(c);
  439.         str[pos] = c;   /* add the character to buffer */
  440.         putch(c);     /* display it */
  441.         pos++;          /* advance the buffer pointer */
  442.         }
  443.              else putch(BELL);
  444.          }
  445.           else putch(BELL);
  446.      } while ( c != ESC && c != ENTER);
  447.     str[pos] = '\0';   /* Terminate the input string */
  448.     return(c);
  449. }
  450.  
  451. void error(char *es)
  452. {
  453.    putch(BELL);
  454.    gotoxy(2,24);
  455.    printf("%s",es);
  456.    getch();
  457.    gotoxy(2,24);
  458.    printf("                                                      ");
  459. }
  460.