Add buf_put_ios() utility function

Fills a "struct buf" with the entire remainder of a stream.
This commit is contained in:
Lassi Kortela 2019-08-14 13:39:11 +03:00
parent a6ecac95e1
commit 44a8208d38
2 changed files with 17 additions and 0 deletions

16
c/buf.c
View File

@ -7,6 +7,9 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "dtypes.h"
#include "ios.h"
#include "buf.h" #include "buf.h"
struct buf *buf_new(void) { return calloc(1, sizeof(struct buf)); } struct buf *buf_new(void) { return calloc(1, sizeof(struct buf)); }
@ -28,6 +31,19 @@ char *buf_resb(struct buf *buf, size_t nbyte)
return place; return place;
} }
void buf_put_ios(struct buf *buf, struct ios *ios)
{
const size_t chunksize = 512;
size_t nread;
char *chunk;
do {
chunk = buf_resb(buf, chunksize);
nread = ios_readall(ios, chunk, chunksize);
buf->fill -= (chunksize - nread);
} while (nread);
}
void buf_putc(struct buf *buf, int c) { buf_resb(buf, 1)[0] = c; } void buf_putc(struct buf *buf, int c) { buf_resb(buf, 1)[0] = c; }
void buf_putb(struct buf *buf, const void *bytes, size_t nbyte) void buf_putb(struct buf *buf, const void *bytes, size_t nbyte)

View File

@ -9,6 +9,7 @@ struct buf {
struct buf *buf_new(void); struct buf *buf_new(void);
char *buf_resb(struct buf *buf, size_t nbyte); char *buf_resb(struct buf *buf, size_t nbyte);
void buf_put_ios(struct buf *buf, struct ios *ios);
void buf_putc(struct buf *buf, int c); void buf_putc(struct buf *buf, int c);
void buf_putb(struct buf *buf, const void *bytes, size_t nbyte); void buf_putb(struct buf *buf, const void *bytes, size_t nbyte);
void buf_puts(struct buf *buf, const char *s); void buf_puts(struct buf *buf, const char *s);