Listing 2

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