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