X-Git-Url: http://j8takagi.net/cgi-bin/gitweb.cgi?p=YACASL2.git;a=blobdiff_plain;f=src%2Fword.c;h=93717580ec69e9ac21e03c7dfd1028965c5de8c6;hp=d2deaeec6bb8c34c5424f6bbc45517e6f4d2a579;hb=e6f41d41cff07e56b0cbc7d515c46327206c9f40;hpb=e07b6371cc8a59c6793a895e968ff2c6cf28181b diff --git a/src/word.c b/src/word.c index d2deaee..9371758 100644 --- a/src/word.c +++ b/src/word.c @@ -1,9 +1,22 @@ #include "word.h" +/* wordのエラー定義 */ +CERR cerr_word[] = { + { 114, "not integer" }, + { 115, "not hex" }, + { 116, "out of hex range" }, +}; + +bool addcerrlist_word() +{ + return addcerrlist(ARRAYSIZE(cerr_word), cerr_word); +} + /* 10進数の文字列をWORD値に変換 */ WORD n2word(const char *str) { assert(isdigit(*str) || *str == '-'); + char *check; int n; /* WORD値に変換 */ @@ -14,7 +27,7 @@ WORD n2word(const char *str) } /* nが-32768〜32767の範囲にないときは、その下位16ビットを格納 */ if(n < -32768 || n > 32767) { - n = n % 0x10000; + n = n & 0xFFFF; } return (WORD)n; } @@ -23,7 +36,8 @@ WORD n2word(const char *str) WORD h2word(const char *str) { assert(*str == '#'); - WORD w = 0x0; + + WORD word = 0x0; char *check; str++; if(*str == '-' || strlen(str) > 4) { @@ -31,21 +45,26 @@ WORD h2word(const char *str) return 0; } /* WORD値に変換 */ - w = (WORD)strtol(str, &check, 16); + word = (WORD)strtol(str, &check, 16); if(*check != '\0') { setcerr(115, str-1); /* not hex */ return 0x0; } - return w; + return word; } /* 10進数または16進数の文字列をWORD値に変換 */ -WORD a2word(const char *str) +WORD nh2word(const char *str) { - WORD word = 0x0; + addcerrlist_word(); + WORD word; + if(!isdigit(*str) && *str != '-' && *str != '#') { + setcerr(114, str); /* not integer */ + return 0x0; + } if(*str == '#') { word = h2word(str); - } else if(isdigit(*str) || *str == '-') { + } else { word = n2word(str); } return word; @@ -54,7 +73,7 @@ WORD a2word(const char *str) /* WORD値を10進数の文字列に変換 */ char *word2n(WORD word) { - char *p = malloc(6), *q = malloc(6); + char *p = malloc_chk(6, "word2n.p"), *q = malloc_chk(6, "word2n.q"); int i = 0, j; do{ *(p + i++) = word % 10 + '0'; @@ -70,8 +89,7 @@ char *word2n(WORD word) char *word2bit(const WORD word) { WORD mask = 0x8000; - char *bit, *p; - bit = malloc(16 + 1); + char *bit = malloc_chk(16 + 1, "word2bit.bit"), *p; p = bit; do { *p++ = (word & mask) ? '1' : '0'; @@ -79,3 +97,23 @@ char *word2bit(const WORD word) *p = '\0'; return bit; } + +/* WORD値を解析して表示 */ +void print_dumpword(WORD word, bool logicalmode) +{ + if(logicalmode == true) { + fprintf(stdout, "%6d", word); + } else { + fprintf(stdout, "%6d", (signed short)word); + } + fprintf(stdout, " = #%04X = %s", word, word2bit(word)); + /* 「文字の組」の符号表に記載された文字と、改行(CR)/タブを表示 */ + if(word >= 0x20 && word <= 0x7E) { + fprintf(stdout, " = \'%c\'", word); + } else if(word == 0xA) { + fprintf(stdout, " = \'\\n\'"); + } else if(word == '\t') { + fprintf(stdout, " = \'\\t\'"); + } + fprintf(stdout, "\n"); +}