From 4ad40244021880e90d670720ac4c3beeac70aebc Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Thu, 2 Jun 2005 11:34:46 +0200 Subject: heh. forgot a bunch of files for the new stream stuff. --- stream/stm_misc.c | 128 ++++++++++++++++++++++++ stream/stm_open.c | 222 ++++++++++++++++++++++++++++++++++++++++++ stream/stm_read.c | 262 +++++++++++++++++++++++++++++++++++++++++++++++++ stream/stm_write.c | 279 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 891 insertions(+) create mode 100644 stream/stm_misc.c create mode 100644 stream/stm_open.c create mode 100644 stream/stm_read.c create mode 100644 stream/stm_write.c (limited to 'stream') diff --git a/stream/stm_misc.c b/stream/stm_misc.c new file mode 100644 index 00000000..010676fd --- /dev/null +++ b/stream/stm_misc.c @@ -0,0 +1,128 @@ +/* + * Miscellaneous I/O functions + */ + +#include "fitz.h" + +int fz_tell(fz_stream *stm) +{ + if (stm->mode == FZ_SREAD) + return fz_rtell(stm); + return fz_wtell(stm); +} + +int fz_seek(fz_stream *stm, int offset, int whence) +{ + if (stm->mode == FZ_SREAD) + return fz_rseek(stm, offset, whence); + return fz_wseek(stm, offset, whence); +} + +/* + * Read a line terminated by LF or CR or CRLF. + */ + +int fz_readline(fz_stream *stm, char *mem, int n) +{ + char *s = mem; + int c = EOF; + while (n > 1) + { + c = fz_readbyte(stm); + if (c == EOF) + break; + if (c == '\r') { + c = fz_peekbyte(stm); + if (c == '\n') + c = fz_readbyte(stm); + break; + } + if (c == '\n') + break; + *s++ = c; + n--; + } + if (n) + *s = '\0'; + return s - mem; +} + +/* + * Utility function to consume all the contents of an input stream into + * a freshly allocated buffer; realloced and trimmed to size. + */ + +enum { CHUNKSIZE = 1024 * 4 }; + +int fz_readall(fz_buffer **bufp, fz_stream *stm) +{ + fz_buffer *real; + unsigned char *newbuf; + unsigned char *buf; + int len; + int pos; + int n; + + *bufp = nil; + + len = 0; + pos = 0; + buf = nil; + + while (1) + { + if (len - pos == 0) + { + len += CHUNKSIZE; + newbuf = fz_realloc(buf, len); + if (!newbuf) + { + fz_free(buf); + return -1; + } + buf = newbuf; + } + + n = fz_read(stm, buf + pos, len - pos); + + if (n < 0) + { + fz_free(buf); + return -1; + } + + pos += n; + + if (n < CHUNKSIZE) + { + if (pos > 0) + { + newbuf = fz_realloc(buf, pos); + if (!newbuf) + { + fz_free(buf); + return -1; + } + } + else newbuf = buf; + + real = *bufp = fz_malloc(sizeof(fz_buffer)); + if (!real) + { + fz_free(newbuf); + return -1; + } + + real->refs = 1; + real->ownsdata = 1; + real->bp = buf; + real->rp = buf; + real->wp = buf + pos; + real->ep = buf + pos; + real->eof = 1; + + return real->wp - real->rp; + } + } +} + diff --git a/stream/stm_open.c b/stream/stm_open.c new file mode 100644 index 00000000..d72d9837 --- /dev/null +++ b/stream/stm_open.c @@ -0,0 +1,222 @@ +/* + * Creation and destruction. + */ + +#include "fitz.h" + +static fz_stream * +newstm(int kind, int mode) +{ + fz_stream *stm; + + stm = fz_malloc(sizeof(fz_stream)); + if (!stm) + return nil; + + stm->refs = 1; + stm->kind = kind; + stm->mode = mode; + stm->dead = 0; + stm->error = nil; + stm->buffer = nil; + + stm->chain = nil; + stm->filter = nil; + stm->file = -1; + + return stm; +} + +fz_error * +fz_ioerror(fz_stream *stm) +{ + fz_error *error; + if (stm->error) + { + error = stm->error; + stm->error = nil; + return error; + } + return fz_throw("ioerror: no error"); +} + +fz_stream * +fz_keepstream(fz_stream *stm) +{ + stm->refs ++; + return stm; +} + +void +fz_dropstream(fz_stream *stm) +{ + stm->refs --; + if (stm->refs == 0) + { + if (stm->error) + { + fz_warn("unhandled %s", stm->error->msg); + fz_droperror(stm->error); + } + + if (stm->mode == FZ_SWRITE) + { + stm->buffer->eof = 1; + fz_flush(stm); + } + + switch (stm->kind) + { + case FZ_SFILE: + close(stm->file); + break; + case FZ_SFILTER: + fz_dropfilter(stm->filter); + fz_dropstream(stm->chain); + break; + case FZ_SBUFFER: + break; + } + + fz_dropbuffer(stm->buffer); + fz_free(stm); + } +} + +static fz_error * +openfile(fz_stream **stmp, char *path, int mode, int realmode) +{ + fz_error *error; + fz_stream *stm; + + stm = newstm(FZ_SFILE, mode); + if (!stm) + return fz_outofmem; + + error = fz_newbuffer(&stm->buffer, FZ_BUFSIZE); + if (error) + { + fz_free(stm); + return error; + } + + stm->file = open(path, realmode, 0666); + if (stm->file < 0) + { + fz_dropbuffer(stm->buffer); + fz_free(stm); + return fz_throw("ioerror: open '%s' failed: %s", path, strerror(errno)); + } + + *stmp = stm; + return nil; +} + +static fz_error * +openfilter(fz_stream **stmp, fz_filter *flt, fz_stream *src, int mode) +{ + fz_error *error; + fz_stream *stm; + + stm = newstm(FZ_SFILTER, mode); + if (!stm) + return fz_outofmem; + + error = fz_newbuffer(&stm->buffer, FZ_BUFSIZE); + if (error) + { + fz_free(stm); + return error; + } + + stm->chain = fz_keepstream(src); + stm->filter = fz_keepfilter(flt); + + *stmp = stm; + return nil; +} + +static fz_error * +openbuffer(fz_stream **stmp, fz_buffer *buf, int mode) +{ + fz_stream *stm; + + stm = newstm(FZ_SBUFFER, mode); + if (!stm) + return fz_outofmem; + + stm->buffer = fz_keepbuffer(buf); + + if (mode == FZ_SREAD) + stm->buffer->eof = 1; + + *stmp = stm; + return nil; +} + +fz_error * fz_openrfile(fz_stream **stmp, char *path) +{ + return openfile(stmp, path, FZ_SREAD, O_BINARY | O_RDONLY); +} + +fz_error * fz_openwfile(fz_stream **stmp, char *path) +{ + return openfile(stmp, path, FZ_SWRITE, + O_BINARY | O_WRONLY | O_CREAT | O_TRUNC); +} + +fz_error * fz_openafile(fz_stream **stmp, char *path) +{ + fz_error *error; + int t; + + error = openfile(stmp, path, FZ_SWRITE, O_BINARY | O_WRONLY); + if (error) + return error; + + t = lseek((*stmp)->file, 0, 2); + if (t < 0) + { + (*stmp)->dead = 1; + return fz_throw("ioerror: lseek: %s", strerror(errno)); + } + + return nil; +} + +fz_error * fz_openrfilter(fz_stream **stmp, fz_filter *flt, fz_stream *src) +{ + return openfilter(stmp, flt, src, FZ_SREAD); +} + +fz_error * fz_openwfilter(fz_stream **stmp, fz_filter *flt, fz_stream *src) +{ + return openfilter(stmp, flt, src, FZ_SWRITE); +} + +fz_error * fz_openrbuffer(fz_stream **stmp, fz_buffer *buf) +{ + return openbuffer(stmp, buf, FZ_SREAD); +} + +fz_error * fz_openwbuffer(fz_stream **stmp, fz_buffer *buf) +{ + return openbuffer(stmp, buf, FZ_SWRITE); +} + +fz_error * fz_openrmemory(fz_stream **stmp, char *mem, int len) +{ + fz_error *error; + fz_buffer *buf; + + error = fz_newbufferwithmemory(&buf, mem, len); + if (error) + return error; + + error = fz_openrbuffer(stmp, buf); + + fz_dropbuffer(buf); + + return error; +} + diff --git a/stream/stm_read.c b/stream/stm_read.c new file mode 100644 index 00000000..cb7c5c02 --- /dev/null +++ b/stream/stm_read.c @@ -0,0 +1,262 @@ +/* + * Input streams. + */ + +#include "fitz.h" + +int +fz_makedata(fz_stream *stm) +{ + fz_buffer *buf = stm->buffer; + fz_error *error; + fz_error *reason; + int produced; + int n; + + if (stm->dead) + return -1; + + if (stm->mode != FZ_SREAD) + return -1; + + if (buf->eof) + return 0; + + error = fz_rewindbuffer(buf); + if (error) + goto cleanup; + + if (buf->ep - buf->wp == 0) + { + error = fz_growbuffer(buf); + if (error) + goto cleanup; + } + + switch (stm->kind) + { + + case FZ_SFILE: + n = read(stm->file, buf->wp, buf->ep - buf->wp); + if (n == -1) + { + stm->error = fz_throw("ioerror: read: %s", strerror(errno)); + stm->dead = 1; + return -1; + } + if (n == 0) + buf->eof = 1; + buf->wp += n; + return n; + + case FZ_SFILTER: + produced = 0; + + while (1) + { + reason = fz_process(stm->filter, stm->chain->buffer, buf); + + if (stm->filter->produced) + produced = 1; + + if (reason == fz_ioneedin) + { + if (fz_makedata(stm->chain) < 0) + { + stm->dead = 1; + return -1; + } + } + + else if (reason == fz_ioneedout) + { + if (produced) + return 0; + + if (buf->rp > buf->bp) + { + error = fz_rewindbuffer(buf); + if (error) + goto cleanup; + } + else + { + error = fz_growbuffer(buf); + if (error) + goto cleanup; + } + } + + else if (reason == fz_iodone) + { + return 0; + } + + else + { + error = reason; + goto cleanup; + } + } + + case FZ_SBUFFER: + return 0; + } + + return -1; + +cleanup: + stm->error = error; + stm->dead = 1; + return -1; +} + +int fz_rtell(fz_stream *stm) +{ + fz_buffer *buf = stm->buffer; + int t; + + if (stm->dead) + return -1; + + if (stm->mode != FZ_SREAD) + return -1; + + switch (stm->kind) + { + case FZ_SFILE: + t = lseek(stm->file, 0, 1); + if (t < 0) + { + stm->dead = 1; + return -1; + } + return t - (buf->wp - buf->rp); + + case FZ_SFILTER: + return stm->filter->count - (buf->wp - buf->rp); + + case FZ_SBUFFER: + return buf->rp - buf->bp; + } + + return -1; +} + +int fz_rseek(fz_stream *stm, int offset, int whence) +{ + fz_buffer *buf = stm->buffer; + int t, c; + + if (stm->dead) + return -1; + + if (stm->mode != FZ_SREAD) + return -1; + + if (whence == 1) + { + int cur = fz_rtell(stm); + if (cur < 0) + return -1; + offset = cur + offset; + whence = 0; + } + + buf->eof = 0; + + switch (stm->kind) + { + case FZ_SFILE: + t = lseek(stm->file, offset, whence); + if (t < 0) + { + stm->error = fz_throw("ioerror: lseek: %s", strerror(errno)); + stm->dead = 1; + return -1; + } + + buf->rp = buf->bp; + buf->wp = buf->bp; + + return t; + + case FZ_SFILTER: + if (whence == 0) + { + if (offset < fz_tell(stm)) + { + stm->error = fz_throw("ioerror: cannot seek back in filter"); + stm->dead = 1; + return -1; + } + while (fz_tell(stm) < offset) + { + c = fz_readbyte(stm); + if (c == EOF) + break; + } + return fz_tell(stm); + } + else + { + stm->dead = 1; + return -1; + } + + case FZ_SBUFFER: + if (whence == 0) + buf->rp = CLAMP(buf->bp + offset, buf->bp, buf->ep); + else + buf->rp = CLAMP(buf->ep + offset, buf->bp, buf->ep); + return buf->rp - buf->bp; + } + + return -1; +} + +int fz_readbytex(fz_stream *stm) +{ + fz_buffer *buf = stm->buffer; + if (buf->rp == buf->wp) + { + if (buf->eof) return EOF; + if (fz_makedata(stm) < 0) return EOF; + } + if (buf->rp < buf->wp) + return *buf->rp++; + return EOF; +} + +int fz_peekbytex(fz_stream *stm) +{ + fz_buffer *buf = stm->buffer; + if (buf->rp == buf->wp) + { + if (buf->eof) return EOF; + if (fz_makedata(stm) < 0) return EOF; + } + if (buf->rp < buf->wp) + return *buf->rp; + return EOF; +} + +int fz_read(fz_stream *stm, unsigned char *mem, int n) +{ + fz_buffer *buf = stm->buffer; + int i = 0; + + while (i < n) + { + while (buf->rp < buf->wp && i < n) + mem[i++] = *buf->rp++; + if (buf->rp == buf->wp) + { + if (buf->eof) return i; + if (fz_makedata(stm) < 0) return -1; + } + } + + return i; +} + diff --git a/stream/stm_write.c b/stream/stm_write.c new file mode 100644 index 00000000..3b4a8baf --- /dev/null +++ b/stream/stm_write.c @@ -0,0 +1,279 @@ +/* + * Output streams. + */ + +#include "fitz.h" + +int fz_wtell(fz_stream *stm) +{ + fz_buffer *buf = stm->buffer; + int t; + + if (stm->dead) + return -1; + + if (stm->mode != FZ_SWRITE) + return -1; + + switch (stm->kind) + { + case FZ_SFILE: + t = lseek(stm->file, 0, 1); + if (t < 0) + { + stm->error = fz_throw("ioerror: lseek: %s", strerror(errno)); + stm->dead = 1; + return -1; + } + return t - (buf->wp - buf->rp); + + case FZ_SFILTER: + return stm->filter->count - (buf->wp - buf->rp); + + case FZ_SBUFFER: + return buf->wp - buf->bp; + } + + return -1; +} + +int fz_wseek(fz_stream *stm, int offset, int whence) +{ + fz_buffer *buf = stm->buffer; + int t; + + if (stm->dead) + return -1; + + if (stm->mode != FZ_SWRITE) + return -1; + + if (stm->kind != FZ_SFILE) + return -1; + + t = lseek(stm->file, offset, whence); + if (t < 0) + { + stm->error = fz_throw("ioerror: lseek: %s", strerror(errno)); + stm->dead = 1; + return -1; + } + + buf->rp = buf->bp; + buf->wp = buf->bp; + buf->eof = 0; + + return t; +} + +static int flushfilter(fz_stream *stm) +{ + fz_buffer *buf = stm->buffer; + fz_error *error; + fz_error *reason; + int t; + +loop: + + reason = fz_process(stm->filter, stm->buffer, stm->chain->buffer); + + if (reason == fz_ioneedin) + { + if (buf->rp > buf->ep) + fz_rewindbuffer(buf); + else + { + error = fz_growbuffer(buf); + if (error) + goto cleanup; + } + } + + else if (reason == fz_ioneedout) + { + t = fz_flush(stm->chain); + if (t < 0) + return -1; + } + + else if (reason == fz_iodone) + { + stm->dead = 2; /* special flag that we are dead because of eod */ + } + + else + { + error = reason; + goto cleanup; + } + + /* if we are at eof, repeat until other filter sets otherside to eof */ + if (buf->eof && !stm->chain->buffer->eof) + goto loop; + + return 0; + +cleanup: + stm->error = error; + stm->dead = 1; + return -1; +} + +/* + * Empty the buffer into the sink. + * Promise to make more space available. + * Called by fz_write and fz_dropstream. + * If buffer is eof, then all data must be flushed. + */ +int fz_flush(fz_stream *stm) +{ + fz_buffer *buf = stm->buffer; + fz_error *error; + int t; + + if (stm->dead == 2) + return 0; + + if (stm->dead) + return -1; + + if (stm->mode != FZ_SWRITE) + return -1; + + switch (stm->kind) + { + case FZ_SFILE: + while (buf->rp < buf->wp) + { + t = write(stm->file, buf->rp, buf->wp - buf->rp); + if (t < 0) + { + stm->error = fz_throw("ioerror: write: %s", strerror(errno)); + stm->dead = 1; + return -1; + } + + buf->rp += t; + } + + if (buf->rp > buf->bp) + fz_rewindbuffer(buf); + + return 0; + + case FZ_SFILTER: + return flushfilter(stm); + + case FZ_SBUFFER: + if (!buf->eof && buf->wp == buf->ep) + { + error = fz_growbuffer(buf); + if (error) + { + stm->error = error; + stm->dead = 1; + return -1; + } + } + return 0; + } + + return -1; +} + +/* + * Write data to stream. + * Buffer until internal buffer is full. + * When full, call fz_flush to make more space available. + */ +int fz_write(fz_stream *stm, unsigned char *mem, int n) +{ + fz_buffer *buf = stm->buffer; + int i = 0; + int t; + + if (stm->dead == 2) + return 0; + + if (stm->dead) + return -1; + + if (stm->mode != FZ_SWRITE) + return -1; + + while (i < n) + { + while (buf->wp < buf->ep && i < n) + *buf->wp++ = mem[i++]; + + if (buf->wp == buf->ep && i < n) + { + t = fz_flush(stm); + if (t < 0) + return -1; + if (stm->dead) + return i; + } + } + + return n; +} + +int fz_printstr(fz_stream *stm, char *s) +{ + return fz_write(stm, s, strlen(s)); +} + +int fz_printobj(fz_stream *file, fz_obj *obj, int tight) +{ + char buf[1024]; + char *ptr; + int n; + + n = fz_sprintobj(nil, 0, obj, tight); + if (n < sizeof buf) + { + fz_sprintobj(buf, sizeof buf, obj, tight); + return fz_write(file, buf, n); + } + else + { + ptr = fz_malloc(n); + if (!ptr) + return -1; + fz_sprintobj(ptr, n, obj, tight); + n = fz_write(file, ptr, n); + fz_free(ptr); + return n; + } +} + +int fz_print(fz_stream *stm, char *fmt, ...) +{ + va_list ap; + char buf[1024]; + char *p; + int n; + + va_start(ap, fmt); + n = vsnprintf(buf, sizeof buf, fmt, ap); + va_end(ap); + + if (n < sizeof buf) + return fz_write(stm, buf, n); + + p = fz_malloc(n); + if (!p) + return -1; + + va_start(ap, fmt); + vsnprintf(p, n, fmt, ap); + va_end(ap); + + n = fz_write(stm, p, n); + + fz_free(p); + + return n; +} + -- cgit v1.2.3