annotate scn2k/scn2k_cmd.cc @ 8:55b577e5f5b5

Some memory leaks fixed
author thib
date Tue, 05 Aug 2008 12:56:51 +0000
parents 223b71206888
children a05bf0823154
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
223b71206888 Initial import
thib
parents:
diff changeset
1 /*
223b71206888 Initial import
thib
parents:
diff changeset
2 * Copyright (c) 2004-2006 Kazunori "jagarl" Ueno
223b71206888 Initial import
thib
parents:
diff changeset
3 * All rights reserved.
223b71206888 Initial import
thib
parents:
diff changeset
4 *
223b71206888 Initial import
thib
parents:
diff changeset
5 * Redistribution and use in source and binary forms, with or without
223b71206888 Initial import
thib
parents:
diff changeset
6 * modification, are permitted provided that the following conditions
223b71206888 Initial import
thib
parents:
diff changeset
7 * are met:
223b71206888 Initial import
thib
parents:
diff changeset
8 * 1. Redistributions of source code must retain the above copyright
223b71206888 Initial import
thib
parents:
diff changeset
9 * notice, this list of conditions and the following disclaimer.
223b71206888 Initial import
thib
parents:
diff changeset
10 * 2. Redistributions in binary form must reproduce the above copyright
223b71206888 Initial import
thib
parents:
diff changeset
11 * notice, this list of conditions and the following disclaimer in the
223b71206888 Initial import
thib
parents:
diff changeset
12 * documentation and/or other materials provided with the distribution.
223b71206888 Initial import
thib
parents:
diff changeset
13 * 3. The name of the author may not be used to endorse or promote products
223b71206888 Initial import
thib
parents:
diff changeset
14 * derived from this software without specific prior written permission.
223b71206888 Initial import
thib
parents:
diff changeset
15 *
223b71206888 Initial import
thib
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
223b71206888 Initial import
thib
parents:
diff changeset
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
223b71206888 Initial import
thib
parents:
diff changeset
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
223b71206888 Initial import
thib
parents:
diff changeset
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
223b71206888 Initial import
thib
parents:
diff changeset
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
223b71206888 Initial import
thib
parents:
diff changeset
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
223b71206888 Initial import
thib
parents:
diff changeset
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
223b71206888 Initial import
thib
parents:
diff changeset
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
223b71206888 Initial import
thib
parents:
diff changeset
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
223b71206888 Initial import
thib
parents:
diff changeset
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
223b71206888 Initial import
thib
parents:
diff changeset
26 */
223b71206888 Initial import
thib
parents:
diff changeset
27
223b71206888 Initial import
thib
parents:
diff changeset
28
223b71206888 Initial import
thib
parents:
diff changeset
29 #include"scn2k.h"
223b71206888 Initial import
thib
parents:
diff changeset
30
223b71206888 Initial import
thib
parents:
diff changeset
31 #include<stdlib.h>
223b71206888 Initial import
thib
parents:
diff changeset
32 #include<stdarg.h>
223b71206888 Initial import
thib
parents:
diff changeset
33 #include<stdio.h>
223b71206888 Initial import
thib
parents:
diff changeset
34 #include<string.h>
223b71206888 Initial import
thib
parents:
diff changeset
35 #include<string>
223b71206888 Initial import
thib
parents:
diff changeset
36 #include"system/file.h"
223b71206888 Initial import
thib
parents:
diff changeset
37
223b71206888 Initial import
thib
parents:
diff changeset
38 using namespace std;
223b71206888 Initial import
thib
parents:
diff changeset
39
223b71206888 Initial import
thib
parents:
diff changeset
40
223b71206888 Initial import
thib
parents:
diff changeset
41 // #define SCN_DUMP
223b71206888 Initial import
thib
parents:
diff changeset
42 /* 注意点: @@@ で表記 */
223b71206888 Initial import
thib
parents:
diff changeset
43
223b71206888 Initial import
thib
parents:
diff changeset
44
223b71206888 Initial import
thib
parents:
diff changeset
45
223b71206888 Initial import
thib
parents:
diff changeset
46 //bool debug_flag = true;
223b71206888 Initial import
thib
parents:
diff changeset
47 bool debug_flag = false;
223b71206888 Initial import
thib
parents:
diff changeset
48 void dprintf(const char* fmt, ...) {
223b71206888 Initial import
thib
parents:
diff changeset
49 if (debug_flag) {
223b71206888 Initial import
thib
parents:
diff changeset
50 va_list ap; va_start(ap, fmt);
223b71206888 Initial import
thib
parents:
diff changeset
51 vprintf(fmt, ap);
223b71206888 Initial import
thib
parents:
diff changeset
52 va_end(ap);
223b71206888 Initial import
thib
parents:
diff changeset
53 }
223b71206888 Initial import
thib
parents:
diff changeset
54 }
223b71206888 Initial import
thib
parents:
diff changeset
55
223b71206888 Initial import
thib
parents:
diff changeset
56
223b71206888 Initial import
thib
parents:
diff changeset
57 void eprintf(const char* fmt, ...) {
223b71206888 Initial import
thib
parents:
diff changeset
58 va_list ap; va_start(ap, fmt);
223b71206888 Initial import
thib
parents:
diff changeset
59 // vprintf(fmt, ap);
223b71206888 Initial import
thib
parents:
diff changeset
60 va_end(ap);
223b71206888 Initial import
thib
parents:
diff changeset
61 }
223b71206888 Initial import
thib
parents:
diff changeset
62
223b71206888 Initial import
thib
parents:
diff changeset
63 /**************************************************************
223b71206888 Initial import
thib
parents:
diff changeset
64 ** Flag
223b71206888 Initial import
thib
parents:
diff changeset
65 */
223b71206888 Initial import
thib
parents:
diff changeset
66
223b71206888 Initial import
thib
parents:
diff changeset
67 Flags::Flags(void) {
223b71206888 Initial import
thib
parents:
diff changeset
68 int i,j;
223b71206888 Initial import
thib
parents:
diff changeset
69 for (i=0; i<=TYPE_VARMAX; i++) {
223b71206888 Initial import
thib
parents:
diff changeset
70 for (j=0; j<2000; j++) {
223b71206888 Initial import
thib
parents:
diff changeset
71 var[i][j] = 0;
223b71206888 Initial import
thib
parents:
diff changeset
72 }
223b71206888 Initial import
thib
parents:
diff changeset
73 }
223b71206888 Initial import
thib
parents:
diff changeset
74 sys = 0;
223b71206888 Initial import
thib
parents:
diff changeset
75 }
223b71206888 Initial import
thib
parents:
diff changeset
76
223b71206888 Initial import
thib
parents:
diff changeset
77 bool Flags::IsInt(int type) const {
223b71206888 Initial import
thib
parents:
diff changeset
78 int v = type % 26;
223b71206888 Initial import
thib
parents:
diff changeset
79 return v >= 0 && v < 7 || v == 25;
223b71206888 Initial import
thib
parents:
diff changeset
80 }
223b71206888 Initial import
thib
parents:
diff changeset
81
223b71206888 Initial import
thib
parents:
diff changeset
82 int Flags::MaxIndex(int type) const {
223b71206888 Initial import
thib
parents:
diff changeset
83 switch (type / 26) {
223b71206888 Initial import
thib
parents:
diff changeset
84 case 1:
223b71206888 Initial import
thib
parents:
diff changeset
85 return 63999;
223b71206888 Initial import
thib
parents:
diff changeset
86 case 2:
223b71206888 Initial import
thib
parents:
diff changeset
87 return 31999;
223b71206888 Initial import
thib
parents:
diff changeset
88 case 3:
223b71206888 Initial import
thib
parents:
diff changeset
89 return 15999;
223b71206888 Initial import
thib
parents:
diff changeset
90 case 4:
223b71206888 Initial import
thib
parents:
diff changeset
91 return 7999;
223b71206888 Initial import
thib
parents:
diff changeset
92 default:
223b71206888 Initial import
thib
parents:
diff changeset
93 return 1999;
223b71206888 Initial import
thib
parents:
diff changeset
94 }
223b71206888 Initial import
thib
parents:
diff changeset
95 }
223b71206888 Initial import
thib
parents:
diff changeset
96
223b71206888 Initial import
thib
parents:
diff changeset
97 int Flags::operator()() const {
223b71206888 Initial import
thib
parents:
diff changeset
98 return sys; // rand() % 10000;
223b71206888 Initial import
thib
parents:
diff changeset
99 }
223b71206888 Initial import
thib
parents:
diff changeset
100
223b71206888 Initial import
thib
parents:
diff changeset
101 int Flags::operator() (VarInfo info) const {
223b71206888 Initial import
thib
parents:
diff changeset
102 return Get(info.type, info.number);
223b71206888 Initial import
thib
parents:
diff changeset
103 }
223b71206888 Initial import
thib
parents:
diff changeset
104
223b71206888 Initial import
thib
parents:
diff changeset
105 int Flags::Get(int type, int number) const {
223b71206888 Initial import
thib
parents:
diff changeset
106 int index = type % 26;
223b71206888 Initial import
thib
parents:
diff changeset
107 type /= 26;
223b71206888 Initial import
thib
parents:
diff changeset
108 if (index == 25) {
223b71206888 Initial import
thib
parents:
diff changeset
109 if (var[7][number] != 0) return var[7][number];
223b71206888 Initial import
thib
parents:
diff changeset
110 if (cgm_data.find(number) == cgm_data.end()) return 0;
223b71206888 Initial import
thib
parents:
diff changeset
111 else return 1;
223b71206888 Initial import
thib
parents:
diff changeset
112 }
223b71206888 Initial import
thib
parents:
diff changeset
113 if (index == 10) index = 8;
223b71206888 Initial import
thib
parents:
diff changeset
114 if (index == 11) index = 9;
223b71206888 Initial import
thib
parents:
diff changeset
115 if (index > TYPE_VARMAX || uint(type) > 4) return 0;
223b71206888 Initial import
thib
parents:
diff changeset
116 if (type == 0) {
223b71206888 Initial import
thib
parents:
diff changeset
117 // A[]..G[], Z[] を直に読む
223b71206888 Initial import
thib
parents:
diff changeset
118 if (uint(number) >= 2000) return 0;
223b71206888 Initial import
thib
parents:
diff changeset
119 return var[index][number];
223b71206888 Initial import
thib
parents:
diff changeset
120 } else {
223b71206888 Initial import
thib
parents:
diff changeset
121 // Ab[]..G4b[], Z8b[] などを読む
223b71206888 Initial import
thib
parents:
diff changeset
122 int factor = 1 << (type - 1);
223b71206888 Initial import
thib
parents:
diff changeset
123 int eltsize = 32 / factor;
223b71206888 Initial import
thib
parents:
diff changeset
124 if (uint(number) >= (64000 / factor)) return 0;
223b71206888 Initial import
thib
parents:
diff changeset
125 return (var[index][number / eltsize] >> ((number % eltsize) * factor)) & ((1 << factor) - 1);
223b71206888 Initial import
thib
parents:
diff changeset
126 }
223b71206888 Initial import
thib
parents:
diff changeset
127 }
223b71206888 Initial import
thib
parents:
diff changeset
128
223b71206888 Initial import
thib
parents:
diff changeset
129 void Flags::Set(VarInfo info, int value) {
223b71206888 Initial import
thib
parents:
diff changeset
130 int type = info.type / 26;
223b71206888 Initial import
thib
parents:
diff changeset
131 int index = info.type % 26;
223b71206888 Initial import
thib
parents:
diff changeset
132 if (index == 25) {
223b71206888 Initial import
thib
parents:
diff changeset
133 if (uint(info.number) >= 2000) return;
223b71206888 Initial import
thib
parents:
diff changeset
134 if (value == 0)
223b71206888 Initial import
thib
parents:
diff changeset
135 cgm_data.erase(info.number);
223b71206888 Initial import
thib
parents:
diff changeset
136 else
223b71206888 Initial import
thib
parents:
diff changeset
137 cgm_data.insert(info.number);
223b71206888 Initial import
thib
parents:
diff changeset
138 index = 7;
223b71206888 Initial import
thib
parents:
diff changeset
139 }
223b71206888 Initial import
thib
parents:
diff changeset
140 if (index == 10) index = 8;
223b71206888 Initial import
thib
parents:
diff changeset
141 if (index == 11) index = 9;
223b71206888 Initial import
thib
parents:
diff changeset
142 if (index < 0 || index > TYPE_VARMAX) {
223b71206888 Initial import
thib
parents:
diff changeset
143 fprintf(stderr,"Error: invalid access to Var<%d>[%d]\n",info.type,info.number);
223b71206888 Initial import
thib
parents:
diff changeset
144 }
223b71206888 Initial import
thib
parents:
diff changeset
145 if (type == 0) {
223b71206888 Initial import
thib
parents:
diff changeset
146 // A[]..G[], Z[] を直に書く
223b71206888 Initial import
thib
parents:
diff changeset
147 if (uint(info.number) >= 2000) return;
223b71206888 Initial import
thib
parents:
diff changeset
148 var[index][info.number] = value;
223b71206888 Initial import
thib
parents:
diff changeset
149 } else {
223b71206888 Initial import
thib
parents:
diff changeset
150 // Ab[]..G4b[], Z8b[] などを書く
223b71206888 Initial import
thib
parents:
diff changeset
151 int factor = 1 << (type - 1);
223b71206888 Initial import
thib
parents:
diff changeset
152 int eltsize = 32 / factor;
223b71206888 Initial import
thib
parents:
diff changeset
153 int eltmask = (1 << factor) - 1;
223b71206888 Initial import
thib
parents:
diff changeset
154 int shift = (info.number % eltsize) * factor;
223b71206888 Initial import
thib
parents:
diff changeset
155 if (uint(info.number) >= (64000 / factor)) return;
223b71206888 Initial import
thib
parents:
diff changeset
156 var[index][info.number / eltsize] =
223b71206888 Initial import
thib
parents:
diff changeset
157 (var[index][info.number / eltsize] & ~(eltmask << shift))
223b71206888 Initial import
thib
parents:
diff changeset
158 | (value & eltmask) << shift;
223b71206888 Initial import
thib
parents:
diff changeset
159 }
223b71206888 Initial import
thib
parents:
diff changeset
160 }
223b71206888 Initial import
thib
parents:
diff changeset
161
223b71206888 Initial import
thib
parents:
diff changeset
162 void Flags::SetSys(int value) {
223b71206888 Initial import
thib
parents:
diff changeset
163 sys = value;
223b71206888 Initial import
thib
parents:
diff changeset
164 }
223b71206888 Initial import
thib
parents:
diff changeset
165 void Flags::SetStr(VarInfo info, string val) {
223b71206888 Initial import
thib
parents:
diff changeset
166 switch(info.type) {
223b71206888 Initial import
thib
parents:
diff changeset
167 case TYPE_VARLOCSTR:
223b71206888 Initial import
thib
parents:
diff changeset
168 if (info.number >= 3) return;
223b71206888 Initial import
thib
parents:
diff changeset
169 loc_str[info.number] = val;
223b71206888 Initial import
thib
parents:
diff changeset
170 break;
223b71206888 Initial import
thib
parents:
diff changeset
171 case TYPE_VARSYSSTR:
223b71206888 Initial import
thib
parents:
diff changeset
172 if (info.number >= 2000) return;
223b71206888 Initial import
thib
parents:
diff changeset
173 sys_str[info.number] = val;
223b71206888 Initial import
thib
parents:
diff changeset
174 break;
223b71206888 Initial import
thib
parents:
diff changeset
175 case TYPE_VARSTR:
223b71206888 Initial import
thib
parents:
diff changeset
176 if (info.number >= 2000) return;
223b71206888 Initial import
thib
parents:
diff changeset
177 str[info.number] = val;
223b71206888 Initial import
thib
parents:
diff changeset
178 break;
223b71206888 Initial import
thib
parents:
diff changeset
179 }
223b71206888 Initial import
thib
parents:
diff changeset
180 return;
223b71206888 Initial import
thib
parents:
diff changeset
181 }
223b71206888 Initial import
thib
parents:
diff changeset
182 void Flags::Str(int type, unsigned int number, char* buf, int sz) const {
223b71206888 Initial import
thib
parents:
diff changeset
183 if (sz <= 0) return;
223b71206888 Initial import
thib
parents:
diff changeset
184 buf[0] = 0;
223b71206888 Initial import
thib
parents:
diff changeset
185 const string* sptr;
223b71206888 Initial import
thib
parents:
diff changeset
186 switch(type) {
223b71206888 Initial import
thib
parents:
diff changeset
187 case TYPE_VARLOCSTR:
223b71206888 Initial import
thib
parents:
diff changeset
188 if (number >= 3) return;
223b71206888 Initial import
thib
parents:
diff changeset
189 sptr = &loc_str[number];
223b71206888 Initial import
thib
parents:
diff changeset
190 break;
223b71206888 Initial import
thib
parents:
diff changeset
191 case TYPE_VARSYSSTR:
223b71206888 Initial import
thib
parents:
diff changeset
192 if (number >= 2000) return;
223b71206888 Initial import
thib
parents:
diff changeset
193 sptr = &sys_str[number];
223b71206888 Initial import
thib
parents:
diff changeset
194 break;
223b71206888 Initial import
thib
parents:
diff changeset
195 case TYPE_VARSTR:
223b71206888 Initial import
thib
parents:
diff changeset
196 if (number >= 2000) return;
223b71206888 Initial import
thib
parents:
diff changeset
197 sptr = &str[number];
223b71206888 Initial import
thib
parents:
diff changeset
198 break;
223b71206888 Initial import
thib
parents:
diff changeset
199 }
223b71206888 Initial import
thib
parents:
diff changeset
200
223b71206888 Initial import
thib
parents:
diff changeset
201 int len = sptr->length();
223b71206888 Initial import
thib
parents:
diff changeset
202 if (sz-1 > len) sz = len;
223b71206888 Initial import
thib
parents:
diff changeset
203 sptr->copy(buf, sz, 0);
223b71206888 Initial import
thib
parents:
diff changeset
204 buf[sz] = 0;
223b71206888 Initial import
thib
parents:
diff changeset
205 return;
223b71206888 Initial import
thib
parents:
diff changeset
206 }
223b71206888 Initial import
thib
parents:
diff changeset
207 string Flags::Str(int type, unsigned int number) const {
223b71206888 Initial import
thib
parents:
diff changeset
208 switch(type) {
223b71206888 Initial import
thib
parents:
diff changeset
209 case TYPE_VARLOCSTR:
223b71206888 Initial import
thib
parents:
diff changeset
210 if (number >= 3) return "";
223b71206888 Initial import
thib
parents:
diff changeset
211 return loc_str[number];
223b71206888 Initial import
thib
parents:
diff changeset
212 case TYPE_VARSYSSTR:
223b71206888 Initial import
thib
parents:
diff changeset
213 if (number >= 2000) return "";
223b71206888 Initial import
thib
parents:
diff changeset
214 return sys_str[number];
223b71206888 Initial import
thib
parents:
diff changeset
215 case TYPE_VARSTR:
223b71206888 Initial import
thib
parents:
diff changeset
216 if (number >= 2000) return "";
223b71206888 Initial import
thib
parents:
diff changeset
217 return str[number];
223b71206888 Initial import
thib
parents:
diff changeset
218 }
223b71206888 Initial import
thib
parents:
diff changeset
219 return "";
223b71206888 Initial import
thib
parents:
diff changeset
220 }
223b71206888 Initial import
thib
parents:
diff changeset
221
223b71206888 Initial import
thib
parents:
diff changeset
222 void Flags::Save(string& save) {
223b71206888 Initial import
thib
parents:
diff changeset
223 char buf[1024];
223b71206888 Initial import
thib
parents:
diff changeset
224 save = "\n[Flags]\n";
223b71206888 Initial import
thib
parents:
diff changeset
225 int i,j;
223b71206888 Initial import
thib
parents:
diff changeset
226 for (i=0; i<=TYPE_NONSYSVARMAX; i++) {
223b71206888 Initial import
thib
parents:
diff changeset
227 for (j=0; j<2000; j++) {
223b71206888 Initial import
thib
parents:
diff changeset
228 if (var[i][j] != 0) {
223b71206888 Initial import
thib
parents:
diff changeset
229 sprintf(buf, "V<%d>[%04d]=%d\n",i,j,var[i][j]);
223b71206888 Initial import
thib
parents:
diff changeset
230 save += buf;
223b71206888 Initial import
thib
parents:
diff changeset
231 }
223b71206888 Initial import
thib
parents:
diff changeset
232 }
223b71206888 Initial import
thib
parents:
diff changeset
233 }
223b71206888 Initial import
thib
parents:
diff changeset
234 for (j=0; j<2000; j++) {
223b71206888 Initial import
thib
parents:
diff changeset
235 if (str[j].length() != 0) {
223b71206888 Initial import
thib
parents:
diff changeset
236 sprintf(buf, "V<C>[%04d]=%s\n", j, str[j].c_str());
223b71206888 Initial import
thib
parents:
diff changeset
237 save += buf;
223b71206888 Initial import
thib
parents:
diff changeset
238 }
223b71206888 Initial import
thib
parents:
diff changeset
239 }
223b71206888 Initial import
thib
parents:
diff changeset
240 }
223b71206888 Initial import
thib
parents:
diff changeset
241 void Flags::Load(const char* save) {
223b71206888 Initial import
thib
parents:
diff changeset
242 int i,j;
223b71206888 Initial import
thib
parents:
diff changeset
243 for (i=0; i<=TYPE_NONSYSVARMAX; i++) {
223b71206888 Initial import
thib
parents:
diff changeset
244 for (j=0; j<2000; j++) {
223b71206888 Initial import
thib
parents:
diff changeset
245 var[i][j] = 0;
223b71206888 Initial import
thib
parents:
diff changeset
246 }
223b71206888 Initial import
thib
parents:
diff changeset
247 }
223b71206888 Initial import
thib
parents:
diff changeset
248 sys = 0;
223b71206888 Initial import
thib
parents:
diff changeset
249 for (j=0; j<2000; j++) {
223b71206888 Initial import
thib
parents:
diff changeset
250 str[j] = "";
223b71206888 Initial import
thib
parents:
diff changeset
251 }
223b71206888 Initial import
thib
parents:
diff changeset
252
223b71206888 Initial import
thib
parents:
diff changeset
253 save = strstr(save, "\n[Flags]\n");
223b71206888 Initial import
thib
parents:
diff changeset
254
223b71206888 Initial import
thib
parents:
diff changeset
255 if (save) {
223b71206888 Initial import
thib
parents:
diff changeset
256 save += strlen("\n[Flags]\n");
223b71206888 Initial import
thib
parents:
diff changeset
257 do {
223b71206888 Initial import
thib
parents:
diff changeset
258 if (save[0] == '[') break; // next section
223b71206888 Initial import
thib
parents:
diff changeset
259 if (strncmp(save, "V<",2) == 0) {
223b71206888 Initial import
thib
parents:
diff changeset
260 if (strncmp(save, "V<C>[",5) == 0) { // string
223b71206888 Initial import
thib
parents:
diff changeset
261 char buf[1024];
223b71206888 Initial import
thib
parents:
diff changeset
262 int n;
223b71206888 Initial import
thib
parents:
diff changeset
263 if (sscanf(save, "V<C>[%04d]=",&n) == 1) {
223b71206888 Initial import
thib
parents:
diff changeset
264 char* s = strchr(save, '=');
223b71206888 Initial import
thib
parents:
diff changeset
265 s++;
223b71206888 Initial import
thib
parents:
diff changeset
266 char* send = strchr(s, '\n');
223b71206888 Initial import
thib
parents:
diff changeset
267 int slen = send - s;
223b71206888 Initial import
thib
parents:
diff changeset
268 strncpy(buf, s, slen);
223b71206888 Initial import
thib
parents:
diff changeset
269 buf[slen] = 0;
223b71206888 Initial import
thib
parents:
diff changeset
270 if (n >= 0 && n < 2000) str[n] = buf;
223b71206888 Initial import
thib
parents:
diff changeset
271 }
223b71206888 Initial import
thib
parents:
diff changeset
272 } else if (save[2] >= '0' && save[2] <= '9') {
223b71206888 Initial import
thib
parents:
diff changeset
273 int c,n,v;
223b71206888 Initial import
thib
parents:
diff changeset
274 if (sscanf(save, "V<%d>[%04d]=%d\n",&c,&n,&v) == 3) {
223b71206888 Initial import
thib
parents:
diff changeset
275 if (c >= 0 && c <= TYPE_NONSYSVARMAX && n >= 0 && n < 2000)
223b71206888 Initial import
thib
parents:
diff changeset
276 var[c][n] = v;
223b71206888 Initial import
thib
parents:
diff changeset
277 }
223b71206888 Initial import
thib
parents:
diff changeset
278 }
223b71206888 Initial import
thib
parents:
diff changeset
279 }
223b71206888 Initial import
thib
parents:
diff changeset
280 save = strchr(save, '\n');
223b71206888 Initial import
thib
parents:
diff changeset
281 if (save) save++;
223b71206888 Initial import
thib
parents:
diff changeset
282 } while (save);
223b71206888 Initial import
thib
parents:
diff changeset
283 }
223b71206888 Initial import
thib
parents:
diff changeset
284 return;
223b71206888 Initial import
thib
parents:
diff changeset
285 }
223b71206888 Initial import
thib
parents:
diff changeset
286
223b71206888 Initial import
thib
parents:
diff changeset
287 void Flags::SaveSys(string& save) {
223b71206888 Initial import
thib
parents:
diff changeset
288 char buf[1024];
223b71206888 Initial import
thib
parents:
diff changeset
289 int j;
223b71206888 Initial import
thib
parents:
diff changeset
290 save = "\n[Flags]\n";
223b71206888 Initial import
thib
parents:
diff changeset
291 for (j=0; j<2000; j++) {
223b71206888 Initial import
thib
parents:
diff changeset
292 if (var[6][j] != 0) {
223b71206888 Initial import
thib
parents:
diff changeset
293 sprintf(buf, "V<6>[%04d]=%d\n",j,var[6][j]);
223b71206888 Initial import
thib
parents:
diff changeset
294 save += buf;
223b71206888 Initial import
thib
parents:
diff changeset
295 }
223b71206888 Initial import
thib
parents:
diff changeset
296 }
223b71206888 Initial import
thib
parents:
diff changeset
297 for (j=0; j<2000; j++) {
223b71206888 Initial import
thib
parents:
diff changeset
298 if (var[7][j] != 0) {
223b71206888 Initial import
thib
parents:
diff changeset
299 sprintf(buf, "V<25>[%04d]=%d\n",j,var[7][j]);
223b71206888 Initial import
thib
parents:
diff changeset
300 save += buf;
223b71206888 Initial import
thib
parents:
diff changeset
301 }
223b71206888 Initial import
thib
parents:
diff changeset
302 }
223b71206888 Initial import
thib
parents:
diff changeset
303 for (j=0; j<2000; j++) {
223b71206888 Initial import
thib
parents:
diff changeset
304 if (sys_str[j].length() != 0) {
223b71206888 Initial import
thib
parents:
diff changeset
305 sprintf(buf, "V<M>[%04d]=%s\n", j, sys_str[j].c_str());
223b71206888 Initial import
thib
parents:
diff changeset
306 save += buf;
223b71206888 Initial import
thib
parents:
diff changeset
307 }
223b71206888 Initial import
thib
parents:
diff changeset
308 }
223b71206888 Initial import
thib
parents:
diff changeset
309 }
223b71206888 Initial import
thib
parents:
diff changeset
310 void Flags::LoadSys(const char* save) {
223b71206888 Initial import
thib
parents:
diff changeset
311 int i,j;
223b71206888 Initial import
thib
parents:
diff changeset
312 for (i=6; i<=7; i++) {
223b71206888 Initial import
thib
parents:
diff changeset
313 for (j=0; j<2000; j++) {
223b71206888 Initial import
thib
parents:
diff changeset
314 var[i][j] = 0;
223b71206888 Initial import
thib
parents:
diff changeset
315 }
223b71206888 Initial import
thib
parents:
diff changeset
316 }
223b71206888 Initial import
thib
parents:
diff changeset
317 for (j=0; j<2000; j++) {
223b71206888 Initial import
thib
parents:
diff changeset
318 sys_str[j] = "";
223b71206888 Initial import
thib
parents:
diff changeset
319 }
223b71206888 Initial import
thib
parents:
diff changeset
320 sys = 0;
223b71206888 Initial import
thib
parents:
diff changeset
321
223b71206888 Initial import
thib
parents:
diff changeset
322 save = strstr(save, "\n[Flags]\n");
223b71206888 Initial import
thib
parents:
diff changeset
323
223b71206888 Initial import
thib
parents:
diff changeset
324 if (save) {
223b71206888 Initial import
thib
parents:
diff changeset
325 save += strlen("\n[Flags]\n");
223b71206888 Initial import
thib
parents:
diff changeset
326 do {
223b71206888 Initial import
thib
parents:
diff changeset
327 if (save[0] == '[') break; // next section
223b71206888 Initial import
thib
parents:
diff changeset
328 if (strncmp(save, "V<",2) == 0) {
223b71206888 Initial import
thib
parents:
diff changeset
329 if (strncmp(save, "V<M>[",5) == 0) { // string
223b71206888 Initial import
thib
parents:
diff changeset
330 char buf[1024];
223b71206888 Initial import
thib
parents:
diff changeset
331 int n;
223b71206888 Initial import
thib
parents:
diff changeset
332 if (sscanf(save, "V<M>[%04d]=",&n) == 1) {
223b71206888 Initial import
thib
parents:
diff changeset
333 char* s = strchr(save, '=');
223b71206888 Initial import
thib
parents:
diff changeset
334 s++;
223b71206888 Initial import
thib
parents:
diff changeset
335 char* send = strchr(s, '\n');
223b71206888 Initial import
thib
parents:
diff changeset
336 int slen = send - s;
223b71206888 Initial import
thib
parents:
diff changeset
337 strncpy(buf, s, slen);
223b71206888 Initial import
thib
parents:
diff changeset
338 buf[slen] = 0;
223b71206888 Initial import
thib
parents:
diff changeset
339 if (n >= 0 && n < 2000) sys_str[n] = buf;
223b71206888 Initial import
thib
parents:
diff changeset
340 }
223b71206888 Initial import
thib
parents:
diff changeset
341 } else if (save[2] >= '0' && save[2] <= '9') {
223b71206888 Initial import
thib
parents:
diff changeset
342 int c,n,v;
223b71206888 Initial import
thib
parents:
diff changeset
343 if (sscanf(save, "V<%d>[%04d]=%d\n",&c,&n,&v) == 3) {
223b71206888 Initial import
thib
parents:
diff changeset
344 if (c == 6 && n >= 0 && n < 2000)
223b71206888 Initial import
thib
parents:
diff changeset
345 var[6][n] = v;
223b71206888 Initial import
thib
parents:
diff changeset
346 else if (c == 25 && n >= 0 && n < 2000)
223b71206888 Initial import
thib
parents:
diff changeset
347 var[7][n] = v;
223b71206888 Initial import
thib
parents:
diff changeset
348 }
223b71206888 Initial import
thib
parents:
diff changeset
349 }
223b71206888 Initial import
thib
parents:
diff changeset
350 }
223b71206888 Initial import
thib
parents:
diff changeset
351 save = strchr(save, '\n');
223b71206888 Initial import
thib
parents:
diff changeset
352 if (save) save++;
223b71206888 Initial import
thib
parents:
diff changeset
353 } while (save);
223b71206888 Initial import
thib
parents:
diff changeset
354 }
223b71206888 Initial import
thib
parents:
diff changeset
355 return;
223b71206888 Initial import
thib
parents:
diff changeset
356 }
223b71206888 Initial import
thib
parents:
diff changeset
357
223b71206888 Initial import
thib
parents:
diff changeset
358 bool Flags::Exec(Cmd& cmd) {
223b71206888 Initial import
thib
parents:
diff changeset
359 if (cmd.cmd_type == CMD_FLAGS) { // 代入演算
223b71206888 Initial import
thib
parents:
diff changeset
360 if (cmd.args.size() != 2) return false;
223b71206888 Initial import
thib
parents:
diff changeset
361 Set(cmd.args[0], cmd.args[1].value);
223b71206888 Initial import
thib
parents:
diff changeset
362 cmd.clear();
223b71206888 Initial import
thib
parents:
diff changeset
363 return true;
223b71206888 Initial import
thib
parents:
diff changeset
364 }
223b71206888 Initial import
thib
parents:
diff changeset
365 if (cmd.cmd1 == 1 && cmd.cmd2 == 0x0a) { // 文字列演算
223b71206888 Initial import
thib
parents:
diff changeset
366 VarInfo arg1 = cmd.args[0];
223b71206888 Initial import
thib
parents:
diff changeset
367 switch(cmd.cmd3) {
223b71206888 Initial import
thib
parents:
diff changeset
368 case 0:
223b71206888 Initial import
thib
parents:
diff changeset
369 if (cmd.cmd4 == 0) {
223b71206888 Initial import
thib
parents:
diff changeset
370 SetStr(arg1, cmd.Str(cmd.args[1]));
223b71206888 Initial import
thib
parents:
diff changeset
371 } else if (cmd.cmd4 == 1) {
223b71206888 Initial import
thib
parents:
diff changeset
372 string s = cmd.Str(cmd.args[1]);
223b71206888 Initial import
thib
parents:
diff changeset
373 const char* sc = s.c_str();
223b71206888 Initial import
thib
parents:
diff changeset
374 int len = cmd.args[2].value;
223b71206888 Initial import
thib
parents:
diff changeset
375 int i;for (i=0; i<sc[i]!=0 && len != 0; i++, len--) {
223b71206888 Initial import
thib
parents:
diff changeset
376 if (sc[i]<0 && sc[i+1]!=0) i++;
223b71206888 Initial import
thib
parents:
diff changeset
377 }
223b71206888 Initial import
thib
parents:
diff changeset
378 s.erase(i); // 全角で len 文字まで切り詰める
223b71206888 Initial import
thib
parents:
diff changeset
379 SetStr(arg1, s);
223b71206888 Initial import
thib
parents:
diff changeset
380 // fprintf(stderr,"Set[%d,%d]<-%s\n",arg1.type,arg1.number,s.c_str());
223b71206888 Initial import
thib
parents:
diff changeset
381 } else break;
223b71206888 Initial import
thib
parents:
diff changeset
382 cmd.clear();
223b71206888 Initial import
thib
parents:
diff changeset
383 break;
223b71206888 Initial import
thib
parents:
diff changeset
384 case 1:
223b71206888 Initial import
thib
parents:
diff changeset
385 if (cmd.cmd4 == 0) {
223b71206888 Initial import
thib
parents:
diff changeset
386 SetStr(arg1, "");
223b71206888 Initial import
thib
parents:
diff changeset
387 cmd.clear();
223b71206888 Initial import
thib
parents:
diff changeset
388 } else if (cmd.cmd4 == 1) {
223b71206888 Initial import
thib
parents:
diff changeset
389 // 領域指定で文字列クリア
223b71206888 Initial import
thib
parents:
diff changeset
390 VarInfo v1 = cmd.args[0];
223b71206888 Initial import
thib
parents:
diff changeset
391 VarInfo v2 = cmd.args[1];
223b71206888 Initial import
thib
parents:
diff changeset
392 eprintf("memclear(str). Var[%d]<%d> - Var[%d]<%d>\n",v1.type, v1.number, v2.type, v2.number);
223b71206888 Initial import
thib
parents:
diff changeset
393 if (v1.type != v2.type || (v1.type != TYPE_VARSTR && v1.type != TYPE_VARSYSSTR && v1.type != TYPE_VARLOCSTR)) {
223b71206888 Initial import
thib
parents:
diff changeset
394 eprintf(" error: bad args\n");
223b71206888 Initial import
thib
parents:
diff changeset
395 } else {
223b71206888 Initial import
thib
parents:
diff changeset
396 if (v1.number < 0) v1.number = 0;
223b71206888 Initial import
thib
parents:
diff changeset
397 if (v2.number > 2000) v2.number = 2000;
223b71206888 Initial import
thib
parents:
diff changeset
398 for (; v1.number <= v2.number; v1.number++) {
223b71206888 Initial import
thib
parents:
diff changeset
399 SetStr(v1, "");
223b71206888 Initial import
thib
parents:
diff changeset
400 }
223b71206888 Initial import
thib
parents:
diff changeset
401 }
223b71206888 Initial import
thib
parents:
diff changeset
402 cmd.clear();
223b71206888 Initial import
thib
parents:
diff changeset
403 }
223b71206888 Initial import
thib
parents:
diff changeset
404 case 2:
223b71206888 Initial import
thib
parents:
diff changeset
405 SetStr(arg1, Str(arg1.type,arg1.number) + cmd.Str(cmd.args[1]));
223b71206888 Initial import
thib
parents:
diff changeset
406 // fprintf(stderr,"Append[%d,%d]<-%s(%d:%d)\n",arg1.type,arg1.number,Str(arg1.type,arg1.number).c_str(),cmd.args[1].type,cmd.args[1].number);
223b71206888 Initial import
thib
parents:
diff changeset
407 cmd.clear();
223b71206888 Initial import
thib
parents:
diff changeset
408 break;
223b71206888 Initial import
thib
parents:
diff changeset
409 case 3:
223b71206888 Initial import
thib
parents:
diff changeset
410 SetSys(strlen(cmd.Str(cmd.args[0])));
223b71206888 Initial import
thib
parents:
diff changeset
411 cmd.clear();
223b71206888 Initial import
thib
parents:
diff changeset
412 break;
223b71206888 Initial import
thib
parents:
diff changeset
413 case 4:
223b71206888 Initial import
thib
parents:
diff changeset
414 { int v = strcmp(cmd.Str(cmd.args[0]), cmd.Str(cmd.args[1]));
223b71206888 Initial import
thib
parents:
diff changeset
415 // string s1=cmd.Str(cmd.args[0]);
223b71206888 Initial import
thib
parents:
diff changeset
416 // string s2=cmd.Str(cmd.args[1]);
223b71206888 Initial import
thib
parents:
diff changeset
417 // fprintf(stderr,"Cmp %s(%d:%d):%s(%d:%d):%d\n",s1.c_str(),cmd.args[0].type,cmd.args[0].number,s2.c_str(),cmd.args[1].type,cmd.args[1].number,v);
223b71206888 Initial import
thib
parents:
diff changeset
418 if (v < 0) SetSys(-1);
223b71206888 Initial import
thib
parents:
diff changeset
419 else if (v > 0) SetSys(1);
223b71206888 Initial import
thib
parents:
diff changeset
420 else SetSys(0);
223b71206888 Initial import
thib
parents:
diff changeset
421 cmd.clear();
223b71206888 Initial import
thib
parents:
diff changeset
422 break; }
223b71206888 Initial import
thib
parents:
diff changeset
423 case 5: // substring, index from left
223b71206888 Initial import
thib
parents:
diff changeset
424 case 6: // substring, index from right
223b71206888 Initial import
thib
parents:
diff changeset
425 // 全角対応らしい
223b71206888 Initial import
thib
parents:
diff changeset
426 { int offset = cmd.args[2].value;
223b71206888 Initial import
thib
parents:
diff changeset
427 int len = strlen(cmd.Str(cmd.args[1]));
223b71206888 Initial import
thib
parents:
diff changeset
428 string str = cmd.Str(cmd.args[1]);
223b71206888 Initial import
thib
parents:
diff changeset
429 const char* s = str.c_str();
223b71206888 Initial import
thib
parents:
diff changeset
430 if (cmd.cmd3 == 6) offset = len - offset;
223b71206888 Initial import
thib
parents:
diff changeset
431 if (offset < 0) offset = 0;
223b71206888 Initial import
thib
parents:
diff changeset
432 // 先頭 N 文字を読み飛ばす
223b71206888 Initial import
thib
parents:
diff changeset
433 int i;
223b71206888 Initial import
thib
parents:
diff changeset
434 int offset_top = 0;
223b71206888 Initial import
thib
parents:
diff changeset
435 for (i=0; i<offset && s[offset_top] != 0; i++) {
223b71206888 Initial import
thib
parents:
diff changeset
436 if (s[offset_top] < 0 && s[offset_top+1] != 0) offset_top += 2;
223b71206888 Initial import
thib
parents:
diff changeset
437 else offset_top += 1;
223b71206888 Initial import
thib
parents:
diff changeset
438 }
223b71206888 Initial import
thib
parents:
diff changeset
439 if (s[offset_top] == 0) {
223b71206888 Initial import
thib
parents:
diff changeset
440 SetStr(arg1, "");
223b71206888 Initial import
thib
parents:
diff changeset
441 } else if (cmd.cmd4 == 0) { // 長さ制限なし
223b71206888 Initial import
thib
parents:
diff changeset
442 SetStr(arg1, string(s, offset_top));
223b71206888 Initial import
thib
parents:
diff changeset
443 } else { // cmd.cmd4 == 1
223b71206888 Initial import
thib
parents:
diff changeset
444 int slen = cmd.args[3].value;
223b71206888 Initial import
thib
parents:
diff changeset
445 int offset_end = offset_top;
223b71206888 Initial import
thib
parents:
diff changeset
446 for (i=0; i<slen && s[offset_end] != 0; i++) {
223b71206888 Initial import
thib
parents:
diff changeset
447 if (s[offset_end] < 0 && s[offset_end]+1 != 0) offset_end += 2;
223b71206888 Initial import
thib
parents:
diff changeset
448 else offset_end += 1;
223b71206888 Initial import
thib
parents:
diff changeset
449 }
223b71206888 Initial import
thib
parents:
diff changeset
450 string result(s, offset_top, offset_end-offset_top);
223b71206888 Initial import
thib
parents:
diff changeset
451 SetStr(arg1, result);
223b71206888 Initial import
thib
parents:
diff changeset
452 }
223b71206888 Initial import
thib
parents:
diff changeset
453 cmd.clear();
223b71206888 Initial import
thib
parents:
diff changeset
454 break; }
223b71206888 Initial import
thib
parents:
diff changeset
455 case 7: {// strlen w/ kanji
223b71206888 Initial import
thib
parents:
diff changeset
456 const char* s = cmd.Str(cmd.args[0]); int i;
223b71206888 Initial import
thib
parents:
diff changeset
457 for (i=0; *s != 0; i++) {
223b71206888 Initial import
thib
parents:
diff changeset
458 if (*s < 0 && s[1] != 0) s += 2;
223b71206888 Initial import
thib
parents:
diff changeset
459 else s++;
223b71206888 Initial import
thib
parents:
diff changeset
460 }
223b71206888 Initial import
thib
parents:
diff changeset
461 SetSys(i);
223b71206888 Initial import
thib
parents:
diff changeset
462 cmd.clear();
223b71206888 Initial import
thib
parents:
diff changeset
463 break; }
223b71206888 Initial import
thib
parents:
diff changeset
464 case 8: // 文字列を切って短くする
223b71206888 Initial import
thib
parents:
diff changeset
465 if (cmd.args[1].value <= 0) {
223b71206888 Initial import
thib
parents:
diff changeset
466 SetStr(arg1, "");
223b71206888 Initial import
thib
parents:
diff changeset
467 } else if (cmd.args[1].value < strlen(cmd.Str(cmd.args[1]))) {
223b71206888 Initial import
thib
parents:
diff changeset
468 Str(arg1.type,arg1.number).erase(cmd.args[1].value);
223b71206888 Initial import
thib
parents:
diff changeset
469 }
223b71206888 Initial import
thib
parents:
diff changeset
470 cmd.clear();
223b71206888 Initial import
thib
parents:
diff changeset
471 break;
223b71206888 Initial import
thib
parents:
diff changeset
472 case 0x0e: // 漢字モードでitoa
223b71206888 Initial import
thib
parents:
diff changeset
473 {
223b71206888 Initial import
thib
parents:
diff changeset
474 int arg1 = cmd.args[0].value;
223b71206888 Initial import
thib
parents:
diff changeset
475 string result;
223b71206888 Initial import
thib
parents:
diff changeset
476 char wc[3]; wc[2]=0;
223b71206888 Initial import
thib
parents:
diff changeset
477 char buf[20];
223b71206888 Initial import
thib
parents:
diff changeset
478 if (cmd.cmd4 == 0) {
223b71206888 Initial import
thib
parents:
diff changeset
479 sprintf(buf, "%d", arg1);
223b71206888 Initial import
thib
parents:
diff changeset
480 } else { // cmd.cmd4 == 1
223b71206888 Initial import
thib
parents:
diff changeset
481 char fmt[20];
223b71206888 Initial import
thib
parents:
diff changeset
482 sprintf(fmt, "%%%dd", cmd.args[2].value);
223b71206888 Initial import
thib
parents:
diff changeset
483 sprintf(buf, fmt, arg1);
223b71206888 Initial import
thib
parents:
diff changeset
484 }
223b71206888 Initial import
thib
parents:
diff changeset
485 int i;
223b71206888 Initial import
thib
parents:
diff changeset
486 for (i=0; buf[i] != 0; i++) {
223b71206888 Initial import
thib
parents:
diff changeset
487 if (buf[i] == ' ') {
223b71206888 Initial import
thib
parents:
diff changeset
488 wc[0] = 0x81;
223b71206888 Initial import
thib
parents:
diff changeset
489 wc[0] = 0x40;
223b71206888 Initial import
thib
parents:
diff changeset
490 } else if (buf[i] == '-') {
223b71206888 Initial import
thib
parents:
diff changeset
491 wc[0] = 0x81;
223b71206888 Initial import
thib
parents:
diff changeset
492 wc[0] = 0x7c;
223b71206888 Initial import
thib
parents:
diff changeset
493 } else if (isdigit(buf[i])) {
223b71206888 Initial import
thib
parents:
diff changeset
494 wc[0] = 0x82;
223b71206888 Initial import
thib
parents:
diff changeset
495 wc[1] = buf[i] - '0' + 0x4f;
223b71206888 Initial import
thib
parents:
diff changeset
496 } else {
223b71206888 Initial import
thib
parents:
diff changeset
497 continue;
223b71206888 Initial import
thib
parents:
diff changeset
498 }
223b71206888 Initial import
thib
parents:
diff changeset
499 result += wc;
223b71206888 Initial import
thib
parents:
diff changeset
500 }
223b71206888 Initial import
thib
parents:
diff changeset
501 SetStr(cmd.args[1], result);
223b71206888 Initial import
thib
parents:
diff changeset
502 cmd.clear();
223b71206888 Initial import
thib
parents:
diff changeset
503 }
223b71206888 Initial import
thib
parents:
diff changeset
504 break;
223b71206888 Initial import
thib
parents:
diff changeset
505 case 0x0f: case 0x11: // itoa (0x11 の方は zero padding するっぽい)
223b71206888 Initial import
thib
parents:
diff changeset
506 if (cmd.cmd4 == 0) {
223b71206888 Initial import
thib
parents:
diff changeset
507 int arg1 = cmd.args[0].value;
223b71206888 Initial import
thib
parents:
diff changeset
508 char buf[1024]; sprintf(buf, "%d", arg1);
223b71206888 Initial import
thib
parents:
diff changeset
509 SetStr(cmd.args[1], buf);
223b71206888 Initial import
thib
parents:
diff changeset
510 cmd.clear();
223b71206888 Initial import
thib
parents:
diff changeset
511 } else if (cmd.cmd4 == 1) {
223b71206888 Initial import
thib
parents:
diff changeset
512 // 漢字(SJIS) : 82 [4f+N]
223b71206888 Initial import
thib
parents:
diff changeset
513 // やはり漢字じゃない?
223b71206888 Initial import
thib
parents:
diff changeset
514 int arg1 = cmd.args[0].value;
223b71206888 Initial import
thib
parents:
diff changeset
515 char buf[1024]; char fmt[1024];
223b71206888 Initial import
thib
parents:
diff changeset
516 if (cmd.cmd3 == 0x0f) {
223b71206888 Initial import
thib
parents:
diff changeset
517 sprintf(fmt, "%%%dd",cmd.args[2].value); /* 空白でパディング */
223b71206888 Initial import
thib
parents:
diff changeset
518 } else {
223b71206888 Initial import
thib
parents:
diff changeset
519 sprintf(fmt, "%%0%dd",cmd.args[2].value);
223b71206888 Initial import
thib
parents:
diff changeset
520 }
223b71206888 Initial import
thib
parents:
diff changeset
521 sprintf(buf, fmt, arg1);
223b71206888 Initial import
thib
parents:
diff changeset
522 SetStr(cmd.args[1], buf);
223b71206888 Initial import
thib
parents:
diff changeset
523 cmd.clear();
223b71206888 Initial import
thib
parents:
diff changeset
524 }
223b71206888 Initial import
thib
parents:
diff changeset
525 break;
223b71206888 Initial import
thib
parents:
diff changeset
526 case 0x64: // 文字列の表示 : 引数をテキストウィンドウに表示
223b71206888 Initial import
thib
parents:
diff changeset
527 if (cmd.cmd4 == 1) {
223b71206888 Initial import
thib
parents:
diff changeset
528 char buf[256];
223b71206888 Initial import
thib
parents:
diff changeset
529 snprintf(buf, 255, "%d", Get(cmd.args[0].type, cmd.args[0].number));
223b71206888 Initial import
thib
parents:
diff changeset
530 cmd.args[0].type = TYPE_STR;
223b71206888 Initial import
thib
parents:
diff changeset
531 cmd.args[0].value = cmd.AddStr(buf);
223b71206888 Initial import
thib
parents:
diff changeset
532 cmd.cmd4 = 0;
223b71206888 Initial import
thib
parents:
diff changeset
533 }
223b71206888 Initial import
thib
parents:
diff changeset
534
223b71206888 Initial import
thib
parents:
diff changeset
535 #if 0
223b71206888 Initial import
thib
parents:
diff changeset
536 @@@
223b71206888 Initial import
thib
parents:
diff changeset
537 save 27
223b71206888 Initial import
thib
parents:
diff changeset
538 ともよメガネのところ
223b71206888 Initial import
thib
parents:
diff changeset
539 - オブジェクト関連:seen9061:0 呼び出しで黒い背景画をかさねるところ、変になる
223b71206888 Initial import
thib
parents:
diff changeset
540 @@@
223b71206888 Initial import
thib
parents:
diff changeset
541 %Xで置換する名前の設定。0x51e で読みだし。セーブファイルごとに保存されるはずなので実装を考えること
223b71206888 Initial import
thib
parents:
diff changeset
542 %は0-3 (4 以降は使ってない)で、渚、秋生、渚、伊吹先生、など
223b71206888 Initial import
thib
parents:
diff changeset
543 StrVar を拡張して代入すること
223b71206888 Initial import
thib
parents:
diff changeset
544 初期値はこの辺
223b71206888 Initial import
thib
parents:
diff changeset
545 Text側に納め、セーブファイルでも同じようにすべきだろうなあ
223b71206888 Initial import
thib
parents:
diff changeset
546 args:0,"渚"
223b71206888 Initial import
thib
parents:
diff changeset
547 args:1,"秋生"
223b71206888 Initial import
thib
parents:
diff changeset
548 args:2,"渚"
223b71206888 Initial import
thib
parents:
diff changeset
549 args:3,"伊吹先生"
223b71206888 Initial import
thib
parents:
diff changeset
550 args:4,"朋也くん"
223b71206888 Initial import
thib
parents:
diff changeset
551 args:5,"岡崎さん"
223b71206888 Initial import
thib
parents:
diff changeset
552
223b71206888 Initial import
thib
parents:
diff changeset
553
223b71206888 Initial import
thib
parents:
diff changeset
554 106737 : 0x23 - cmd 01-04:051f:00[ 2]
223b71206888 Initial import
thib
parents:
diff changeset
555 args:0,"古河"
223b71206888 Initial import
thib
parents:
diff changeset
556 106758 : line 1712
223b71206888 Initial import
thib
parents:
diff changeset
557 106761 : 0x23 - cmd 01-04:051f:00[ 2]
223b71206888 Initial import
thib
parents:
diff changeset
558 args:2,"古河"
223b71206888 Initial import
thib
parents:
diff changeset
559 106782 : line 1713
223b71206888 Initial import
thib
parents:
diff changeset
560 106785 : 0x23 - cmd 01-04:051f:00[ 2]
223b71206888 Initial import
thib
parents:
diff changeset
561 args:4,"岡崎さん"
223b71206888 Initial import
thib
parents:
diff changeset
562
223b71206888 Initial import
thib
parents:
diff changeset
563 47382 : 0x23 - cmd 01-04:051e:00[ 2]
223b71206888 Initial import
thib
parents:
diff changeset
564 args:4,V<18>[0](=0)
223b71206888 Initial import
thib
parents:
diff changeset
565
223b71206888 Initial import
thib
parents:
diff changeset
566 47408 : 0x23 - cmd 01-0a:0004:00[ 2]
223b71206888 Initial import
thib
parents:
diff changeset
567 args:V<18>[0](=0),"岡崎さん"
223b71206888 Initial import
thib
parents:
diff changeset
568 47437 : expr: V<0>[1000](=0)=V<sys>
223b71206888 Initial import
thib
parents:
diff changeset
569 47451 : 0x23 - cmd 01-0a:0004:00[ 2]
223b71206888 Initial import
thib
parents:
diff changeset
570 args:V<18>[0](=0),"朋也くん"
223b71206888 Initial import
thib
parents:
diff changeset
571 47480 : expr: V<0>[1001](=0)=V<sys>
223b71206888 Initial import
thib
parents:
diff changeset
572 47494 : V<0>[1000](=0)==0(=true)-> 47589
223b71206888 Initial import
thib
parents:
diff changeset
573 47526 : 0x23 - cmd 01-04:0514:00[ 2]
223b71206888 Initial import
thib
parents:
diff changeset
574 args:0,V<18>[0](=0) /* NAME.A を帰す */
223b71206888 Initial import
thib
parents:
diff changeset
575 47552 : 0x23 - cmd 01-0a:0002:00[ 2]
223b71206888 Initial import
thib
parents:
diff changeset
576 args:V<18>[0](=0),"さん"
223b71206888 Initial import
thib
parents:
diff changeset
577 47577 : jmp -> 47672
223b71206888 Initial import
thib
parents:
diff changeset
578 47589 : V<0>[1001](=0)==0(=true)-> 47672
223b71206888 Initial import
thib
parents:
diff changeset
579 47621 : 0x23 - cmd 01-04:0514:00[ 2]
223b71206888 Initial import
thib
parents:
diff changeset
580 args:1,V<18>[0](=0) /* NAME.B を帰す */
223b71206888 Initial import
thib
parents:
diff changeset
581 47647 : 0x23 - cmd 01-0a:0002:00[ 2]
223b71206888 Initial import
thib
parents:
diff changeset
582 args:V<18>[0](=0),"くん"
223b71206888 Initial import
thib
parents:
diff changeset
583 47672 : pos. 279
223b71206888 Initial import
thib
parents:
diff changeset
584 47675 : 0x23 - cmd 01-0a:0064:00[ 1]
223b71206888 Initial import
thib
parents:
diff changeset
585 args:V<18>[0](=0)
223b71206888 Initial import
thib
parents:
diff changeset
586
223b71206888 Initial import
thib
parents:
diff changeset
587 #endif
223b71206888 Initial import
thib
parents:
diff changeset
588 cmd.cmd_type = CMD_TEXT;
223b71206888 Initial import
thib
parents:
diff changeset
589 break;
223b71206888 Initial import
thib
parents:
diff changeset
590 }
223b71206888 Initial import
thib
parents:
diff changeset
591 }
223b71206888 Initial import
thib
parents:
diff changeset
592 if (cmd.cmd1 == 1 && cmd.cmd2 == 0x0b) { // 数値変数演算
223b71206888 Initial import
thib
parents:
diff changeset
593 if (cmd.cmd3 == 0 && cmd.cmd4 == 0) {
223b71206888 Initial import
thib
parents:
diff changeset
594 /* 複数の変数をセット */
223b71206888 Initial import
thib
parents:
diff changeset
595 VarInfo v1 = cmd.args[0];
223b71206888 Initial import
thib
parents:
diff changeset
596 eprintf("set multiple-var Var[%d]<%d> <- ",v1.type, v1.number);
223b71206888 Initial import
thib
parents:
diff changeset
597 int i;
223b71206888 Initial import
thib
parents:
diff changeset
598 if (cmd.args.size() < cmd.argc) {
223b71206888 Initial import
thib
parents:
diff changeset
599 eprintf(" error: argsize changed %d -> %d\n",cmd.argc, cmd.args.size());
223b71206888 Initial import
thib
parents:
diff changeset
600 cmd.argc = cmd.args.size();
223b71206888 Initial import
thib
parents:
diff changeset
601 }
223b71206888 Initial import
thib
parents:
diff changeset
602 for (i=0; i<cmd.argc; i++) {
223b71206888 Initial import
thib
parents:
diff changeset
603 eprintf("%d, ",cmd.args[i+1].value);
223b71206888 Initial import
thib
parents:
diff changeset
604 Set(v1, cmd.args[i+1].value);
223b71206888 Initial import
thib
parents:
diff changeset
605 v1.number++;
223b71206888 Initial import
thib
parents:
diff changeset
606 }
223b71206888 Initial import
thib
parents:
diff changeset
607 eprintf("\n");
223b71206888 Initial import
thib
parents:
diff changeset
608 cmd.clear();
223b71206888 Initial import
thib
parents:
diff changeset
609 } else if (cmd.cmd3 == 1 && cmd.cmd4 == 0) {
223b71206888 Initial import
thib
parents:
diff changeset
610 /* 領域指定で変数をクリア */
223b71206888 Initial import
thib
parents:
diff changeset
611 VarInfo v1 = cmd.args[0];
223b71206888 Initial import
thib
parents:
diff changeset
612 VarInfo v2 = cmd.args[1];
223b71206888 Initial import
thib
parents:
diff changeset
613 eprintf("memclear. Var[%d]<%d> - Var[%d]<%d>\n",v1.type, v1.number, v2.type, v2.number);
223b71206888 Initial import
thib
parents:
diff changeset
614 if (v1.type != v2.type || !IsInt(v1.type)) eprintf(" error: bad args\n");
223b71206888 Initial import
thib
parents:
diff changeset
615 else {
223b71206888 Initial import
thib
parents:
diff changeset
616 if (v1.number < 0) v1.number = 0;
223b71206888 Initial import
thib
parents:
diff changeset
617 if (v2.number > MaxIndex(v2.type)) v2.number = MaxIndex(v2.type);
223b71206888 Initial import
thib
parents:
diff changeset
618 for (; v1.number <= v2.number; v1.number++)
223b71206888 Initial import
thib
parents:
diff changeset
619 Set(v1, 0);
223b71206888 Initial import
thib
parents:
diff changeset
620 }
223b71206888 Initial import
thib
parents:
diff changeset
621 cmd.clear();
223b71206888 Initial import
thib
parents:
diff changeset
622 } else if (cmd.cmd3 == 1 && cmd.cmd4 == 1) {
223b71206888 Initial import
thib
parents:
diff changeset
623 /* 領域指定で変数をセット */
223b71206888 Initial import
thib
parents:
diff changeset
624 VarInfo v1 = cmd.args[0];
223b71206888 Initial import
thib
parents:
diff changeset
625 VarInfo v2 = cmd.args[1];
223b71206888 Initial import
thib
parents:
diff changeset
626 int value = cmd.args[2].value;
223b71206888 Initial import
thib
parents:
diff changeset
627 eprintf("memset. Var[%d]<%d> - Var[%d]<%d> <- %d\n",v1.type, v1.number, v2.type, v2.number, value);
223b71206888 Initial import
thib
parents:
diff changeset
628 if (v1.type != v2.type || !IsInt(v1.type)) eprintf(" error: bad args\n");
223b71206888 Initial import
thib
parents:
diff changeset
629 else {
223b71206888 Initial import
thib
parents:
diff changeset
630 if (v1.number < 0) v1.number = 0;
223b71206888 Initial import
thib
parents:
diff changeset
631 if (v2.number > MaxIndex(v2.type)) v2.number = MaxIndex(v2.type);
223b71206888 Initial import
thib
parents:
diff changeset
632 for (; v1.number <= v2.number; v1.number++)
223b71206888 Initial import
thib
parents:
diff changeset
633 Set(v1, value);
223b71206888 Initial import
thib
parents:
diff changeset
634 }
223b71206888 Initial import
thib
parents:
diff changeset
635 cmd.clear();
223b71206888 Initial import
thib
parents:
diff changeset
636 } else if (cmd.cmd3 == 4 && cmd.cmd4 == 1) { // 領域クリア(sysfunc.txt)
223b71206888 Initial import
thib
parents:
diff changeset
637 VarInfo v1 = cmd.args[0];
223b71206888 Initial import
thib
parents:
diff changeset
638 int step = cmd.args[1].value;
223b71206888 Initial import
thib
parents:
diff changeset
639 int deal = cmd.args[2].value;
223b71206888 Initial import
thib
parents:
diff changeset
640 int val = cmd.args[3].value;
223b71206888 Initial import
thib
parents:
diff changeset
641 eprintf("memclear. Var[%d]<%d> step %d deal %d <- val %d\n",v1.type, v1.number, step, deal, val);
223b71206888 Initial import
thib
parents:
diff changeset
642 int i; for (i=0; i<deal; i++) {
223b71206888 Initial import
thib
parents:
diff changeset
643 Set(v1, val);
223b71206888 Initial import
thib
parents:
diff changeset
644 v1.number += step;
223b71206888 Initial import
thib
parents:
diff changeset
645 }
223b71206888 Initial import
thib
parents:
diff changeset
646 cmd.clear();
223b71206888 Initial import
thib
parents:
diff changeset
647 } else if (cmd.cmd3 == 0x64 && cmd.cmd4 == 0) { //領域で数値を合計する
223b71206888 Initial import
thib
parents:
diff changeset
648 VarInfo v1 = cmd.args[0];
223b71206888 Initial import
thib
parents:
diff changeset
649 VarInfo v2 = cmd.args[1];
223b71206888 Initial import
thib
parents:
diff changeset
650 eprintf("sum var. Var[%d]<%d> - Var[%d]<%d>\n",v1.type, v1.number, v2.type, v2.number);
223b71206888 Initial import
thib
parents:
diff changeset
651 int sum = 0;
223b71206888 Initial import
thib
parents:
diff changeset
652 if (v1.type != v2.type || !IsInt(v1.type)) eprintf(" error: bad args\n");
223b71206888 Initial import
thib
parents:
diff changeset
653 else {
223b71206888 Initial import
thib
parents:
diff changeset
654 if (v1.number < 0) v1.number = 0;
223b71206888 Initial import
thib
parents:
diff changeset
655 if (v2.number > MaxIndex(v2.type)) v2.number = MaxIndex(v2.type);
223b71206888 Initial import
thib
parents:
diff changeset
656 for (; v1.number <= v2.number; v1.number++)
223b71206888 Initial import
thib
parents:
diff changeset
657 sum += (*this)(v1);
223b71206888 Initial import
thib
parents:
diff changeset
658 }
223b71206888 Initial import
thib
parents:
diff changeset
659 eprintf(" ret %d\n",sum);
223b71206888 Initial import
thib
parents:
diff changeset
660 cmd.SetSysvar(sum);
223b71206888 Initial import
thib
parents:
diff changeset
661 }
223b71206888 Initial import
thib
parents:
diff changeset
662 }
223b71206888 Initial import
thib
parents:
diff changeset
663 return false;
223b71206888 Initial import
thib
parents:
diff changeset
664 }
223b71206888 Initial import
thib
parents:
diff changeset
665
223b71206888 Initial import
thib
parents:
diff changeset
666 /*********************************************************************
223b71206888 Initial import
thib
parents:
diff changeset
667 ** Cmd
223b71206888 Initial import
thib
parents:
diff changeset
668 */
223b71206888 Initial import
thib
parents:
diff changeset
669
223b71206888 Initial import
thib
parents:
diff changeset
670 /* 数値 num := 0x24 0xff <int num> */
223b71206888 Initial import
thib
parents:
diff changeset
671 /* 変数 var := 0x24 <uchar type> 0x5b <exp> 0x5d */
223b71206888 Initial import
thib
parents:
diff changeset
672 /* 項 token := num | var | 0x28 <exp> 0x29 | <plus|minus> token */
223b71206888 Initial import
thib
parents:
diff changeset
673
223b71206888 Initial import
thib
parents:
diff changeset
674 int Cmd::GetLeftToken(const char*& d, VarInfo& info) {
223b71206888 Initial import
thib
parents:
diff changeset
675 bool var_flag = true;
223b71206888 Initial import
thib
parents:
diff changeset
676 int minus_flag = 0;
223b71206888 Initial import
thib
parents:
diff changeset
677 int value = 0;
223b71206888 Initial import
thib
parents:
diff changeset
678 if (d[0] == 0x5c && (d[1] == 1 || d[1] == 0) ) {
223b71206888 Initial import
thib
parents:
diff changeset
679 if (d[1] == 1) {dprintf("minus-"); minus_flag ^= 1;}
223b71206888 Initial import
thib
parents:
diff changeset
680 else dprintf("plus-");
223b71206888 Initial import
thib
parents:
diff changeset
681 d += 2;
223b71206888 Initial import
thib
parents:
diff changeset
682 var_flag = false;
223b71206888 Initial import
thib
parents:
diff changeset
683 }
223b71206888 Initial import
thib
parents:
diff changeset
684 if (d[0] == 0x24 && ((unsigned const char*)d)[1] == 0xff) {
223b71206888 Initial import
thib
parents:
diff changeset
685 // if ( (d[0] == 0x30 || d[0] == 0x31) && d[1] == 0x24 && ((unsigned const char*)d)[2] == 0xff) /* @@@ not supported; selection 内で、0x30|0x31 が付随することがある */
223b71206888 Initial import
thib
parents:
diff changeset
686 // numerical atom
223b71206888 Initial import
thib
parents:
diff changeset
687 d += 6;
223b71206888 Initial import
thib
parents:
diff changeset
688 value = read_little_endian_int(d-4);
223b71206888 Initial import
thib
parents:
diff changeset
689 dprintf("%d",value);
223b71206888 Initial import
thib
parents:
diff changeset
690 var_flag = false;
223b71206888 Initial import
thib
parents:
diff changeset
691 } else if (d[0] == 0x24 && *(unsigned char*)(d+1) == 0xc8) {
223b71206888 Initial import
thib
parents:
diff changeset
692 dprintf("V<sys>");
223b71206888 Initial import
thib
parents:
diff changeset
693 d += 2;
223b71206888 Initial import
thib
parents:
diff changeset
694 info.type = TYPE_SYS; info.number = 0;
223b71206888 Initial import
thib
parents:
diff changeset
695 value = info.value = flags();
223b71206888 Initial import
thib
parents:
diff changeset
696 } else if (d[0] == 0x24 && d[2] == 0x5b) {
223b71206888 Initial import
thib
parents:
diff changeset
697 // 0x24,<type>,0x5b,<expr>,0x5d-terminated term
223b71206888 Initial import
thib
parents:
diff changeset
698 info.type = *(unsigned char*)(d+1);
223b71206888 Initial import
thib
parents:
diff changeset
699 d += 3;
223b71206888 Initial import
thib
parents:
diff changeset
700 dprintf("V<%d>[",info.type);
223b71206888 Initial import
thib
parents:
diff changeset
701 info.number = GetExpression(d);
223b71206888 Initial import
thib
parents:
diff changeset
702 dprintf("]");
223b71206888 Initial import
thib
parents:
diff changeset
703 if (*d == 0x5d) d++;
223b71206888 Initial import
thib
parents:
diff changeset
704 else SetError();
223b71206888 Initial import
thib
parents:
diff changeset
705 if (info.type == TYPE_VARSTR || info.type == TYPE_VARSYSSTR || info.type == TYPE_VARLOCSTR) {
223b71206888 Initial import
thib
parents:
diff changeset
706 value = 0;
223b71206888 Initial import
thib
parents:
diff changeset
707 info.value = StrVar(info.type, info.number);
223b71206888 Initial import
thib
parents:
diff changeset
708 } else {
223b71206888 Initial import
thib
parents:
diff changeset
709 value = info.value = flags(info);
223b71206888 Initial import
thib
parents:
diff changeset
710 }
223b71206888 Initial import
thib
parents:
diff changeset
711 dprintf("(=%d)",value);
223b71206888 Initial import
thib
parents:
diff changeset
712 } else SetError();
223b71206888 Initial import
thib
parents:
diff changeset
713
223b71206888 Initial import
thib
parents:
diff changeset
714 if (minus_flag) value = -value;
223b71206888 Initial import
thib
parents:
diff changeset
715 if (!var_flag) {
223b71206888 Initial import
thib
parents:
diff changeset
716 info.type = TYPE_VAL;
223b71206888 Initial import
thib
parents:
diff changeset
717 info.value = value;
223b71206888 Initial import
thib
parents:
diff changeset
718 }
223b71206888 Initial import
thib
parents:
diff changeset
719 return value;
223b71206888 Initial import
thib
parents:
diff changeset
720 }
223b71206888 Initial import
thib
parents:
diff changeset
721
223b71206888 Initial import
thib
parents:
diff changeset
722 static char* op_str[70] = {
223b71206888 Initial import
thib
parents:
diff changeset
723 // 0 1 2 3 4 5 6 7 8 9
223b71206888 Initial import
thib
parents:
diff changeset
724 "+", "-", "*", "/", "%", "&", "|", "^", "<<", ">>", // +00
223b71206888 Initial import
thib
parents:
diff changeset
725 "err.","err.","err.","err.","err.","err.","err.","err.","err.","err.", // +10
223b71206888 Initial import
thib
parents:
diff changeset
726 "+=", "-=", "*=", "/=", "%=", "&=", "|=", "^=", "<<=", ">>=", // +20
223b71206888 Initial import
thib
parents:
diff changeset
727 "=", "err.","err.","err.","err.","err.","err.","err.","err.","err.", // +30
223b71206888 Initial import
thib
parents:
diff changeset
728 "==", "!=", "<=", "<", ">=", ">", "err.","err.","err.","err.", // +40
223b71206888 Initial import
thib
parents:
diff changeset
729 "err.","err.","err.","err.","err.","err.","err.","err.","err.","err.", // +50
223b71206888 Initial import
thib
parents:
diff changeset
730 "&&", "||", "err.","err.","err.","err.","err.","err.","err.","err.", // +60
223b71206888 Initial import
thib
parents:
diff changeset
731 };
223b71206888 Initial import
thib
parents:
diff changeset
732
223b71206888 Initial import
thib
parents:
diff changeset
733 static int op_pri_tbl[12] = {
223b71206888 Initial import
thib
parents:
diff changeset
734 // + - * / % & | ^ << >>
223b71206888 Initial import
thib
parents:
diff changeset
735 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 10, 10};
223b71206888 Initial import
thib
parents:
diff changeset
736
223b71206888 Initial import
thib
parents:
diff changeset
737 inline int op_pri(int op) {
223b71206888 Initial import
thib
parents:
diff changeset
738 if (op > 11) return 10;
223b71206888 Initial import
thib
parents:
diff changeset
739 return op_pri_tbl[op];
223b71206888 Initial import
thib
parents:
diff changeset
740 }
223b71206888 Initial import
thib
parents:
diff changeset
741 inline int op_pri_cond(int op) {
223b71206888 Initial import
thib
parents:
diff changeset
742 if (op <= 11) return op_pri_tbl[op];
223b71206888 Initial import
thib
parents:
diff changeset
743 else if (op < 50) return 7;
223b71206888 Initial import
thib
parents:
diff changeset
744 else if (op == 60) return 8;
223b71206888 Initial import
thib
parents:
diff changeset
745 else if (op == 61) return 8;
223b71206888 Initial import
thib
parents:
diff changeset
746 else return 10;
223b71206888 Initial import
thib
parents:
diff changeset
747 }
223b71206888 Initial import
thib
parents:
diff changeset
748
223b71206888 Initial import
thib
parents:
diff changeset
749
223b71206888 Initial import
thib
parents:
diff changeset
750 inline int eval(int v1, int op, int v2) {
223b71206888 Initial import
thib
parents:
diff changeset
751 switch(op) {
223b71206888 Initial import
thib
parents:
diff changeset
752 case 0: return v1+v2;
223b71206888 Initial import
thib
parents:
diff changeset
753 case 1: return v1-v2;
223b71206888 Initial import
thib
parents:
diff changeset
754 case 2: return v1*v2;
223b71206888 Initial import
thib
parents:
diff changeset
755 case 3: return v2!=0 ? v1/v2 : v1;
223b71206888 Initial import
thib
parents:
diff changeset
756 case 4: return v2!=0 ? v1%v2 : v1;
223b71206888 Initial import
thib
parents:
diff changeset
757 case 5: return v1&v2;
223b71206888 Initial import
thib
parents:
diff changeset
758 case 6: return v1|v2;
223b71206888 Initial import
thib
parents:
diff changeset
759 case 7: return v1^v2;
223b71206888 Initial import
thib
parents:
diff changeset
760 case 8: return v1<<v2;
223b71206888 Initial import
thib
parents:
diff changeset
761 case 9: return v1>>v2;
223b71206888 Initial import
thib
parents:
diff changeset
762 case 40: return v1 == v2;
223b71206888 Initial import
thib
parents:
diff changeset
763 case 41: return v1 != v2;
223b71206888 Initial import
thib
parents:
diff changeset
764 case 42: return v1 <= v2;
223b71206888 Initial import
thib
parents:
diff changeset
765 case 43: return v1 < v2;
223b71206888 Initial import
thib
parents:
diff changeset
766 case 44: return v1 >= v2;
223b71206888 Initial import
thib
parents:
diff changeset
767 case 45: return v1 > v2;
223b71206888 Initial import
thib
parents:
diff changeset
768 case 60: return v1 && v2;
223b71206888 Initial import
thib
parents:
diff changeset
769 case 61: return v1 || v2;
223b71206888 Initial import
thib
parents:
diff changeset
770 }
223b71206888 Initial import
thib
parents:
diff changeset
771 return v2;
223b71206888 Initial import
thib
parents:
diff changeset
772 }
223b71206888 Initial import
thib
parents:
diff changeset
773
223b71206888 Initial import
thib
parents:
diff changeset
774 /* 演算子 op := 0x5c <uchar op> */
223b71206888 Initial import
thib
parents:
diff changeset
775 /* 数式 exp: [op] <token> [op <token> [...]] */
223b71206888 Initial import
thib
parents:
diff changeset
776 int Cmd::GetExpression(const char*& d, VarInfo* info_ptr) {
223b71206888 Initial import
thib
parents:
diff changeset
777 #define STACK_DEPTH 1024
223b71206888 Initial import
thib
parents:
diff changeset
778 #define OP_LB 11
223b71206888 Initial import
thib
parents:
diff changeset
779 char op_stack[STACK_DEPTH];
223b71206888 Initial import
thib
parents:
diff changeset
780 int val_stack[STACK_DEPTH];
223b71206888 Initial import
thib
parents:
diff changeset
781 int stack_count = 0;
223b71206888 Initial import
thib
parents:
diff changeset
782
223b71206888 Initial import
thib
parents:
diff changeset
783 // 第一項の読み込み
223b71206888 Initial import
thib
parents:
diff changeset
784 while(*d == 0x28) {
223b71206888 Initial import
thib
parents:
diff changeset
785 d++;
223b71206888 Initial import
thib
parents:
diff changeset
786 dprintf("(");
223b71206888 Initial import
thib
parents:
diff changeset
787 op_stack[stack_count++] = OP_LB;
223b71206888 Initial import
thib
parents:
diff changeset
788 }
223b71206888 Initial import
thib
parents:
diff changeset
789 VarInfo info;
223b71206888 Initial import
thib
parents:
diff changeset
790 int value = GetLeftToken(d, info);
223b71206888 Initial import
thib
parents:
diff changeset
791
223b71206888 Initial import
thib
parents:
diff changeset
792 while(*d == 0x29 && stack_count > 0 && op_stack[stack_count-1] == OP_LB) {
223b71206888 Initial import
thib
parents:
diff changeset
793 d++;
223b71206888 Initial import
thib
parents:
diff changeset
794 dprintf(")");
223b71206888 Initial import
thib
parents:
diff changeset
795 stack_count--;
223b71206888 Initial import
thib
parents:
diff changeset
796 }
223b71206888 Initial import
thib
parents:
diff changeset
797
223b71206888 Initial import
thib
parents:
diff changeset
798 if (*d != 0x5c && stack_count == 0) {
223b71206888 Initial import
thib
parents:
diff changeset
799 if (info_ptr) *info_ptr = info;
223b71206888 Initial import
thib
parents:
diff changeset
800 return value; // 単純なleft-termはここで終了。有効なinfo_ptrを帰す(可能性がある)
223b71206888 Initial import
thib
parents:
diff changeset
801 }
223b71206888 Initial import
thib
parents:
diff changeset
802
223b71206888 Initial import
thib
parents:
diff changeset
803 while(*d == 0x5c) {
223b71206888 Initial import
thib
parents:
diff changeset
804 int op_type = *(unsigned char*)(d+1);
223b71206888 Initial import
thib
parents:
diff changeset
805 d += 2;
223b71206888 Initial import
thib
parents:
diff changeset
806 if (op_type < 70) dprintf("%s",op_str[op_type]);
223b71206888 Initial import
thib
parents:
diff changeset
807 else dprintf("err.");
223b71206888 Initial import
thib
parents:
diff changeset
808 if (op_type >= 10) SetError();
223b71206888 Initial import
thib
parents:
diff changeset
809 int cur_pri = op_pri(op_type);
223b71206888 Initial import
thib
parents:
diff changeset
810 while(stack_count != 0 && op_pri(op_stack[stack_count-1]) <= cur_pri) {
223b71206888 Initial import
thib
parents:
diff changeset
811 // 優先順位の高い、先行する演算を行う
223b71206888 Initial import
thib
parents:
diff changeset
812 value = eval(val_stack[stack_count-1], op_stack[stack_count-1], value);
223b71206888 Initial import
thib
parents:
diff changeset
813 stack_count--;
223b71206888 Initial import
thib
parents:
diff changeset
814 }
223b71206888 Initial import
thib
parents:
diff changeset
815 val_stack[stack_count] = value;
223b71206888 Initial import
thib
parents:
diff changeset
816 op_stack[stack_count++] = op_type;
223b71206888 Initial import
thib
parents:
diff changeset
817 while(*d == 0x28) {
223b71206888 Initial import
thib
parents:
diff changeset
818 d++;
223b71206888 Initial import
thib
parents:
diff changeset
819 dprintf("(");
223b71206888 Initial import
thib
parents:
diff changeset
820 op_stack[stack_count++] = OP_LB;
223b71206888 Initial import
thib
parents:
diff changeset
821 }
223b71206888 Initial import
thib
parents:
diff changeset
822 if (stack_count >= STACK_DEPTH) SetError();
223b71206888 Initial import
thib
parents:
diff changeset
823 value = GetLeftToken(d, info);
223b71206888 Initial import
thib
parents:
diff changeset
824
223b71206888 Initial import
thib
parents:
diff changeset
825 while (*d != 0x5c && stack_count > 0) {
223b71206888 Initial import
thib
parents:
diff changeset
826 // 未実行の演算を終わらせる
223b71206888 Initial import
thib
parents:
diff changeset
827 if (op_stack[stack_count-1] != OP_LB) {
223b71206888 Initial import
thib
parents:
diff changeset
828 value = eval(val_stack[stack_count-1], op_stack[stack_count-1], value);
223b71206888 Initial import
thib
parents:
diff changeset
829 stack_count--;
223b71206888 Initial import
thib
parents:
diff changeset
830 } else if (*d == 0x29) { /* op_stack == OP_LB */
223b71206888 Initial import
thib
parents:
diff changeset
831 // bracket 終端があれば、閉じておく
223b71206888 Initial import
thib
parents:
diff changeset
832 d++;
223b71206888 Initial import
thib
parents:
diff changeset
833 dprintf(")");
223b71206888 Initial import
thib
parents:
diff changeset
834 stack_count--;
223b71206888 Initial import
thib
parents:
diff changeset
835 } else break; // error
223b71206888 Initial import
thib
parents:
diff changeset
836 }
223b71206888 Initial import
thib
parents:
diff changeset
837 }
223b71206888 Initial import
thib
parents:
diff changeset
838 if (stack_count) SetError(); // unbalanced bracket
223b71206888 Initial import
thib
parents:
diff changeset
839 dprintf("(=%d)",value);
223b71206888 Initial import
thib
parents:
diff changeset
840 if (info_ptr) {
223b71206888 Initial import
thib
parents:
diff changeset
841 info_ptr->type = TYPE_VAL;
223b71206888 Initial import
thib
parents:
diff changeset
842 info_ptr->value = value;
223b71206888 Initial import
thib
parents:
diff changeset
843 }
223b71206888 Initial import
thib
parents:
diff changeset
844 return value;
223b71206888 Initial import
thib
parents:
diff changeset
845 }
223b71206888 Initial import
thib
parents:
diff changeset
846
223b71206888 Initial import
thib
parents:
diff changeset
847 // 条件分岐専用に、条件演算と算術演算の混合を検知できる専用ルーチン(本来はGetExpressionで差し支えない)
223b71206888 Initial import
thib
parents:
diff changeset
848 int Cmd::GetExpressionCond(const char*& d) {
223b71206888 Initial import
thib
parents:
diff changeset
849 char op_stack[STACK_DEPTH];
223b71206888 Initial import
thib
parents:
diff changeset
850 int val_stack[STACK_DEPTH];
223b71206888 Initial import
thib
parents:
diff changeset
851 int valattr_stack[STACK_DEPTH];
223b71206888 Initial import
thib
parents:
diff changeset
852 #define ATTR_VAL 0
223b71206888 Initial import
thib
parents:
diff changeset
853 #define ATTR_FLAG 1
223b71206888 Initial import
thib
parents:
diff changeset
854 int stack_count = 0;
223b71206888 Initial import
thib
parents:
diff changeset
855
223b71206888 Initial import
thib
parents:
diff changeset
856 // 第一項の読み込み
223b71206888 Initial import
thib
parents:
diff changeset
857 while(*d == 0x28) {
223b71206888 Initial import
thib
parents:
diff changeset
858 d++;
223b71206888 Initial import
thib
parents:
diff changeset
859 dprintf("(");
223b71206888 Initial import
thib
parents:
diff changeset
860 op_stack[stack_count++] = OP_LB;
223b71206888 Initial import
thib
parents:
diff changeset
861 }
223b71206888 Initial import
thib
parents:
diff changeset
862 VarInfo info;
223b71206888 Initial import
thib
parents:
diff changeset
863 int value = GetLeftToken(d, info);
223b71206888 Initial import
thib
parents:
diff changeset
864 while(*d == 0x29 && stack_count > 0 && op_stack[stack_count-1] == OP_LB) {
223b71206888 Initial import
thib
parents:
diff changeset
865 d++;
223b71206888 Initial import
thib
parents:
diff changeset
866 dprintf(")");
223b71206888 Initial import
thib
parents:
diff changeset
867 stack_count--;
223b71206888 Initial import
thib
parents:
diff changeset
868 }
223b71206888 Initial import
thib
parents:
diff changeset
869 bool valattr = ATTR_VAL;
223b71206888 Initial import
thib
parents:
diff changeset
870
223b71206888 Initial import
thib
parents:
diff changeset
871 while(*d == 0x5c) {
223b71206888 Initial import
thib
parents:
diff changeset
872 int op_type = *(unsigned char*)(d+1);
223b71206888 Initial import
thib
parents:
diff changeset
873 d += 2;
223b71206888 Initial import
thib
parents:
diff changeset
874 if (op_type < 70) dprintf("%s",op_str[op_type]);
223b71206888 Initial import
thib
parents:
diff changeset
875 else dprintf("err.");
223b71206888 Initial import
thib
parents:
diff changeset
876 int cur_pri = op_pri_cond(op_type);
223b71206888 Initial import
thib
parents:
diff changeset
877 while(stack_count != 0 && op_pri_cond(op_stack[stack_count-1]) <= cur_pri) {
223b71206888 Initial import
thib
parents:
diff changeset
878 // 優先順位の高い、先行する演算を行う
223b71206888 Initial import
thib
parents:
diff changeset
879 if (op_stack[stack_count-1] >= 60) {
223b71206888 Initial import
thib
parents:
diff changeset
880 if (valattr_stack[stack_count-1] != ATTR_FLAG || valattr != ATTR_FLAG) SetError();
223b71206888 Initial import
thib
parents:
diff changeset
881 } else {
223b71206888 Initial import
thib
parents:
diff changeset
882 if (valattr_stack[stack_count-1] != ATTR_VAL || valattr != ATTR_VAL) SetError();
223b71206888 Initial import
thib
parents:
diff changeset
883 }
223b71206888 Initial import
thib
parents:
diff changeset
884 value = eval(val_stack[stack_count-1], op_stack[stack_count-1], value);
223b71206888 Initial import
thib
parents:
diff changeset
885 if (op_stack[stack_count-1] >= 40) valattr = ATTR_FLAG;
223b71206888 Initial import
thib
parents:
diff changeset
886 stack_count--;
223b71206888 Initial import
thib
parents:
diff changeset
887 }
223b71206888 Initial import
thib
parents:
diff changeset
888 val_stack[stack_count] = value;
223b71206888 Initial import
thib
parents:
diff changeset
889 valattr_stack[stack_count] = valattr;
223b71206888 Initial import
thib
parents:
diff changeset
890 op_stack[stack_count++] = op_type;
223b71206888 Initial import
thib
parents:
diff changeset
891 while(*d == 0x28) {
223b71206888 Initial import
thib
parents:
diff changeset
892 d++;
223b71206888 Initial import
thib
parents:
diff changeset
893 dprintf("(");
223b71206888 Initial import
thib
parents:
diff changeset
894 op_stack[stack_count++] = OP_LB;
223b71206888 Initial import
thib
parents:
diff changeset
895 }
223b71206888 Initial import
thib
parents:
diff changeset
896 if (stack_count >= STACK_DEPTH) SetError();
223b71206888 Initial import
thib
parents:
diff changeset
897 value = GetLeftToken(d, info);
223b71206888 Initial import
thib
parents:
diff changeset
898 valattr = ATTR_VAL;
223b71206888 Initial import
thib
parents:
diff changeset
899
223b71206888 Initial import
thib
parents:
diff changeset
900 while (*d != 0x5c && stack_count > 0) {
223b71206888 Initial import
thib
parents:
diff changeset
901 // 未実行の演算を終わらせる
223b71206888 Initial import
thib
parents:
diff changeset
902 if (op_stack[stack_count-1] != OP_LB) {
223b71206888 Initial import
thib
parents:
diff changeset
903 if (op_stack[stack_count-1] >= 60) {
223b71206888 Initial import
thib
parents:
diff changeset
904 if (valattr_stack[stack_count-1] != ATTR_FLAG || valattr != ATTR_FLAG) SetError();
223b71206888 Initial import
thib
parents:
diff changeset
905 } else {
223b71206888 Initial import
thib
parents:
diff changeset
906 if (valattr_stack[stack_count-1] != ATTR_VAL || valattr != ATTR_VAL) SetError();
223b71206888 Initial import
thib
parents:
diff changeset
907 }
223b71206888 Initial import
thib
parents:
diff changeset
908 value = eval(val_stack[stack_count-1], op_stack[stack_count-1], value);
223b71206888 Initial import
thib
parents:
diff changeset
909 if (op_stack[stack_count-1] >= 40) valattr = ATTR_FLAG;
223b71206888 Initial import
thib
parents:
diff changeset
910 stack_count--;
223b71206888 Initial import
thib
parents:
diff changeset
911 // bracket 終端があれば、閉じておく
223b71206888 Initial import
thib
parents:
diff changeset
912 } else if (*d == 0x29) { /* op_stack == OP_LB */
223b71206888 Initial import
thib
parents:
diff changeset
913 d++;
223b71206888 Initial import
thib
parents:
diff changeset
914 dprintf(")");
223b71206888 Initial import
thib
parents:
diff changeset
915 stack_count--;
223b71206888 Initial import
thib
parents:
diff changeset
916 } else break; // error
223b71206888 Initial import
thib
parents:
diff changeset
917 }
223b71206888 Initial import
thib
parents:
diff changeset
918 }
223b71206888 Initial import
thib
parents:
diff changeset
919 if (stack_count) SetError(); // unbalanced bracket
223b71206888 Initial import
thib
parents:
diff changeset
920 if (value) dprintf("(=true)");
223b71206888 Initial import
thib
parents:
diff changeset
921 else dprintf("(=false)");
223b71206888 Initial import
thib
parents:
diff changeset
922 return value;
223b71206888 Initial import
thib
parents:
diff changeset
923 }
223b71206888 Initial import
thib
parents:
diff changeset
924
223b71206888 Initial import
thib
parents:
diff changeset
925
223b71206888 Initial import
thib
parents:
diff changeset
926 /*
223b71206888 Initial import
thib
parents:
diff changeset
927 str =
223b71206888 Initial import
thib
parents:
diff changeset
928 arg =
223b71206888 Initial import
thib
parents:
diff changeset
929 args = 0x28 <exp> [[0x2c] <exp> [[0x2c] <exp> [...] ]]
223b71206888 Initial import
thib
parents:
diff changeset
930 */
223b71206888 Initial import
thib
parents:
diff changeset
931
223b71206888 Initial import
thib
parents:
diff changeset
932 int Cmd::GetArgs(const char*& d) {
223b71206888 Initial import
thib
parents:
diff changeset
933 if (*d != 0x28) return 0; /* 引数なし */
223b71206888 Initial import
thib
parents:
diff changeset
934 d++;
223b71206888 Initial import
thib
parents:
diff changeset
935 dprintf("args:");
223b71206888 Initial import
thib
parents:
diff changeset
936 VarInfo var;
223b71206888 Initial import
thib
parents:
diff changeset
937 int i; for (i=0; i<100 ; i++) {
223b71206888 Initial import
thib
parents:
diff changeset
938 /* number, variable, string の種別なく値を得る */
223b71206888 Initial import
thib
parents:
diff changeset
939 if (*d == 0x61) { // よくわからない(智代アフター)
223b71206888 Initial import
thib
parents:
diff changeset
940 dprintf("*%d*",d[1]);
223b71206888 Initial import
thib
parents:
diff changeset
941 d += 2;
223b71206888 Initial import
thib
parents:
diff changeset
942 if (*d == 0x28) {
223b71206888 Initial import
thib
parents:
diff changeset
943 dprintf("{");
223b71206888 Initial import
thib
parents:
diff changeset
944 GetArgs(d); // (A,B,C)節が含まれることがある
223b71206888 Initial import
thib
parents:
diff changeset
945 dprintf("}");
223b71206888 Initial import
thib
parents:
diff changeset
946 } else {
223b71206888 Initial import
thib
parents:
diff changeset
947 dprintf("{}");
223b71206888 Initial import
thib
parents:
diff changeset
948 }
223b71206888 Initial import
thib
parents:
diff changeset
949 } else if (d[0] == 0x0a || d[0] == 0x40) { // よくわからない (Little Busters!)
223b71206888 Initial import
thib
parents:
diff changeset
950 int var;
223b71206888 Initial import
thib
parents:
diff changeset
951 if (system_version == 0) { var = read_little_endian_int(d+1); d += 5;}
223b71206888 Initial import
thib
parents:
diff changeset
952 else { var = read_little_endian_short(d+1); d += 3;}
223b71206888 Initial import
thib
parents:
diff changeset
953 dprintf("line %d; ",var);
223b71206888 Initial import
thib
parents:
diff changeset
954 } else if (*d == 0x24 || (*d == 0x5c && (d[1] == 1 || d[1] == 0)) || *d == 0x28) {
223b71206888 Initial import
thib
parents:
diff changeset
955 GetExpression(d, &var);
223b71206888 Initial import
thib
parents:
diff changeset
956 args.push_back(var);
223b71206888 Initial import
thib
parents:
diff changeset
957 } else if (StrType(d)) {
223b71206888 Initial import
thib
parents:
diff changeset
958 var.type = TYPE_STR;
223b71206888 Initial import
thib
parents:
diff changeset
959 var.value = GetString(d);
223b71206888 Initial import
thib
parents:
diff changeset
960 args.push_back(var);
223b71206888 Initial import
thib
parents:
diff changeset
961 } else SetError();
223b71206888 Initial import
thib
parents:
diff changeset
962 if (*d == 0x29) break;
223b71206888 Initial import
thib
parents:
diff changeset
963 if (*d == 0x2c) {d++;} // 次の arg が演算子で始まる、などがなければ存在しない
223b71206888 Initial import
thib
parents:
diff changeset
964 dprintf(",");
223b71206888 Initial import
thib
parents:
diff changeset
965 }
223b71206888 Initial import
thib
parents:
diff changeset
966 if (*d == 0x29) d++;
223b71206888 Initial import
thib
parents:
diff changeset
967 else SetError();
223b71206888 Initial import
thib
parents:
diff changeset
968 return i;
223b71206888 Initial import
thib
parents:
diff changeset
969 }
223b71206888 Initial import
thib
parents:
diff changeset
970
223b71206888 Initial import
thib
parents:
diff changeset
971 int Cmd::GetArgsSpecial(int normal_args,const char*& d) {
223b71206888 Initial import
thib
parents:
diff changeset
972 if (*d != 0x28) return 0; /* 引数なし */
223b71206888 Initial import
thib
parents:
diff changeset
973 d++;
223b71206888 Initial import
thib
parents:
diff changeset
974 dprintf("args:");
223b71206888 Initial import
thib
parents:
diff changeset
975 int i; for (i=0; i<normal_args; i++) {
223b71206888 Initial import
thib
parents:
diff changeset
976 /* number, variable, string の種別なく値を得る */
223b71206888 Initial import
thib
parents:
diff changeset
977 if (*d == 0x24 || (*d == 0x5c && (d[1] == 1 || d[1] == 0)) || *d == 0x28) {
223b71206888 Initial import
thib
parents:
diff changeset
978 GetExpression(d);
223b71206888 Initial import
thib
parents:
diff changeset
979 } else if (StrType(d)) {
223b71206888 Initial import
thib
parents:
diff changeset
980 GetString(d);
223b71206888 Initial import
thib
parents:
diff changeset
981 } else SetError();
223b71206888 Initial import
thib
parents:
diff changeset
982 if (*d == 0x29) break;
223b71206888 Initial import
thib
parents:
diff changeset
983 if (*d == 0x2c) {d++;} // 次の arg が演算子で始まる、などがなければ存在しない
223b71206888 Initial import
thib
parents:
diff changeset
984 dprintf(",");
223b71206888 Initial import
thib
parents:
diff changeset
985 }
223b71206888 Initial import
thib
parents:
diff changeset
986 for (i=0; i<argc ; i++) {
223b71206888 Initial import
thib
parents:
diff changeset
987 if (*d == 0x28) {
223b71206888 Initial import
thib
parents:
diff changeset
988 /*
223b71206888 Initial import
thib
parents:
diff changeset
989 ** cmd 01-22:0c1c, 01-22:0835
223b71206888 Initial import
thib
parents:
diff changeset
990 ** Princess Bride のカードが落ちるアニメの場面
223b71206888 Initial import
thib
parents:
diff changeset
991 ** なお、_PBCARDANM* の画像はこのコマンドでのみ使われているので、特殊処理として無視することも可能
223b71206888 Initial import
thib
parents:
diff changeset
992 **
223b71206888 Initial import
thib
parents:
diff changeset
993 ** cmd 01-04:0276, 026c, 0270
223b71206888 Initial import
thib
parents:
diff changeset
994 ** 複数の enum が args の数だけ続く処理。特殊処理として分離する
223b71206888 Initial import
thib
parents:
diff changeset
995 */
223b71206888 Initial import
thib
parents:
diff changeset
996 dprintf("enum.<");
223b71206888 Initial import
thib
parents:
diff changeset
997 /* (...) は列挙型 or 構造体の可能性がある */
223b71206888 Initial import
thib
parents:
diff changeset
998 const char* d_orig = d;
223b71206888 Initial import
thib
parents:
diff changeset
999 int pt = args.size(); args.push_back(VarInfo(0));
223b71206888 Initial import
thib
parents:
diff changeset
1000 int count = GetArgs(d);
223b71206888 Initial import
thib
parents:
diff changeset
1001 args[pt] = VarInfo(count);
223b71206888 Initial import
thib
parents:
diff changeset
1002 dprintf(">");
223b71206888 Initial import
thib
parents:
diff changeset
1003 } else if (*d == 0x61 && (d[1] >= 0x00 && d[1] <= 0x04) && d[2] == 0x28 ) {
223b71206888 Initial import
thib
parents:
diff changeset
1004 /* 使われるコマンドは 01-21:004b, 01-28:0064 のいずれか(R,C,PB,LO)
223b71206888 Initial import
thib
parents:
diff changeset
1005 ** それらのコマンドは
223b71206888 Initial import
thib
parents:
diff changeset
1006 ** arg1: 画像ファイル名
223b71206888 Initial import
thib
parents:
diff changeset
1007 ** arg2 : Sel 番号
223b71206888 Initial import
thib
parents:
diff changeset
1008 ** らしく、arg3 以降が 0x61 <00-04> (a,b,c,...) となる(ダンプ上は enum と表記される)
223b71206888 Initial import
thib
parents:
diff changeset
1009 ** () 内の引数はさまざまで、a のみ(画像ファイル名)、
223b71206888 Initial import
thib
parents:
diff changeset
1010 ** a,b b=SEL?
223b71206888 Initial import
thib
parents:
diff changeset
1011 ** a,b,c (b,c)=座標?
223b71206888 Initial import
thib
parents:
diff changeset
1012 ** a,(b,c,d,e,f,g) b-g = src / dest?
223b71206888 Initial import
thib
parents:
diff changeset
1013 ** らしい
223b71206888 Initial import
thib
parents:
diff changeset
1014 */
223b71206888 Initial import
thib
parents:
diff changeset
1015 dprintf("kasane. #%d <",d[1]);
223b71206888 Initial import
thib
parents:
diff changeset
1016 d += 2;
223b71206888 Initial import
thib
parents:
diff changeset
1017 int pt = args.size(); args.push_back(VarInfo(0));
223b71206888 Initial import
thib
parents:
diff changeset
1018 int count = GetArgs(d);
223b71206888 Initial import
thib
parents:
diff changeset
1019 args[pt] = VarInfo(count);
223b71206888 Initial import
thib
parents:
diff changeset
1020 dprintf(">");
223b71206888 Initial import
thib
parents:
diff changeset
1021 } else if (*d == 0x24 || (*d == 0x5c && (d[1] == 1 || d[1] == 0))) {
223b71206888 Initial import
thib
parents:
diff changeset
1022 /* cmd 01-15:0028 ; 始めに 0x24 節があり、続いて 0x28 節になる */
223b71206888 Initial import
thib
parents:
diff changeset
1023 VarInfo var;
223b71206888 Initial import
thib
parents:
diff changeset
1024 GetExpression(d, &var);
223b71206888 Initial import
thib
parents:
diff changeset
1025 args.push_back(var);
223b71206888 Initial import
thib
parents:
diff changeset
1026 i--; // この引数はargc の数には入らない
223b71206888 Initial import
thib
parents:
diff changeset
1027 } else SetError();
223b71206888 Initial import
thib
parents:
diff changeset
1028 if (d[0] == 0x0a || d[0] == 0x40) {
223b71206888 Initial import
thib
parents:
diff changeset
1029 /* cmd 01-15:0028 ; 0x28 節の後に毎回 0x0a 節が来る */
223b71206888 Initial import
thib
parents:
diff changeset
1030 int var;
223b71206888 Initial import
thib
parents:
diff changeset
1031 if (system_version == 0) { var = read_little_endian_int(d+1); d += 5;}
223b71206888 Initial import
thib
parents:
diff changeset
1032 else { var = read_little_endian_short(d+1); d += 3;}
223b71206888 Initial import
thib
parents:
diff changeset
1033 dprintf("line %d; ",var);
223b71206888 Initial import
thib
parents:
diff changeset
1034 }
223b71206888 Initial import
thib
parents:
diff changeset
1035 if (*d == 0x29) break;
223b71206888 Initial import
thib
parents:
diff changeset
1036 if (*d == 0x2c) {d++;} // 次の arg が演算子で始まる、などがなければ存在しない
223b71206888 Initial import
thib
parents:
diff changeset
1037 dprintf(",");
223b71206888 Initial import
thib
parents:
diff changeset
1038 }
223b71206888 Initial import
thib
parents:
diff changeset
1039 if (*d == 0x29) d++;
223b71206888 Initial import
thib
parents:
diff changeset
1040 else SetError();
223b71206888 Initial import
thib
parents:
diff changeset
1041 return 0;
223b71206888 Initial import
thib
parents:
diff changeset
1042 }
223b71206888 Initial import
thib
parents:
diff changeset
1043
223b71206888 Initial import
thib
parents:
diff changeset
1044 /* switch
223b71206888 Initial import
thib
parents:
diff changeset
1045 <exp>
223b71206888 Initial import
thib
parents:
diff changeset
1046 0x7b
223b71206888 Initial import
thib
parents:
diff changeset
1047 <exp> <int>
223b71206888 Initial import
thib
parents:
diff changeset
1048 ...
223b71206888 Initial import
thib
parents:
diff changeset
1049 0x7d
223b71206888 Initial import
thib
parents:
diff changeset
1050 */
223b71206888 Initial import
thib
parents:
diff changeset
1051
223b71206888 Initial import
thib
parents:
diff changeset
1052 int Cmd::GetSwitch(const char*& d) {
223b71206888 Initial import
thib
parents:
diff changeset
1053 if (*d != 0x28) {SetError(); return -1;}
223b71206888 Initial import
thib
parents:
diff changeset
1054 d++;
223b71206888 Initial import
thib
parents:
diff changeset
1055 dprintf("switch. ");
223b71206888 Initial import
thib
parents:
diff changeset
1056 int var = GetExpression(d);
223b71206888 Initial import
thib
parents:
diff changeset
1057 if (*d != 0x29) {SetError(); return -1;}
223b71206888 Initial import
thib
parents:
diff changeset
1058 d++;
223b71206888 Initial import
thib
parents:
diff changeset
1059 dprintf("->\n");
223b71206888 Initial import
thib
parents:
diff changeset
1060 if (*d == 0x7b) {
223b71206888 Initial import
thib
parents:
diff changeset
1061 d++;
223b71206888 Initial import
thib
parents:
diff changeset
1062 } else SetError();
223b71206888 Initial import
thib
parents:
diff changeset
1063
223b71206888 Initial import
thib
parents:
diff changeset
1064 int default_jmp = -1; int jmpto = -1;
223b71206888 Initial import
thib
parents:
diff changeset
1065 int i; for (i=0; i<argc; i++) {
223b71206888 Initial import
thib
parents:
diff changeset
1066 dprintf("\t");
223b71206888 Initial import
thib
parents:
diff changeset
1067 if (*d++ != 0x28) {SetError(); return -1;}
223b71206888 Initial import
thib
parents:
diff changeset
1068 int item = -1; // default
223b71206888 Initial import
thib
parents:
diff changeset
1069 if (*d != 0x29) {
223b71206888 Initial import
thib
parents:
diff changeset
1070 int item = GetExpression(d);
223b71206888 Initial import
thib
parents:
diff changeset
1071 if (*d++ != 0x29) {SetError(); return -1;}
223b71206888 Initial import
thib
parents:
diff changeset
1072 int jmp = read_little_endian_int(d);
223b71206888 Initial import
thib
parents:
diff changeset
1073 if (var == item) {
223b71206888 Initial import
thib
parents:
diff changeset
1074 dprintf("(selected)");
223b71206888 Initial import
thib
parents:
diff changeset
1075 jmpto = jmp;
223b71206888 Initial import
thib
parents:
diff changeset
1076 }
223b71206888 Initial import
thib
parents:
diff changeset
1077 dprintf(" -> %d\n", jmp);
223b71206888 Initial import
thib
parents:
diff changeset
1078 } else {
223b71206888 Initial import
thib
parents:
diff changeset
1079 d++;
223b71206888 Initial import
thib
parents:
diff changeset
1080 default_jmp = read_little_endian_int(d);
223b71206888 Initial import
thib
parents:
diff changeset
1081 }
223b71206888 Initial import
thib
parents:
diff changeset
1082 d += 4;
223b71206888 Initial import
thib
parents:
diff changeset
1083 }
223b71206888 Initial import
thib
parents:
diff changeset
1084 if (default_jmp != -1) {
223b71206888 Initial import
thib
parents:
diff changeset
1085 dprintf("default -> %d\n",default_jmp);
223b71206888 Initial import
thib
parents:
diff changeset
1086 if (jmpto == -1) jmpto = default_jmp;
223b71206888 Initial import
thib
parents:
diff changeset
1087 }
223b71206888 Initial import
thib
parents:
diff changeset
1088 if (*d == 0x7d) {
223b71206888 Initial import
thib
parents:
diff changeset
1089 d++;
223b71206888 Initial import
thib
parents:
diff changeset
1090 } else SetError();
223b71206888 Initial import
thib
parents:
diff changeset
1091 return jmpto;
223b71206888 Initial import
thib
parents:
diff changeset
1092 }
223b71206888 Initial import
thib
parents:
diff changeset
1093 /* simple switch
223b71206888 Initial import
thib
parents:
diff changeset
1094 <exp>
223b71206888 Initial import
thib
parents:
diff changeset
1095 0x7b
223b71206888 Initial import
thib
parents:
diff changeset
1096 <int>
223b71206888 Initial import
thib
parents:
diff changeset
1097 ...
223b71206888 Initial import
thib
parents:
diff changeset
1098 0x7d
223b71206888 Initial import
thib
parents:
diff changeset
1099 */
223b71206888 Initial import
thib
parents:
diff changeset
1100 int Cmd::GetSimpleSwitch(const char*& d) {
223b71206888 Initial import
thib
parents:
diff changeset
1101 if (*d != 0x28) {SetError(); return -1;}
223b71206888 Initial import
thib
parents:
diff changeset
1102 d++;
223b71206888 Initial import
thib
parents:
diff changeset
1103 dprintf("simple switch. ");
223b71206888 Initial import
thib
parents:
diff changeset
1104 int var = GetExpression(d);
223b71206888 Initial import
thib
parents:
diff changeset
1105 if (*d != 0x29) {SetError(); return -1;}
223b71206888 Initial import
thib
parents:
diff changeset
1106 d++;
223b71206888 Initial import
thib
parents:
diff changeset
1107 dprintf(" ->\n");
223b71206888 Initial import
thib
parents:
diff changeset
1108 int jumpto = -1;
223b71206888 Initial import
thib
parents:
diff changeset
1109 if (*d == 0x7b) {
223b71206888 Initial import
thib
parents:
diff changeset
1110 d++;
223b71206888 Initial import
thib
parents:
diff changeset
1111 } else SetError();
223b71206888 Initial import
thib
parents:
diff changeset
1112 int i; for (i=0; i<argc; i++) {
223b71206888 Initial import
thib
parents:
diff changeset
1113 int j = read_little_endian_int(d);
223b71206888 Initial import
thib
parents:
diff changeset
1114 d += 4;
223b71206888 Initial import
thib
parents:
diff changeset
1115 dprintf("\t%d -> %d\n", i+1, j);
223b71206888 Initial import
thib
parents:
diff changeset
1116 if (var == i) jumpto = j;
223b71206888 Initial import
thib
parents:
diff changeset
1117 }
223b71206888 Initial import
thib
parents:
diff changeset
1118 if (*d == 0x7d) {
223b71206888 Initial import
thib
parents:
diff changeset
1119 d++;
223b71206888 Initial import
thib
parents:
diff changeset
1120 } else SetError();
223b71206888 Initial import
thib
parents:
diff changeset
1121 return jumpto;
223b71206888 Initial import
thib
parents:
diff changeset
1122 }
223b71206888 Initial import
thib
parents:
diff changeset
1123
223b71206888 Initial import
thib
parents:
diff changeset
1124 /*
223b71206888 Initial import
thib
parents:
diff changeset
1125 selection
223b71206888 Initial import
thib
parents:
diff changeset
1126 ? <exp>
223b71206888 Initial import
thib
parents:
diff changeset
1127 0x7b
223b71206888 Initial import
thib
parents:
diff changeset
1128 <0x0a|0x40> <ushort | uint>
223b71206888 Initial import
thib
parents:
diff changeset
1129 */
223b71206888 Initial import
thib
parents:
diff changeset
1130 void Cmd::GetSelection(const char*& d) {
223b71206888 Initial import
thib
parents:
diff changeset
1131 dprintf("selection. ");
223b71206888 Initial import
thib
parents:
diff changeset
1132 if (*d == 0x28) {
223b71206888 Initial import
thib
parents:
diff changeset
1133 d++;
223b71206888 Initial import
thib
parents:
diff changeset
1134 GetExpression(d);
223b71206888 Initial import
thib
parents:
diff changeset
1135 if (*d != 0x29) { SetError(); return;}
223b71206888 Initial import
thib
parents:
diff changeset
1136 d++;
223b71206888 Initial import
thib
parents:
diff changeset
1137 }
223b71206888 Initial import
thib
parents:
diff changeset
1138 if (*d == 0x7b) {
223b71206888 Initial import
thib
parents:
diff changeset
1139 d++;
223b71206888 Initial import
thib
parents:
diff changeset
1140 dprintf("{\n\t");
223b71206888 Initial import
thib
parents:
diff changeset
1141 } else SetError();
223b71206888 Initial import
thib
parents:
diff changeset
1142 int arg_count = 0;
223b71206888 Initial import
thib
parents:
diff changeset
1143 string text = "";
223b71206888 Initial import
thib
parents:
diff changeset
1144 int cond_result = false;
223b71206888 Initial import
thib
parents:
diff changeset
1145 int sel_no = 0;
223b71206888 Initial import
thib
parents:
diff changeset
1146 while(*d != 0x7d) {
223b71206888 Initial import
thib
parents:
diff changeset
1147 if (d[0] == 0x0a || d[0] == 0x40) {
223b71206888 Initial import
thib
parents:
diff changeset
1148 int var;
223b71206888 Initial import
thib
parents:
diff changeset
1149 if (system_version == 0) { var = read_little_endian_int(d+1); d += 5;}
223b71206888 Initial import
thib
parents:
diff changeset
1150 else { var = read_little_endian_short(d+1); d += 3;}
223b71206888 Initial import
thib
parents:
diff changeset
1151 dprintf("Line %d; ",var);
223b71206888 Initial import
thib
parents:
diff changeset
1152 if (text.length() != 0) {
223b71206888 Initial import
thib
parents:
diff changeset
1153 if (cond_result) ; // 条件節が true なら表示しない
223b71206888 Initial import
thib
parents:
diff changeset
1154 else {
223b71206888 Initial import
thib
parents:
diff changeset
1155 const char* str = text.c_str();
223b71206888 Initial import
thib
parents:
diff changeset
1156 VarInfo var;
223b71206888 Initial import
thib
parents:
diff changeset
1157 var.type = TYPE_STR;
223b71206888 Initial import
thib
parents:
diff changeset
1158 var.value = CopyString(str);
223b71206888 Initial import
thib
parents:
diff changeset
1159 args.push_back(var);
223b71206888 Initial import
thib
parents:
diff changeset
1160 var.type = TYPE_VAL;
223b71206888 Initial import
thib
parents:
diff changeset
1161 var.value = sel_no;
223b71206888 Initial import
thib
parents:
diff changeset
1162 args.push_back(var);
223b71206888 Initial import
thib
parents:
diff changeset
1163 }
223b71206888 Initial import
thib
parents:
diff changeset
1164 sel_no++;
223b71206888 Initial import
thib
parents:
diff changeset
1165 }
223b71206888 Initial import
thib
parents:
diff changeset
1166 text = "";
223b71206888 Initial import
thib
parents:
diff changeset
1167 cond_result = false;
223b71206888 Initial import
thib
parents:
diff changeset
1168 } else if (d[0] == 0x2c) {
223b71206888 Initial import
thib
parents:
diff changeset
1169 dprintf(":comma:");
223b71206888 Initial import
thib
parents:
diff changeset
1170 } else if (d[0] == 0x28) {
223b71206888 Initial import
thib
parents:
diff changeset
1171 dprintf(":cond:");
223b71206888 Initial import
thib
parents:
diff changeset
1172 d++;
223b71206888 Initial import
thib
parents:
diff changeset
1173 while(d[0] != 0x29) {
223b71206888 Initial import
thib
parents:
diff changeset
1174 int result = GetExpressionCond(d); // PRINT- 節でないばあい、条件表示。次は文字節、またはPRINT節のはず
223b71206888 Initial import
thib
parents:
diff changeset
1175 if (*d == 0x32) { // 0x32 なら、現在の条件節を表示しない
223b71206888 Initial import
thib
parents:
diff changeset
1176 d++; dprintf("##");
223b71206888 Initial import
thib
parents:
diff changeset
1177 cond_result = result;
223b71206888 Initial import
thib
parents:
diff changeset
1178 } else if (*d == 0x31) { // 0x31 なら、現在の条件節を表示する
223b71206888 Initial import
thib
parents:
diff changeset
1179 // Little Busters! : この条件で正しいかは未検証
223b71206888 Initial import
thib
parents:
diff changeset
1180 d++; dprintf("***");
223b71206888 Initial import
thib
parents:
diff changeset
1181 cond_result = !result;
223b71206888 Initial import
thib
parents:
diff changeset
1182 }
223b71206888 Initial import
thib
parents:
diff changeset
1183 dprintf(":");
223b71206888 Initial import
thib
parents:
diff changeset
1184 }
223b71206888 Initial import
thib
parents:
diff changeset
1185 d++;
223b71206888 Initial import
thib
parents:
diff changeset
1186 } else if (StrType(d)) {
223b71206888 Initial import
thib
parents:
diff changeset
1187 int strpt = GetString(d);
223b71206888 Initial import
thib
parents:
diff changeset
1188 text += strheap + strpt;
223b71206888 Initial import
thib
parents:
diff changeset
1189 arg_count++;
223b71206888 Initial import
thib
parents:
diff changeset
1190 dprintf("\n\t");
223b71206888 Initial import
thib
parents:
diff changeset
1191 } else if (*d == 0x23 && strncmp(d,"###PRINT",8) == 0) {
223b71206888 Initial import
thib
parents:
diff changeset
1192 d += 8;
223b71206888 Initial import
thib
parents:
diff changeset
1193 if (d[0] != 0x28) SetError();
223b71206888 Initial import
thib
parents:
diff changeset
1194 else { // 文字変数の内容の表示
223b71206888 Initial import
thib
parents:
diff changeset
1195 d++;
223b71206888 Initial import
thib
parents:
diff changeset
1196 dprintf("Print.");
223b71206888 Initial import
thib
parents:
diff changeset
1197 VarInfo info;
223b71206888 Initial import
thib
parents:
diff changeset
1198 GetLeftToken(d, info);
223b71206888 Initial import
thib
parents:
diff changeset
1199 if (d[0] != 0x29 || info.type == -1) SetError();
223b71206888 Initial import
thib
parents:
diff changeset
1200 d++;
223b71206888 Initial import
thib
parents:
diff changeset
1201 dprintf(";");
223b71206888 Initial import
thib
parents:
diff changeset
1202 // 数値を全角文字に変換して登録
223b71206888 Initial import
thib
parents:
diff changeset
1203 char str[10], str2[20]; // itoa
223b71206888 Initial import
thib
parents:
diff changeset
1204 sprintf(str, "%d", info.value);
223b71206888 Initial import
thib
parents:
diff changeset
1205 int i; for (i=0; str[i] != 0; i++) {
223b71206888 Initial import
thib
parents:
diff changeset
1206 str2[i*2] = 0xa3;
223b71206888 Initial import
thib
parents:
diff changeset
1207 str2[i*2+1] = 0xb0 + str[i]-'0';
223b71206888 Initial import
thib
parents:
diff changeset
1208 }
223b71206888 Initial import
thib
parents:
diff changeset
1209 str2[i*2] = 0;
223b71206888 Initial import
thib
parents:
diff changeset
1210 text += str2;
223b71206888 Initial import
thib
parents:
diff changeset
1211 }
223b71206888 Initial import
thib
parents:
diff changeset
1212 } else { SetError(); break;}
223b71206888 Initial import
thib
parents:
diff changeset
1213 }
223b71206888 Initial import
thib
parents:
diff changeset
1214 d++;
223b71206888 Initial import
thib
parents:
diff changeset
1215 /* @@@ */
223b71206888 Initial import
thib
parents:
diff changeset
1216 /* 一致しない場合があるのでコメントアウト */
223b71206888 Initial import
thib
parents:
diff changeset
1217 // if (arg_count != argc) SetError();
223b71206888 Initial import
thib
parents:
diff changeset
1218 dprintf("\n}\n");
223b71206888 Initial import
thib
parents:
diff changeset
1219 return;
223b71206888 Initial import
thib
parents:
diff changeset
1220 }
223b71206888 Initial import
thib
parents:
diff changeset
1221
223b71206888 Initial import
thib
parents:
diff changeset
1222 static char* op_str3[11] = { "+=", "-=", "*=", "/=", "%=", "&=", "|=", "^=", "<<=", ">>=", "="};
223b71206888 Initial import
thib
parents:
diff changeset
1223 void Cmd::GetCmd(Flags& flags_orig, const char*& d ) {
223b71206888 Initial import
thib
parents:
diff changeset
1224 if (d == 0) { SetError(); return;}
223b71206888 Initial import
thib
parents:
diff changeset
1225 if (cmd_type != CMD_NOP) return;
223b71206888 Initial import
thib
parents:
diff changeset
1226
223b71206888 Initial import
thib
parents:
diff changeset
1227 cmdstr[0] = 0;
223b71206888 Initial import
thib
parents:
diff changeset
1228 rawdata = d;
223b71206888 Initial import
thib
parents:
diff changeset
1229 if (*d == 0x23) { /* コマンド */
223b71206888 Initial import
thib
parents:
diff changeset
1230 cmd_type = CMD_OTHER;
223b71206888 Initial import
thib
parents:
diff changeset
1231 cmd1 = *(unsigned const char*)(d+1);
223b71206888 Initial import
thib
parents:
diff changeset
1232 cmd2 = *(unsigned const char*)(d+2);
223b71206888 Initial import
thib
parents:
diff changeset
1233 cmd3 = read_little_endian_short(d+3);
223b71206888 Initial import
thib
parents:
diff changeset
1234 argc = read_little_endian_short(d+5);
223b71206888 Initial import
thib
parents:
diff changeset
1235 cmd4 = *(unsigned const char*)(d+7);
223b71206888 Initial import
thib
parents:
diff changeset
1236 d += 8;
223b71206888 Initial import
thib
parents:
diff changeset
1237 /* verbose */
223b71206888 Initial import
thib
parents:
diff changeset
1238 // dprintf(" 0x23 - cmd %02x-%02x:%04x:%02x[%2d] \n",cmd1,cmd2,cmd3,cmd4,argc);
223b71206888 Initial import
thib
parents:
diff changeset
1239 sprintf(cmdstr, "%02x-%02x:%04x:%02x",cmd1,cmd2,cmd3,cmd4);
223b71206888 Initial import
thib
parents:
diff changeset
1240 /* 引数を得る */
223b71206888 Initial import
thib
parents:
diff changeset
1241 /* 特殊引数のもの */
223b71206888 Initial import
thib
parents:
diff changeset
1242 int is_special = 0;
223b71206888 Initial import
thib
parents:
diff changeset
1243 if (cmd1 == 0) {
223b71206888 Initial import
thib
parents:
diff changeset
1244 if (cmd2 == 1) {
223b71206888 Initial import
thib
parents:
diff changeset
1245 int jump_arg = -1;
223b71206888 Initial import
thib
parents:
diff changeset
1246 if (cmd3 == 0 || cmd3 == 5) {
223b71206888 Initial import
thib
parents:
diff changeset
1247 /* gosub / goto */
223b71206888 Initial import
thib
parents:
diff changeset
1248 jump_arg =read_little_endian_int(d);
223b71206888 Initial import
thib
parents:
diff changeset
1249 d += 4;
223b71206888 Initial import
thib
parents:
diff changeset
1250 if (cmd3 == 0)
223b71206888 Initial import
thib
parents:
diff changeset
1251 dprintf("\tjmp -> %d\n", jump_arg);
223b71206888 Initial import
thib
parents:
diff changeset
1252 else /* cmd3 == 5 */
223b71206888 Initial import
thib
parents:
diff changeset
1253 dprintf("\tcall -> %d\n", jump_arg);
223b71206888 Initial import
thib
parents:
diff changeset
1254 is_special = 1;
223b71206888 Initial import
thib
parents:
diff changeset
1255 } else if (cmd3 == 1 || cmd3 == 2) {
223b71206888 Initial import
thib
parents:
diff changeset
1256 /* conditional jump (if / unless) */
223b71206888 Initial import
thib
parents:
diff changeset
1257 if (*d++ != 0x28) { SetError(); return;}
223b71206888 Initial import
thib
parents:
diff changeset
1258 dprintf("\t");
223b71206888 Initial import
thib
parents:
diff changeset
1259 int cond = GetExpressionCond(d);
223b71206888 Initial import
thib
parents:
diff changeset
1260 if (cmd3 == 1) cond = !cond; // 逆になる
223b71206888 Initial import
thib
parents:
diff changeset
1261 if (*d++ != 0x29) { SetError(); return; }
223b71206888 Initial import
thib
parents:
diff changeset
1262 int jumpto = read_little_endian_int(d);
223b71206888 Initial import
thib
parents:
diff changeset
1263 d += 4;
223b71206888 Initial import
thib
parents:
diff changeset
1264 dprintf("-> %d\n", jumpto);
223b71206888 Initial import
thib
parents:
diff changeset
1265 if (! cond) jump_arg = jumpto; /* condition が満たされない場合、ジャンプ */
223b71206888 Initial import
thib
parents:
diff changeset
1266 is_special = 1;
223b71206888 Initial import
thib
parents:
diff changeset
1267 } else if (cmd3 == 4) {
223b71206888 Initial import
thib
parents:
diff changeset
1268 /* switch to */
223b71206888 Initial import
thib
parents:
diff changeset
1269 jump_arg = GetSwitch(d);
223b71206888 Initial import
thib
parents:
diff changeset
1270 is_special = 1;
223b71206888 Initial import
thib
parents:
diff changeset
1271 } else if (cmd3 == 8 || cmd3 == 3) {
223b71206888 Initial import
thib
parents:
diff changeset
1272 /* switch to */
223b71206888 Initial import
thib
parents:
diff changeset
1273 jump_arg = GetSimpleSwitch(d);
223b71206888 Initial import
thib
parents:
diff changeset
1274 is_special = 1;
223b71206888 Initial import
thib
parents:
diff changeset
1275 } else if (cmd3 == 16) { // call with parameters
223b71206888 Initial import
thib
parents:
diff changeset
1276 GetArgs(d);
223b71206888 Initial import
thib
parents:
diff changeset
1277 jump_arg = read_little_endian_int(d);
223b71206888 Initial import
thib
parents:
diff changeset
1278 d += 4;
223b71206888 Initial import
thib
parents:
diff changeset
1279 is_special = 1;
223b71206888 Initial import
thib
parents:
diff changeset
1280 } else goto retry;
223b71206888 Initial import
thib
parents:
diff changeset
1281 if (jump_arg == -1) {
223b71206888 Initial import
thib
parents:
diff changeset
1282 cmd_type = CMD_NOP;
223b71206888 Initial import
thib
parents:
diff changeset
1283 }
223b71206888 Initial import
thib
parents:
diff changeset
1284 else {
223b71206888 Initial import
thib
parents:
diff changeset
1285 cmd_type = CMD_JMP;
223b71206888 Initial import
thib
parents:
diff changeset
1286 args.push_back(VarInfo(jump_arg));
223b71206888 Initial import
thib
parents:
diff changeset
1287 }
223b71206888 Initial import
thib
parents:
diff changeset
1288 } else if (cmd2 == 2 && (cmd3 == 0 || cmd3 == 1 || cmd3 == 2 || cmd3 == 3 || cmd3 == 0x0d) ) {
223b71206888 Initial import
thib
parents:
diff changeset
1289 /* selection */
223b71206888 Initial import
thib
parents:
diff changeset
1290 GetSelection(d);
223b71206888 Initial import
thib
parents:
diff changeset
1291 is_special = 1;
223b71206888 Initial import
thib
parents:
diff changeset
1292 }
223b71206888 Initial import
thib
parents:
diff changeset
1293 }
223b71206888 Initial import
thib
parents:
diff changeset
1294 retry:
223b71206888 Initial import
thib
parents:
diff changeset
1295 /* 一般引数のもの */
223b71206888 Initial import
thib
parents:
diff changeset
1296 if (!is_special) {
223b71206888 Initial import
thib
parents:
diff changeset
1297 dprintf(" 0x23 - cmd %02x-%02x:%04x:%02x[%2d] \n",cmd1,cmd2,cmd3,cmd4,argc);
223b71206888 Initial import
thib
parents:
diff changeset
1298 dprintf("\t");
223b71206888 Initial import
thib
parents:
diff changeset
1299 if (cmd1 == 1 && cmd2 == 0x22 && (cmd3 == 0xc1c || cmd3 == 0x835)) GetArgsSpecial(3, d);
223b71206888 Initial import
thib
parents:
diff changeset
1300 else if (cmd1 == 1 && cmd2 == 0x0b && cmd3 == 0x65) GetArgsSpecial(0, d);
223b71206888 Initial import
thib
parents:
diff changeset
1301 else if (cmd1 == 1 && cmd2 == 0x15 && cmd3 == 0x28) GetArgsSpecial(0, d);
223b71206888 Initial import
thib
parents:
diff changeset
1302 else if (cmd1 == 1 && cmd2 == 4 && (cmd3 == 0x26c || cmd3 == 0x26d || cmd3 == 0x270 || cmd3 == 0x276)) GetArgsSpecial(0, d);
223b71206888 Initial import
thib
parents:
diff changeset
1303 else if (cmd1 == 1 && cmd2 == 4 && cmd3 == 0x586) GetArgsSpecial(1, d);
223b71206888 Initial import
thib
parents:
diff changeset
1304 else if (cmd1 == 1 && (cmd2 == 0x21 && cmd3 == 0x4b) || (cmd2 == 0x28 && cmd3 == 0x64)) GetArgsSpecial(2,d);
223b71206888 Initial import
thib
parents:
diff changeset
1305 else GetArgs(d);
223b71206888 Initial import
thib
parents:
diff changeset
1306 dprintf("\n");
223b71206888 Initial import
thib
parents:
diff changeset
1307
223b71206888 Initial import
thib
parents:
diff changeset
1308 }
223b71206888 Initial import
thib
parents:
diff changeset
1309 } else if (*d == 0x24) { /* 代入演算 */
223b71206888 Initial import
thib
parents:
diff changeset
1310 if (d[1] == 0x12 || d[2] != 0x5b) SetError();
223b71206888 Initial import
thib
parents:
diff changeset
1311 dprintf("expr: ");
223b71206888 Initial import
thib
parents:
diff changeset
1312 sprintf(cmdstr, "expr");
223b71206888 Initial import
thib
parents:
diff changeset
1313
223b71206888 Initial import
thib
parents:
diff changeset
1314 VarInfo info;
223b71206888 Initial import
thib
parents:
diff changeset
1315 int value = GetLeftToken(d, info);
223b71206888 Initial import
thib
parents:
diff changeset
1316 if (d[0] != 0x5c) SetError();
223b71206888 Initial import
thib
parents:
diff changeset
1317 int type = d[1];
223b71206888 Initial import
thib
parents:
diff changeset
1318 if (type < 20 || type > 30) SetError();
223b71206888 Initial import
thib
parents:
diff changeset
1319 else dprintf("%s",op_str[type]);
223b71206888 Initial import
thib
parents:
diff changeset
1320 d += 2;
223b71206888 Initial import
thib
parents:
diff changeset
1321 int value2 = GetExpression(d);
223b71206888 Initial import
thib
parents:
diff changeset
1322 // 代入情報を埋め込む
223b71206888 Initial import
thib
parents:
diff changeset
1323 if (type != 30) value2 = eval(value, type-20, value2);
223b71206888 Initial import
thib
parents:
diff changeset
1324 cmd_type = CMD_FLAGS;
223b71206888 Initial import
thib
parents:
diff changeset
1325 args.push_back(info);
223b71206888 Initial import
thib
parents:
diff changeset
1326 args.push_back(value2);
223b71206888 Initial import
thib
parents:
diff changeset
1327 dprintf("\n");
223b71206888 Initial import
thib
parents:
diff changeset
1328 } else if (StrType(d)) { /* 文字出力 */
223b71206888 Initial import
thib
parents:
diff changeset
1329 VarInfo info;
223b71206888 Initial import
thib
parents:
diff changeset
1330 info.type = TYPE_STR;
223b71206888 Initial import
thib
parents:
diff changeset
1331 info.value = GetString(d);
223b71206888 Initial import
thib
parents:
diff changeset
1332 args.push_back(info);
223b71206888 Initial import
thib
parents:
diff changeset
1333 cmd_type = CMD_TEXT;
223b71206888 Initial import
thib
parents:
diff changeset
1334 dprintf("\n");
223b71206888 Initial import
thib
parents:
diff changeset
1335 } else if (*d == 0x0a || *d == 0x40 || *d == 0x21) { /* デバッグ用データと既読フラグ */
223b71206888 Initial import
thib
parents:
diff changeset
1336 cmd_type = CMD_NOP;
223b71206888 Initial import
thib
parents:
diff changeset
1337 if (*d == 0x0a) {
223b71206888 Initial import
thib
parents:
diff changeset
1338 dprintf("line ");
223b71206888 Initial import
thib
parents:
diff changeset
1339 d++;
223b71206888 Initial import
thib
parents:
diff changeset
1340 int l;
223b71206888 Initial import
thib
parents:
diff changeset
1341 if (system_version == 0) {
223b71206888 Initial import
thib
parents:
diff changeset
1342 l = read_little_endian_int(d);
223b71206888 Initial import
thib
parents:
diff changeset
1343 d += 4;
223b71206888 Initial import
thib
parents:
diff changeset
1344 } else {
223b71206888 Initial import
thib
parents:
diff changeset
1345 l = read_little_endian_short(d);
223b71206888 Initial import
thib
parents:
diff changeset
1346 d += 2;
223b71206888 Initial import
thib
parents:
diff changeset
1347 }
223b71206888 Initial import
thib
parents:
diff changeset
1348 dprintf("%d\n", l);
223b71206888 Initial import
thib
parents:
diff changeset
1349 } else { /* 0x40, 0x21 */
223b71206888 Initial import
thib
parents:
diff changeset
1350 // 既読マーカーらしい。エントリーポイントとセーブポイントも使われる。
223b71206888 Initial import
thib
parents:
diff changeset
1351 // RealLive 1.2.5から、0x40はセーブポイント、0x21はエントリーポイント。
223b71206888 Initial import
thib
parents:
diff changeset
1352 // 1.2.5以前、どちらも0x40が使われる。
223b71206888 Initial import
thib
parents:
diff changeset
1353 int kidoku_index;
223b71206888 Initial import
thib
parents:
diff changeset
1354 d++;
223b71206888 Initial import
thib
parents:
diff changeset
1355 if (system_version == 0) {
223b71206888 Initial import
thib
parents:
diff changeset
1356 kidoku_index = read_little_endian_int(d);
223b71206888 Initial import
thib
parents:
diff changeset
1357 d += 4;
223b71206888 Initial import
thib
parents:
diff changeset
1358 } else {
223b71206888 Initial import
thib
parents:
diff changeset
1359 kidoku_index = read_little_endian_short(d);
223b71206888 Initial import
thib
parents:
diff changeset
1360 d += 2;
223b71206888 Initial import
thib
parents:
diff changeset
1361 }
223b71206888 Initial import
thib
parents:
diff changeset
1362 dprintf("kidoku marker %d\n", kidoku_index);
223b71206888 Initial import
thib
parents:
diff changeset
1363 // text_readflagは、このkidoku_indexを使ったら良いかな。
223b71206888 Initial import
thib
parents:
diff changeset
1364 }
223b71206888 Initial import
thib
parents:
diff changeset
1365 } else if (*d == 0x2c) { /* ??? */
223b71206888 Initial import
thib
parents:
diff changeset
1366 dprintf("commd;0x2c\n"); // conditional jump の行き先によくあるらしい(常に、かはわからない)
223b71206888 Initial import
thib
parents:
diff changeset
1367 d++;
223b71206888 Initial import
thib
parents:
diff changeset
1368 } else {
223b71206888 Initial import
thib
parents:
diff changeset
1369 SetError();
223b71206888 Initial import
thib
parents:
diff changeset
1370 }
223b71206888 Initial import
thib
parents:
diff changeset
1371 return;
223b71206888 Initial import
thib
parents:
diff changeset
1372 }
223b71206888 Initial import
thib
parents:
diff changeset
1373 void Cmd::clear(void) {
223b71206888 Initial import
thib
parents:
diff changeset
1374 cmd_type = CMD_NOP;
223b71206888 Initial import
thib
parents:
diff changeset
1375 ResetString();
223b71206888 Initial import
thib
parents:
diff changeset
1376 args.clear();
223b71206888 Initial import
thib
parents:
diff changeset
1377 errorflag = false;
223b71206888 Initial import
thib
parents:
diff changeset
1378 pos = -1;
223b71206888 Initial import
thib
parents:
diff changeset
1379 }
223b71206888 Initial import
thib
parents:
diff changeset
1380
223b71206888 Initial import
thib
parents:
diff changeset
1381 char Cmd::strtype[256] = {
223b71206888 Initial import
thib
parents:
diff changeset
1382 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, /* +00 */
223b71206888 Initial import
thib
parents:
diff changeset
1383 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, /* +10 */ // 0123456789ABCDEF
223b71206888 Initial import
thib
parents:
diff changeset
1384 1,0,3,0, 0,0,0,1, 0,0,0,0, 0,1,1,0, /* +20 */ // !"#$%&'()*+,-./
223b71206888 Initial import
thib
parents:
diff changeset
1385 1,1,1,1, 1,1,1,1, 1,1,1,0, 0,0,0,1, /* +30 */ // 0123456789:;<=>?
223b71206888 Initial import
thib
parents:
diff changeset
1386 0,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, /* +40 */ // @ABCDEFGHIJKLMNO
223b71206888 Initial import
thib
parents:
diff changeset
1387 1,1,1,1, 1,1,1,1, 1,1,1,0, 0,0,0,1, /* +50 */ // PQRSTUVWXYZ[\]^_
223b71206888 Initial import
thib
parents:
diff changeset
1388 0,0,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, /* +60 */ // `abcdefghijklmno
223b71206888 Initial import
thib
parents:
diff changeset
1389 1,1,1,1, 1,1,1,1, 1,1,1,1, 0,0,0,0, /* +70 */ // pqrstuvwxyz{|}~
223b71206888 Initial import
thib
parents:
diff changeset
1390 2,2,2,2, 2,2,2,2, 2,2,2,2, 2,2,2,2, /* +80 */
223b71206888 Initial import
thib
parents:
diff changeset
1391 2,2,2,2, 2,2,2,2, 2,2,2,2, 2,2,2,2, /* +90 */
223b71206888 Initial import
thib
parents:
diff changeset
1392 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, /* +A0 */
223b71206888 Initial import
thib
parents:
diff changeset
1393 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, /* +B0 */
223b71206888 Initial import
thib
parents:
diff changeset
1394 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, /* +C0 */
223b71206888 Initial import
thib
parents:
diff changeset
1395 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, /* +D0 */
223b71206888 Initial import
thib
parents:
diff changeset
1396 2,2,2,2, 2,2,2,2, 2,2,2,2, 2,2,2,2, /* +E0 */
223b71206888 Initial import
thib
parents:
diff changeset
1397 2,2,2,2, 2,2,2,2, 2,2,2,2, 2,2,0,0 /* +F0 */
223b71206888 Initial import
thib
parents:
diff changeset
1398 };
223b71206888 Initial import
thib
parents:
diff changeset
1399
223b71206888 Initial import
thib
parents:
diff changeset
1400 int Cmd::GetString(const char*& d) {
223b71206888 Initial import
thib
parents:
diff changeset
1401 int retnum = -1;
223b71206888 Initial import
thib
parents:
diff changeset
1402 bool quote_flag = false;
223b71206888 Initial import
thib
parents:
diff changeset
1403 int stype;
223b71206888 Initial import
thib
parents:
diff changeset
1404 retnum = strend;
223b71206888 Initial import
thib
parents:
diff changeset
1405 while(1) {
223b71206888 Initial import
thib
parents:
diff changeset
1406 if (*d == '\\') {
223b71206888 Initial import
thib
parents:
diff changeset
1407 d++;
223b71206888 Initial import
thib
parents:
diff changeset
1408 strheap[strend++] = *d++;
223b71206888 Initial import
thib
parents:
diff changeset
1409 } else if (*d == '"') {
223b71206888 Initial import
thib
parents:
diff changeset
1410 if (quote_flag) quote_flag = false;
223b71206888 Initial import
thib
parents:
diff changeset
1411 else quote_flag = true;
223b71206888 Initial import
thib
parents:
diff changeset
1412 d++;
223b71206888 Initial import
thib
parents:
diff changeset
1413 } else if (quote_flag) {
223b71206888 Initial import
thib
parents:
diff changeset
1414 strheap[strend++] = *d++;
223b71206888 Initial import
thib
parents:
diff changeset
1415 } else if (stype = StrType(d)) {
223b71206888 Initial import
thib
parents:
diff changeset
1416 strheap[strend++] = *d++;
223b71206888 Initial import
thib
parents:
diff changeset
1417 if (stype == 2) strheap[strend++] = *d++;
223b71206888 Initial import
thib
parents:
diff changeset
1418 } else break;
223b71206888 Initial import
thib
parents:
diff changeset
1419 }
223b71206888 Initial import
thib
parents:
diff changeset
1420 strheap[strend++] = 0;
223b71206888 Initial import
thib
parents:
diff changeset
1421 dprintf("\"%s\"", strheap + retnum);
223b71206888 Initial import
thib
parents:
diff changeset
1422 if (strend >= STRHEAP_SIZE) {
223b71206888 Initial import
thib
parents:
diff changeset
1423 dprintf("Error: string heap overflow\n");
223b71206888 Initial import
thib
parents:
diff changeset
1424 }
223b71206888 Initial import
thib
parents:
diff changeset
1425 return retnum;
223b71206888 Initial import
thib
parents:
diff changeset
1426 }
223b71206888 Initial import
thib
parents:
diff changeset
1427
223b71206888 Initial import
thib
parents:
diff changeset
1428 int Cmd::CopyString(const char* d) {
223b71206888 Initial import
thib
parents:
diff changeset
1429 int retnum = strend;
223b71206888 Initial import
thib
parents:
diff changeset
1430 int len = strlen(d);
223b71206888 Initial import
thib
parents:
diff changeset
1431 memcpy(strheap+strend, d, len+1);
223b71206888 Initial import
thib
parents:
diff changeset
1432 strend += len+1;
223b71206888 Initial import
thib
parents:
diff changeset
1433 d += len+1;
223b71206888 Initial import
thib
parents:
diff changeset
1434 return retnum;
223b71206888 Initial import
thib
parents:
diff changeset
1435 }
223b71206888 Initial import
thib
parents:
diff changeset
1436
223b71206888 Initial import
thib
parents:
diff changeset
1437 int Cmd::StrVar(int type, int var_num) {
223b71206888 Initial import
thib
parents:
diff changeset
1438 int retnum = strend;
223b71206888 Initial import
thib
parents:
diff changeset
1439 flags.Str(type, var_num, strheap+strend, STRHEAP_SIZE-strend);
223b71206888 Initial import
thib
parents:
diff changeset
1440 strend += strlen(strheap+strend)+1;
223b71206888 Initial import
thib
parents:
diff changeset
1441 return retnum;
223b71206888 Initial import
thib
parents:
diff changeset
1442 }
223b71206888 Initial import
thib
parents:
diff changeset
1443
223b71206888 Initial import
thib
parents:
diff changeset
1444 void Cmd::SetSysvar(int n, int val) {
223b71206888 Initial import
thib
parents:
diff changeset
1445 VarInfo info;
223b71206888 Initial import
thib
parents:
diff changeset
1446 if (cmd_type != CMD_SYSVAR) {
223b71206888 Initial import
thib
parents:
diff changeset
1447 args.clear();
223b71206888 Initial import
thib
parents:
diff changeset
1448 }
223b71206888 Initial import
thib
parents:
diff changeset
1449 cmd_type = CMD_SYSVAR;
223b71206888 Initial import
thib
parents:
diff changeset
1450
223b71206888 Initial import
thib
parents:
diff changeset
1451 info.type = TYPE_SYS;
223b71206888 Initial import
thib
parents:
diff changeset
1452 info.number = n;
223b71206888 Initial import
thib
parents:
diff changeset
1453 info.value = val;
223b71206888 Initial import
thib
parents:
diff changeset
1454 args.push_back(info);
223b71206888 Initial import
thib
parents:
diff changeset
1455 }
223b71206888 Initial import
thib
parents:
diff changeset
1456 void Cmd::SetFlagvar(VarInfo info, int val) {
223b71206888 Initial import
thib
parents:
diff changeset
1457 if (cmd_type != CMD_SYSVAR) {
223b71206888 Initial import
thib
parents:
diff changeset
1458 args.clear();
223b71206888 Initial import
thib
parents:
diff changeset
1459 }
223b71206888 Initial import
thib
parents:
diff changeset
1460 cmd_type = CMD_SYSVAR;
223b71206888 Initial import
thib
parents:
diff changeset
1461
223b71206888 Initial import
thib
parents:
diff changeset
1462 info.value = val;
223b71206888 Initial import
thib
parents:
diff changeset
1463 args.push_back(info);
223b71206888 Initial import
thib
parents:
diff changeset
1464 }
223b71206888 Initial import
thib
parents:
diff changeset
1465
223b71206888 Initial import
thib
parents:
diff changeset
1466 void Cmd::SetStrvar(VarInfo info, const string& s) {
223b71206888 Initial import
thib
parents:
diff changeset
1467 if (cmd_type != CMD_SYSVAR) {
223b71206888 Initial import
thib
parents:
diff changeset
1468 args.clear();
223b71206888 Initial import
thib
parents:
diff changeset
1469 }
223b71206888 Initial import
thib
parents:
diff changeset
1470
223b71206888 Initial import
thib
parents:
diff changeset
1471 cmd_type = CMD_SYSVAR;
223b71206888 Initial import
thib
parents:
diff changeset
1472 const char* ss = s.c_str();
223b71206888 Initial import
thib
parents:
diff changeset
1473 info.value = CopyString(ss);
223b71206888 Initial import
thib
parents:
diff changeset
1474 args.push_back(info);
223b71206888 Initial import
thib
parents:
diff changeset
1475 }
223b71206888 Initial import
thib
parents:
diff changeset
1476
223b71206888 Initial import
thib
parents:
diff changeset
1477 void Cmd::read(const CmdSimplified& from) {
223b71206888 Initial import
thib
parents:
diff changeset
1478 errorflag = false;
223b71206888 Initial import
thib
parents:
diff changeset
1479 ResetString();
223b71206888 Initial import
thib
parents:
diff changeset
1480
223b71206888 Initial import
thib
parents:
diff changeset
1481 cmd_type = Cmdtype(from.type);
223b71206888 Initial import
thib
parents:
diff changeset
1482 cmd1 = from.cmd1;
223b71206888 Initial import
thib
parents:
diff changeset
1483 cmd2 = from.cmd2;
223b71206888 Initial import
thib
parents:
diff changeset
1484 cmd3 = from.cmd3;
223b71206888 Initial import
thib
parents:
diff changeset
1485 cmd4 = from.cmd4;
223b71206888 Initial import
thib
parents:
diff changeset
1486 argc = from.argc;
223b71206888 Initial import
thib
parents:
diff changeset
1487 /* args の読み込み */
223b71206888 Initial import
thib
parents:
diff changeset
1488 args.clear();
223b71206888 Initial import
thib
parents:
diff changeset
1489 char* d = from.args;
223b71206888 Initial import
thib
parents:
diff changeset
1490 if (d == 0) return;
223b71206888 Initial import
thib
parents:
diff changeset
1491 while(*d != TYPE_END) {
223b71206888 Initial import
thib
parents:
diff changeset
1492 VarInfo info;
223b71206888 Initial import
thib
parents:
diff changeset
1493 switch(*d) {
223b71206888 Initial import
thib
parents:
diff changeset
1494 case TYPE_VAL:
223b71206888 Initial import
thib
parents:
diff changeset
1495 info.type = TYPE_VAL;
223b71206888 Initial import
thib
parents:
diff changeset
1496 info.number = 0;
223b71206888 Initial import
thib
parents:
diff changeset
1497 info.value = read_little_endian_int(d+1);
223b71206888 Initial import
thib
parents:
diff changeset
1498 d += 5;
223b71206888 Initial import
thib
parents:
diff changeset
1499 args.push_back(info);
223b71206888 Initial import
thib
parents:
diff changeset
1500 break;
223b71206888 Initial import
thib
parents:
diff changeset
1501 case TYPE_STR:
223b71206888 Initial import
thib
parents:
diff changeset
1502 info.type = TYPE_STR;
223b71206888 Initial import
thib
parents:
diff changeset
1503 info.number = 0;
223b71206888 Initial import
thib
parents:
diff changeset
1504 d++;
223b71206888 Initial import
thib
parents:
diff changeset
1505 info.value = CopyString( d);
223b71206888 Initial import
thib
parents:
diff changeset
1506 d += strlen(d)+1;
223b71206888 Initial import
thib
parents:
diff changeset
1507 args.push_back(info);
223b71206888 Initial import
thib
parents:
diff changeset
1508 break;
223b71206888 Initial import
thib
parents:
diff changeset
1509 default:
223b71206888 Initial import
thib
parents:
diff changeset
1510 fprintf(stderr,"Cmd::read: Invalid Load Data\n");
223b71206888 Initial import
thib
parents:
diff changeset
1511 *d = TYPE_END;
223b71206888 Initial import
thib
parents:
diff changeset
1512 }
223b71206888 Initial import
thib
parents:
diff changeset
1513 }
223b71206888 Initial import
thib
parents:
diff changeset
1514 return;
223b71206888 Initial import
thib
parents:
diff changeset
1515 }
223b71206888 Initial import
thib
parents:
diff changeset
1516 void Cmd::write(CmdSimplified& to, char*& buffer) const {
223b71206888 Initial import
thib
parents:
diff changeset
1517 /*
223b71206888 Initial import
thib
parents:
diff changeset
1518 if (cmd_type != CMD_OTHER) {
223b71206888 Initial import
thib
parents:
diff changeset
1519 fprintf(stderr,"Cmd::write: Invalid Cmd during Saving Data\n");
223b71206888 Initial import
thib
parents:
diff changeset
1520 to.cmd1 = 0; to.cmd2 = 0; to.cmd3 = 0; to.cmd4 = 0; to.argc = 0; to.args = 0;
223b71206888 Initial import
thib
parents:
diff changeset
1521 return;
223b71206888 Initial import
thib
parents:
diff changeset
1522 }
223b71206888 Initial import
thib
parents:
diff changeset
1523 */
223b71206888 Initial import
thib
parents:
diff changeset
1524 to.type = cmd_type;
223b71206888 Initial import
thib
parents:
diff changeset
1525 to.cmd1 = cmd1;
223b71206888 Initial import
thib
parents:
diff changeset
1526 to.cmd2 = cmd2;
223b71206888 Initial import
thib
parents:
diff changeset
1527 to.cmd3 = cmd3;
223b71206888 Initial import
thib
parents:
diff changeset
1528 to.cmd4 = cmd4;
223b71206888 Initial import
thib
parents:
diff changeset
1529 to.argc = argc;
223b71206888 Initial import
thib
parents:
diff changeset
1530 /* args の書き込み */
223b71206888 Initial import
thib
parents:
diff changeset
1531 if (args.empty()) {
223b71206888 Initial import
thib
parents:
diff changeset
1532 to.args = 0;
223b71206888 Initial import
thib
parents:
diff changeset
1533 } else {
223b71206888 Initial import
thib
parents:
diff changeset
1534 to.args = buffer;
223b71206888 Initial import
thib
parents:
diff changeset
1535 char* d = to.args;
223b71206888 Initial import
thib
parents:
diff changeset
1536 vector<VarInfo>::const_iterator it;
223b71206888 Initial import
thib
parents:
diff changeset
1537 for (it = args.begin(); it != args.end(); it++) {
223b71206888 Initial import
thib
parents:
diff changeset
1538 int type = it->type;
223b71206888 Initial import
thib
parents:
diff changeset
1539 if ( (type >= 0 && type < 7) || type == TYPE_VAL || type == char(TYPE_SYS)) { // digits
223b71206888 Initial import
thib
parents:
diff changeset
1540 *d++ = TYPE_VAL;
223b71206888 Initial import
thib
parents:
diff changeset
1541 write_little_endian_int(d, it->value);
223b71206888 Initial import
thib
parents:
diff changeset
1542 d += 4;
223b71206888 Initial import
thib
parents:
diff changeset
1543 } else if (type == TYPE_VARSTR || type == TYPE_VARSYSSTR || type == TYPE_VARLOCSTR || type == TYPE_STR) { // string
223b71206888 Initial import
thib
parents:
diff changeset
1544 *d++ = TYPE_STR;
223b71206888 Initial import
thib
parents:
diff changeset
1545 const char* s = Str(*it);
223b71206888 Initial import
thib
parents:
diff changeset
1546 int len = strlen(s);
223b71206888 Initial import
thib
parents:
diff changeset
1547 memcpy(d, s, len+1);
223b71206888 Initial import
thib
parents:
diff changeset
1548 d += len+1;
223b71206888 Initial import
thib
parents:
diff changeset
1549 } else {
223b71206888 Initial import
thib
parents:
diff changeset
1550 fprintf(stderr,"Cmd::write: Invalid Cmd args during Saving Data\n");
223b71206888 Initial import
thib
parents:
diff changeset
1551 }
223b71206888 Initial import
thib
parents:
diff changeset
1552 }
223b71206888 Initial import
thib
parents:
diff changeset
1553 *d++ = TYPE_END;
223b71206888 Initial import
thib
parents:
diff changeset
1554 buffer = d;
223b71206888 Initial import
thib
parents:
diff changeset
1555 }
223b71206888 Initial import
thib
parents:
diff changeset
1556 }
223b71206888 Initial import
thib
parents:
diff changeset
1557 void CmdSimplified::copy(const CmdSimplified& from, char*& args_buffer) {
223b71206888 Initial import
thib
parents:
diff changeset
1558 *this = from;
223b71206888 Initial import
thib
parents:
diff changeset
1559 if (args == 0) return;
223b71206888 Initial import
thib
parents:
diff changeset
1560 char* args_old = from.args;
223b71206888 Initial import
thib
parents:
diff changeset
1561 /* args のコピー */
223b71206888 Initial import
thib
parents:
diff changeset
1562 while(*args_old != TYPE_END) {
223b71206888 Initial import
thib
parents:
diff changeset
1563 if (*args_old == TYPE_VAL) {
223b71206888 Initial import
thib
parents:
diff changeset
1564 args_old += 5;
223b71206888 Initial import
thib
parents:
diff changeset
1565 } else { /* TYPE_STR */
223b71206888 Initial import
thib
parents:
diff changeset
1566 args_old += strlen(args_old)+1;
223b71206888 Initial import
thib
parents:
diff changeset
1567 }
223b71206888 Initial import
thib
parents:
diff changeset
1568 }
223b71206888 Initial import
thib
parents:
diff changeset
1569 args_old++;
223b71206888 Initial import
thib
parents:
diff changeset
1570 int args_len = args_old - from.args;
223b71206888 Initial import
thib
parents:
diff changeset
1571 memmove(args_buffer, from.args, args_len);
223b71206888 Initial import
thib
parents:
diff changeset
1572 args = args_buffer;
223b71206888 Initial import
thib
parents:
diff changeset
1573 args_buffer += args_len;
223b71206888 Initial import
thib
parents:
diff changeset
1574 }
223b71206888 Initial import
thib
parents:
diff changeset
1575 void CmdSimplified::Save(string& saveret) {
223b71206888 Initial import
thib
parents:
diff changeset
1576 char buf[1024];
223b71206888 Initial import
thib
parents:
diff changeset
1577 sprintf(buf, "%02x-%02x:%04x:%02x(%02d),", cmd1, cmd2, cmd3, cmd4, argc);
223b71206888 Initial import
thib
parents:
diff changeset
1578 saveret += buf;
223b71206888 Initial import
thib
parents:
diff changeset
1579
223b71206888 Initial import
thib
parents:
diff changeset
1580 /* args のコピー */
223b71206888 Initial import
thib
parents:
diff changeset
1581 char* d = args;
223b71206888 Initial import
thib
parents:
diff changeset
1582 while(d && *d != TYPE_END) {
223b71206888 Initial import
thib
parents:
diff changeset
1583 if (*d == TYPE_VAL) {
223b71206888 Initial import
thib
parents:
diff changeset
1584 d++;
223b71206888 Initial import
thib
parents:
diff changeset
1585 sprintf(buf, "%d,", read_little_endian_int(d));
223b71206888 Initial import
thib
parents:
diff changeset
1586 d += 4;
223b71206888 Initial import
thib
parents:
diff changeset
1587 } else { /* TYPE_STR と仮定 */
223b71206888 Initial import
thib
parents:
diff changeset
1588 d++;
223b71206888 Initial import
thib
parents:
diff changeset
1589 if (strlen(d) > 1000) d[1000] = 0; // ありえない・・・
223b71206888 Initial import
thib
parents:
diff changeset
1590 int i; int cnt = 0;
223b71206888 Initial import
thib
parents:
diff changeset
1591 buf[cnt++] = '"';
223b71206888 Initial import
thib
parents:
diff changeset
1592 for (i=0; d[i] != 0; i++) {
223b71206888 Initial import
thib
parents:
diff changeset
1593 if (d[i] == '"') buf[cnt++] = '"';
223b71206888 Initial import
thib
parents:
diff changeset
1594 buf[cnt++] = d[i];
223b71206888 Initial import
thib
parents:
diff changeset
1595 }
223b71206888 Initial import
thib
parents:
diff changeset
1596 buf[cnt++]='"';
223b71206888 Initial import
thib
parents:
diff changeset
1597 buf[cnt++] = ',';
223b71206888 Initial import
thib
parents:
diff changeset
1598 buf[cnt++] = 0;
223b71206888 Initial import
thib
parents:
diff changeset
1599 d += strlen(d)+1;
223b71206888 Initial import
thib
parents:
diff changeset
1600 }
223b71206888 Initial import
thib
parents:
diff changeset
1601 saveret += buf;
223b71206888 Initial import
thib
parents:
diff changeset
1602 }
223b71206888 Initial import
thib
parents:
diff changeset
1603 saveret += 'E';
223b71206888 Initial import
thib
parents:
diff changeset
1604 }
223b71206888 Initial import
thib
parents:
diff changeset
1605
223b71206888 Initial import
thib
parents:
diff changeset
1606 void CmdSimplified::Load(const char* save, char*& args_buffer) {
223b71206888 Initial import
thib
parents:
diff changeset
1607 args = args_buffer;
223b71206888 Initial import
thib
parents:
diff changeset
1608
223b71206888 Initial import
thib
parents:
diff changeset
1609 type = CMD_OTHER;
223b71206888 Initial import
thib
parents:
diff changeset
1610 sscanf(save, "%02x-%02x:%04x:%02x(%02d),", &cmd1, &cmd2, &cmd3, &cmd4, &argc);
223b71206888 Initial import
thib
parents:
diff changeset
1611 save = strchr(save, ',');
223b71206888 Initial import
thib
parents:
diff changeset
1612 if (save == 0) {
223b71206888 Initial import
thib
parents:
diff changeset
1613 *args_buffer++ = TYPE_END;
223b71206888 Initial import
thib
parents:
diff changeset
1614 return;
223b71206888 Initial import
thib
parents:
diff changeset
1615 }
223b71206888 Initial import
thib
parents:
diff changeset
1616 save++;
223b71206888 Initial import
thib
parents:
diff changeset
1617 while(*save != 'E' && *save != '\n' && *save != '\0') {
223b71206888 Initial import
thib
parents:
diff changeset
1618 if (isdigit(*save)) {
223b71206888 Initial import
thib
parents:
diff changeset
1619 int v;
223b71206888 Initial import
thib
parents:
diff changeset
1620 sscanf(save,"%d,",&v);
223b71206888 Initial import
thib
parents:
diff changeset
1621 *args_buffer++ = TYPE_VAL;
223b71206888 Initial import
thib
parents:
diff changeset
1622 write_little_endian_int(args_buffer, v);
223b71206888 Initial import
thib
parents:
diff changeset
1623 args_buffer+= 4;
223b71206888 Initial import
thib
parents:
diff changeset
1624 save = strchr(save, ',');
223b71206888 Initial import
thib
parents:
diff changeset
1625 if (save) save++;
223b71206888 Initial import
thib
parents:
diff changeset
1626 } else { // *save == '"'
223b71206888 Initial import
thib
parents:
diff changeset
1627 save++;
223b71206888 Initial import
thib
parents:
diff changeset
1628 *args_buffer++ = TYPE_STR;
223b71206888 Initial import
thib
parents:
diff changeset
1629 while(1) {
223b71206888 Initial import
thib
parents:
diff changeset
1630 if (*save == 0) break;
223b71206888 Initial import
thib
parents:
diff changeset
1631 if (*save == '"') {
223b71206888 Initial import
thib
parents:
diff changeset
1632 if (save[1] != '"') break;
223b71206888 Initial import
thib
parents:
diff changeset
1633 save++;
223b71206888 Initial import
thib
parents:
diff changeset
1634 }
223b71206888 Initial import
thib
parents:
diff changeset
1635 *args_buffer++ = *save++;
223b71206888 Initial import
thib
parents:
diff changeset
1636 }
223b71206888 Initial import
thib
parents:
diff changeset
1637 save += 2;
223b71206888 Initial import
thib
parents:
diff changeset
1638 *args_buffer++ = 0;
223b71206888 Initial import
thib
parents:
diff changeset
1639 }
223b71206888 Initial import
thib
parents:
diff changeset
1640 }
223b71206888 Initial import
thib
parents:
diff changeset
1641 *args_buffer++ = TYPE_END;
223b71206888 Initial import
thib
parents:
diff changeset
1642 return;
223b71206888 Initial import
thib
parents:
diff changeset
1643 }
223b71206888 Initial import
thib
parents:
diff changeset
1644
223b71206888 Initial import
thib
parents:
diff changeset
1645 #ifdef SCN_DUMP
223b71206888 Initial import
thib
parents:
diff changeset
1646 void usage(void) {
223b71206888 Initial import
thib
parents:
diff changeset
1647 fprintf(stderr,"usage : scn2kdump [inputfile] [outputfile]\n");
223b71206888 Initial import
thib
parents:
diff changeset
1648 fprintf(stderr," inputfile: seen.txt(default)\n");
223b71206888 Initial import
thib
parents:
diff changeset
1649 fprintf(stderr," outputfile: seen.txt_out(default)\n");
223b71206888 Initial import
thib
parents:
diff changeset
1650 exit(-1);
223b71206888 Initial import
thib
parents:
diff changeset
1651 }
223b71206888 Initial import
thib
parents:
diff changeset
1652 int main(int argc, char** argv) {
223b71206888 Initial import
thib
parents:
diff changeset
1653 /* determine file names */
223b71206888 Initial import
thib
parents:
diff changeset
1654 bool verbose = false;
223b71206888 Initial import
thib
parents:
diff changeset
1655 char* inname = "seen.txt";
223b71206888 Initial import
thib
parents:
diff changeset
1656 char* outname = 0;
223b71206888 Initial import
thib
parents:
diff changeset
1657 if (argc > 2 && strcmp(argv[1],"-v") == 0) {
223b71206888 Initial import
thib
parents:
diff changeset
1658 int i; for (i=1; i<argc; i++) argv[i] = argv[i+1];
223b71206888 Initial import
thib
parents:
diff changeset
1659 argc--;
223b71206888 Initial import
thib
parents:
diff changeset
1660 verbose = true;
223b71206888 Initial import
thib
parents:
diff changeset
1661 }
223b71206888 Initial import
thib
parents:
diff changeset
1662 switch(argc) {
223b71206888 Initial import
thib
parents:
diff changeset
1663 case 1: break;
223b71206888 Initial import
thib
parents:
diff changeset
1664 case 2: inname = argv[1]; break;
223b71206888 Initial import
thib
parents:
diff changeset
1665 case 3: inname = argv[1]; outname = argv[2]; break;
223b71206888 Initial import
thib
parents:
diff changeset
1666 default: usage();
223b71206888 Initial import
thib
parents:
diff changeset
1667 }
223b71206888 Initial import
thib
parents:
diff changeset
1668 /* open output file */
223b71206888 Initial import
thib
parents:
diff changeset
1669 FILE* outstream = stdout;
223b71206888 Initial import
thib
parents:
diff changeset
1670 /* create archive instance */
223b71206888 Initial import
thib
parents:
diff changeset
1671 SCN2kFILE archive(inname);
223b71206888 Initial import
thib
parents:
diff changeset
1672 archive.Init();
223b71206888 Initial import
thib
parents:
diff changeset
1673 if (archive.Deal() == 0) {
223b71206888 Initial import
thib
parents:
diff changeset
1674 fprintf(stderr,"Cannot open / Invalid archive file %s\n",inname);
223b71206888 Initial import
thib
parents:
diff changeset
1675 usage();
223b71206888 Initial import
thib
parents:
diff changeset
1676 }
223b71206888 Initial import
thib
parents:
diff changeset
1677 /* dump files */
223b71206888 Initial import
thib
parents:
diff changeset
1678 archive.InitList();
223b71206888 Initial import
thib
parents:
diff changeset
1679 char* fname;
223b71206888 Initial import
thib
parents:
diff changeset
1680 fprintf(stderr,"Dump start\n");
223b71206888 Initial import
thib
parents:
diff changeset
1681 int system_version = 0;
223b71206888 Initial import
thib
parents:
diff changeset
1682 while( (fname = archive.ListItem()) != 0) {
223b71206888 Initial import
thib
parents:
diff changeset
1683 ARCINFO* info = archive.Find(fname,"");
223b71206888 Initial import
thib
parents:
diff changeset
1684 if (info == 0) continue;
223b71206888 Initial import
thib
parents:
diff changeset
1685 char* data = info->CopyRead();
223b71206888 Initial import
thib
parents:
diff changeset
1686 char* d = data;
223b71206888 Initial import
thib
parents:
diff changeset
1687 char* dend = d + info->Size();
223b71206888 Initial import
thib
parents:
diff changeset
1688 /* version 確認 */
223b71206888 Initial import
thib
parents:
diff changeset
1689 if (read_little_endian_int(d) == 0x1cc) {
223b71206888 Initial import
thib
parents:
diff changeset
1690 system_version = 0;
223b71206888 Initial import
thib
parents:
diff changeset
1691 } else if (read_little_endian_int(d) == 0x1d0) {
223b71206888 Initial import
thib
parents:
diff changeset
1692 system_version = 1;
223b71206888 Initial import
thib
parents:
diff changeset
1693 } else {
223b71206888 Initial import
thib
parents:
diff changeset
1694 continue;
223b71206888 Initial import
thib
parents:
diff changeset
1695 }
223b71206888 Initial import
thib
parents:
diff changeset
1696 if (read_little_endian_int(d+4) == 0x1adb2) ; // little busters!
223b71206888 Initial import
thib
parents:
diff changeset
1697 else if (read_little_endian_int(d+4) != 0x2712) continue;
223b71206888 Initial import
thib
parents:
diff changeset
1698 int header_size;
223b71206888 Initial import
thib
parents:
diff changeset
1699 if (system_version == 0) {
223b71206888 Initial import
thib
parents:
diff changeset
1700 header_size = 0x1cc + read_little_endian_int(d+0x20) * 4;
223b71206888 Initial import
thib
parents:
diff changeset
1701 } else {
223b71206888 Initial import
thib
parents:
diff changeset
1702 header_size = read_little_endian_int(d+0x20);
223b71206888 Initial import
thib
parents:
diff changeset
1703 }
223b71206888 Initial import
thib
parents:
diff changeset
1704 d += header_size;
223b71206888 Initial import
thib
parents:
diff changeset
1705
223b71206888 Initial import
thib
parents:
diff changeset
1706 const char* dcur = d;
223b71206888 Initial import
thib
parents:
diff changeset
1707 const char* dstart = d;
223b71206888 Initial import
thib
parents:
diff changeset
1708 fprintf(stderr,"Dumping %s\n",fname);
223b71206888 Initial import
thib
parents:
diff changeset
1709 Flags flags;
223b71206888 Initial import
thib
parents:
diff changeset
1710 /* 最初から最後までコマンド取得 -> 出力を繰り返す */
223b71206888 Initial import
thib
parents:
diff changeset
1711 while(dcur<dend) {
223b71206888 Initial import
thib
parents:
diff changeset
1712 const char* dprev = dcur;
223b71206888 Initial import
thib
parents:
diff changeset
1713 Cmd cmd(flags, system_version); cmd.ClearError();
223b71206888 Initial import
thib
parents:
diff changeset
1714
223b71206888 Initial import
thib
parents:
diff changeset
1715 /* end? */
223b71206888 Initial import
thib
parents:
diff changeset
1716 if (*dcur == -1) {
223b71206888 Initial import
thib
parents:
diff changeset
1717 /* 0xff x 32byte + 0x00 : end sign */
223b71206888 Initial import
thib
parents:
diff changeset
1718 int i; for (i=0; i<0x20; i++)
223b71206888 Initial import
thib
parents:
diff changeset
1719 if (dcur[i] != -1) break;
223b71206888 Initial import
thib
parents:
diff changeset
1720 if (i == 0x20 && dcur[i] == 0) break;
223b71206888 Initial import
thib
parents:
diff changeset
1721 }
223b71206888 Initial import
thib
parents:
diff changeset
1722 dprintf("%d : ",dcur-dstart);
223b71206888 Initial import
thib
parents:
diff changeset
1723 cmd.GetCmd(flags, dcur);
223b71206888 Initial import
thib
parents:
diff changeset
1724 if (cmd.IsError()) {
223b71206888 Initial import
thib
parents:
diff changeset
1725 fprintf(outstream, "Error at %6d\n",dprev-dstart);
223b71206888 Initial import
thib
parents:
diff changeset
1726 while(dcur < dend) {
223b71206888 Initial import
thib
parents:
diff changeset
1727 if (*dcur == 0x29 && dcur[1] == 0x0a) {dcur++;break;}
223b71206888 Initial import
thib
parents:
diff changeset
1728 dcur++;
223b71206888 Initial import
thib
parents:
diff changeset
1729 }
223b71206888 Initial import
thib
parents:
diff changeset
1730 dprev -= 2*16;
223b71206888 Initial import
thib
parents:
diff changeset
1731 int ilen = (dcur-dprev+15)/16;
223b71206888 Initial import
thib
parents:
diff changeset
1732 int i; for (i=0; i<ilen; i++) {
223b71206888 Initial import
thib
parents:
diff changeset
1733 fprintf(outstream, "%6d: ",dprev-dstart);
223b71206888 Initial import
thib
parents:
diff changeset
1734 int j; for (j=0; j<16; j++) {
223b71206888 Initial import
thib
parents:
diff changeset
1735 if (dprev >= dend) break;
223b71206888 Initial import
thib
parents:
diff changeset
1736 if (dprev < data) continue;
223b71206888 Initial import
thib
parents:
diff changeset
1737 fprintf(outstream, "%02x ",*(unsigned char*)(dprev));
223b71206888 Initial import
thib
parents:
diff changeset
1738 dprev++;
223b71206888 Initial import
thib
parents:
diff changeset
1739 }
223b71206888 Initial import
thib
parents:
diff changeset
1740 fprintf(outstream, "\n");
223b71206888 Initial import
thib
parents:
diff changeset
1741 }
223b71206888 Initial import
thib
parents:
diff changeset
1742 }
223b71206888 Initial import
thib
parents:
diff changeset
1743 }
223b71206888 Initial import
thib
parents:
diff changeset
1744 delete info;
223b71206888 Initial import
thib
parents:
diff changeset
1745 }
223b71206888 Initial import
thib
parents:
diff changeset
1746 return 0;
223b71206888 Initial import
thib
parents:
diff changeset
1747 }
223b71206888 Initial import
thib
parents:
diff changeset
1748 #endif
223b71206888 Initial import
thib
parents:
diff changeset
1749