add buffer_tosa (buffer writing to auto-growing stralloc)

master
leitner 16 years ago
parent 1d76baf2ef
commit 02818883df

@ -1,5 +1,6 @@
0.29:
save 8 bytes in taia.h for 64-bit systems
add buffer_tosa (buffer writing to auto-growing stralloc)
0.28:
add uint64 pack and unpack routines

@ -14,9 +14,10 @@ typedef struct buffer {
int fd; /* passed as first argument to op */
ssize_t (*op)(); /* use read(2) or write(2) */
enum { NOTHING, FREE, MUNMAP } todo;
void* cookie;
} buffer;
#define BUFFER_INIT(op,fd,buf,len) { (buf), 0, 0, (len), (fd), (op), NOTHING }
#define BUFFER_INIT(op,fd,buf,len) { (buf), 0, 0, (len), (fd), (op), NOTHING, NULL }
#define BUFFER_INIT_FREE(op,fd,buf,len) { (buf), 0, 0, (len), (fd), (op), FREE }
#define BUFFER_INIT_READ(op,fd,buf,len) BUFFER_INIT(op,fd,buf,len) /*obsolete*/
#define BUFFER_INSIZE 8192
@ -135,7 +136,8 @@ int buffer_get_new_token_sa_pred(buffer* b,stralloc* sa,sa_predicate p);
/* make a buffer from a stralloc.
* Do not change the stralloc after this! */
void buffer_fromsa(buffer* b,stralloc* sa);
void buffer_fromsa(buffer* b,stralloc* sa); /* read from sa */
int buffer_tosa(buffer*b,stralloc* sa); /* write to sa, auto-growing it */
#endif
#endif

@ -1,11 +1,11 @@
#include "buffer.h"
extern int buffer_stubborn_read(ssize_t (*op)(),int fd,const char* buf, size_t len);
extern int buffer_stubborn_read(ssize_t (*op)(),int fd,const char* buf, size_t len,void* cookie);
int buffer_feed(buffer* b) {
if (b->p==b->n) {
int w;
if ((w=buffer_stubborn_read(b->op,b->fd,b->x,b->a))<0)
if ((w=buffer_stubborn_read(b->op,b->fd,b->x,b->a,b))<0)
return -1;
b->n=w;
b->p=0;

@ -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);
}

@ -9,4 +9,5 @@ void buffer_init(buffer* b,ssize_t (*op)(),int fd,
b->p=0;
b->n=0;
b->todo=NOTHING;
b->cookie=0;
}

@ -1,13 +1,13 @@
#include "byte.h"
#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);
int buffer_put(buffer* b,const char* buf,size_t len) {
if (len>b->a-b->p) { /* doesn't fit */
if (buffer_flush(b)==-1) return -1;
if (len>b->a) {
if (buffer_stubborn(b->op,b->fd,buf,len)<0) return -1;
if (buffer_stubborn(b->op,b->fd,buf,len,b)<0) return -1;
return 0;
}
}

@ -1,10 +1,10 @@
#include <errno.h>
#include "buffer.h"
int buffer_stubborn(ssize_t (*op)(),int fd,const char* buf, size_t len) {
int buffer_stubborn(ssize_t (*op)(),int fd,const char* buf, size_t len,void* cookie) {
int w;
while (len) {
if ((w=op(fd,buf,len))<0) {
if ((w=op(fd,buf,len,cookie))<0) {
if (errno == EINTR) continue;
return -1;
};

@ -1,10 +1,10 @@
#include <errno.h>
#include "buffer.h"
int buffer_stubborn_read(ssize_t (*op)(),int fd,const char* buf, size_t len) {
int buffer_stubborn_read(ssize_t (*op)(),int fd,const char* buf, size_t len,void* cookie) {
int w;
for (;;) {
if ((w=op(fd,buf,len))<0)
if ((w=op(fd,buf,len,cookie))<0)
if (errno == EINTR) continue;
break;
}

@ -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;
}

14
t.c

@ -41,13 +41,25 @@ int64 writecb(int64 fd,const void* buf,uint64 n) {
}
int main(int argc,char* argv[]) {
stralloc a;
buffer b;
int i;
stralloc_init(&a);
buffer_tosa(&b,&a);
for (i=0; i<100; ++i)
buffer_puts(&b,"foo bar baz!\n");
buffer_flush(&b);
buffer_putsa(buffer_1,&a);
buffer_flush(buffer_1);
#if 0
char* c=fmt_strm_alloca("foo"," bar","\n");
write(1,c,strlen(c));
(void)argc;
(void)argv;
#endif
#if 0
io_batch* b=iob_new(1234);
int64 fd=open("t.c",0);

Loading…
Cancel
Save