home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_08_07 / 8n07125a < prev    next >
Text File  |  1990-06-19  |  5KB  |  164 lines

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