#include <string.h>
#include "parse.h"
int
parse_set(const char *input, uint16_t set[], unsigned int num)
{
unsigned int idx;
const char *str = input;
char *end = NULL;
unsigned int min, max;
memset(set, 0, num * sizeof(uint16_t));
while (isblank(*str))
str++;
if (!isdigit(*str) || *str == '\0')
return -1;
while (isblank(*str))
str++;
if (*str == '\0')
return -1;
min = num;
do {
while (isblank(*str))
str++;
if (!isdigit(*str))
return -1;
errno = 0;
idx = strtoul(str, &end, 10);
if (errno || end == NULL || idx >= num)
return -1;
while (isblank(*end))
end++;
if (*end == '-') {
if (min == num)
min = idx;
else
return -1;
} else if ((*end == ',') || (*end == ':') || (*end == '\0')) {
max = idx;
if (min == num)
min = idx;
set[idx] = 1;
}
min = num;
} else
return -1;
str = end + 1;
} while ((*end != '\0') && (*end != ':'));
return str - input;
}
int
parse_branch_ratio(const char *input, float *branch_ratio)
{
const char *str = input;
char *end = NULL;
while (isblank(*str))
str++;
if (*str == '\0')
return -1;
while ((*str != '\0') && (*str != ':'))
str++;
if (*str == '\0')
return 0;
if (*str != ':')
return -1;
str++;
errno = 0;
*branch_ratio = strtof(str, &end);
if (errno || end == NULL)
return -1;
if (*end != '\0')
return -1;
str = end + 1;
return str - input;
}