be3e58bf313738a80789669352ee956515c66c51
[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     assert(cerr != NULL && num > 0);
11
12     cerrno = num;
13     cerrmsg = malloc(MSGSIZE + 1);
14     if(val != NULL) {
15         strcpy(cerrmsg, val);
16         strcat(cerrmsg, ": ");
17         strcat(cerrmsg, getcerrmsg(cerrno));
18     } else {
19         strcpy(cerrmsg, getcerrmsg(cerrno));
20     }
21 }
22
23 /* エラー番号からメッセージを返す */
24 char *getcerrmsg(int num)
25 {
26     assert(cerr != NULL && num > 0);
27     int i = 0;
28     CERRARRAY *ptr;
29
30     do {
31         if((ptr = &cerr[i++])->num == num) {
32             return ptr->msg;
33         }
34     } while(ptr->num > 0);
35     return "unkown error";
36 }
37
38 /* エラーを解放する */
39 void freecerr()
40 {
41     assert(cerrno > 0);
42     cerrno = 0;
43     if(strlen(cerrmsg) > 0) {
44         free(cerrmsg);
45     }
46 }