diff options
-rw-r--r-- | stream/stm_open.c | 442 | ||||
-rw-r--r-- | stream/stm_read.c | 522 | ||||
-rw-r--r-- | stream/stm_write.c | 556 |
3 files changed, 760 insertions, 760 deletions
diff --git a/stream/stm_open.c b/stream/stm_open.c index baec93fa..6481288a 100644 --- a/stream/stm_open.c +++ b/stream/stm_open.c @@ -1,223 +1,223 @@ -/*
- * Creation and destruction.
- */
-
+/* + * Creation and destruction. + */ + #include "fitz-base.h" #include "fitz-stream.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;
-}
-
+ +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 index 484414c5..12e6a288 100644 --- a/stream/stm_read.c +++ b/stream/stm_read.c @@ -1,263 +1,263 @@ -/*
- * Input streams.
- */
-
+/* + * Input streams. + */ + #include "fitz-base.h" #include "fitz-stream.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;
-}
-
+ +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 index e0dad571..cc6c0ef1 100644 --- a/stream/stm_write.c +++ b/stream/stm_write.c @@ -1,280 +1,280 @@ -/*
- * Output streams.
- */
-
+/* + * Output streams. + */ + #include "fitz-base.h" #include "fitz-stream.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;
-}
-
+ +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; +} + |