# HG changeset patch # User Matti Hamalainen # Date 1509113941 -10800 # Node ID 7c254d09022101c2813b54c4627e17d3c4195b65 # Parent f4c49fd6557e998170a45182484afdb39382d1de And some more cleanups. diff -r f4c49fd6557e -r 7c254d090221 liblocfile.c --- a/liblocfile.c Fri Oct 27 05:17:35 2017 +0300 +++ b/liblocfile.c Fri Oct 27 17:19:01 2017 +0300 @@ -39,44 +39,39 @@ } -LocMarker * locCopyLocMarker(LocMarker *src) +LocMarker * locCopyLocMarker(const LocMarker *src) { - int i; LocMarker *res = th_malloc0(sizeof(LocMarker)); - if (res == NULL) return NULL; + // Just copy the data, as most of it is "static" + // and then replace the pointers etc. as necessary. memcpy(res, src, sizeof(LocMarker)); res->file = NULL; - res->uri = th_strdup(src->uri); res->freeform = th_strdup(src->freeform); - for (i = 0; i < res->nnames; i++) + for (int i = 0; i < res->nnames; i++) res->names[i].name = th_strdup(src->names[i].name); - for (i = 0; i < res->ncoders; i++) + for (int i = 0; i < res->ncoders; i++) res->coders[i].name = th_strdup(src->coders[i].name); return res; } -void locCopyLocations(MapLocations *dst, MapLocations *src) +void locCopyLocations(MapLocations *dst, const MapLocations *src) { - int i; - if (dst == NULL || src == NULL) return; dst->n = src->n; dst->locations = (LocMarker **) th_calloc(dst->n, sizeof(LocMarker *)); - for (i = 0; i < dst->n; i++) - { + for (int i = 0; i < dst->n; i++) dst->locations[i] = locCopyLocMarker(src->locations[i]); - } } @@ -134,17 +129,20 @@ } -int locFindByCoords(MapLocations *l, int x, int y, BOOL locTrue) +int locFindByCoords(const MapLocations *l, const int x, const int y, const BOOL locTrue) { - int i; - - for (i = 0; i < l->n; i++) + for (int i = 0; i < l->n; i++) { LocMarker *tmp = l->locations[i]; - if (locTrue) { - if (tmp->ox == x && tmp->oy == y) return i; - } else { - if (tmp->x == x && tmp->y == y) return i; + if (locTrue) + { + if (tmp->ox == x && tmp->oy == y) + return i; + } + else + { + if (tmp->x == x && tmp->y == y) + return i; } } @@ -154,8 +152,7 @@ void locFreeMarkerData(LocMarker *marker) { - int i; - for (i = 0; i < LOC_MAX_NAMES; i++) + for (int i = 0; i < LOC_MAX_NAMES; i++) { th_free(marker->names[i].name); th_free(marker->coders[i].name); @@ -172,8 +169,7 @@ { if (loc->locations != NULL) { - int i; - for (i = 0; i < loc->n; i++) + for (int i = 0; i < loc->n; i++) if (loc->locations[i] != NULL) { locFreeMarkerData(loc->locations[i]); @@ -265,6 +261,7 @@ res += f->ch - '0'; f->ch = locFGetc(f); } + *val = res; return TRUE; } @@ -331,7 +328,7 @@ } -static BOOL locParseFlags(LocFileParseContext *f, int *flags) +static BOOL locParseFlags(LocFileParseContext *ctx, int *flags) { BOOL endFlags; @@ -339,49 +336,49 @@ endFlags = FALSE; while (!endFlags) { - switch (f->ch) + switch (ctx->ch) { // Scenic marker flags case '?': - if (!locCheckMutex(f, flags, LOCF_M_MASK, LOCF_M_SCENIC1)) + if (!locCheckMutex(ctx, flags, LOCF_M_MASK, LOCF_M_SCENIC1)) return FALSE; break; case '%': - if (!locCheckMutex(f, flags, LOCF_M_MASK, LOCF_M_SCENIC2)) + if (!locCheckMutex(ctx, flags, LOCF_M_MASK, LOCF_M_SCENIC2)) return FALSE; break; case 'C': - if (!locCheckMutex(f, flags, LOCF_M_MASK, LOCF_M_PCITY)) + if (!locCheckMutex(ctx, flags, LOCF_M_MASK, LOCF_M_PCITY)) return FALSE; break; case 'c': - if (!locCheckMutex(f, flags, LOCF_M_MASK, LOCF_M_CITY)) + if (!locCheckMutex(ctx, flags, LOCF_M_MASK, LOCF_M_CITY)) return FALSE; break; // Marker type flags case 'S': - if (!locCheckMutex(f, flags, LOCF_T_MASK, LOCF_T_SHRINE)) + if (!locCheckMutex(ctx, flags, LOCF_T_MASK, LOCF_T_SHRINE)) return FALSE; break; case 'G': - if (!locCheckMutex(f, flags, LOCF_T_MASK, LOCF_T_GUILD)) + if (!locCheckMutex(ctx, flags, LOCF_T_MASK, LOCF_T_GUILD)) return FALSE; break; case 'P': - if (!locCheckMutex(f, flags, LOCF_T_MASK, LOCF_T_SS)) + if (!locCheckMutex(ctx, flags, LOCF_T_MASK, LOCF_T_SS)) return FALSE; break; case 'M': - if (!locCheckMutex(f, flags, LOCF_T_MASK, LOCF_T_MONSTER)) + if (!locCheckMutex(ctx, flags, LOCF_T_MASK, LOCF_T_MONSTER)) return FALSE; break; case 'T': - if (!locCheckMutex(f, flags, LOCF_T_MASK, LOCF_T_TRAINER)) + if (!locCheckMutex(ctx, flags, LOCF_T_MASK, LOCF_T_TRAINER)) return FALSE; break; case 'F': - if (!locCheckMutex(f, flags, LOCF_T_MASK, LOCF_T_FORT)) + if (!locCheckMutex(ctx, flags, LOCF_T_MASK, LOCF_T_FORT)) return FALSE; break; @@ -403,136 +400,136 @@ break; } - f->ch = locFGetc(f); + ctx->ch = locFGetc(ctx); } return TRUE; } -static void locParseMultiField(LocFileParseContext *f, char *fieldsep, char sep, const char *desc, LocName *data) +static void locParseMultiField(LocFileParseContext *ctx, char *fieldsep, char sep, const char *desc, LocName *data) { - if (f->subField < 0) + if (ctx->subField < 0) { - f->subField = 0; - f->fieldSep = fieldsep; - f->sep = sep; + ctx->subField = 0; + ctx->fieldSep = fieldsep; + ctx->sep = sep; } - if (f->sep == sep) + if (ctx->sep == sep) { - if (f->subField < LOC_MAX_NAMES) + if (ctx->subField < LOC_MAX_NAMES) { - th_free(data[f->subField].name); - data[f->subField++].name = parseFieldString(f, f->fieldSep); - locPMSet(f, PM_NEXT, PM_FIELD_SEP); - if (!strchr(f->fieldSep, f->ch)) - locPMErr(f, "Expected field separator '%s' after %s.\n", f->fieldSep, desc); + th_free(data[ctx->subField].name); + data[ctx->subField++].name = parseFieldString(ctx, ctx->fieldSep); + locPMSet(ctx, PM_NEXT, PM_FIELD_SEP); + if (!strchr(ctx->fieldSep, ctx->ch)) + locPMErr(ctx, "Expected field separator '%s' after %s.\n", ctx->fieldSep, desc); } else - locPMErr(f, "Too many %s (max %d).\n", desc, LOC_MAX_NAMES); + locPMErr(ctx, "Too many %s (max %d).\n", desc, LOC_MAX_NAMES); } else { - f->fieldSep = ";"; - f->subField = -1; - f->field++; - locPMSet(f, PM_FIELD, -1); + ctx->fieldSep = ";"; + ctx->subField = -1; + ctx->field++; + locPMSet(ctx, PM_FIELD, -1); } } -static void locParseLocField(LocFileParseContext *f, MapLocations *l, LocMarker *marker) +static void locParseLocField(LocFileParseContext *ctx, MapLocations *l, LocMarker *marker) { BOOL res = FALSE; char *tmpStr; int i; - if ((f->ch == '\n' || f->ch == '\r') && f->field < 8) + if ((ctx->ch == '\n' || ctx->ch == '\r') && ctx->field < 8) { - locPMErr(f, "Unexpected end of line.\n"); + locPMErr(ctx, "Unexpected end of line.\n"); return; } - switch (f->field) + switch (ctx->field) { case 1: // X-coordinate - res = parseFieldInt(f, &marker->x); - f->fieldSep = ";"; + res = parseFieldInt(ctx, &marker->x); + ctx->fieldSep = ";"; if (res) { - f->field++; - locPMSet(f, PM_NEXT, PM_FIELD_SEP); + ctx->field++; + locPMSet(ctx, PM_NEXT, PM_FIELD_SEP); } else - locPMErr(f, "Error parsing X-coordinate.\n"); + locPMErr(ctx, "Error parsing X-coordinate.\n"); break; case 2: // Y-coordinate - res = parseFieldInt(f, &marker->y); + res = parseFieldInt(ctx, &marker->y); if (res) { - f->field++; - locPMSet(f, PM_NEXT, PM_FIELD_SEP); + ctx->field++; + locPMSet(ctx, PM_NEXT, PM_FIELD_SEP); } else - locPMErr(f, "Error parsing Y-coordinate.\n"); + locPMErr(ctx, "Error parsing Y-coordinate.\n"); break; case 3: // Label orientation and flags - res = parseFieldInt(f, &marker->align); + res = parseFieldInt(ctx, &marker->align); if (res) - res = locParseFlags(f, &marker->flags); + res = locParseFlags(ctx, &marker->flags); if (res) { - f->field++; - locPMSet(f, PM_NEXT, PM_FIELD_SEP); + ctx->field++; + locPMSet(ctx, PM_NEXT, PM_FIELD_SEP); } else - locPMErr(f, "Error parsing orientation and flags field.\n"); + locPMErr(ctx, "Error parsing orientation and flags field.\n"); break; case 4: // Location name(s) - locParseMultiField(f, "|;", '|', "location names", marker->names); + locParseMultiField(ctx, "|;", '|', "location names", marker->names); break; case 5: // Coders - locParseMultiField(f, ",;", ',', "coder names", marker->coders); + locParseMultiField(ctx, ",;", ',', "coder names", marker->coders); break; case 6: // Date marker->valid = FALSE; - tmpStr = parseFieldString(f, f->fieldSep); + tmpStr = parseFieldString(ctx, ctx->fieldSep); if (tmpStr && tmpStr[0]) { if (sscanf(tmpStr, LOC_TIMEFMT, &marker->added.day, &marker->added.month, &marker->added.year) == 3) marker->valid = TRUE; else { - locPMErr(f, "Warning, invalid timestamp '%s' in '%s'.\n", + locPMErr(ctx, "Warning, invalid timestamp '%s' in '%s'.\n", tmpStr, marker->names[0].name); } } th_free(tmpStr); - f->field++; - locPMSet(f, PM_NEXT, PM_FIELD_SEP); + ctx->field++; + locPMSet(ctx, PM_NEXT, PM_FIELD_SEP); break; case 7: // URI th_free(marker->uri); - marker->uri = parseFieldString(f, f->fieldSep); - f->field++; - locPMSet(f, PM_NEXT, PM_FIELD_SEP); + marker->uri = parseFieldString(ctx, ctx->fieldSep); + ctx->field++; + locPMSet(ctx, PM_NEXT, PM_FIELD_SEP); break; case 8: // Freeform - tmpStr = parseFieldString(f, "\r\n"); + tmpStr = parseFieldString(ctx, "\r\n"); // Check coordinates if (marker->x < 1 || marker->y < 1) { - locPMErr(f, "Invalid X or Y coordinate (%d, %d), for location '%s'. Must be > 0.\n", + locPMErr(ctx, "Invalid X or Y coordinate (%d, %d), for location '%s'. Must be > 0.\n", marker->x, marker->y, marker->names[0].name); } @@ -544,7 +541,7 @@ if (i >= 0) { LocMarker *tloc = l->locations[i]; - locPMErr(f, "Warning, location already in list! (%d,%d) '%s' <-> (%d,%d) '%s'\n", + locPMErr(ctx, "Warning, location already in list! (%d,%d) '%s' <-> (%d,%d) '%s'\n", tloc->x + 1, tloc->y + 1, tloc->names[0].name, marker->x + 1, marker->y + 1, marker->names[0].name); } @@ -553,8 +550,8 @@ // Add new location to our list locAddNew(l, marker->x, marker->y, marker->align, marker->flags, marker->names, marker->coders, &marker->added, - marker->valid, marker->uri, tmpStr, f->file); - locPMSet(f, PM_IDLE, -1); + marker->valid, marker->uri, tmpStr, ctx->file); + locPMSet(ctx, PM_IDLE, -1); } locFreeMarkerData(marker); @@ -562,16 +559,16 @@ break; default: - locPMErr(f, "FATAL ERROR! Invalid state=%d!\n", f->parseMode); + locPMErr(ctx, "FATAL ERROR! Invalid state=%d!\n", ctx->parseMode); } } -BOOL locParseLocStream(FILE *fp, LocFileInfo *file, MapLocations *l, int offX, int offY) +BOOL locParseLocStream(FILE *fp, LocFileInfo *file, MapLocations *l, const int offX, const int offY) { LocFileParseContext ctx; + LocMarker marker; int i; - LocMarker marker; memset(&ctx, 0, sizeof(ctx)); ctx.fp = fp; diff -r f4c49fd6557e -r 7c254d090221 liblocfile.h --- a/liblocfile.h Fri Oct 27 05:17:35 2017 +0300 +++ b/liblocfile.h Fri Oct 27 17:19:01 2017 +0300 @@ -140,18 +140,18 @@ LocName *names, LocName *coders, LocDateStruct *added, BOOL valid, const char *uri, const char *freeform, LocFileInfo *file); -BOOL locParseLocStream(FILE *fp, LocFileInfo *file, MapLocations *l, int offX, int offY); +BOOL locParseLocStream(FILE *fp, LocFileInfo *file, MapLocations *l, const int offX, const int offY); void locFreeMarkerData(LocMarker *marker); void locFreeMapLocations(MapLocations *loc); -int locFindByCoords(MapLocations *l, int x, int y, BOOL locTrue); +int locFindByCoords(const MapLocations *l, const int x, const int y, const BOOL locTrue); const char * locGetType(int flags); const char * locGetLocationType(int flags); -LocMarker *locCopyLocMarker(LocMarker *); -void locCopyLocations(MapLocations *dst, MapLocations *src); +LocMarker *locCopyLocMarker(const LocMarker *); +void locCopyLocations(MapLocations *dst, const MapLocations *src); -void setLocFileInfo(LocFileInfo *f, const char *filename, const char *continent); +void setLocFileInfo(LocFileInfo *file, const char *filename, const char *continent); #endif diff -r f4c49fd6557e -r 7c254d090221 libmaputils.h --- a/libmaputils.h Fri Oct 27 05:17:35 2017 +0300 +++ b/libmaputils.h Fri Oct 27 17:19:01 2017 +0300 @@ -123,7 +123,7 @@ int mapBlockPutDo(MapBlock *map, const MapBlock *src, const int ox, const int oy); int mapBlockPut(MapBlock **pmap, const MapBlock *src, const int ox, const int oy); void mapBlockClean(MapBlock *block, const char *symbols); -void mapBlockPrintRaw(FILE *fg, const MapBlock *block); +void mapBlockPrintRaw(FILE *fh, const MapBlock *block); int mapBlockGetEntropy(const MapBlock *map, const unsigned char *exclude, const int nexclude);