f7971f68ca4c0e1de9bf8b3e204ed3538d96956f
[YACASL2.git] / src / cerr.c
1 #include "cerr.h"
2
3 /* エラーメッセージ */
4 int cerrno = 0;
5 char *cerrmsg;
6
7 /* エラー番号とエラーメッセージを設定する */
8 void setcerr(int num, const char *val)
9 {
10     cerrno = num;
11     cerrmsg = malloc(MSGSIZE + 1);
12     if(val != NULL) {
13         strcpy(cerrmsg, val);
14         strcat(cerrmsg, ": ");
15         strcat(cerrmsg, getcerrmsg(cerrno));
16     } else {
17         strcpy(cerrmsg, getcerrmsg(cerrno));
18     }
19 }
20
21 /* エラー番号からメッセージを返す */
22 char *getcerrmsg(int num)
23 {
24     assert(num > 0);
25     int i = 0;
26     CERRARRAY *ptr;
27     do {
28         if((ptr = &cerr[i++])->num == num) {
29             return ptr->msg;
30         }
31     } while(ptr->num > 0);
32     return "unkown error";
33 }
34
35 /* エラーを解放する */
36 void freecerr()
37 {
38     assert(cerrno > 0);
39     cerrno = 0;
40     if(strlen(cerrmsg) > 0) {
41         free(cerrmsg);
42     }
43 }