ソースコードの推敲
[YACASL2.git] / src / word.c
index 9371758..e355a67 100644 (file)
@@ -1,18 +1,32 @@
+#include <stdio.h>
+#include <string.h>
+#include <assert.h>
+#include <stdlib.h>
+#include <ctype.h>
+
 #include "word.h"
+#include "cerr.h"
 
-/* wordのエラー定義 */
-CERR cerr_word[] = {
+/**
+ * wordのエラー定義
+ */
+static CERR cerr_word[] = {
     { 114, "not integer" },
     { 115, "not hex" },
     { 116, "out of hex range" },
 };
 
-bool addcerrlist_word()
+/**
+ * wordのエラーをエラーリストに追加
+ */
+void addcerrlist_word()
 {
-    return addcerrlist(ARRAYSIZE(cerr_word), cerr_word);
+    addcerrlist(ARRAYSIZE(cerr_word), cerr_word);
 }
 
-/* 10進数の文字列をWORD値に変換 */
+/**
+ * 10進数の文字列をWORD値に変換
+ */
 WORD n2word(const char *str)
 {
     assert(isdigit(*str) || *str == '-');
@@ -25,19 +39,21 @@ WORD n2word(const char *str)
         setcerr(114, str);    /* not integer */
         return 0x0;
     }
-    /* nã\81\8c-32768ã\80\9c32767の範囲にないときは、その下位16ビットを格納 */
+    /* nã\81\8c-32768ã\81\8bã\82\8932767の範囲にないときは、その下位16ビットを格納 */
     if(n < -32768 || n > 32767) {
         n = n & 0xFFFF;
     }
     return (WORD)n;
 }
 
-/* 16進数の文字列をWORD値に変換 */
+/**
+ * 16進数の文字列をWORD値に変換
+ */
 WORD h2word(const char *str)
 {
     assert(*str == '#');
 
-    WORD word = 0x0;
+    WORD w = 0x0;
     char *check;
     str++;
     if(*str == '-' || strlen(str) > 4) {
@@ -45,52 +61,68 @@ WORD h2word(const char *str)
         return 0;
     }
     /* WORD値に変換 */
-    word = (WORD)strtol(str, &check, 16);
+    w = (WORD)strtol(str, &check, 16);
     if(*check != '\0') {
         setcerr(115, str-1);    /* not hex */
         return 0x0;
     }
-    return word;
+    return w;
 }
 
-/* 10進数または16進数の文字列をWORD値に変換 */
+/**
+ * 10進数または16進数の文字列をWORD値に変換
+ */
 WORD nh2word(const char *str)
 {
-    addcerrlist_word();
-    WORD word;
+    WORD w;
+
+    assert(sizeof(WORD) * 8 == 16); /* WORD型のサイズが16ビットであることを確認 */
     if(!isdigit(*str) && *str != '-' && *str != '#') {
         setcerr(114, str);    /* not integer */
         return 0x0;
     }
     if(*str == '#') {
-        word = h2word(str);
+        w = h2word(str);
     } else {
-        word = n2word(str);
+        w = n2word(str);
     }
-    return word;
+    return w;
 }
 
-/* WORD値を10進数の文字列に変換 */
+/**
+ * WORD値を10進数の文字列に変換
+ */
 char *word2n(WORD word)
 {
-    char *p = malloc_chk(6, "word2n.p"), *q = malloc_chk(6, "word2n.q");
+    enum {
+        MAXLEN = 5,        /* WORD値を10進数で表したときの最大桁数 */
+    };
+    char *p = malloc_chk(MAXLEN + 1, "word2n.p"), *digit = malloc_chk(MAXLEN + 1, "word2n.digit");
     int i = 0, j;
+
     do{
         *(p + i++) = word % 10 + '0';
     } while((word /= 10) > 0);
     for(j = 0; j < i; j++) {
-        *(q + j) = *(p + (i - 1) - j);
+        *(digit + j) = *(p + (i - 1) - j);
     }
-    *(q + j + 1) = '\0';
-    return q;
+    *(digit + j + 1) = '\0';
+    FREE(p);
+    return digit;
 }
 
-/* WORD値を2進数の文字列に変換 */
+/**
+ * WORD値を2進数の文字列に変換
+ */
 char *word2bit(const WORD word)
 {
+    enum {
+        MAXLEN = 16,        /* WORD値を2進数で表したときの最大桁数 */
+    };
     WORD mask = 0x8000;
-    char *bit = malloc_chk(16 + 1, "word2bit.bit"), *p;
-    p = bit;
+    char *bit, *p;
+
+    bit = p = malloc_chk(MAXLEN + 1, "word2bit.bit");
     do {
         *p++ = (word & mask) ? '1' : '0';
     } while((mask >>= 1) > 0);
@@ -98,7 +130,9 @@ char *word2bit(const WORD word)
     return bit;
 }
 
-/* WORD値を解析して表示 */
+/**
+ * WORD値を解析して表示
+ */
 void print_dumpword(WORD word, bool logicalmode)
 {
     if(logicalmode == true) {