YACASL2
Loading...
Searching...
No Matches
cerr.c
Go to the documentation of this file.
1#include "cerr.h"
2
4{
5 cerr = malloc_chk(sizeof(CERR), "cerr");
6 cerr->num = 0;
7}
8
9CERR *cerr = NULL;
10
12
13void 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
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
45void 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
58char *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
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}
void addcerrlist(int cerrc, CERR cerrv[])
エラーリストを作成・追加する
Definition cerr.c:13
void freecerr()
エラーリストと現在のエラーを解放する
Definition cerr.c:72
void printcerrlist()
エラーリストを表示する
Definition cerr.c:32
char * getcerrmsg(int num)
エラー番号に対応するエラーメッセージを返す
Definition cerr.c:58
void cerr_init()
エラーを初期化する
Definition cerr.c:3
void setcerr(int num, const char *str)
現在のエラーを設定する
Definition cerr.c:45
CERR * cerr
現在のエラー
Definition cerr.c:9
@ CERRSTRSIZE
Definition cerr.h:38
@ CERRMSGSIZE
Definition cerr.h:39
struct _CERR CERR
エラーを表すデータ型
CERRLIST * cerrlist
エラーリスト
Definition cerr.c:11
char * getcerrmsg(int num)
エラー番号に対応するエラーメッセージを返す
Definition cerr.c:58
struct _CERRLIST CERRLIST
エラーリストのデータ型
#define FREE(ptr)
メモリを解放するマクロ
Definition cmem.h:21
void * malloc_chk(size_t size, const char *tag)
mallocを実行し、0で初期化する
Definition cmem.c:3
char * msg
Definition cerr.h:16
int num
Definition cerr.h:15
CERR * cerr
Definition cerr.h:29
struct _CERRLIST * next
Definition cerr.h:28