#include <ctype.h>
#include <errno.h>
#include <stdlib.h>
#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 == '\0')) {
max = idx;
if (min == num)
min = idx;
set[idx] = 1;
}
min = num;
} else
return -1;
str = end + 1;
} while (*end != '\0');
return str - input;
}