nissy-core

The "engine" of nissy, including the H48 optimal solver.
git clone https://git.tronto.net/nissy-core
Download | Log | Files | Refs | README | LICENSE

utils.h (1343B)


      1 #if SIZE_MAX == UINT64_MAX
      2 #define H48_HMAX UINT8_C(11)
      3 #else
      4 #define H48_HMAX UINT8_C(7)
      5 #endif
      6 
      7 long long parse_h48h(const char *, uint8_t [static 1]);
      8 STATIC long long dataid_h48(const char *, char [static NISSY_SIZE_DATAID]);
      9 
     10 long long
     11 parse_h48h(const char *buf, uint8_t h[static 1])
     12 {
     13 	char format_error_msg[100];
     14 	sprintf(format_error_msg, "[H48] Error parsing H48 solver: must be in "
     15 	    "'h48hNN' format, but got '%s'\n", buf);
     16 
     17 	buf += 3;
     18 
     19 	if (*buf != 'h') {
     20 		LOG(format_error_msg);
     21 		goto parse_h48h_error;
     22 	}
     23 	buf++;
     24 
     25 	if (strlen(buf) > 2 ||
     26 	    (strlen(buf) >= 1 && *buf < '1' && *buf > '9') ||
     27 	    (strlen(buf) == 2 && *buf < '0' && *buf > '9')) {
     28 		LOG(format_error_msg);
     29 		goto parse_h48h_error;
     30 	}
     31 
     32 	*h = atoi(buf);
     33 	if (*h > H48_HMAX) {
     34 		LOG("[H48] Invalid value %" PRIu8 " for parameter h (must be "
     35 		     "at most %" PRIu8 ")\n", *h, H48_HMAX);
     36 		goto parse_h48h_error;
     37 	}
     38 
     39 	for ( ; *buf >= 0 + '0' && *buf <= 9 + '0'; buf++) {
     40 		if (*buf == 0) {
     41 			LOG(format_error_msg);
     42 			goto parse_h48h_error;
     43 		}
     44 	}
     45 
     46 	return NISSY_OK;
     47 
     48 parse_h48h_error:
     49 	*h = 0;
     50 	return NISSY_ERROR_INVALID_SOLVER;
     51 }
     52 
     53 STATIC long long
     54 dataid_h48(const char *str, char buf[static NISSY_SIZE_DATAID])
     55 {
     56 	uint8_t h;
     57 	long long err;
     58 
     59 	err = parse_h48h(str, &h);
     60 	if (err < 0)
     61 		return err;
     62 
     63 	sprintf(buf, "h48h%" PRIu8, h);
     64 	return NISSY_OK;
     65 }