Listing 2

  1:  /*
  2:   *  MDBMAIN.C   (Static Array Only Version)
  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:
 57:    while (1)
 58:    {
 59:      switch(do_menu(main_menu, "Main Menu"))
 60:      {
 61:        case CREATE:
 62:        if (db_active)
 63:          goto still_open;
 64:        printf("Name for new Database? ");
 65:        gets(db_name);
 66:        if ((fp = fopen(db_name,"r")) != NULL)
 67:        {
 68:          printf("That filename already exists.\n");
 69:          fclose(fp);
 70:          break;
 71:        }
 72:        max_recs = MAX_RECS;
 73:        db_active = TRUE;
 74:        n_recs = 0;
 75:        printf("Entering EDIT mode:\n");
 76:         /* After creating, fall through to EDIT */
 77:
 78:       case EDIT:
 79:       if (!db_active)
 80:         goto inactive;
 81:       edit_db(db_name);    /* Edit recs in memory */
 82:       break;
 83:
 84:       case OPEN:
 85:       if (db_active)
 86:       {
 87:   still_open: printf("Current Database still open.\n");
 88:         break;
 89:       }
 90:       printf("Database Name? ");
 91:       gets(db_name);
 92:       if ((n_recs = read_db(db_name)) != NULL)
 93:       {
 94:         printf("\nLoaded %d Record(s).\n",
 95:                   n_recs);
 96:          db_active = TRUE;
 97:       }
 98:
 99:       edit_db(db_name);
100:        break;
101:
102:       case BAKUP:
103:       if (!db_active)
104:         goto inactive;
105:       backup_db();  /* Perform backup  */
106:       break;
107:
108:       case CLOSE:
109:       if (!db_active)
110:         goto inactive;
111:       write_db(db_name); /* write to disk */
112:       free_up();
113:       db_active = FALSE;
114:       break;
115:
116:       case SAVE:
117:       if (!db_active)
118:         goto inactive;
119:       write_db(db_name); /* write to disk */
120:       break;
121:
122:       case ABANDON:
123:       if (!db_active)
124:       {
125:   inactive: printf("Please select a Database!\n");
126:         break;
127:       }
128:       free_up();
129:       db_active = FALSE;
130:       break;
131:
132:       case QUIT:
133:       if (db_active)
134:       {
135:         write_db(db_name);  /* write to disk  */
136:         free_up();
137:       }
138:       exit(0);
139:     }
140:    }
141:  }
142:
143:  /*
144:   * Function:    backup_db
145:   * Purpose:     Backup current Database to floppies
146:   * Parameters:    None
147:   * Return Value:  None
148:   */
149:
150:  void backup_db()    /* Backup module */
151:  {}