home *** CD-ROM | disk | FTP | other *** search
/ Hall of Fame / HallofFameCDROM.cdr / proglc / index.lzh / ADR.C next >
Text File  |  1987-11-03  |  7KB  |  305 lines

  1. /*
  2.  * ADR.C - address book.  Illustrates use of index file functions.
  3.  *
  4.  *                      Copyright (c) 1987, Jim Mischel
  5.  * Modifications:
  6.  *
  7.  * 08/21/87 - jim - original coding
  8.  *
  9.  */
  10.  
  11. #include <stdio.h>
  12. #include <conio.h>
  13. #include <ctype.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include "index.h"
  17.  
  18. typedef struct {
  19.   char first_name[31],
  20.        last_name[31],
  21.        business[31],
  22.        street[31],
  23.        city[26],
  24.        state[3],
  25.        zip[10],
  26.        phone1[11],
  27.        phone2[11],
  28.        name_key[21];
  29. } address_rec;
  30.  
  31. #define ed printf("\033[2J")    /* erase display */
  32. #define el printf("\033[K")     /* erase line */
  33.  
  34. void cup(int row, int col);
  35. void edit_it(void);
  36. void change_item(int item);
  37. void get_ret(void);
  38. void usage(void);
  39. void lookup(void);
  40. void list_forward(void);
  41. void list_backward(void);
  42. int icmp_string(void *arg1, void *arg2);
  43.  
  44. char *adr_file;                 /* file descriptor for indexed file routines */
  45. address_rec addr;
  46.  
  47. int main(int argc, char *argv[])
  48. {
  49.   unsigned offset;
  50.   char c;
  51.  
  52.   if (!argc) {
  53.     usage();
  54.     return(1);
  55.   }
  56.  
  57.   offset = (char *) &addr.name_key - (char *) &addr;
  58.  
  59.   if ((adr_file = iopen(argv[1],sizeof(address_rec),offset,1,icmp_string)) == NULL) {
  60.     printf("\nCannot open file %s\n",argv[1]);
  61.     printf("Error status is %X\n",ierrno);
  62.     return(1);
  63.   }
  64.  
  65.   do {
  66.     ed;
  67.     cup(3,34);  printf("ADDRESS BOOK");
  68.     cup(24,1);  printf("(L)ookup, (F)orward list, (B)ackward list, (Q)uit");
  69.     cup(23,1);  printf("Enter function ");
  70.     switch (c = toupper(getche())) {
  71.       case 'L' :
  72.         lookup();
  73.         break;
  74.       case 'F' :
  75.         list_forward();
  76.         break;
  77.       case 'B' : list_backward();
  78.     }
  79.   } while (c != 'Q');
  80.  
  81.   ed;
  82.   iclose(adr_file);
  83.   return(0);
  84. }
  85.  
  86. void lookup(void)
  87. {
  88.   char quit = 0;
  89.   char tempkey[24];
  90.  
  91.   do {
  92.     ed;                         /* erase display */
  93.     cup(3,34);  printf("ADDRESS BOOK");
  94.     cup(5,1);   printf(" 1. Key        :");
  95.     cup(6,1);   printf(" 2. First name :");
  96.     cup(7,1);   printf(" 3. Last name  :");
  97.     cup(8,1);   printf(" 4. Business   :");
  98.     cup(9,1);   printf(" 5. Street     :");
  99.     cup(10,1);  printf(" 6. City       :");
  100.     cup(11,1);  printf(" 7. State      :");
  101.     cup(12,1);  printf(" 8. Zip        :");
  102.     cup(13,1);  printf(" 9. Home phone :");
  103.     cup(14,1);  printf("10. Work phone :");
  104.     tempkey[0] = 21;
  105.     cup(5,18);
  106.     cgets(tempkey);
  107.     if (tempkey[1] == 0)
  108.       quit = 1;
  109.     else {
  110.       strcpy(addr.name_key,&tempkey[2]);
  111.       edit_it();
  112.     }
  113.   } while(!quit);
  114.   return;
  115. }
  116.  
  117. void edit_it(void)
  118. {
  119.   int item;
  120.   char c,
  121.        trash[6];
  122.  
  123.   if (iread(adr_file,&addr)) {
  124.     cup(23,1); printf("%s Not found.  Add new record? ",addr.name_key);
  125.     if (toupper(getche()) != 'Y')
  126.       return;                   /* not found */
  127.     cup(23,1); el;              /* erase line */
  128.     for (item = 2; item <= 10; change_item(item++));
  129.     if (iwrite(adr_file,&addr)) {
  130.       cup(23,1); printf("Error write.  Status is %X.\n",ierrno);
  131.       get_ret();
  132.       return;
  133.     }
  134.   }
  135.   else {
  136.     cup(5,18);  puts(addr.name_key);
  137.     cup(6,18);  puts(addr.first_name);
  138.     cup(7,18);  puts(addr.last_name);
  139.     cup(8,18);  puts(addr.business);
  140.     cup(9,18);  puts(addr.street);
  141.     cup(10,18); puts(addr.city);
  142.     cup(11,18); puts(addr.state);
  143.     cup(12,18); puts(addr.zip);
  144.     cup(13,18); puts(addr.phone1);
  145.     cup(14,18); puts(addr.phone2);
  146.   }
  147.   do {
  148.     cup(23,1); printf("Enter function * (C,D,Q)");
  149.     cup(23,16);
  150.     switch (c = toupper(getche())) {
  151.       case 'C'  :
  152.         do {
  153.           cup(23,1); el;
  154.           printf("Enter item to change ");
  155.           trash[0] = 3;
  156.           cgets(trash);
  157.           if (trash[1] == 0)
  158.             item = 0;
  159.           else {
  160.             item = atoi(&trash[2]);
  161.             if (item > 1 && item < 11)
  162.               change_item(item);
  163.           }
  164.         } while (item != 0);
  165.         if (irewrite(adr_file,&addr)) { /* update the record */
  166.           cup(23,1);  printf("Error rewrite.  Status is %X\n",ierrno);
  167.           get_ret();
  168.           return;
  169.         }
  170.         break;
  171.       case 'D'  :
  172.         if (idelete(adr_file,&addr)) {
  173.           cup(23,1); printf("Error delete.  Status is %X\n",ierrno);
  174.           get_ret();
  175.           return;
  176.         }
  177.         c = 'Q';
  178.         break;
  179.       case 'Q'  :
  180.         break;
  181.       default   :
  182.         putch('\007');
  183.     } /* switch */
  184.   } while (c != 'Q');
  185. } /* edit_it */
  186.  
  187. void change_item(int item)
  188. {
  189.   char temp[35];
  190.   switch (item) {
  191.     case 2 :
  192.       cup(6,18);
  193.       temp[0] = 31;
  194.       cgets(temp);
  195.       strcpy(addr.first_name,&temp[2]);
  196.       break;
  197.     case 3 :
  198.       cup(7,18);
  199.       temp[0] = 31;
  200.       cgets(temp);
  201.       strcpy(addr.last_name,&temp[2]);
  202.       break;
  203.     case 4 :
  204.       cup(8,18);
  205.       temp[0] = 31;
  206.       cgets(temp);
  207.       strcpy(addr.business,&temp[2]);
  208.       break;
  209.     case 5 :
  210.       cup(9,18);
  211.       temp[0] = 31;
  212.       cgets(temp);
  213.       strcpy(addr.street,&temp[2]);
  214.       break;
  215.     case 6 :
  216.       cup(10,18);
  217.       temp[0] = 26;
  218.       cgets(temp);
  219.       strcpy(addr.city,&temp[2]);
  220.       break;
  221.     case 7 :
  222.       cup(11,18);
  223.       temp[0] = 3;
  224.       cgets(temp);
  225.       strcpy(addr.state,&temp[2]);
  226.       break;
  227.     case 8 :
  228.       cup(12,18);
  229.       temp[0] = 10;
  230.       cgets(temp);
  231.       strcpy(addr.zip,&temp[2]);
  232.       break;
  233.     case 9 :
  234.       cup(13,18);
  235.       temp[0] = 11;
  236.       cgets(temp);
  237.       strcpy(addr.phone1,&temp[2]);
  238.       break;
  239.     case 10 :
  240.       cup(14,18);
  241.       temp[0] = 11;
  242.       cgets(temp);
  243.       strcpy(addr.phone2,&temp[2]);
  244.       break;
  245.   } /* switch */
  246. } /* change_item */
  247.  
  248. void list_forward(void)
  249. {
  250.   ed;
  251.   if (istart(adr_file,START_FILE,&addr)) {
  252.     printf("Error starting file.  Error code is %X\n",ierrno);
  253.     return;
  254.   }
  255.   while (!iread_next(adr_file,&addr))
  256.     printf("%s:  %s %s\n",addr.name_key,addr.first_name,addr.last_name);
  257.   get_ret();
  258. }
  259.  
  260. void list_backward(void)
  261. {
  262.   ed;
  263.   if (istart(adr_file,END_FILE,&addr)) {
  264.     printf("Error starting file.  Error code is %X\n",ierrno);
  265.     return;
  266.   }
  267.   while (!iread_prev(adr_file,&addr))
  268.     printf("%s:  %s %s\n",addr.name_key,addr.first_name,addr.last_name);
  269.   get_ret();
  270. }
  271.  
  272. void cup(int row, int col)
  273. {
  274.   char rows[6],
  275.        cols[6];
  276.   itoa(row,rows,10);
  277.   itoa(col,cols,10);
  278.   printf("\033[%s;%sH",rows,cols);
  279. }
  280.  
  281. void get_ret(void)
  282. {
  283.   cup(24,1);  printf("Press any key ... ");
  284.   getche();
  285. }
  286.  
  287. /*
  288.  * icmp_string() - string comparison routine.  Returns:
  289.  *        -1 if arg1 < arg2
  290.  *         0 if arg1 == arg2
  291.  *         1 if arg1 > arg2
  292.  */
  293. int icmp_string(void *arg1, void *arg2)
  294. {
  295.   int r;
  296.  
  297.   return(((r = strcmp((char *)arg1, (char *)arg2)) == 0) ? 0 :
  298.          (r > 0) ? 1 : -1);
  299. }
  300.  
  301. void usage(void)
  302. {
  303.   printf("Usage: adr <filename>\n");
  304. }
  305.