Mercurial > hg > batmud > maputils
changeset 2085:9ce9ccc8f212
Clean up block parsing function and its error handling.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Wed, 21 Aug 2019 15:08:29 +0300 |
parents | 5933039ce1fd |
children | 749f7df5981f |
files | mkspecial.c |
diffstat | 1 files changed, 48 insertions(+), 41 deletions(-) [+] |
line wrap: on
line diff
--- a/mkspecial.c Wed Aug 21 15:07:57 2019 +0300 +++ b/mkspecial.c Wed Aug 21 15:08:29 2019 +0300 @@ -269,7 +269,7 @@ } -int getLineWidth(char *buf, const size_t bufSize) +size_t getLineWidth(char *buf, const size_t bufSize) { size_t width; @@ -282,50 +282,54 @@ MapBlock * parseBlock(FILE *inFile) { - MapBlock *tmp; - int x, y, tmpW, tmpH; - uint_t i; + MapBlock *tmp = NULL; + int xc, yc, tmpW, tmpH; + char str[4096], *pos = NULL; BOOL isFound; - char s[4096], *p = NULL; + size_t i; isFound = FALSE; - while (!feof(inFile) && !isFound && fgets(s, sizeof(s), inFile)) + while (!feof(inFile) && !isFound && fgets(str, sizeof(str), inFile)) { - if ((p = strstr(s, ": !!PLR: ")) != NULL && (strstr(s, "party") || strstr(s, "report"))) + if ((pos = strstr(str, ": !!PLR: ")) != NULL && + (strstr(str, "party") || strstr(str, "report"))) isFound = TRUE; } if (!isFound) { THERR("No block start marker found.\n"); - return NULL; + goto err; } - for (i = 0; i < sizeof(s) && s[i] && s[i] != '\r' && s[i] != '\n'; i++); - s[i] = 0; + // Trim end + for (i = 0; i < sizeof(str) && str[i] && str[i] != '\r' && str[i] != '\n'; i++); + str[i] = 0; - if (sscanf(p, ": !!PLR: %d,%d", &x, &y) < 2) + if (sscanf(pos, ": !!PLR: %d,%d", &xc, &yc) < 2) { - THERR("Could not get location coordinates:\n'%s'\n", s); - return NULL; + THERR("Could not get location coordinates:\n'%s'\n", + str); + goto err; } // Check for discardable lines - if (!getLineData(inFile, s, sizeof(s))) - return NULL; + if (!getLineData(inFile, str, sizeof(str))) + goto err; - if (!th_strcasecmp(s, "You glance around.")) + if (!th_strcasecmp(str, "You glance around.")) { - if (!getLineData(inFile, s, sizeof(s))) - return NULL; + if (!getLineData(inFile, str, sizeof(str))) + goto err; } // New 'map' output in city maps has an empty line before the data, ignore it - if ((tmpW = getLineWidth(s, sizeof(s))) <= 9) + if ((tmpW = getLineWidth(str, sizeof(str))) <= 9) { - if (!getLineData(inFile, s, sizeof(s))) - return NULL; - tmpW = getLineWidth(s, sizeof(s)); + if (!getLineData(inFile, str, sizeof(str))) + goto err; + + tmpW = getLineWidth(str, sizeof(str)); } if (optWalkMode) @@ -336,7 +340,7 @@ case 13: tmpH = 7; break; case 9: tmpH = 9; break; default: - THERR("Invalid block width %d [%d, %d], rejecting.\n", tmpW, x, y); + THERR("Invalid block width %d [%d, %d], rejecting.\n", tmpW, xc, yc); return NULL; break; } @@ -347,43 +351,45 @@ if ((tmp = mapBlockAlloc(tmpW, tmpH)) == NULL) { THERR("Could not allocate mapblock (%d, %d)\n", tmpW, tmpH); - return NULL; + goto err; } - tmp->xc = x; - tmp->yc = y; + tmp->xc = xc; + tmp->yc = yc; - y = 0; + yc = 0; do { - i = getLineWidth(s, tmpW); + i = getLineWidth(str, tmpW); - if (i != (uint_t) tmp->width) + if (i != (size_t) tmp->width) { THERR("Broken block, line width %d != %d!\n", - i, tmp->width, s); - mapBlockFree(tmp); - return NULL; + i, tmp->width, str); + goto err; } - for (x = 0; x < tmp->width; x++) + for (xc = 0; xc < tmp->width; xc++) { - tmp->data[(y * tmp->scansize) + x] = s[x]; + tmp->data[(yc * tmp->scansize) + xc] = str[xc]; } - y++; - } while (!feof(inFile) && (y < tmp->height) && fgets(s, sizeof(s), inFile)); + yc++; + } while (!feof(inFile) && (yc < tmp->height) && fgets(str, sizeof(str), inFile)); - if (y < tmp->height) + if (yc < tmp->height) { THERR("Broken block, height %d < %d\n", - y, tmp->height); - mapBlockFree(tmp); - return NULL; + yc, tmp->height); + goto err; } return tmp; + +err: + mapBlockFree(tmp); + return NULL; } @@ -683,7 +689,8 @@ if (optDumpRejected) { fprintf(stderr, "\n#%d: %d,%d (%d,%d)\n", - i, mapBlocks[i]->xc, mapBlocks[i]->yc, + i, + mapBlocks[i]->xc, mapBlocks[i]->yc, mapBlocks[i]->xc + offsetX, mapBlocks[i]->yc + offsetY);