a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync#include <stdint.h>
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync#include <string.h>
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync#include <stdio.h>
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync#include <ipxe/linebuf.h>
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsyncstatic const char data1[] =
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync"Hello world\r\n"
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync"This is a reasonably nice set of lines\n"
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync"with not many different terminators\r\n\r\n"
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync"There should be exactly one blank line above\n"
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync"and this line should never appear at all since it has no terminator";
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsyncvoid linebuf_test ( void ) {
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync struct line_buffer linebuf;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync const char *data = data1;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync size_t len = ( sizeof ( data1 ) - 1 /* be mean; strip the NUL */ );
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync ssize_t frag_len;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync char *line;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync memset ( &linebuf, 0, sizeof ( linebuf ) );
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync while ( len ) {
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync frag_len = line_buffer ( &linebuf, data, len );
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync if ( frag_len < 0 ) {
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync printf ( "line_buffer() failed: %s\n",
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync strerror ( frag_len ) );
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync return;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync }
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync data += frag_len;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync len -= frag_len;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync if ( ( line = buffered_line ( &linebuf ) ) )
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync printf ( "\"%s\"\n", line );
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync }
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync empty_line_buffer ( &linebuf );
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync}