aboutsummaryrefslogtreecommitdiff
path: root/src/core/io_cube.h
blob: 58b332c57194d4d2429e5158375dde83a9679306 (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
STATIC uint8_t readco(const char *);
STATIC uint8_t readcp(const char *);
STATIC uint8_t readeo(const char *);
STATIC uint8_t readep(const char *);
STATIC cube_t readcube_B32(const char *);
STATIC cube_t readcube_H48(const char *);
STATIC uint8_t readpiece_LST(const char **);
STATIC cube_t readcube_LST(const char *);

STATIC int writepiece_LST(uint8_t, char *);
STATIC void writecube_B32(cube_t, char *);
STATIC void writecube_H48(cube_t, char *);
STATIC void writecube_LST(cube_t, char *);

STATIC uint8_t b32toedge(char);
STATIC uint8_t b32tocorner(char);
STATIC char edgetob32(uint8_t);
STATIC char cornertob32(uint8_t);

STATIC struct {
	const char *name;
	cube_t (*read)(const char *);
	void (*write)(cube_t, char *);
} ioformat[] =
{
	{ .name = "B32", .read = readcube_B32, .write = writecube_B32 },
	{ .name = "LST", .read = readcube_LST, .write = writecube_LST },
	{ .name = "H48", .read = readcube_H48, .write = writecube_H48 },
	{ .name = "NONE", .read = NULL, .write = NULL },
};

cube_t
readcube(const char *format, const char *buf)
{
	int i;

	for (i = 0; ioformat[i].read != NULL; i++)
		if (!strcmp(format, ioformat[i].name))
			return ioformat[i].read(buf);

	LOG("Cannot read cube in the given format\n");
	return ZERO_CUBE;
}

void
writecube(const char *format, cube_t cube, char *buf)
{
	char *errormsg;
	size_t len;

	if (!isconsistent(cube)) {
		errormsg = "ERROR: inconsistent";
		goto writecube_error;
	}

	int i;

	for (i = 0; ioformat[i].write != NULL; i++) {
		if (!strcmp(format, ioformat[i].name)) {
			ioformat[i].write(cube, buf);
			return;
		}
	}

	errormsg = "ERROR: format";

writecube_error:
	LOG("writecube error, see stdout for details\n");
	len = strlen(errormsg);
	memcpy(buf, errormsg, len);
	buf[len] = '\n';
	buf[len+1] = '\0';
}

STATIC uint8_t
readco(const char *str)
{
	if (*str == '0')
		return 0;
	if (*str == '1')
		return CTWIST_CW;
	if (*str == '2')
		return CTWIST_CCW;

	LOG("Error reading CO\n");
	return UINT8_ERROR;
}

STATIC uint8_t
readcp(const char *str)
{
	uint8_t c;

	for (c = 0; c < 8; c++)
		if (!strncmp(str, cornerstr[c], 3) ||
		    !strncmp(str, cornerstralt[c], 3))
			return c;

	LOG("Error reading CP\n");
	return UINT8_ERROR;
}

STATIC uint8_t
readeo(const char *str)
{
	if (*str == '0')
		return 0;
	if (*str == '1')
		return EFLIP;

	LOG("Error reading EO\n");
	return UINT8_ERROR;
}

STATIC uint8_t
readep(const char *str)
{
	uint8_t e;

	for (e = 0; e < 12; e++)
		if (!strncmp(str, edgestr[e], 2))
			return e;

	LOG("Error reading EP\n");
	return UINT8_ERROR;
}

STATIC cube_t
readcube_B32(const char *buf)
{
	int i;
	uint8_t c[8], e[12];

	for (i = 0; i < 8; i++) {
		c[i] = b32tocorner(buf[i]);
		DBG_ASSERT(c[i] < 255, ZERO_CUBE,
		    "Error reading B32 corner %d (char %d)\n", i, i);
	}

	for (i = 0; i < 12; i++) {
		e[i] = b32toedge(buf[i+9]);
		DBG_ASSERT(e[i] < 255, ZERO_CUBE,
		    "Error reading B32 edge %d (char %d)\n", i, i+9);
	}

	return cubefromarray_ce(c, e);
}

STATIC cube_t
readcube_H48(const char *buf)
{
	int i;
	uint8_t piece, orient, c[8], e[12];
	const char *b;
	
	b = buf;

	for (i = 0; i < 12; i++) {
		while (*b == ' ' || *b == '\t' || *b == '\n')
			b++;
		if ((piece = readep(b)) == UINT8_ERROR)
			return ZERO_CUBE;
		b += 2;
		if ((orient = readeo(b)) == UINT8_ERROR)
			return ZERO_CUBE;
		b++;
		e[i] = piece | orient;
	}
	for (i = 0; i < 8; i++) {
		while (*b == ' ' || *b == '\t' || *b == '\n')
			b++;
		if ((piece = readcp(b)) == UINT8_ERROR)
			return ZERO_CUBE;
		b += 3;
		if ((orient = readco(b)) == UINT8_ERROR)
			return ZERO_CUBE;
		b++;
		c[i] = piece | orient;
	}

	return cubefromarray_ce(c, e);
}

STATIC uint8_t
readpiece_LST(const char **b)
{
	uint8_t ret;
	bool read;

	while (**b == ',' || **b == ' ' || **b == '\t' || **b == '\n')
		(*b)++;

	for (ret = 0, read = false; **b >= '0' && **b <= '9'; (*b)++) {
		read = true;
		ret = ret * 10 + (**b) - '0';
	}

	return read ? ret : UINT8_ERROR;
}

STATIC cube_t
readcube_LST(const char *buf)
{
	int i;
	uint8_t c[8], e[12];

	for (i = 0; i < 8; i++)
		c[i] = readpiece_LST(&buf);

	for (i = 0; i < 12; i++)
		e[i] = readpiece_LST(&buf);

	return cubefromarray_ce(c, e);
}

STATIC int
writepiece_LST(uint8_t piece, char *buf)
{
	char digits[3];
	int i, len;

	len = 0;
	while (piece != 0) {
		digits[len++] = (piece % 10) + '0';
		piece /= 10;
	}

	if (len == 0)
		digits[len++] = '0';

	for (i = 0; i < len; i++)
		buf[i] = digits[len-i-1];

	buf[len] = ',';
	buf[len+1] = ' ';

	return len+2;
}

STATIC void
writecube_B32(cube_t cube, char *buf)
{
	int i;
	uint8_t corner[8], edge[12];

	pieces(&cube, corner, edge);

	for (i = 0; i < 8; i++)
		buf[i] = cornertob32(corner[i]);

	buf[8] = '=';

	for (i = 0; i < 12; i++)
		buf[i+9] = edgetob32(edge[i]);

	buf[21] = '\0';
}

STATIC void
writecube_H48(cube_t cube, char *buf)
{
	uint8_t piece, perm, orient, corner[8], edge[12];
	int i;

	pieces(&cube, corner, edge);

	for (i = 0; i < 12; i++) {
		piece = edge[i];
		perm = piece & PBITS;
		orient = (piece & EOBIT) >> EOSHIFT;
		buf[4*i    ] = edgestr[perm][0];
		buf[4*i + 1] = edgestr[perm][1];
		buf[4*i + 2] = orient + '0';
		buf[4*i + 3] = ' ';
	}
	for (i = 0; i < 8; i++) {
		piece = corner[i];
		perm = piece & PBITS;
		orient = (piece & COBITS) >> COSHIFT;
		buf[48 + 5*i    ] = cornerstr[perm][0];
		buf[48 + 5*i + 1] = cornerstr[perm][1];
		buf[48 + 5*i + 2] = cornerstr[perm][2];
		buf[48 + 5*i + 3] = orient + '0';
		buf[48 + 5*i + 4] = ' ';
	}

	buf[48+39] = '\0';
}

STATIC void
writecube_LST(cube_t cube, char *buf)
{
	int i;
	size_t ptr;
	uint8_t piece, corner[8], edge[12];

	ptr = 0;
	pieces(&cube, corner, edge);

	for (i = 0; i < 8; i++) {
		piece = corner[i];
		ptr += writepiece_LST(piece, buf + ptr);
	}

	for (i = 0; i < 12; i++) {
		piece = edge[i];
		ptr += writepiece_LST(piece, buf + ptr);
	}

	*(buf+ptr-2) = 0;
}

STATIC uint8_t
b32toedge(char c)
{
	if (!((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'f')))
		return 255;

	return c <= 'Z' ? (uint8_t)(c - 'A') : (uint8_t)(c - 'a') + 26;
}

STATIC uint8_t
b32tocorner(char c) {
	uint8_t val;

	if (!((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'f')))
		return 255;

	val = c <= 'Z' ? (uint8_t)(c - 'A') : (uint8_t)(c - 'a') + 26;

	return (val & 7) | ((val & 24) << 2);
}

STATIC char
edgetob32(uint8_t edge)
{
	return edge < 26 ? 'A' + (char)edge : 'a' + (char)(edge - 26);
}

STATIC char
cornertob32(uint8_t corner)
{
	uint8_t val;

	val = (corner & 7) | ((corner & 96) >> 2);

	return val < 26 ? 'A' + (char)val : 'a' + (char)(val - 26);
}

Generated with cgit - Back to sebastiano.tronto.net