bc511807d68a3535ca6c602b331cfed219571f2c
[YACASL2.git] / src / word.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <assert.h>
4 #include <stdlib.h>
5 #include <ctype.h>
6
7 #include "word.h"
8 #include "cerr.h"
9
10 /**
11  * wordのエラー定義
12  */
13 static CERR cerr_word[] = {
14     { 114, "not integer" },
15     { 115, "not hex" },
16     { 116, "out of hex range" },
17 };
18
19 /**
20  * 10進数の文字列をWORD値に変換
21  */
22 WORD n2word(const char *str)
23 {
24     assert(isdigit(*str) || *str == '-');
25
26     char *check;
27     int n;
28     /* WORD値に変換 */
29     n = strtol(str, &check, 10);
30     if(*check != '\0') {
31         setcerr(114, str);    /* not integer */
32         return 0x0;
33     }
34     /* nが-32768から32767の範囲にないときは、その下位16ビットを格納 */
35     if(n < -32768 || n > 32767) {
36         n = n & 0xFFFF;
37     }
38     return (WORD)n;
39 }
40
41 /**
42  * 16進数の文字列をWORD値に変換
43  */
44 WORD h2word(const char *str)
45 {
46     assert(*str == '#');
47
48     WORD word = 0x0;
49     char *check;
50     str++;
51     if(*str == '-' || strlen(str) > 4) {
52         setcerr(116, str-1);    /* out of hex range */
53         return 0;
54     }
55     /* WORD値に変換 */
56     word = (WORD)strtol(str, &check, 16);
57     if(*check != '\0') {
58         setcerr(115, str-1);    /* not hex */
59         return 0x0;
60     }
61     return word;
62 }
63
64 /**
65  * 10進数または16進数の文字列をWORD値に変換
66  */
67 WORD nh2word(const char *str)
68 {
69     assert(sizeof(WORD)*8 == 16); /* WORD型のサイズが16ビットであることを確認 */
70     addcerrlist(ARRAYSIZE(cerr_word), cerr_word); /* エラーの設定 */
71
72     WORD word;
73
74     if(!isdigit(*str) && *str != '-' && *str != '#') {
75         setcerr(114, str);    /* not integer */
76         return 0x0;
77     }
78     if(*str == '#') {
79         word = h2word(str);
80     } else {
81         word = n2word(str);
82     }
83     return word;
84 }
85
86 /**
87  * WORD値を10進数の文字列に変換
88  */
89 char *word2n(WORD word)
90 {
91     enum {
92         MAXLEN = 6,        /* WORD値を10進数で表したときの最大桁数 */
93     };
94     char *p = malloc_chk(MAXLEN, "word2n.p"), *digit = malloc_chk(MAXLEN, "word2n.digit");
95     int i = 0, j;
96
97     do{
98         *(p + i++) = word % 10 + '0';
99     } while((word /= 10) > 0);
100     for(j = 0; j < i; j++) {
101         *(digit + j) = *(p + (i - 1) - j);
102     }
103     *(digit + j + 1) = '\0';
104     free_chk(p, "word2n.p");
105     return digit;
106 }
107
108 /**
109  * WORD値を2進数の文字列に変換
110  */
111 char *word2bit(const WORD word)
112 {
113     enum {
114         MAXLEN = 16,        /* WORD値を2進数で表したときの最大桁数 */
115     };
116     WORD mask = 0x8000;
117     char *bit, *p;
118
119     bit = p = malloc_chk(MAXLEN + 1, "word2bit.bit");
120     do {
121         *p++ = (word & mask) ? '1' : '0';
122     } while((mask >>= 1) > 0);
123     *p = '\0';
124     return bit;
125 }
126
127 /**
128  * WORD値を解析して表示
129  */
130 void print_dumpword(WORD word, bool logicalmode)
131 {
132     if(logicalmode == true) {
133         fprintf(stdout, "%6d", word);
134     } else {
135         fprintf(stdout, "%6d", (signed short)word);
136     }
137     fprintf(stdout, " = #%04X = %s", word, word2bit(word));
138     /* 「文字の組」の符号表に記載された文字と、改行(CR)/タブを表示 */
139     if(word >= 0x20 && word <= 0x7E) {
140         fprintf(stdout, " = \'%c\'", word);
141     } else if(word == 0xA) {
142         fprintf(stdout, " = \'\\n\'");
143     } else if(word == '\t') {
144         fprintf(stdout, " = \'\\t\'");
145     }
146     fprintf(stdout, "\n");
147 }