libscomp/input.c

63 lines
1.1 KiB
C

#include "input.h"
enum { READ, DISCARD };
void libscomm_reset(struct libscomm_input *in) {
in->state = READ;
in->len = 0;
}
static enum libscomm_input_r parse(struct libscomm_input *in,
struct libscomm_line *line) {
char *s = in->intbuf;
enum libscomm_input_r r = LIBSCOMM_ARG_OVERFLOW;
line->name = (*s == ':');
line->len = 0;
while (line->len < LIBSCOMM_MAXARG) {
line->buf[line->len++] = s;
for (; *s && *s != '\t'; s++);
if (!*s) {
r = LIBSCOMM_COMPLETE;
break;
} else {
*s = 0;
s++;
}
}
libscomm_reset(in);
return r;
}
enum libscomm_input_r libscomm_read(struct libscomm_input *in,
char **s,
struct libscomm_line *line) {
char c;
while ((c = **s)) {
(*s)++;
if (in->state == DISCARD) {
if (c == '\n') {
libscomm_reset(in);
return LIBSCOMM_OVERFLOW;
}
} else {
switch (c) {
case '\n':
in->intbuf[in->len] = 0;
return parse(in, line);
default:
in->intbuf[in->len++] = c;
if (in->len == LIBSCOMM_MAXBUF)
in->state = DISCARD;
}
}
}
return LIBSCOMM_MORE;
}