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

  1. *****Listing 3*****
  2.  
  3.    1:   /*
  4.    2:    *  MDBUTIL.C
  5.    3:    *
  6.    4:    *  Program:    Mini-Database
  7.    5:    *  Written by: Leor Zolman
  8.    6:    *  Module:     Utility functions
  9.    7:    */
  10.    8:   
  11.    9:   #include <stdio.h>
  12.   10:   #include <stdlib.h>
  13.   11:   #include "mdb.h"
  14.   12:   
  15.   13:   
  16.   14:   /*
  17.   15:    * Function:        do_menu
  18.   16:    * Purpose:         Simple line-oriented menu handler
  19.   17:    * Parameters:      None
  20.   18:    * Return Value:    None
  21.   19:    */
  22.   20:   
  23.   21:   int do_menu(struct menu_item *mnu, char *title)
  24.   22:   {
  25.   23:       int i, j;
  26.   24:       char buf[150];
  27.   25:       
  28.   26:       printf("\n%s -- Options:\n", title);
  29.   27:       for (i = 0; mnu[i].action_code != NULL; i++)
  30.   28:           printf("%2d) %s\n", i+1, mnu[i].descrip);
  31.   29:   
  32.   30:       while (1)
  33.   31:       {
  34.   32:           printf("\nYour choice? ");
  35.   33:           j = atoi(gets(buf));
  36.   34:           if (j >= 1 && j <= i)
  37.   35:               break;
  38.   36:           printf("Please select from options 1-%d: ", i+1);
  39.   37:       }
  40.   38:       
  41.   39:       return mnu[j - 1].action_code;
  42.   40:   }
  43.   41:   
  44.   42:   
  45.   43:   /*
  46.   44:    * Function:        error
  47.   45:    * Purpose:         Report error and terminate program
  48.   46:    * Parameters:      Message to display
  49.   47:    * Return Value:    None
  50.   48:    */
  51.   49:   
  52.   50:   void error(char *msg)
  53.   51:   {
  54.   52:       printf("Fatal Condition: %s\n", msg);
  55.   53:       exit(-1);
  56.   54:   }
  57.   55:   
  58.   56:   
  59.   57:   /*
  60.   58:    * Function:        alloc_rec
  61.   59:    * Purpose:         Allocate memory for a Database record,
  62.   60:    *                       checking for an allocation error
  63.   61:    * Parameters:      None
  64.   62:    * Return Value:    Pointer to memory, or NULL on error
  65.   63:    */
  66.   64:   
  67.   65:   struct record *alloc_rec(void)
  68.   66:   {
  69.   67:       struct record *temp;
  70.   68:       
  71.   69:       if ((temp = malloc(sizeof(struct record))) == NULL)
  72.   70:           return NULL;
  73.   71:       else
  74.   72:           return temp;
  75.   73:   }
  76.   74:   
  77.   75:   
  78.   76:   /*
  79.   77:    * Function:        free_up
  80.   78:    * Purpose:         De-allocate all records in current Database
  81.   79:    * Parameters:      None
  82.   80:    * Return Value:    None
  83.   81:    */
  84.   82:   
  85.   83:   void free_up()
  86.   84:   {
  87.   85:       int i;
  88.   86:   
  89.   87:       for (i = 0; i < n_recs; i++)
  90.   88:           free(RECS[i]);
  91.   89:   }
  92.   90:                   
  93.   91:   
  94.