home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / progm / cwt2.zip / DBUPDATE.C < prev    next >
Text File  |  1989-11-26  |  4KB  |  145 lines

  1. #include <stdio.h>
  2. #include <c_wndw.h>
  3. #include <c_ndx.h>
  4. struct MAP {int field, col;};
  5. void setup (int, struct DBF_DEF*, struct MAP*);
  6. void record_display (int, struct MAP*, int);
  7. int  record_accept  (int, struct MAP*, int);
  8.  
  9. /*
  10. *    Copyright 1989, Marietta Systems, Inc.
  11. *    All rights reserved
  12. */
  13.  
  14. /*
  15. * * * * * * * * * * * * *
  16. *   This function builds the entry control map array for the entry
  17. *   of the fields.
  18. */
  19. void setup (int fh, struct DBF_DEF *dict, struct MAP *map)
  20. {
  21.     int col = 1, i;
  22. /* get the field numbers from data dictionary */
  23.     map[0].field = dbffield (fh, "FIRSTNAME");
  24.     map[1].field = dbffield (fh, "INITIAL");
  25.     map[2].field = dbffield (fh, "LASTNAME");
  26.     map[3].field = dbffield (fh, "NICKNAME");
  27.     map[4].field = dbffield (fh, "INCOME");
  28.     map[5].field = -1;
  29. /* setup starting field locations on the screen */
  30.     for (i = 0 ; i < 5 ; i++)
  31.     {
  32.         map[i].col = col;
  33.         col += dict[map[i].field].dbf_len + 1;
  34.     }
  35.     display ("Firstname          Init Lastname            \
  36. Nickname             Income", 1, 1, alt_low);
  37. }
  38.  
  39. /*
  40. * * * * * * * * * * * * *
  41. *    This function displays the current record from the dBASE file
  42. */
  43. void record_display (int fh, struct MAP *map, int row)
  44. {
  45.     int i;
  46.     for (i = 0 ; i < 5 ; i++)
  47.         dispdbf (fh, map[i].field, row, map[i].col, reverse);
  48. }
  49.  
  50. /*
  51. * * * * * * * * * * * * *
  52. *     This function accepts a record for the dBASE file
  53. *    It returns 1 if the record is completely entered, o/w zero
  54. */
  55. int record_accept (int fh, struct MAP *map, int row)
  56. {
  57.     int i, ret;
  58.     for (i = 0; i < 5 && i >= 0 ; )
  59.     {
  60.         set_crsr (row, map[i].col); /* set cursor */
  61.         ret = acptdbf (fh, map[i].field, alt_high);
  62.         if (ret < 0) return 0; /* error */
  63.  
  64.         switch (ret)
  65.         {
  66.             case QUIT: case ESC: return 0;
  67.  
  68.             case 0: case ENTER: case CRS_RT: i++; break;
  69.  
  70.             case CRS_LT: i--; break;
  71.  
  72.             default: shortbeep(ret); break;
  73.         }
  74.     }
  75.     return (i < 0 ? 0 : 1);
  76. }
  77.  
  78. /*
  79. * * * * * * * * * * * * * * * *
  80. *     This program reads opens a dBASE file and its associated index file.
  81. *    Then reads sequentially through the file to allow the operator to
  82. *    modify the INCOME field.
  83. *    This continues until the end of the file or the F10 key is pressed.
  84. */
  85.  
  86. void main ()
  87. {
  88.      int fh, ndx, ret, x, action = 1;
  89.      struct DBF_DEF *dict;
  90.      struct MAP map[6];
  91. /* initialize */
  92.      clr_scrn ("Test dBASE screen I/O");
  93.      set_keys (3, ESC, CRS_RT, CRS_LT);
  94.      fh = fileopen ("MARIETTA\\FRIENDS.DBF", dbase3, update);
  95.      if (!fh) goodbye (err_wndw (" Cannot find FRIENDS.DBF file ", 0, 0));
  96.      dict = FN[fh].fnext;     /* dBASE data dictionary */
  97.      ndx = ndxopen (fh, "MARIETTA\\FRIENDS.NDX"); /* index file */
  98.      if (ndx <= 0) goodbye (10);
  99.      setup (fh, dict, map);
  100.  
  101. /* read and display the first record */
  102.      ret = ndxread (ndx, firstrec);
  103.      record_display (fh, map, (x = 2));
  104.  
  105.  /* read and update file */
  106.     for (;;)
  107.     {
  108.         flt_wndw (5, 20, "Select action");
  109.         action = menu ("Next record&Previous record&Modify record&\
  110. Add a record&Quit (or press ESC)&", 20, action, 1);
  111.         rm_wndw(); /* remove menu window */
  112.  
  113.         switch (action)
  114.         {
  115.             case 1: /* next record */
  116.                 ret = ndxread (ndx, nextrec);
  117.                 if (ret <= 0) err_wndw (" End of file reached ", 0, 9);
  118.                 else record_display (fh, map, ++x);
  119.                 break;
  120.  
  121.             case 2: /* previous record */
  122.                 ret = ndxread (ndx, previous);
  123.                  if (ret <= 0) err_wndw (" End of file reached ", 0, 9);
  124.                 else record_display (fh, map, ++x);
  125.                 break;
  126.  
  127.             case 3: /* modify record */
  128.                 ret = record_accept (fh, map, x);
  129.                 if (ret) ndxwrite (ndx); /* rewrite modified record */
  130.                 break;
  131.  
  132.             case 4: /* add new record */
  133.                 memset (FN[fh].record, 32, FN[fh].rec_len); /* clear record */
  134.                 *FN[fh].record = IN_USE;
  135.                 ret = record_accept (fh, map, ++x);
  136.                 if (ret) ndxappend (ndx); /* write new record */
  137.                 break;
  138.  
  139.             default: /* end with F10 or Esc */
  140.                 fileclos (fh);
  141.                 goodbye (0);
  142.         }
  143.      }
  144. }
  145.