home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_01_04 / 1n04027a < prev    next >
Text File  |  1990-08-06  |  3KB  |  124 lines

  1. /*             LISTING 2                */
  2.  
  3.  
  4. /* Error module */
  5.  
  6. #include <stddef.h>
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include "err.h"
  11.  
  12. #define MAX_STACK       5
  13.  
  14. /* Local variables */
  15. static int       error_stack[MAX_STACK];
  16. static PSTR    str_stack[MAX_STACK];
  17. static int       stack_index = 0;
  18.  
  19. static PSTR local_errlist[] = {
  20.     "invalid error number",
  21.     "undefined error occured",
  22.     "unable to read file header",
  23.     "unable to open index file",
  24.     "unable to delete record",
  25.     "unable to complete requested command"
  26. };
  27.  
  28. /* Local prototypes */
  29. static PSTR err_get_msg(int);
  30.  
  31. /*-------------------------------------------------
  32.     err_push - add an error message to the stack
  33. -------------------------------------------------*/
  34. void  err_push(int ercode, PSTR p_str)
  35. {
  36.     if (stack_index < MAX_STACK) {
  37.         error_stack[stack_index] = ercode;
  38.         if (p_str != NULL) {
  39.             if ((str_stack[stack_index] =
  40.                     malloc(strlen(p_str) + 1)) != NULL) {
  41.                 strcpy(str_stack[stack_index], p_str);
  42.             }
  43.         }
  44.         stack_index++;
  45.     }
  46. }
  47.  
  48. /*-------------------------------------------------
  49.     err_state - check for error(s) on stack
  50. -------------------------------------------------*/
  51. BOOLEAN  err_state()
  52. {
  53.     if (stack_index > 0) {
  54.         return(TRUE);
  55.     } else {
  56.         return(FALSE);
  57.     }
  58. }
  59.  
  60. /*-------------------------------------------------
  61.     err_clear - clear the error stack
  62. -------------------------------------------------*/
  63. void  err_clear()
  64. {
  65.     int i;
  66.  
  67.     for (i = 0; i < MAX_STACK; i++) {
  68.         if (str_stack[i] != NULL) {
  69.             free(str_stack[i]);
  70.             str_stack[i] = NULL;
  71.         }
  72.     }
  73.     stack_index = 0;
  74. }
  75.  
  76. /*-------------------------------------------------
  77.     err_disp - display an error message
  78. -------------------------------------------------*/
  79. void  err_disp()
  80. {
  81.     int  i;
  82.     char reply[10];
  83.  
  84.     while (stack_index > 0) {
  85.         stack_index--;
  86.         fprintf(stderr, "ERROR: %s",
  87.                 err_get_msg(error_stack[stack_index]));
  88.         if (str_stack[stack_index] != NULL) {
  89.             fprintf(stderr, ": %s\n",
  90.                     str_stack[stack_index]);
  91.         } else {
  92.             fprintf(stderr, "\n");
  93.         }
  94.         if (stack_index > 0) {
  95.             fprintf(stderr, "    Do you want more"
  96.                     " information? (y/n) ");
  97.             gets(reply);
  98.             if (reply[0] != 'y' && reply[0] != 'Y') break;
  99.         }
  100.     }
  101.  
  102.     /* Clear error stack */
  103.     err_clear();
  104. }
  105.  
  106. /*-------------------------------------------------
  107.     err_get_msg - get message for error number
  108. -------------------------------------------------*/
  109. static PSTR err_get_msg(int msg)
  110. {
  111.     PSTR p_str = NULL;
  112.  
  113.     if (msg < sys_nerr) {
  114.         p_str = sys_errlist[msg];
  115.     } else if (msg >= MIN_ERR_NUMBER && msg <=
  116.             MAX_ERR_NUMBER) {
  117.         p_str = local_errlist[msg - MIN_ERR_NUMBER];
  118.     } else {
  119.         p_str = local_errlist[ERR_INVALID_ERCODE -
  120.                 MIN_ERR_NUMBER];
  121.     }
  122.     return(p_str);
  123. }
  124.