X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;ds=inline;f=lib%2Ffastbuf.c;h=0939b6d512533ad5bb9979b45eb3193a2b6879f8;hb=4390bf90103265fe6e11f4c446b78f38bdc132b3;hp=ee7d634660b3d8e88c5cd8ee9ecab5c6db70d15c;hpb=c8f4b2b31f28fc981da92536e4bede7712a0ba81;p=libucw.git diff --git a/lib/fastbuf.c b/lib/fastbuf.c index ee7d6346..0939b6d5 100644 --- a/lib/fastbuf.c +++ b/lib/fastbuf.c @@ -1,94 +1,20 @@ /* - * Sherlock Library -- Fast File Buffering + * Sherlock Library -- Fast Buffered I/O * - * (c) 1997--1999 Martin Mares + * (c) 1997--2000 Martin Mares */ +#include "lib/lib.h" +#include "lib/fastbuf.h" + #include #include -#include -#include -#include - -#include "lib.h" -#include "fastbuf.h" -#include "lfs.h" - -struct fastbuf *__bfdopen(int fd, uns buffer, byte *name) -{ - struct fastbuf *b = xmalloc(sizeof(struct fastbuf)); - - b->buflen = buffer; - b->buffer = xmalloc(buffer); - b->bptr = b->bstop = b->buffer; - b->bufend = b->buffer + buffer; - b->name = stralloc(name); - b->pos = b->fdpos = 0; - b->fd = fd; - return b; -} - -struct fastbuf * -bopen(byte *name, uns mode, uns buffer) -{ - int fd; - -#ifdef SHERLOCK_CONFIG_LFS - mode |= O_LARGEFILE; -#endif - fd = open(name, mode, 0666); - - if (fd < 0) - die("Unable to %s file %s: %m", - (mode & O_CREAT) ? "create" : "open", name); - return __bfdopen(fd, buffer, name); -} - -struct fastbuf * -bfdopen(int fd, uns buffer) -{ - byte x[32]; - - sprintf(x, "fd%d", fd); - return __bfdopen(fd, buffer, x); -} void bclose(struct fastbuf *f) { bflush(f); - close(f->fd); - free(f->name); - free(f->buffer); - free(f); -} - -static int -rdbuf(struct fastbuf *f) -{ - int l = read(f->fd, f->buffer, f->buflen); - - if (l < 0) - die("Error reading %s: %m", f->name); - f->bptr = f->buffer; - f->bstop = f->buffer + l; - f->pos = f->fdpos; - f->fdpos += l; - return l; -} - -static void -wrbuf(struct fastbuf *f) -{ - int l = f->bptr - f->buffer; - - if (l) - { - if (write(f->fd, f->buffer, l) != l) - die("Error writing %s: %m", f->name); - f->bptr = f->buffer; - f->fdpos += l; - f->pos = f->fdpos; - } + f->close(f); + xfree(f); } void bflush(struct fastbuf *f) @@ -101,7 +27,7 @@ void bflush(struct fastbuf *f) f->pos = f->fdpos; } else /* Write data... */ - wrbuf(f); + f->spout(f); } } @@ -112,16 +38,12 @@ inline void bsetpos(struct fastbuf *f, sh_off_t pos) else { bflush(f); - if (f->fdpos != pos && sh_seek(f->fd, pos, SEEK_SET) < 0) - die("lseek on %s: %m", f->name); - f->fdpos = f->pos = pos; + f->seek(f, pos, SEEK_SET); } } void bseek(struct fastbuf *f, sh_off_t pos, int whence) { - sh_off_t l; - switch (whence) { case SEEK_SET: @@ -130,10 +52,7 @@ void bseek(struct fastbuf *f, sh_off_t pos, int whence) return bsetpos(f, btell(f) + pos); case SEEK_END: bflush(f); - l = sh_seek(f->fd, pos, whence); - if (l < 0) - die("lseek on %s: %m", f->name); - f->fdpos = f->pos = l; + f->seek(f, pos, SEEK_END); break; default: die("bseek: invalid whence=%d", whence); @@ -144,7 +63,7 @@ int bgetc_slow(struct fastbuf *f) { if (f->bptr < f->bstop) return *f->bptr++; - if (!rdbuf(f)) + if (!f->refill(f)) return EOF; return *f->bptr++; } @@ -153,7 +72,7 @@ int bpeekc_slow(struct fastbuf *f) { if (f->bptr < f->bstop) return *f->bptr; - if (!rdbuf(f)) + if (!f->refill(f)) return EOF; return *f->bptr; } @@ -161,7 +80,7 @@ int bpeekc_slow(struct fastbuf *f) void bputc_slow(struct fastbuf *f, byte c) { if (f->bptr >= f->bufend) - wrbuf(f); + f->spout(f); *f->bptr++ = c; } @@ -273,7 +192,7 @@ void bread_slow(struct fastbuf *f, void *b, uns l) if (!k) { - rdbuf(f); + f->refill(f); k = f->bstop - f->bptr; if (!k) die("bread on %s: file exhausted", f->name); @@ -295,7 +214,7 @@ void bwrite_slow(struct fastbuf *f, void *b, uns l) if (!k) { - wrbuf(f); + f->spout(f); k = f->bufend - f->bptr; } if (k > l) @@ -328,60 +247,3 @@ bgets(struct fastbuf *f, byte *b, uns l) } die("%s: Line too long", f->name); } - -void bbcopy(struct fastbuf *f, struct fastbuf *t, uns l) -{ - uns rf = f->bstop - f->bptr; - - if (!l) - return; - if (rf) - { - uns k = (rf <= l) ? rf : l; - bwrite(t, f->bptr, k); - f->bptr += k; - l -= k; - } - while (l >= t->buflen) - { - wrbuf(t); - if ((uns) read(f->fd, t->buffer, t->buflen) != t->buflen) - die("bbcopy: %s exhausted", f->name); - f->pos = f->fdpos; - f->fdpos += t->buflen; - f->bstop = f->bptr = f->buffer; - t->bptr = t->bufend; - l -= t->buflen; - } - while (l) - { - uns k = t->bufend - t->bptr; - - if (!k) - { - wrbuf(t); - k = t->bufend - t->bptr; - } - if (k > l) - k = l; - bread(f, t->bptr, k); - t->bptr += k; - l -= k; - } -} - -#ifdef TEST - -int main(int argc, char **argv) -{ - struct fastbuf *f, *t; - int c; - - f = bopen("/etc/profile", O_RDONLY, 16); - t = bfdopen(1, 13); - bbcopy(f, t, 100); - bclose(f); - bclose(t); -} - -#endif