home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_08_04 / 8n04073a < prev    next >
Text File  |  1990-03-20  |  6KB  |  151 lines

  1. *****Listing 2*****
  2.    1:   /*
  3.    2:    *  MDBMAIN.C       (Static Array Only Version)
  4.    3:    *
  5.    4:    *  Program:    Mini-Database
  6.    5:    *  Written by: Leor Zolman
  7.    6:    *  Module:     Main Program Module
  8.    7:    *
  9.    8:    *  Program Description:
  10.    9:    *      This system is an "introductory showcase" of
  11.   10:    *      C programming techniques for File I/O-related
  12.   11:    *      applications. Areas of focus include:
  13.   12:    *          Static and Dynamic Array Allocation
  14.   13:    *          Text-based and Binary-based Disk Data Storage
  15.   14:    *          Elementary user-interface and error-handling
  16.   15:    *
  17.   16:    *  Compile & Link (Turbo C):
  18.   17:    *      tcc mdbmain.c mdbedit.c mdbutil.c
  19.   18:    *                          {mdbftxt.c or mdbfbin.c}
  20.   19:    */     
  21.   20:   
  22.   21:   #include <stdio.h>
  23.   22:   #include <stdlib.h>
  24.   23:   
  25.   24:   #define MAIN_MODULE 1       /* force data definitions   */
  26.   25:   #include "mdb.h"
  27.   26:   
  28.   27:   
  29.   28:   #define CREATE  1           /* Main menu action codes   */
  30.   29:   #define OPEN    2
  31.   30:   #define EDIT    3
  32.   31:   #define SAVE    4
  33.   32:   #define BAKUP   5
  34.   33:   #define CLOSE   6
  35.   34:   #define ABANDON 7
  36.   35:   #define QUIT    8
  37.   36:   
  38.   37:   static struct menu_item main_menu[] =
  39.   38:   {
  40.   39:       {CREATE, "Create New Database"},
  41.   40:       {OPEN, "Select Existing Database to Work With"},
  42.   41:       {EDIT, "Edit Database Records"},
  43.   42:       {SAVE, "Write Database to Disk"},
  44.   43:       {BAKUP, "Backup Database to Floppies"},
  45.   44:       {CLOSE, "Close the Database"},
  46.   45:       {ABANDON, "Abandon Changes to the Current Database"},
  47.   46:       {QUIT, "Quit"},
  48.   47:       {NULL}                          /* End of list              */
  49.   48:   };
  50.   49:   
  51.   50:   
  52.   51:   main(int argc, char **argv)
  53.   52:   {
  54.   53:       char db_name[150];
  55.   54:       int db_active = FALSE;      /* No Database currently open   */
  56.   55:       FILE *fp;
  57.   56:   
  58.   57:       while (1)
  59.   58:       {
  60.   59:           switch(do_menu(main_menu, "Main Menu"))
  61.   60:           {
  62.   61:               case CREATE:
  63.   62:                   if (db_active)
  64.   63:                       goto still_open;
  65.   64:                   printf("Name for new Database? ");
  66.   65:                   gets(db_name);
  67.   66:                   if ((fp = fopen(db_name,"r")) != NULL)
  68.   67:                   {
  69.   68:                       printf("A file by that name already exists.\n");
  70.   69:                       fclose(fp);
  71.   70:                       break;
  72.   71:                   }
  73.   72:                   max_recs = MAX_RECS;
  74.   73:                   db_active = TRUE;
  75.   74:                   n_recs = 0;
  76.   75:                   printf("Database created; entering EDIT mode:\n");
  77.   76:                           /* After creating, fall through to EDIT */
  78.   77:   
  79.   78:               case EDIT:
  80.   79:                   if (!db_active)
  81.   80:                       goto inactive;
  82.   81:                   edit_db();  /* Edit records in memory   */
  83.   82:                   break;
  84.   83:                   
  85.   84:               case OPEN:
  86.   85:                   if (db_active)
  87.   86:                   {
  88.   87:           still_open: printf("Please close the current Database.\n");
  89.   88:                       break;
  90.   89:                   }
  91.   90:                   printf("Database Name? ");
  92.   91:                   gets(db_name);
  93.   92:                   if ((n_recs = read_db(db_name)) != NULL)
  94.   93:                   {
  95.   94:                       printf("\nLoaded %d Record(s).\n", n_recs);
  96.   95:                       db_active = TRUE;
  97.   96:                   }
  98.   97:   
  99.   98:                   edit_db();
  100.   99:                   break;
  101.  100:                           
  102.  101:               case BAKUP:
  103.  102:                   if (!db_active)
  104.  103:                       goto inactive;
  105.  104:                   backup_db();    /* Perform backup       */
  106.  105:                   break;
  107.  106:                           
  108.  107:               case CLOSE:
  109.  108:                   if (!db_active)
  110.  109:                       goto inactive;
  111.  110:                   write_db(db_name);  /* write to disk    */
  112.  111:                   free_up();
  113.  112:                   db_active = FALSE;
  114.  113:                   break;
  115.  114:               
  116.  115:               case SAVE:
  117.  116:                   if (!db_active)
  118.  117:                       goto inactive;
  119.  118:                   write_db(db_name);  /* write to disk    */
  120.  119:                   break;
  121.  120:   
  122.  121:               case ABANDON:
  123.  122:                   if (!db_active)
  124.  123:                   {
  125.  124:             inactive: printf("Please select a Database first!\n");
  126.  125:                       break;
  127.  126:                   }
  128.  127:                   free_up();
  129.  128:                   db_active = FALSE;
  130.  129:                   break;
  131.  130:                           
  132.  131:               case QUIT:
  133.  132:                   if (db_active)
  134.  133:                       goto still_open;
  135.  134:                   exit(0);
  136.  135:           }
  137.  136:       }
  138.  137:   }
  139.  138:   
  140.  139:   /*
  141.  140:    * Function:        backup_db
  142.  141:    * Purpose:         Backup current Database to floppies
  143.  142:    * Parameters:      None
  144.  143:    * Return Value:    None
  145.  144:    */
  146.  145:   
  147.  146:   void backup_db()        /* Backup module */
  148.  147:   {}
  149.  148:   
  150.  149:   
  151.