add buffer_tosa (buffer writing to auto-growing stralloc)
parent
1d76baf2ef
commit
02818883df
@ -1,10 +1,10 @@
|
||||
#include "buffer.h"
|
||||
|
||||
extern int buffer_stubborn(ssize_t (*op)(),int fd,const char* buf, size_t len);
|
||||
extern int buffer_stubborn(ssize_t (*op)(),int fd,const char* buf, size_t len,void* cookie);
|
||||
|
||||
extern int buffer_flush(buffer* b) {
|
||||
register int p;
|
||||
if (!(p=b->p)) return 0; /* buffer already empty */
|
||||
b->p=0;
|
||||
return buffer_stubborn(b->op,b->fd,b->x,p);
|
||||
return buffer_stubborn(b->op,b->fd,b->x,p,b);
|
||||
}
|
||||
|
@ -0,0 +1,27 @@
|
||||
#include "stralloc.h"
|
||||
#include "buffer.h"
|
||||
|
||||
static ssize_t strallocwrite(int fd,char* buf,size_t len,void* myself) {
|
||||
buffer* b=myself;
|
||||
stralloc* sa=b->cookie;
|
||||
sa->len+=len;
|
||||
if (stralloc_readyplus(sa,1024)==0) return 0;
|
||||
b->x=sa->s+sa->len;
|
||||
b->p=0;
|
||||
b->a=1024;
|
||||
(void)fd;
|
||||
(void)buf;
|
||||
return len;
|
||||
}
|
||||
|
||||
int buffer_tosa(buffer* b,stralloc* sa) {
|
||||
if (stralloc_ready(sa,1024)==0) return -1;
|
||||
b->x=sa->s;
|
||||
b->p=0;
|
||||
b->n=0;
|
||||
b->a=1024;
|
||||
b->fd=0;
|
||||
b->op=strallocwrite;
|
||||
b->cookie=sa;
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue