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