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