X-Git-Url: http://j8takagi.net/cgi-bin/gitweb.cgi?p=YACASL2.git;a=blobdiff_plain;f=src%2Fcerr.c;fp=src%2Fcerr.c;h=662cc6680d5b974d6fbd9700b807cc406d04f9ca;hp=1aeb8c42c29ba3ab3b0813ebec91ae4f5f6c1f8f;hb=3ea92ba68cb320156d5bc7fe92f5e5fa0c150635;hpb=b591284405a5b29213db6250c830e1f63d11a4f0 diff --git a/src/cerr.c b/src/cerr.c index 1aeb8c4..662cc66 100644 --- a/src/cerr.c +++ b/src/cerr.c @@ -1,35 +1,66 @@ #include "cerr.h" -/* エラーメッセージ */ +/* エラー番号 */ int cerrno = 0; + +/* エラーメッセージ */ char *cerrmsg; +/* エラーリスト */ +CERRLIST *cerr; + +/* エラーリストを作成する */ +void addcerrlist(int newerrc, CERRARRAY newerrv[]) +{ + int i; + CERRLIST *p = cerr, *q; + + if(cerr != NULL) { + for(p = cerr; p != NULL; p = p->next) { + ; + } + if((p = malloc(sizeof(CERRLIST *))) == NULL) { + fprintf(stderr, "addcerrlist: cannot allocate memory\n"); + exit(-1); + } + } else if((p = cerr = malloc(sizeof(CERRLIST *))) == NULL) { + fprintf(stderr, "addcerrlist: cannot allocate memory\n"); + exit(-1); + } + for(i = 0; i < newerrc; i++) { + p->err = &(newerrv[i]); + if((p->next = malloc(sizeof(CERRLIST *))) == NULL) { + fprintf(stderr, "addcerrlist: cannot allocate memory\n"); + exit(-1); + } + q = p; + p = p->next; + } + q->next = NULL; +} + /* エラー番号とエラーメッセージを設定する */ void setcerr(int num, const char *str) { - assert(cerr != NULL && num > 0); - cerrno = num; cerrmsg = malloc(CERRMSGSIZE + 1); - if(str != NULL && strlen(str) < 10) { + if(str != NULL && strlen(str) <= CERRSTRSIZE) { sprintf(cerrmsg, "%s: %s", str, getcerrmsg(cerrno)); } else { strcpy(cerrmsg, getcerrmsg(cerrno)); } } -/* エラー番号からメッセージを返す */ +/* リストから、エラー番号に対応するメッセージを返す */ char *getcerrmsg(int num) { - assert(cerr != NULL && num > 0); - int i = 0; - CERRARRAY *ptr; + CERRLIST *p; - do { - if((ptr = &cerr[i++])->num == num) { - return ptr->msg; + for(p = cerr; p != NULL; p = p->next) { + if(num == p->err->num) { + return p->err->msg; } - } while(ptr->num > 0); + } return "unkown error"; }