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
  7. warn_ignore_arg

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

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