X-Git-Url: http://j8takagi.net/cgi-bin/gitweb.cgi?p=YACASL2.git;a=blobdiff_plain;f=src%2Fword.c;h=e355a67d8cddbab448730bf010ae80d7d1f9d1df;hp=294c4d4d7b777feddae9ee9900bf872ecc845ce5;hb=d609e3d54f40e0c4bd497a5287288d2fe3d78212;hpb=93cee61e8fc0ddfb3f23469ad840f3e77067eb48 diff --git a/src/word.c b/src/word.c index 294c4d4..e355a67 100644 --- a/src/word.c +++ b/src/word.c @@ -1,9 +1,36 @@ +#include +#include +#include +#include +#include + #include "word.h" +#include "cerr.h" + +/** + * wordのエラー定義 + */ +static CERR cerr_word[] = { + { 114, "not integer" }, + { 115, "not hex" }, + { 116, "out of hex range" }, +}; + +/** + * wordのエラーをエラーリストに追加 + */ +void addcerrlist_word() +{ + addcerrlist(ARRAYSIZE(cerr_word), cerr_word); +} -/* 10進数の文字列をWORD値に変換 */ +/** + * 10進数の文字列をWORD値に変換 + */ WORD n2word(const char *str) { assert(isdigit(*str) || *str == '-'); + char *check; int n; /* WORD値に変換 */ @@ -12,18 +39,21 @@ WORD n2word(const char *str) setcerr(114, str); /* not integer */ return 0x0; } - /* nが-32768〜32767の範囲にないときは、その下位16ビットを格納 */ + /* nが-32768から32767の範囲にないときは、その下位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) { @@ -31,51 +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) { - 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(6), *q = malloc(6); + 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(16 + 1), *p; - p = bit; + char *bit, *p; + + bit = p = malloc_chk(MAXLEN + 1, "word2bit.bit"); do { *p++ = (word & mask) ? '1' : '0'; } while((mask >>= 1) > 0); @@ -83,7 +130,9 @@ char *word2bit(const WORD word) return bit; } -/* WORD値を解析して表示 */ +/** + * WORD値を解析して表示 + */ void print_dumpword(WORD word, bool logicalmode) { if(logicalmode == true) {