comparison mapsearch.c @ 1815:7ac487466456

Fixes to the block parsing.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 30 Oct 2017 02:53:05 +0200
parents daf7dcc635d6
children 89829bd4122f
comparison
equal deleted inserted replaced
1814:daf7dcc635d6 1815:7ac487466456
452 452
453 void mapBlockGetDimensions(const unsigned char *data, const size_t len, int *width, int *height) 453 void mapBlockGetDimensions(const unsigned char *data, const size_t len, int *width, int *height)
454 { 454 {
455 size_t offs = 0; 455 size_t offs = 0;
456 int x = 0, x2 = 0; 456 int x = 0, x2 = 0;
457 BOOL flag = TRUE; 457
458 458 *width = *height = 0;
459 *height = 0;
460 *width = -1;
461 459
462 while (offs < len) 460 while (offs < len)
463 { 461 {
464 const unsigned char c = data[offs++]; 462 const unsigned char ch = data[offs++];
465 if (c == '\n') 463 if (ch == '\n')
466 { 464 {
467 if (x == 0)
468 return;
469
470 if (x > *width) 465 if (x > *width)
471 *width = x; 466 *width = x;
472 467
468 (*height)++;
473 x = x2 = 0; 469 x = x2 = 0;
474 flag = TRUE;
475 } 470 }
476 else 471 else
477 { 472 {
478 if (flag)
479 {
480 (*height)++;
481 flag = FALSE;
482 }
483 x2++; 473 x2++;
484 if (c != ' ') 474 if (ch != ' ')
485 x = x2; 475 x = x2;
486 } 476 }
487 } 477 }
478
479 if (x > *width)
480 *width = x;
488 481
489 if (x > 0) 482 if (x > 0)
490 (*height)++; 483 (*height)++;
491
492 if (x > *width)
493 *width = x;
494 } 484 }
495 485
496 486
497 BOOL mapBlockParse(const unsigned char *data, const size_t len, MapBlock *res) 487 BOOL mapBlockParse(const unsigned char *data, const size_t len, MapBlock *res)
498 { 488 {
499 unsigned char *dp = res->data;
500 size_t offs = 0; 489 size_t offs = 0;
501 BOOL flag = FALSE; 490
502 int x = 0, y = 0; 491 for (int yc = 0; yc < res->height; yc++)
503 492 {
504 while (offs < len && y < res->height) 493 unsigned char *dp = res->data + (yc * res->scansize);
505 { 494 for (int xc = 0; xc < res->width; xc++)
506 const unsigned char c = data[offs++]; 495 {
507 if (c == '\n') 496 if (offs < len)
508 {
509 if (x == 0)
510 return TRUE;
511 else
512 if (x != res->width)
513 { 497 {
514 THERR("Broken block line #%d width %d != %d!\n", 498 dp[xc] = data[offs++];
515 y, x, res->width);
516
517 return FALSE;
518 } 499 }
519 flag = TRUE; 500 }
520 } 501 while (offs < len && data[offs] != '\n')
521 else 502 offs++;
522 { 503 }
523 if (flag) 504
524 { 505 return offs == len;
525 x = 0;
526 y++;
527 dp = res->data + (y * res->scansize);
528 flag = FALSE;
529 }
530 dp[x++] = c;
531
532 if (x > res->scansize)
533 {
534 THERR("Broken block line #%d width %d > scansize %d!\n",
535 y, x, res->scansize);
536
537 return FALSE;
538 }
539 }
540 }
541
542 return TRUE;
543 } 506 }
544 507
545 508
546 BOOL mapStrChr(const unsigned char *symbols, const size_t nsymbols, const unsigned char ch) 509 BOOL mapStrChr(const unsigned char *symbols, const size_t nsymbols, const unsigned char ch)
547 { 510 {