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