ソースの書き方を変更
[YACASL2.git] / src / cerr.c
index 36b33a4..24f620d 100644 (file)
@@ -25,7 +25,7 @@ CERR *cerr;
 CERRLIST *cerrlist;
 
 /**
- * ã\82¨ã\83©ã\83¼ã\83ªã\82¹ã\83\88ã\82\92ä½\9cæ\88\90ã\83»追加する
+ * ã\82¨ã\83©ã\83¼ã\83ªã\82¹ã\83\88ã\82\92ä½\9cæ\88\90ã\81¾ã\81\9fã\81¯追加する
  */
 bool addcerrlist(int newerrc, CERR newerrv[])
 {
@@ -42,15 +42,30 @@ bool addcerrlist(int newerrc, CERR newerrv[])
         p = q->next = malloc_chk(sizeof(CERRLIST), "cerrlist.next");
     }
     for(i = 0; i < newerrc; i++) {
-        p->cerr = &(newerrv[i]);
-        p->next = malloc_chk(sizeof(CERRLIST), "cerrlist.next");
+        p->cerr = &newerrv[i];
         q = p;
-        p = p->next;
+        p = p->next = malloc_chk(sizeof(CERRLIST), "cerrlist.next");
     }
     q->next = NULL;
     return true;
 }
 
+/**
+ * エラーリストを表示する
+ */
+void printcerrlist()
+{
+    CERRLIST *p;
+
+    if(cerrlist == NULL) {
+        puts("error list is null.");
+    } else {
+        for(p = cerrlist; p != NULL; p = p->next) {
+            printf("%d: %s\n", p->cerr->num, p->cerr->msg);
+        }
+    }
+}
+
 /**
  * 現在のエラーを設定する
  */
@@ -73,13 +88,15 @@ void setcerr(int num, const char *str)
 char *getcerrmsg(int num)
 {
     CERRLIST *p;
+    char *msg = "unknown error";
 
     for(p = cerrlist; p != NULL; p = p->next) {
         if(num == p->cerr->num) {
-            return p->cerr->msg;
+            msg = p->cerr->msg;
+            break;
         }
     }
-    return "unknown error";
+    return msg;
 }
 
 /**
@@ -89,14 +106,13 @@ void freecerr()
 {
     CERRLIST *p = cerrlist, *q;
 
-    /* エラーリストを解放 */
-    while(p != NULL) {
-        q = p->next;
-        free_chk(p, "freecerr.p");
-        p = q;
-    }
     /* 現在のエラーメッセージを解放 */
     free_chk(cerr->msg, "cerr.msg");
     /* 現在のエラーを解放 */
     free_chk(cerr, "cerr");
+    /* エラーリストを解放 */
+    for(p = cerrlist; p != NULL; p = q) {
+        q = p->next;
+        free_chk(p, "freecerr.p");
+    }
 }