From: Martin Mares Date: Tue, 21 Mar 2006 09:24:36 +0000 (+0100) Subject: Renamed fb-gbuf to fb-grow as Robert suggested. X-Git-Tag: holmes-import~650^2~19 X-Git-Url: http://mj.ucw.cz/gitweb/?a=commitdiff_plain;h=fd6518b811bbe6986a933f9cfc73d14834153dcd;p=libucw.git Renamed fb-gbuf to fb-grow as Robert suggested. --- diff --git a/lib/Makefile b/lib/Makefile index 451a11d8..8535b687 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -1,4 +1,4 @@ -# Makefile for the UCW Library (c) 1997--2005 Martin Mares +# Makefile for the UCW Library (c) 1997--2006 Martin Mares DIRS+=lib @@ -14,7 +14,7 @@ LIBUCW_MODS= \ conf ipaccess \ profile \ fastbuf ff-printf ff-utf8 \ - fb-file carefulio fb-mem fb-temp fb-mmap fb-limfd fb-buffer fb-gbuf \ + fb-file carefulio fb-mem fb-temp fb-mmap fb-limfd fb-buffer fb-grow \ str_ctype str_upper str_lower unicode-utf8 stkstring \ wildmatch wordsplit ctmatch patimatch patmatch regex \ prime primetable random timer randomkey \ diff --git a/lib/fb-gbuf.c b/lib/fb-gbuf.c deleted file mode 100644 index 0b04817f..00000000 --- a/lib/fb-gbuf.c +++ /dev/null @@ -1,137 +0,0 @@ -/* - * UCW Library -- Fast Buffered I/O on Growing Buffers - * - * (c) 2006 Martin Mares - * - * This software may be freely distributed and used according to the terms - * of the GNU Lesser General Public License. - */ - -#include "lib/lib.h" -#include "lib/fastbuf.h" - -#include - -struct fb_gbuf { - struct fastbuf fb; - byte *last_written; -}; -#define FB_GBUF(f) ((struct fb_gbuf *)(f)->is_fastbuf) - -static int -fbgrow_refill(struct fastbuf *b) -{ - if (b->bstop != FB_GBUF(b)->last_written) - { - /* There was an intervening flush */ - b->bstop = FB_GBUF(b)->last_written; - b->pos = b->bstop - b->buffer; - return 1; - } - /* We are at the end */ - return 0; -} - -static void -fbgrow_spout(struct fastbuf *b) -{ - if (b->bptr >= b->bufend) - { - uns len = b->bufend - b->buffer; - b->buffer = xrealloc(b->buffer, 2*len); - b->bufend = b->buffer + 2*len; - b->bstop = b->buffer; - b->bptr = b->buffer + len; - } -} - -static void -fbgrow_seek(struct fastbuf *b, sh_off_t pos, int whence) -{ - ASSERT(FB_GBUF(b)->last_written); /* Seeks allowed only in read mode */ - sh_off_t len = FB_GBUF(b)->last_written - b->buffer; - if (whence == SEEK_END) - pos += len; - ASSERT(pos >= 0 && pos <= len); - b->bptr = b->buffer + pos; - b->bstop = FB_GBUF(b)->last_written; - b->pos = len; -} - -static void -fbgrow_close(struct fastbuf *b) -{ - xfree(b->buffer); - xfree(b); -} - -struct fastbuf * -fbgrow_create(unsigned basic_size) -{ - struct fastbuf *b = xmalloc_zero(sizeof(struct fb_gbuf)); - b->buffer = xmalloc(basic_size); - b->bufend = b->buffer + basic_size; - b->bptr = b->bstop = b->buffer; - b->name = ""; - b->refill = fbgrow_refill; - b->spout = fbgrow_spout; - b->seek = fbgrow_seek; - b->close = fbgrow_close; - b->can_overwrite_buffer = 1; - return b; -} - -void -fbgrow_reset(struct fastbuf *b) -{ - b->bptr = b->bstop = b->buffer; - b->pos = 0; - FB_GBUF(b)->last_written = NULL; -} - -void -fbgrow_rewind(struct fastbuf *b) -{ - if (!FB_GBUF(b)->last_written) - { - /* Last operation was a write, so remember the end position */ - FB_GBUF(b)->last_written = b->bptr; - } - b->bptr = b->buffer; - b->bstop = FB_GBUF(b)->last_written; - b->pos = b->bstop - b->buffer; -} - -#ifdef TEST - -int main(void) -{ - struct fastbuf *f; - int t; - - f = fbgrow_create(3); - for (uns i=0; i<5; i++) - { - fbgrow_write(f); - bwrite(f, "12345", 5); - bwrite(f, "12345", 5); - printf("<%d>", (int)btell(f)); - bflush(f); - printf("<%d>", (int)btell(f)); - fbgrow_rewind(f); - printf("<%d>", (int)btell(f)); - while ((t = bgetc(f)) >= 0) - putchar(t); - printf("<%d>", (int)btell(f)); - fbgrow_rewind(f); - bseek(f, -1, SEEK_END); - printf("<%d>", (int)btell(f)); - while ((t = bgetc(f)) >= 0) - putchar(t); - printf("<%d>\n", (int)btell(f)); - } - bclose(f); - return 0; -} - -#endif diff --git a/lib/fb-grow.c b/lib/fb-grow.c new file mode 100644 index 00000000..0b04817f --- /dev/null +++ b/lib/fb-grow.c @@ -0,0 +1,137 @@ +/* + * UCW Library -- Fast Buffered I/O on Growing Buffers + * + * (c) 2006 Martin Mares + * + * This software may be freely distributed and used according to the terms + * of the GNU Lesser General Public License. + */ + +#include "lib/lib.h" +#include "lib/fastbuf.h" + +#include + +struct fb_gbuf { + struct fastbuf fb; + byte *last_written; +}; +#define FB_GBUF(f) ((struct fb_gbuf *)(f)->is_fastbuf) + +static int +fbgrow_refill(struct fastbuf *b) +{ + if (b->bstop != FB_GBUF(b)->last_written) + { + /* There was an intervening flush */ + b->bstop = FB_GBUF(b)->last_written; + b->pos = b->bstop - b->buffer; + return 1; + } + /* We are at the end */ + return 0; +} + +static void +fbgrow_spout(struct fastbuf *b) +{ + if (b->bptr >= b->bufend) + { + uns len = b->bufend - b->buffer; + b->buffer = xrealloc(b->buffer, 2*len); + b->bufend = b->buffer + 2*len; + b->bstop = b->buffer; + b->bptr = b->buffer + len; + } +} + +static void +fbgrow_seek(struct fastbuf *b, sh_off_t pos, int whence) +{ + ASSERT(FB_GBUF(b)->last_written); /* Seeks allowed only in read mode */ + sh_off_t len = FB_GBUF(b)->last_written - b->buffer; + if (whence == SEEK_END) + pos += len; + ASSERT(pos >= 0 && pos <= len); + b->bptr = b->buffer + pos; + b->bstop = FB_GBUF(b)->last_written; + b->pos = len; +} + +static void +fbgrow_close(struct fastbuf *b) +{ + xfree(b->buffer); + xfree(b); +} + +struct fastbuf * +fbgrow_create(unsigned basic_size) +{ + struct fastbuf *b = xmalloc_zero(sizeof(struct fb_gbuf)); + b->buffer = xmalloc(basic_size); + b->bufend = b->buffer + basic_size; + b->bptr = b->bstop = b->buffer; + b->name = ""; + b->refill = fbgrow_refill; + b->spout = fbgrow_spout; + b->seek = fbgrow_seek; + b->close = fbgrow_close; + b->can_overwrite_buffer = 1; + return b; +} + +void +fbgrow_reset(struct fastbuf *b) +{ + b->bptr = b->bstop = b->buffer; + b->pos = 0; + FB_GBUF(b)->last_written = NULL; +} + +void +fbgrow_rewind(struct fastbuf *b) +{ + if (!FB_GBUF(b)->last_written) + { + /* Last operation was a write, so remember the end position */ + FB_GBUF(b)->last_written = b->bptr; + } + b->bptr = b->buffer; + b->bstop = FB_GBUF(b)->last_written; + b->pos = b->bstop - b->buffer; +} + +#ifdef TEST + +int main(void) +{ + struct fastbuf *f; + int t; + + f = fbgrow_create(3); + for (uns i=0; i<5; i++) + { + fbgrow_write(f); + bwrite(f, "12345", 5); + bwrite(f, "12345", 5); + printf("<%d>", (int)btell(f)); + bflush(f); + printf("<%d>", (int)btell(f)); + fbgrow_rewind(f); + printf("<%d>", (int)btell(f)); + while ((t = bgetc(f)) >= 0) + putchar(t); + printf("<%d>", (int)btell(f)); + fbgrow_rewind(f); + bseek(f, -1, SEEK_END); + printf("<%d>", (int)btell(f)); + while ((t = bgetc(f)) >= 0) + putchar(t); + printf("<%d>\n", (int)btell(f)); + } + bclose(f); + return 0; +} + +#endif