comparison scn2k/scn2k_cmd.cc @ 63:4b9ffe15a87d

Refactor Load/LoadSys
author Thibaut GIRKA <thib@sitedethib.com>
date Sat, 06 Feb 2010 17:29:33 +0100
parents 3b1593186f12
children 4416cfac86ae
comparison
equal deleted inserted replaced
62:3b1593186f12 63:4b9ffe15a87d
221 return str[number]; 221 return str[number];
222 } 222 }
223 return ""; 223 return "";
224 } 224 }
225 225
226 void Flags::Save(string& save) { 226 void Flags::Load(const char* save) {
227 char buf[1024]; 227 Load(save, false);
228 save = "\n[Flags]\n"; 228 }
229 int i,j; 229
230 for (i=0; i<=TYPE_NONSYSVARMAX; i++) { 230 void Flags::LoadSys(const char* save) {
231 for (j=0; j<2000; j++) { 231 Load(save, true);
232 if (var[i][j] != 0) { 232 }
233 sprintf(buf, "V<%d>[%04d]=%d\n",i,j,var[i][j]); 233
234 save += buf; 234 void Flags::Load(const char* save, bool sys) {
235 } 235 int i, j;
236 int start, end;
237 std::string *var_str;
238 char string_field[] = "V<?>[%04d]=";
239
240 start = (sys) ? (TYPE_NONSYSVARMAX + 1) : 0;
241 end = (sys) ? (TYPE_NONSYSVARMAX + 2) : TYPE_NONSYSVARMAX;
242
243 var_str = (sys) ? sys_str : str;
244 string_field[2] = (sys) ? 'M' : 'C';
245
246 for (i=start; i <= end; i++) {
247 for (j=0; j < 2000; j++) {
248 var[i][j] = 0;
236 } 249 }
237 } 250 }
238 for (j=0; j<2000; j++) { 251 for (j=0; j<2000; j++) {
239 if (str[j].length() != 0) { 252 var_str[j] = "";
240 sprintf(buf, "V<C>[%04d]=%s\n", j, str[j].c_str());
241 save += buf;
242 }
243 }
244 }
245
246 void Flags::Load(const char* save) {
247 int i,j;
248 for (i=0; i<=TYPE_NONSYSVARMAX; i++) {
249 for (j=0; j<2000; j++) {
250 var[i][j] = 0;
251 }
252 } 253 }
253 sys = 0; 254 sys = 0;
254 for (j=0; j<2000; j++) {
255 str[j] = "";
256 }
257 255
258 save = strstr(save, "\n[Flags]\n"); 256 save = strstr(save, "\n[Flags]\n");
259 257
260 if (save) { 258 if (save) {
261 save += strlen("\n[Flags]\n"); 259 save += strlen("\n[Flags]\n");
262 do { 260 do {
263 if (save[0] == '[') break; // next section 261 if (save[0] == '[') break; // next section
264 if (strncmp(save, "V<",2) == 0) { 262 if (strncmp(save, string_field, 2) == 0) {
265 if (strncmp(save, "V<C>[",5) == 0) { // string 263 if (strncmp(save, string_field, 5) == 0) { // string
266 char buf[1024]; 264 char buf[1024];
267 int n; 265 int n;
268 if (sscanf(save, "V<C>[%04d]=",&n) == 1) { 266 if (sscanf(save, string_field, &n) == 1) {
269 const char* s = strchr(save, '='); 267 const char* s = strchr(save, '=');
270 s++; 268 s++;
271 const char* send = strchr(s, '\n'); 269 const char* send = strchr(s, '\n');
272 int slen = send - s; 270 int slen = send - s;
273 strncpy(buf, s, slen); 271 strncpy(buf, s, slen);
274 buf[slen] = 0; 272 buf[slen] = 0;
275 if (n >= 0 && n < 2000) str[n] = buf; 273 if (n >= 0 && n < 2000)
274 var_str[n] = buf;
276 } 275 }
277 } else if (save[2] >= '0' && save[2] <= '9') { 276 } else if (save[2] >= '0' && save[2] <= '9') {
278 int c,n,v; 277 int c, n, v;
279 if (sscanf(save, "V<%d>[%04d]=%d\n",&c,&n,&v) == 3) { 278 if (sscanf(save, "V<%d>[%04d]=%d\n",&c,&n,&v) == 3) {
280 if (c >= 0 && c <= TYPE_NONSYSVARMAX && n >= 0 && n < 2000) 279 if (n >= 0 && n < 2000)
281 var[c][n] = v; 280 {
281 if (!sys && c >= 0 && c <= TYPE_NONSYSVARMAX)
282 var[c][n] = v;
283 else if (sys && c == TYPE_NONSYSVARMAX + 1)
284 var[TYPE_NONSYSVARMAX + 1][n] = v;
285 else if (sys && c == 25)
286 var[7][n] = v;
287 }
282 } 288 }
283 } 289 }
284 } 290 }
285 save = strchr(save, '\n'); 291 save = strchr(save, '\n');
286 if (save) save++; 292 if (save)
293 save++;
287 } while (save); 294 } while (save);
295 }
296 }
297
298 void Flags::Save(string& save) {
299 char buf[1024];
300 save = "\n[Flags]\n";
301 int i, j;
302 for (i=0; i<=TYPE_NONSYSVARMAX; i++) {
303 for (j=0; j<2000; j++) {
304 if (var[i][j] != 0) {
305 sprintf(buf, "V<%d>[%04d]=%d\n",i,j,var[i][j]);
306 save += buf;
307 }
308 }
309 }
310 for (j=0; j<2000; j++) {
311 if (str[j].length() != 0) {
312 sprintf(buf, "V<C>[%04d]=%s\n", j, str[j].c_str());
313 save += buf;
314 }
288 } 315 }
289 } 316 }
290 317
291 void Flags::SaveSys(string& save) { //FIXME: see how to factorize with Save 318 void Flags::SaveSys(string& save) { //FIXME: see how to factorize with Save
292 char buf[1024]; 319 char buf[1024];
293 int j; 320 int j;
294 save = "\n[Flags]\n"; 321 save = "\n[Flags]\n";
295 for (j=0; j<2000; j++) { 322 for (j=0; j<2000; j++) {
296 if (var[6][j] != 0) { 323 if (var[6][j] != 0) {
297 sprintf(buf, "V<6>[%04d]=%d\n",j,var[6][j]); 324 sprintf(buf, "V<6>[%04d]=%d\n", j, var[6][j]);
298 save += buf; 325 save += buf;
299 } 326 }
300 } 327 }
301 for (j=0; j<2000; j++) { 328 for (j=0; j<2000; j++) {
302 if (var[7][j] != 0) { 329 if (var[7][j] != 0) {
303 sprintf(buf, "V<25>[%04d]=%d\n",j,var[7][j]); 330 sprintf(buf, "V<25>[%04d]=%d\n",j,var[7][j]);
304 save += buf; 331 save += buf;
305 } 332 }
307 for (j=0; j<2000; j++) { 334 for (j=0; j<2000; j++) {
308 if (sys_str[j].length() != 0) { 335 if (sys_str[j].length() != 0) {
309 sprintf(buf, "V<M>[%04d]=%s\n", j, sys_str[j].c_str()); 336 sprintf(buf, "V<M>[%04d]=%s\n", j, sys_str[j].c_str());
310 save += buf; 337 save += buf;
311 } 338 }
312 }
313 }
314
315 void Flags::LoadSys(const char* save) { //FIXME: Same as Save and SaveSys
316 int i,j;
317 for (i=6; i<=7; i++) {
318 for (j=0; j<2000; j++) {
319 var[i][j] = 0;
320 }
321 }
322 for (j=0; j<2000; j++) {
323 sys_str[j] = "";
324 }
325 sys = 0;
326
327 save = strstr(save, "\n[Flags]\n");
328
329 if (save) {
330 save += strlen("\n[Flags]\n");
331 do {
332 if (save[0] == '[') break; // next section
333 if (strncmp(save, "V<",2) == 0) {
334 if (strncmp(save, "V<M>[",5) == 0) { // string
335 char buf[1024];
336 int n;
337 if (sscanf(save, "V<M>[%04d]=",&n) == 1) {
338 const char* s = strchr(save, '=');
339 s++;
340 const char* send = strchr(s, '\n');
341 int slen = send - s;
342 strncpy(buf, s, slen);
343 buf[slen] = 0;
344 if (n >= 0 && n < 2000) sys_str[n] = buf;
345 }
346 } else if (save[2] >= '0' && save[2] <= '9') {
347 int c,n,v;
348 if (sscanf(save, "V<%d>[%04d]=%d\n",&c,&n,&v) == 3) {
349 if (c == 6 && n >= 0 && n < 2000)
350 var[6][n] = v;
351 else if (c == 25 && n >= 0 && n < 2000)
352 var[7][n] = v;
353 }
354 }
355 }
356 save = strchr(save, '\n');
357 if (save) save++;
358 } while (save);
359 } 339 }
360 } 340 }
361 341
362 bool Flags::Exec(Cmd& cmd) { 342 bool Flags::Exec(Cmd& cmd) {
363 if (cmd.cmd_type == CMD_FLAGS) { // ÂåÆþ±é»» 343 if (cmd.cmd_type == CMD_FLAGS) { // ÂåÆþ±é»»