h48

A prototype for an optimal Rubik's cube solver, work in progress.
git clone https://git.tronto.net/h48
Download | Log | Files | Refs | README | LICENSE

utils.h (1447B)


      1 STATIC coord_t *parse_coord(size_t n, const char [n]);
      2 STATIC uint8_t parse_axis(size_t n, const char [n]);
      3 STATIC void parse_coord_and_axis(
      4     size_t n, const char [n], coord_t **, uint8_t *);
      5 STATIC int64_t dataid_coord(const char *, char [static NISSY_DATAID_SIZE]);
      6 
      7 STATIC coord_t *
      8 parse_coord(size_t n, const char coord[n])
      9 {
     10 	int i;
     11 
     12 	for (i = 0; all_coordinates[i] != NULL; i++)
     13 		if (!strncmp(all_coordinates[i]->name, coord, n))
     14 			return all_coordinates[i];
     15 
     16 	return NULL;
     17 }
     18 
     19 STATIC uint8_t
     20 parse_axis(size_t n, const char axis[n])
     21 {
     22 	if (!strncmp(axis, "UD", n) || !strncmp(axis, "DU", n)) {
     23 		return AXIS_UD;
     24 	} else if (!strncmp(axis, "RL", n) || !strncmp(axis, "LR", n)) {
     25 		return AXIS_RL;
     26 	} else if (!strncmp(axis, "FB", n) || !strncmp(axis, "BF", n)) {
     27 		return AXIS_FB;
     28 	}
     29 
     30 	return UINT8_ERROR;
     31 }
     32 
     33 STATIC void
     34 parse_coord_and_axis(
     35 	size_t n,
     36 	const char str[n],
     37 	coord_t **coord,
     38 	uint8_t *axis
     39 )
     40 {
     41 	size_t i;
     42 
     43 	for (i = 0; i < n; i++)
     44 		if (str[i] == '_')
     45 			break;
     46 
     47 	if (coord != NULL)
     48 		*coord = parse_coord(i, str);
     49 
     50 	if (axis != NULL)
     51 		*axis = i == n ? UINT8_ERROR : parse_axis(n-i-1, str+i+1);
     52 }
     53 
     54 STATIC int64_t
     55 dataid_coord(const char *ca, char dataid[static NISSY_DATAID_SIZE])
     56 {
     57 	coord_t *c;
     58 
     59 	parse_coord_and_axis(strlen(ca), ca, &c, NULL);
     60 
     61 	if (c == NULL) {
     62 		LOG("dataid_coord: cannot parse coordinate from '%s'\n", ca);
     63 		return NISSY_ERROR_INVALID_SOLVER;
     64 	}
     65 
     66 	strcpy(dataid, c->name);
     67 
     68 	return NISSY_OK;
     69 }