aboutsummaryrefslogtreecommitdiff
path: root/map.c
diff options
context:
space:
mode:
Diffstat (limited to 'map.c')
-rw-r--r--map.c39
1 files changed, 25 insertions, 14 deletions
diff --git a/map.c b/map.c
index fff4486..1122466 100644
--- a/map.c
+++ b/map.c
@@ -1724,8 +1724,8 @@ static const char *parse_edge_list(const game_params *params,
int i, k, pos;
bool state;
const char *p = *desc;
-
- dsf_init(map+wh, wh);
+ const char *err = NULL;
+ int *dsf = snew_dsf(wh);
pos = -1;
state = false;
@@ -1736,8 +1736,10 @@ static const char *parse_edge_list(const game_params *params,
* pairs of squares whenever the edge list shows a non-edge).
*/
while (*p && *p != ',') {
- if (*p < 'a' || *p > 'z')
- return "Unexpected character in edge list";
+ if (*p < 'a' || *p > 'z') {
+ err = "Unexpected character in edge list";
+ goto out;
+ }
if (*p == 'z')
k = 25;
else
@@ -1760,10 +1762,12 @@ static const char *parse_edge_list(const game_params *params,
y = (pos - w*(h-1)) % h;
dx = 1;
dy = 0;
- } else
- return "Too much data in edge list";
+ } else {
+ err = "Too much data in edge list";
+ goto out;
+ }
if (!state)
- dsf_merge(map+wh, y*w+x, (y+dy)*w+(x+dx));
+ dsf_merge(dsf, y*w+x, (y+dy)*w+(x+dx));
pos++;
}
@@ -1772,8 +1776,10 @@ static const char *parse_edge_list(const game_params *params,
p++;
}
assert(pos <= 2*wh-w-h);
- if (pos < 2*wh-w-h)
- return "Too little data in edge list";
+ if (pos < 2*wh-w-h) {
+ err = "Too little data in edge list";
+ goto out;
+ }
/*
* Now go through again and allocate region numbers.
@@ -1782,17 +1788,22 @@ static const char *parse_edge_list(const game_params *params,
for (i = 0; i < wh; i++)
map[i] = -1;
for (i = 0; i < wh; i++) {
- k = dsf_canonify(map+wh, i);
+ k = dsf_canonify(dsf, i);
if (map[k] < 0)
map[k] = pos++;
map[i] = map[k];
}
- if (pos != n)
- return "Edge list defines the wrong number of regions";
+ if (pos != n) {
+ err = "Edge list defines the wrong number of regions";
+ goto out;
+ }
*desc = p;
+ err = NULL; /* no error */
- return NULL;
+ out:
+ sfree(dsf);
+ return err;
}
static const char *validate_desc(const game_params *params, const char *desc)
@@ -1802,7 +1813,7 @@ static const char *validate_desc(const game_params *params, const char *desc)
int *map;
const char *ret;
- map = snewn(2*wh, int);
+ map = snewn(wh, int);
ret = parse_edge_list(params, &desc, map);
sfree(map);
if (ret)