Add buf_put_ios() utility function
Fills a "struct buf" with the entire remainder of a stream.
This commit is contained in:
@@ -7,6 +7,9 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "dtypes.h"
|
||||
#include "ios.h"
|
||||
|
||||
#include "buf.h"
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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_putb(struct buf *buf, const void *bytes, size_t nbyte)
|
||||
|
||||
@@ -9,6 +9,7 @@ struct buf {
|
||||
|
||||
struct buf *buf_new(void);
|
||||
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_putb(struct buf *buf, const void *bytes, size_t nbyte);
|
||||
void buf_puts(struct buf *buf, const char *s);
|
||||
|
||||
Reference in New Issue
Block a user