From a327b758311b8abc082261107a8a2d652a46793e Mon Sep 17 00:00:00 2001 From: leitner Date: Sun, 8 Jun 2003 22:10:10 +0000 Subject: [PATCH] add buffer_fromsa (make virtual buffer from stralloc) --- CHANGES | 3 +++ buffer.h | 4 ++++ buffer/buffer_fromsa.3 | 19 +++++++++++++++++++ buffer/buffer_fromsa.c | 15 +++++++++++++++ test/buffer_fromsa.c | 21 +++++++++++++++++++++ 5 files changed, 62 insertions(+) create mode 100644 buffer/buffer_fromsa.3 create mode 100644 buffer/buffer_fromsa.c create mode 100644 test/buffer_fromsa.c diff --git a/CHANGES b/CHANGES index 1d1e97f..a57de4c 100644 --- a/CHANGES +++ b/CHANGES @@ -1,3 +1,6 @@ +0.16: + add buffer_fromsa (make buffer from stralloc) + 0.15: man page update (document stralloc return values) add stralloc_chop and stralloc_chomp diff --git a/buffer.h b/buffer.h index 3b9c5c2..04721e2 100644 --- a/buffer.h +++ b/buffer.h @@ -102,6 +102,10 @@ typedef int (*sa_predicate)(stralloc* sa); /* like buffer_get_token_sa but the token ends when your predicate says so */ extern int buffer_get_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); #endif #endif diff --git a/buffer/buffer_fromsa.3 b/buffer/buffer_fromsa.3 new file mode 100644 index 0000000..577aa4e --- /dev/null +++ b/buffer/buffer_fromsa.3 @@ -0,0 +1,19 @@ +.TH buffer_fromsa 3 +.SH NAME +buffer_fromsa \- initialize buffer structure from stralloc +.SH SYNTAX +.B #include + +int \fBbuffer_fromsa\fR(buffer* \fIb\fR,stralloc* \fIsa\fR); +.SH DESCRIPTION +buffer_fromsa makes a virtual read buffer from a stralloc. The buffer +reading functions will be able to read until the end of the data in the +stralloc, then signal EOF. The buffer does not have its own buffer +space, it reuses the one in the stralloc. + +Do not touch the stralloc until your are done with the buffer! In +particular, do not call stralloc_free on it! + +Reading data from the buffer will not change the stralloc. +.SH "SEE ALSO" +buffer_init(3), buffer(3) diff --git a/buffer/buffer_fromsa.c b/buffer/buffer_fromsa.c new file mode 100644 index 0000000..6c57e43 --- /dev/null +++ b/buffer/buffer_fromsa.c @@ -0,0 +1,15 @@ +#include "stralloc.h" +#include "buffer.h" + +static int dummyreadwrite(int fd,char* buf,unsigned long int len) { + return 0; +} + +void buffer_fromsa(buffer* b,stralloc* sa) { + b->x=sa->s; + b->p=0; + b->n=sa->len; + b->a=sa->a; + b->fd=0; + b->op=dummyreadwrite; +} diff --git a/test/buffer_fromsa.c b/test/buffer_fromsa.c new file mode 100644 index 0000000..5bc8946 --- /dev/null +++ b/test/buffer_fromsa.c @@ -0,0 +1,21 @@ +#include "stralloc.h" +#include "buffer.h" + +int main() { + stralloc sa; + buffer b; + stralloc line; + + stralloc_init(&sa); + stralloc_init(&line); + stralloc_copys(&sa,"this is a test\nline 2\n"); + buffer_fromsa(&b,&sa); + + while (buffer_getline_sa(&b,&line)==0) { + buffer_puts(buffer_1,"got line: \""); + if (stralloc_chop(&line)!='\n') break; + buffer_putsa(buffer_1,&line); + buffer_putsflush(buffer_1,"\"\n"); + stralloc_copys(&line,""); + } +}