aboutsummaryrefslogtreecommitdiff
path: root/src/solvers/coord/utils.h
blob: ef3f817cbbf40cb105eaa5194886222303727f80 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
STATIC coord_t *parse_coord(size_t n, const char [n]);
STATIC uint8_t parse_axis(size_t n, const char [n]);
STATIC void parse_coord_and_axis(
    size_t n, const char [n], coord_t **, uint8_t *);
STATIC int64_t dataid_coord(const char *, char [static NISSY_SIZE_DATAID]);

STATIC coord_t *
parse_coord(size_t n, const char coord[n])
{
	int i;

	for (i = 0; all_coordinates[i] != NULL; i++)
		if (!strncmp(all_coordinates[i]->name, coord, n))
			return all_coordinates[i];

	return NULL;
}

STATIC uint8_t
parse_axis(size_t n, const char axis[n])
{
	if (!strncmp(axis, "UD", n) || !strncmp(axis, "DU", n)) {
		return AXIS_UD;
	} else if (!strncmp(axis, "RL", n) || !strncmp(axis, "LR", n)) {
		return AXIS_RL;
	} else if (!strncmp(axis, "FB", n) || !strncmp(axis, "BF", n)) {
		return AXIS_FB;
	}

	return UINT8_ERROR;
}

STATIC void
parse_coord_and_axis(
	size_t n,
	const char str[n],
	coord_t **coord,
	uint8_t *axis
)
{
	size_t i;

	for (i = 0; i < n; i++)
		if (str[i] == '_')
			break;

	if (coord != NULL)
		*coord = parse_coord(i, str);

	if (axis != NULL)
		*axis = i == n ? UINT8_ERROR : parse_axis(n-i-1, str+i+1);
}

STATIC int64_t
dataid_coord(const char *ca, char dataid[static NISSY_SIZE_DATAID])
{
	coord_t *c;

	parse_coord_and_axis(strlen(ca), ca, &c, NULL);

	if (c == NULL) {
		LOG("Error: cannot parse coordinate from '%s'\n", ca);
		return NISSY_ERROR_INVALID_SOLVER;
	}

	strcpy(dataid, c->name);

	return NISSY_OK;
}

Generated with cgit - Back to sebastiano.tronto.net