libscomp/input.c

61 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;
line->name = (*s == ':');
line->len = 0;
while (line->len < LIBSCOMM_MAXARG) {
line->buf[line->len++] = s;
for (; *s && *s != '\t'; s++);
if (!*s) {
return LIBSCOMM_COMPLETE;
} else {
*s = 0;
s++;
}
}
return LIBSCOMM_ARG_OVERFLOW;
}
enum libscomm_input_r libscomm_read(struct libscomm_input *in,
struct libscomm_inbuf *buf,
struct libscomm_line *line) {
char c;
while (buf->l > 0) {
buf->l--;
c = *buf->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;
}