メモリ確保時のサイズを修正
[YACASL2.git] / src / cerr.c
index 121b680..c0146a2 100644 (file)
@@ -1,37 +1,68 @@
 #include "cerr.h"
 
-/* エラーメッセージ */
+/* エラー番号 */
 int cerrno = 0;
+
+/* エラーメッセージ */
 char *cerrmsg;
 
-/* エラー番号とエラーメッセージを設定する */
-void setcerr(int num, const char *val)
+/* エラーリスト */
+CERRLIST *cerr;
+
+/* エラーリストを作成する */
+bool addcerrlist(int newerrc, CERRARRAY newerrv[])
 {
-    assert(&cerr != NULL && num > 0);
+    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 *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));
     }
 }
 
-/* ã\82¨ã\83©ã\83¼ç\95ªå\8f·ã\81\8bã\82\89メッセージを返す */
+/* ã\83ªã\82¹ã\83\88ã\81\8bã\82\89ã\80\81ã\82¨ã\83©ã\83¼ç\95ªå\8f·ã\81«å¯¾å¿\9cã\81\99ã\82\8bメッセージを返す */
 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";
 }