ファイルからのロードについての記述を、load.cとload.hに
[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 = NULL;
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 = 0;
52     char *check = NULL;
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 /* word.hで定義された関数群 */
77 void addcerrlist_word()
78 {
79     addcerrlist(ARRAYSIZE(cerr_word), cerr_word);
80 }
81
82 WORD nh2word(const char *str)
83 {
84     WORD w;
85
86     assert(sizeof(WORD) * 8 == 16); /* WORD型のサイズが16ビットであることを確認 */
87     if(str == NULL) {
88         return 0x0;
89     }
90     if(!isdigit(*str) && *str != '-' && *str != '#') {
91         setcerr(114, str);    /* not integer */
92         return 0x0;
93     }
94     if(*str == '#') {
95         w = h2word(str);
96     } else {
97         w = n2word(str);
98     }
99     return w;
100 }
101
102 char *word2n(WORD word)
103 {
104     enum {
105         MAXLEN = 5,        /* WORD値を10進数で表したときの最大桁数 */
106     };
107     char *n = malloc_chk(MAXLEN + 1, "word2n.n"), tmp;
108     int i = 0, j;
109
110     do{
111         n[i++] = word % 10 + '0';
112     } while((word /= 10) > 0);
113     for(j = 0; j < i; j++) {
114         tmp = n[j];
115         n[j] = n[(i-1)-j];
116         n[(i-1)-j] = tmp;
117     }
118     n[j] = '\0';
119     return n;
120 }
121
122 char *word2bit(const WORD word)
123 {
124     enum {
125         MAXLEN = 16,        /* WORD値を2進数で表したときの最大桁数 */
126     };
127     WORD mask = 0x8000;
128     char *bit = NULL;
129     int i = 0;
130
131     bit = malloc_chk(MAXLEN + 1, "word2bit.bit");
132     do {
133         bit[i++] = (word & mask) ? '1' : '0';
134     } while((mask >>= 1) > 0);
135     bit[i] = '\0';
136     return bit;
137 }
138
139 void print_dumpword(WORD word, bool logicalmode)
140 {
141     char *bit = NULL;
142
143     if(logicalmode == true) {
144         fprintf(stdout, "%6d", word);
145     } else {
146         fprintf(stdout, "%6d", (signed short)word);
147     }
148     fprintf(stdout, " = #%04X = %s", word, (bit = word2bit(word)));
149     /* 「文字の組」の符号表に記載された文字と、改行(CR)/タブを表示 */
150     if(word >= 0x20 && word <= 0x7E) {
151         fprintf(stdout, " = \'%c\'", word);
152     } else if(word == 0xA) {
153         fprintf(stdout, " = \'\\n\'");
154     } else if(word == '\t') {
155         fprintf(stdout, " = \'\\t\'");
156     }
157     FREE(bit);
158 }