#include "tests.h"
size_t lineno = 0;
void check(int cond, const char *format, ...)
{
if (!cond) {
va_list args;
fprintf(stderr, "line %zd: ", lineno);
va_start(args, format);
vfprintf(stderr, format, args);
va_end(args);
fprintf(stderr, "\n");
exit(1);
}
}
size_t skipspaces(const unsigned char *buf, size_t i)
{
while (isspace(buf[i])) ++i;
return i;
}
size_t encode(unsigned char *dest, size_t *dest_len, const unsigned char *buf)
{
size_t i = 0, j;
utf8proc_ssize_t d = 0;
for (;;) {
int c;
i = skipspaces(buf, i);
for (j=i; buf[j] && strchr("0123456789abcdef", tolower(buf[j])); ++j)
;
if (j == i) {
dest[d] = 0;
*dest_len = (size_t)d;
return i + 1;
}
check(sscanf((char *) (buf + i), "%x", (unsigned int *)&c) == 1, "invalid hex input %s", buf+i);
i = j;
d += utf8proc_encode_char(c, (utf8proc_uint8_t *) (dest + d));
}
}
size_t simple_getline(unsigned char buf[8192], FILE *f) {
size_t i = 0;
while (i < 8191) {
int c = getc(f);
if (c == EOF || c == '\n') break;
buf[i++] = (unsigned char) c;
}
buf[i] = 0;
return i;
}