pkin/strff/strff_read.c
2026-09-04 19:59:24 +02:00

44 lines
892 B
C

#include "../strff.h"
#include <errno.h>
int strff_read(struct strff *strff, size_t buflen, char buf[buflen])
{
size_t nread;
int r;
char c;
int isnewline;
if (strff->fieldtype == STRFF_TYPE_NONE) {
errno = EINVAL; /* cannot read NONE field */
return -1;
}
isnewline = 1;
nread = 0;
while (nread < buflen) {
r = buffer_peekc(&strff->b, &c);
if (r < 0) return -1;
if (r == 0) {
if (isnewline && nread > 0)
nread--; /* remove last \n */
break;
}
if (strff->fieldtype == STRFF_TYPE_EQUAL && c == '\n')
break;
else if (strff->fieldtype == STRFF_TYPE_BRACKET) {
if (isnewline && c == '[') {
if (nread > 0)
nread--; /* remove last \n */
break;
}
isnewline = 0;
if (c == '\n')
isnewline = 1;
}
buf[nread++] = c;
strff->b.p++;
}
return nread;
}