root/src/cerr.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. cerr_init
  2. addcerrlist
  3. printcerrlist
  4. setcerr
  5. getcerrmsg
  6. freecerr

   1 #include "cerr.h"
   2 
   3 void cerr_init()
   4 {
   5     cerr = malloc_chk(sizeof(CERR), "cerr");
   6     cerr->num = 0;
   7 }
   8 
   9 CERR *cerr = NULL;
  10 
  11 CERRLIST *cerrlist = NULL;
  12 
  13 void addcerrlist(int cerrc, CERR cerrv[])
  14 {
  15     CERRLIST *stat = NULL;
  16     CERRLIST *p = NULL;
  17 
  18     assert(cerrc > 0 && cerrv != NULL);
  19     for(int i = 0; i < cerrc; i++) {
  20         if(p == NULL) {
  21             stat = p = malloc_chk(sizeof(CERRLIST), "cerrlist");
  22         } else {
  23             p = p->next = malloc_chk(sizeof(CERRLIST), "cerrlist->next");
  24         }
  25         p->cerr = &cerrv[i];
  26         p->next = NULL;
  27     }
  28     p->next = cerrlist;
  29     cerrlist = stat;
  30 }
  31 
  32 void printcerrlist()
  33 {
  34     CERRLIST *p = NULL;
  35 
  36     if(cerrlist == NULL) {
  37         puts("error list is null.");
  38     } else {
  39         for(p = cerrlist; p != NULL; p = p->next) {
  40             printf("%d: %s\n", p->cerr->num, p->cerr->msg);
  41         }
  42     }
  43 }
  44 
  45 void setcerr(int num, const char *str)
  46 {
  47     /* 現在のエラー番号を設定  */
  48     cerr->num = num;
  49     /* 現在のエラーメッセージを設定 */
  50     cerr->msg = malloc_chk(CERRMSGSIZE + 1, "cerr.msg");
  51     if(0 < strlen(str) && strlen(str) <= CERRSTRSIZE) {
  52         sprintf(cerr->msg, "%s: %s", str, getcerrmsg(cerr->num));
  53     } else {
  54         strcpy(cerr->msg, getcerrmsg(cerr->num));
  55     }
  56 }
  57 
  58 char *getcerrmsg(int num)
  59 {
  60     CERRLIST *p = NULL;
  61     char *msg = "unknown error";
  62 
  63     for(p = cerrlist; p != NULL; p = p->next) {
  64         if(num == p->cerr->num) {
  65             msg = p->cerr->msg;
  66             break;
  67         }
  68     }
  69     return msg;
  70 }
  71 
  72 void freecerr()
  73 {
  74     CERRLIST *p = NULL;
  75     CERRLIST *q = NULL;
  76 
  77     /* 現在のエラーメッセージを解放 */
  78     FREE(cerr->msg);
  79     /* 現在のエラーを解放 */
  80     FREE(cerr);
  81     /* エラーリストを解放 */
  82     for(p = cerrlist; p != NULL; p = q) {
  83         q = p->next;
  84         FREE(p);
  85     }
  86 }

/* [<][>][^][v][top][bottom][index][help] */