diff options
author | Tor Andersson <tor@ghostscript.com> | 2004-10-05 04:50:44 +0200 |
---|---|---|
committer | Tor Andersson <tor@ghostscript.com> | 2004-10-05 04:50:44 +0200 |
commit | 00905a4e2b959ccc3b79381acbf1d3b8e5319e8e (patch) | |
tree | e880b845138781aa72473a7be579152a5d20e4eb /filter | |
parent | 970ce563601547adcaa131229bf296f5c3e2db3c (diff) | |
download | mupdf-00905a4e2b959ccc3b79381acbf1d3b8e5319e8e.tar.xz |
refactor file code. fix file writing.
Diffstat (limited to 'filter')
-rw-r--r-- | filter/buffer.c | 27 | ||||
-rw-r--r-- | filter/file.c | 589 | ||||
-rw-r--r-- | filter/filec.c | 302 | ||||
-rw-r--r-- | filter/filer.c | 237 | ||||
-rw-r--r-- | filter/filew.c | 235 |
5 files changed, 800 insertions, 590 deletions
diff --git a/filter/buffer.c b/filter/buffer.c index 7b728b9b..25e30a55 100644 --- a/filter/buffer.c +++ b/filter/buffer.c @@ -8,6 +8,7 @@ fz_newbuffer(fz_buffer **bp, int size) b = *bp = fz_malloc(sizeof(fz_buffer)); if (!b) return fz_outofmem; + b->ownsdata = 1; b->bp = fz_malloc(size); if (!b->bp) { fz_free(b); return fz_outofmem; } @@ -19,10 +20,31 @@ fz_newbuffer(fz_buffer **bp, int size) return nil; } +fz_error * +fz_newbufferwithdata(fz_buffer **bp, unsigned char *data, int size) +{ + fz_buffer *b; + + b = *bp = fz_malloc(sizeof(fz_buffer)); + if (!b) return fz_outofmem; + + b->ownsdata = 0; + b->bp = data; + + b->rp = b->bp; + b->wp = b->bp; + b->ep = b->bp + size; + b->eof = 0; + + return nil; +} + + void fz_freebuffer(fz_buffer *buf) { - fz_free(buf->bp); + if (buf->ownsdata) + fz_free(buf->bp); fz_free(buf); } @@ -35,6 +57,8 @@ fz_growbuffer(fz_buffer *buf) int wp = buf->wp - buf->bp; int ep = buf->ep - buf->bp; + assert(buf->ownsdata); + newbp = fz_realloc(buf->bp, ep * 2); if (!newbp) return fz_outofmem; @@ -49,6 +73,7 @@ fz_growbuffer(fz_buffer *buf) fz_error * fz_rewindbuffer(fz_buffer *buf) { + assert(buf->ownsdata); memmove(buf->bp, buf->rp, buf->wp - buf->rp); buf->wp = buf->bp + (buf->wp - buf->rp); buf->rp = buf->bp; diff --git a/filter/file.c b/filter/file.c deleted file mode 100644 index c3ab63a4..00000000 --- a/filter/file.c +++ /dev/null @@ -1,589 +0,0 @@ -#include <fitz.h> - -/* TODO: nil filter on write */ - -fz_error * -fz_ferror(fz_file *f) -{ - fz_error *e = f->error; - f->error = nil; - return e; -} - -fz_error * -fz_openfile(fz_file **filep, char *path, int mode) -{ - fz_error *error; - fz_file *file; - int fd; - - assert(mode == O_RDONLY || mode == O_WRONLY); - - file = *filep = fz_malloc(sizeof(fz_file)); - if (!file) - return fz_outofmem; - - fd = open(path, mode, 0); - if (fd == -1) - return fz_throw("ioerror: open '%s': %s", path, strerror(errno)); - - file->mode = mode; - file->fd = fd; - file->depth = 0; - file->error = nil; - file->filter = nil; - file->in = nil; - file->out = nil; - - error = fz_newbuffer(&file->in, FZ_BUFSIZE); - if (error) - goto cleanup; - - error = fz_newbuffer(&file->out, FZ_BUFSIZE); - if (error) - goto cleanup; - - return nil; - -cleanup: - close(fd); - fz_free(file->out); - fz_free(file->in); - fz_free(file); - *filep = nil; - return error; -} - -void -fz_closefile(fz_file *file) -{ - assert(file->depth == 0); - - if (file->mode == O_WRONLY) - fz_flush(file); - - if (file->error) - { - fz_warn("%s", file->error->msg); - fz_freeerror(file->error); - file->error = nil; - } - - close(file->fd); - - if (file->filter) fz_freefilter(file->filter); - fz_freebuffer(file->in); - fz_freebuffer(file->out); - fz_free(file); -} - -fz_error * -fz_pushfilter(fz_file *file, fz_filter *filter) -{ - fz_error *error; - fz_buffer *buf; - - if (file->depth == 0) - { - buf = file->out; - file->out = file->in; - file->in = buf; - - file->out->rp = file->out->bp; - file->out->wp = file->out->bp; - file->out->eof = 0; - - file->filter = filter; - } - else - { - error = fz_chainpipeline(&file->filter, file->filter, filter, file->out); - if (error) - return error; - - error = fz_newbuffer(&file->out, FZ_BUFSIZE); - if (error) - { - fz_unchainpipeline(file->filter, &file->filter, &file->out); - return error; - } - } - - file->depth ++; - - return nil; -} - -void -fz_popfilter(fz_file *file) -{ - fz_buffer *buf; - - assert(file->depth > 0); - - if (file->mode == O_WRONLY) - fz_flush(file); - - if (file->error) - { - fz_warn("%s", file->error->msg); - fz_freeerror(file->error); - file->error = nil; - } - - if (file->depth == 1) - { - fz_freefilter(file->filter); - file->filter = nil; - - buf = file->out; - file->out = file->in; - file->in = buf; - - file->in->rp = file->in->bp; - file->in->wp = file->in->bp; - file->in->eof = 0; - } - else - { - fz_freebuffer(file->out); - fz_unchainpipeline(file->filter, &file->filter, &file->out); - } - - file->depth --; -} - -int -fz_seek(fz_file *f, int ofs) -{ - int t; - int c; - - assert(f->mode == O_RDONLY); - - if (f->filter) - { - if (ofs < fz_tell(f)) - { - f->error = fz_throw("ioerror: cannot seek backwards in filter"); - return -1; - } - while (fz_tell(f) < ofs) - { - c = fz_readbyte(f); - if (c == EOF) - return -1; - } - return 0; - } - - t = lseek(f->fd, ofs, 0); - if (t == -1) - { - f->error = fz_throw("ioerror: lseek: %s", strerror(errno)); - return -1; - } - - f->out->rp = f->out->bp; - f->out->wp = f->out->bp; - f->out->eof = 0; - - return 0; -} - -int -fz_tell(fz_file *f) -{ - int t; - - if (f->filter) - { - return f->filter->count - (f->out->wp - f->out->rp); - } - - t = lseek(f->fd, 0, 1); - if (t == -1) - { - f->error = fz_throw("ioerror: lseek: %s", strerror(errno)); - return -1; - } - - return t - (f->out->wp - f->out->rp); -} - -/* - * Read mode - */ - -static int doread(fz_buffer *b, int fd) -{ - int n = read(fd, b->wp, b->ep - b->wp); - if (n == -1) - return -1; - if (n == 0) - b->eof = 1; - b->wp += n; - return n; -} - -static int producedata(fz_file *f) -{ - fz_error *reason; - int produced; - int n; - - assert(f->mode == O_RDONLY); - assert(f->error == nil); - - if (!f->filter) - { - fz_rewindbuffer(f->out); - n = doread(f->out, f->fd); - if (n < 0) { - f->error = fz_throw("ioerror in read: %s", strerror(errno)); - return -1; - } - return 0; - } - - produced = 0; - - while (1) - { - reason = fz_process(f->filter, f->in, f->out); - - if (f->filter->produced) - produced = 1; - - if (reason == fz_ioneedin) - { - if (f->in->eof) { - f->error = fz_throw("ioerror: premature eof in filter"); - return -1; - } - - /* no space to produce, rewind or grow */ - if (f->in->wp == f->in->ep) - { - if (f->in->rp > f->in->bp) - f->error = fz_rewindbuffer(f->in); - else - f->error = fz_growbuffer(f->in); - if (f->error) - return -1; - } - - /* then fill with more input */ - n = doread(f->in, f->fd); - if (n < 0) { - f->error = fz_throw("ioerror in read: %s", strerror(errno)); - return -1; - } - - if (produced) - return 0; - } - - else if (reason == fz_ioneedout) - { - if (produced) - return 0; - - /* need more outspace, and produced no data */ - if (f->out->rp > f->out->bp) - f->error = fz_rewindbuffer(f->out); - else - f->error = fz_growbuffer(f->out); - if (f->error) - return -1; - } - - else if (reason == fz_iodone) - return 0; - - else { - f->error = reason; - return -1; - } - } -} - -int -fz_peekbyte(fz_file *f) -{ - if (f->out->rp == f->out->wp) - { - if (f->out->eof) return EOF; - if (producedata(f)) return EOF; - } - - if (f->out->rp < f->out->wp) - return *f->out->rp; - - return EOF; -} - -int -fz_readbyte(fz_file *f) -{ - if (f->out->rp == f->out->wp) - { - if (f->out->eof) return EOF; - if (producedata(f)) return EOF; - } - - if (f->out->rp < f->out->wp) - return *f->out->rp++; - - return EOF; -} - -int -fz_read(fz_file *f, char *buf, int n) -{ - int i = 0; - - while (i < n) - { - while (f->out->rp < f->out->wp && i < n) - buf[i++] = *f->out->rp ++; - - if (f->out->rp == f->out->wp) - { - if (f->out->eof) return i; - if (producedata(f) < 0) return -1; - } - } - - return i; -} - -int -fz_readline(fz_file *f, char *buf, int n) -{ - int c = EOF; - char *s = buf; - - while (n > 1) - { - c = fz_readbyte(f); - if (c == EOF) - break; - if (c == '\r') { - c = fz_peekbyte(f); - if (c == '\n') - c = fz_readbyte(f); - break; - } - if (c == '\n') - break; - *s++ = c; - n--; - } - if (n) - *s = '\0'; - return s - buf; -} - -/* - * Write mode - */ - -static int dowrite(fz_buffer *b, int fd) -{ - int n = write(fd, b->rp, b->wp - b->rp); - if (n == -1) - return -1; - b->rp += n; - return n; -} - -int -fz_write(fz_file *f, char *buf, int n) -{ - fz_error *reason; - int i = 0; - int x; - - assert(f->mode == O_WRONLY); - assert(f->error == nil); - - while (i < n) - { - while (f->in->rp < f->in->wp && i < n) - { - *f->in->rp++ = buf[i++]; - } - - if (f->in->rp == f->in->wp) - { - reason = fz_process(f->filter, f->in, f->out); - - if (reason == fz_ioneedin) - { - if (f->in->wp == f->in->ep) { - if (f->in->rp > f->in->bp) - f->error = fz_rewindbuffer(f->in); - else - f->error = fz_growbuffer(f->in); - if (f->error) - return -1; - } - } - - else if (reason == fz_ioneedout) - { - x = dowrite(f->out, f->fd); - if (x < 0) { - f->error = fz_throw("ioerror in write: %s", strerror(errno)); - return -1; - } - - if (f->out->rp > f->out->bp) - f->error = fz_rewindbuffer(f->out); - else - f->error = fz_growbuffer(f->out); - if (f->error) - return -1; - } - - else if (reason == fz_iodone) - { - x = dowrite(f->out, f->fd); - if (x < 0) { - f->error = fz_throw("ioerror in write: %s", strerror(errno)); - return -1; - } - break; - } - - else { - f->error = reason; - return -1; - } - } - } - - return i; -} - -int -fz_flush(fz_file *f) -{ - fz_error *reason; - int n; - - assert(f->mode == O_WRONLY); - assert(f->error == nil); - - f->in->eof = 1; - - while (!f->out->eof) - { - reason = fz_process(f->filter, f->in, f->out); - - if (reason == fz_ioneedin) { - f->error = fz_throw("ioerror: premature eof in filter"); - return -1; - } - - else if (reason == fz_ioneedout) - { - n = dowrite(f->out, f->fd); - if (n < 0) { - f->error = fz_throw("ioerror in write: %s", strerror(errno)); - return -1; - } - - if (f->out->rp > f->out->bp) - f->error = fz_rewindbuffer(f->out); - else - f->error = fz_growbuffer(f->out); - if (f->error) - return -1; - } - - else if (reason == fz_iodone) - { - n = dowrite(f->out, f->fd); - if (n < 0) { - f->error = fz_throw("ioerror in write: %s", strerror(errno)); - return -1; - } - break; - } - - else { - f->error = reason; - return -1; - } - } - - return 0; -} - -/* - * Utility function to consume contents of file stream into - * a freshly allocated buffer; realloced and trimmed to size. - */ - -enum { CHUNKSIZE = 4096 }; - -fz_error * -fz_readfile(unsigned char **bufp, int *lenp, fz_file *file) -{ - unsigned char *newbuf; - unsigned char *buf; - int len; - int pos; - int n; - - *bufp = nil; - *lenp = 0; - - 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 fz_outofmem; - } - buf = newbuf; - } - - n = fz_read(file, buf + pos, len - pos); - -printf("fz_read %d bytes\n", n); - - if (n < 0) - { - fz_free(buf); - return fz_ferror(file); - } - - pos += n; - - if (n < CHUNKSIZE) - { - newbuf = fz_realloc(buf, pos); - if (!newbuf) - { - fz_free(buf); - return fz_outofmem; - } - - *bufp = newbuf; - *lenp = pos; - return nil; - } - } -} - diff --git a/filter/filec.c b/filter/filec.c new file mode 100644 index 00000000..f62d1265 --- /dev/null +++ b/filter/filec.c @@ -0,0 +1,302 @@ +#include <fitz.h> + +fz_error * +fz_ferror(fz_file *f) +{ + fz_error *e = f->error; + f->error = nil; + return e; +} + +fz_error * +fz_openfile(fz_file **filep, char *path, int mode) +{ + fz_error *error; + fz_file *file; + int fd; + + assert(mode == O_RDONLY || mode == O_WRONLY); + + file = *filep = fz_malloc(sizeof(fz_file)); + if (!file) + return fz_outofmem; + + fd = open(path, mode, 0); + if (fd == -1) + return fz_throw("ioerror: open '%s': %s", path, strerror(errno)); + + file->mode = mode; + file->fd = fd; + file->depth = 0; + file->error = nil; + file->filter = nil; + file->in = nil; + file->out = nil; + + error = fz_newbuffer(&file->in, FZ_BUFSIZE); + if (error) + goto cleanup; + + error = fz_newbuffer(&file->out, FZ_BUFSIZE); + if (error) + goto cleanup; + + return nil; + +cleanup: + *filep = nil; + close(fd); + fz_free(file->out); + fz_free(file->in); + fz_free(file); + return error; +} + +fz_error * +fz_openbuffer(fz_file **filep, fz_buffer *buf, int mode) +{ + fz_error *error; + fz_file *file; + + assert(mode == O_RDONLY || mode == O_WRONLY); + + file = *filep = fz_malloc(sizeof(fz_file)); + if (!file) + return fz_outofmem; + + file->mode = mode; + file->fd = -1; + file->depth = 0; + file->error = nil; + file->filter = nil; + + if (mode == O_RDONLY) + { + file->in = buf; + error = fz_newbuffer(&file->out, FZ_BUFSIZE); + if (error) + goto cleanup; + } + + else + { + error = fz_newbuffer(&file->in, FZ_BUFSIZE); + if (error) + goto cleanup; + file->out = buf; + } + + return nil; + +cleanup: + *filep = nil; + fz_free(file); + return error; +} + +void +fz_closefile(fz_file *file) +{ + assert(file->depth == 0); + + if (file->mode != O_RDONLY) + fz_flush(file); + + if (file->error) + { + fz_warn("%s", file->error->msg); + fz_freeerror(file->error); + file->error = nil; + } + + if (file->fd == -1) /* open to buffer not file */ + { + if (file->mode == O_RDONLY) + fz_freebuffer(file->out); + else + fz_freebuffer(file->in); + } + else + { + fz_freebuffer(file->in); + fz_freebuffer(file->out); + close(file->fd); + } + + if (file->filter) + fz_freefilter(file->filter); + + fz_free(file); +} + +fz_error * +fz_pushfilter(fz_file *file, fz_filter *filter) +{ + fz_error *error; + + /* without a filter, one buffer is ignored: unignore. */ + if (file->depth == 0) + { + fz_buffer *buf; + + buf = file->out; + file->out = file->in; + file->in = buf; + +// XXX + if (file->mode == O_RDONLY) + { + file->out->rp = file->out->bp; + file->out->wp = file->out->bp; + file->out->eof = 0; + } + + file->filter = filter; + } + + else + { + if (file->mode == O_RDONLY) + { + error = fz_chainpipeline(&file->filter, file->filter, filter, file->out); + if (error) + return error; + + error = fz_newbuffer(&file->out, FZ_BUFSIZE); + if (error) + { + fz_unchainpipeline(file->filter, &file->filter, &file->out); + return error; + } + } + + else + { + error = fz_chainpipeline(&file->filter, filter, file->filter, file->in); + if (error) + return error; + + error = fz_newbuffer(&file->in, FZ_BUFSIZE); + if (error) + { + fz_unchainpipeline(file->filter, &file->filter, &file->in); + return error; + } + } + } + + file->depth ++; + + return nil; +} + +void +fz_popfilter(fz_file *file) +{ + fz_buffer *buf; + + assert(file->depth > 0); + + if (file->mode != O_RDONLY) + fz_flush(file); + + if (file->error) + { + fz_warn("%s", file->error->msg); + fz_freeerror(file->error); + file->error = nil; + } + + if (file->depth == 1) + { + fz_freefilter(file->filter); + file->filter = nil; + + buf = file->out; + file->out = file->in; + file->in = buf; + +// XXX + file->in->rp = file->in->bp; + file->in->wp = file->in->bp; + file->in->eof = 0; + } + else + { + if (file->mode == O_RDONLY) + { + fz_freebuffer(file->out); + fz_unchainpipeline(file->filter, &file->filter, &file->out); + } + else + { + fz_freebuffer(file->in); + fz_unchainpipeline(file->filter, &file->filter, &file->in); + } + } + + file->depth --; +} + +int +fz_seek(fz_file *f, int ofs) +{ + int t; + int c; + + assert(f->mode == O_RDONLY); + + if (f->filter) + { + if (ofs < fz_tell(f)) + { + f->error = fz_throw("ioerror: cannot seek backwards in filter"); + return -1; + } + while (fz_tell(f) < ofs) + { + c = fz_readbyte(f); + if (c == EOF) + return -1; + } + return 0; + } + + t = lseek(f->fd, ofs, 0); + if (t == -1) + { + f->error = fz_throw("ioerror: lseek: %s", strerror(errno)); + return -1; + } + + f->out->rp = f->out->bp; + f->out->wp = f->out->bp; + f->out->eof = 0; + + return 0; +} + +int +fz_tell(fz_file *f) +{ + int t; + + if (f->filter) + { + assert(f->mode == O_RDONLY); + return f->filter->count - (f->out->wp - f->out->rp); + } + + t = lseek(f->fd, 0, 1); + if (t == -1) + { + f->error = fz_throw("ioerror: lseek: %s", strerror(errno)); + return -1; + } + + if (f->mode == O_RDONLY) + return t - (f->out->wp - f->out->rp); + else + return t - (f->in->wp - f->in->rp); +} + diff --git a/filter/filer.c b/filter/filer.c new file mode 100644 index 00000000..804d7f7e --- /dev/null +++ b/filter/filer.c @@ -0,0 +1,237 @@ +#include <fitz.h> + +static int doread(fz_buffer *b, int fd) +{ + int n = read(fd, b->wp, b->ep - b->wp); + if (n == -1) + return -1; + if (n == 0) + b->eof = 1; + b->wp += n; + return n; +} + +static int producedata(fz_file *f) +{ + fz_error *reason; + int produced; + int n; + + assert(f->mode == O_RDONLY); + assert(f->error == nil); + + if (!f->filter) + { + fz_rewindbuffer(f->out); + n = doread(f->out, f->fd); + if (n < 0) { + f->error = fz_throw("ioerror in read: %s", strerror(errno)); + return -1; + } + return 0; + } + + produced = 0; + + while (1) + { + reason = fz_process(f->filter, f->in, f->out); + + if (f->filter->produced) + produced = 1; + + if (reason == fz_ioneedin) + { + if (f->in->eof) { + f->error = fz_throw("ioerror: premature eof in filter"); + return -1; + } + + /* no space to produce, rewind or grow */ + if (f->in->wp == f->in->ep) + { + if (f->in->rp > f->in->bp) + f->error = fz_rewindbuffer(f->in); + else + f->error = fz_growbuffer(f->in); + if (f->error) + return -1; + } + + /* then fill with more input */ + n = doread(f->in, f->fd); + if (n < 0) { + f->error = fz_throw("ioerror in read: %s", strerror(errno)); + return -1; + } + + if (produced) + return 0; + } + + else if (reason == fz_ioneedout) + { + if (produced) + return 0; + + /* need more outspace, and produced no data */ + if (f->out->rp > f->out->bp) + f->error = fz_rewindbuffer(f->out); + else + f->error = fz_growbuffer(f->out); + if (f->error) + return -1; + } + + else if (reason == fz_iodone) + return 0; + + else { + f->error = reason; + return -1; + } + } +} + +int +fz_peekbyte(fz_file *f) +{ + if (f->out->rp == f->out->wp) + { + if (f->out->eof) return EOF; + if (producedata(f)) return EOF; + } + + if (f->out->rp < f->out->wp) + return *f->out->rp; + + return EOF; +} + +int +fz_readbyte(fz_file *f) +{ + if (f->out->rp == f->out->wp) + { + if (f->out->eof) return EOF; + if (producedata(f)) return EOF; + } + + if (f->out->rp < f->out->wp) + return *f->out->rp++; + + return EOF; +} + +int +fz_read(fz_file *f, char *buf, int n) +{ + int i = 0; + + while (i < n) + { + while (f->out->rp < f->out->wp && i < n) + buf[i++] = *f->out->rp ++; + + if (f->out->rp == f->out->wp) + { + if (f->out->eof) return i; + if (producedata(f) < 0) return -1; + } + } + + return i; +} + +int +fz_readline(fz_file *f, char *buf, int n) +{ + int c = EOF; + char *s = buf; + + while (n > 1) + { + c = fz_readbyte(f); + if (c == EOF) + break; + if (c == '\r') { + c = fz_peekbyte(f); + if (c == '\n') + c = fz_readbyte(f); + break; + } + if (c == '\n') + break; + *s++ = c; + n--; + } + if (n) + *s = '\0'; + return s - buf; +} + +/* + * Utility function to consume contents of file stream into + * a freshly allocated buffer; realloced and trimmed to size. + */ + +enum { CHUNKSIZE = 4096 }; + +fz_error * +fz_readfile(unsigned char **bufp, int *lenp, fz_file *file) +{ + unsigned char *newbuf; + unsigned char *buf; + int len; + int pos; + int n; + + *bufp = nil; + *lenp = 0; + + 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 fz_outofmem; + } + buf = newbuf; + } + + n = fz_read(file, buf + pos, len - pos); + +printf("fz_read %d bytes\n", n); + + if (n < 0) + { + fz_free(buf); + return fz_ferror(file); + } + + pos += n; + + if (n < CHUNKSIZE) + { + newbuf = fz_realloc(buf, pos); + if (!newbuf) + { + fz_free(buf); + return fz_outofmem; + } + + *bufp = newbuf; + *lenp = pos; + return nil; + } + } +} + diff --git a/filter/filew.c b/filter/filew.c new file mode 100644 index 00000000..0422e60e --- /dev/null +++ b/filter/filew.c @@ -0,0 +1,235 @@ +#include <fitz.h> + +int +fz_printstring(fz_file *f, char *s) +{ + return fz_write(f, s, strlen(s)); +} + +int +fz_printobj(fz_file *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) { + file->error = fz_outofmem; + return -1; + } + fz_sprintobj(ptr, n, obj, tight); + n = fz_write(file, buf, n); + fz_free(ptr); + return n; + } +} + +int +fz_print(fz_file *f, 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(f, buf, n); + + p = fz_malloc(n); + if (!p) { + f->error = fz_outofmem; + return -1; + } + + va_start(ap, fmt); + vsnprintf(p, n, fmt, ap); + va_end(ap); + + n = fz_write(f, p, n); + + fz_free(p); + + return n; +} + +static int dowrite(fz_buffer *b, int fd) +{ + int n = write(fd, b->rp, b->wp - b->rp); + if (n == -1) + return -1; + b->rp += n; + return n; +} + +int +fz_write(fz_file *f, char *buf, int n) +{ + fz_error *reason; + int i = 0; + int x; + + assert(f->mode == O_WRONLY); + assert(f->error == nil); + + if (!f->filter) + { + while (i < n) + { + while (f->in->wp < f->in->ep && i < n) + *f->in->wp++ = buf[i++]; + + if (f->in->wp == f->in->ep) + { + x = dowrite(f->in, f->fd); + if (x < 0) { + f->error = fz_throw("ioerror in write: %s", strerror(errno)); + return -1; + } + + fz_rewindbuffer(f->in); + } + } + + return 0; + } + + while (i < n) + { + while (f->in->rp < f->in->wp && i < n) + { + *f->in->rp++ = buf[i++]; + } + + if (f->in->rp == f->in->wp) + { + reason = fz_process(f->filter, f->in, f->out); + + if (reason == fz_ioneedin) + { + if (f->in->wp == f->in->ep) { + if (f->in->rp > f->in->bp) + f->error = fz_rewindbuffer(f->in); + else + f->error = fz_growbuffer(f->in); + if (f->error) + return -1; + } + } + + else if (reason == fz_ioneedout) + { + x = dowrite(f->out, f->fd); + if (x < 0) { + f->error = fz_throw("ioerror in write: %s", strerror(errno)); + return -1; + } + + if (f->out->rp > f->out->bp) + f->error = fz_rewindbuffer(f->out); + else + f->error = fz_growbuffer(f->out); + if (f->error) + return -1; + } + + else if (reason == fz_iodone) + { + x = dowrite(f->out, f->fd); + if (x < 0) { + f->error = fz_throw("ioerror in write: %s", strerror(errno)); + return -1; + } + break; + } + + else { + f->error = reason; + return -1; + } + } + } + + return i; +} + +int +fz_flush(fz_file *f) +{ + fz_error *reason; + int n; + + assert(f->mode == O_WRONLY); + assert(f->error == nil); + + f->in->eof = 1; + + if (!f->filter) + { + while (f->in->rp < f->in->wp) + { + n = dowrite(f->in, f->fd); + if (n < 0) { + f->error = fz_throw("ioerror in write: %s", strerror(errno)); + return -1; + } + } + return 0; + } + + while (!f->out->eof) + { + reason = fz_process(f->filter, f->in, f->out); + + if (reason == fz_ioneedin) { + f->error = fz_throw("ioerror: premature eof in filter"); + return -1; + } + + else if (reason == fz_ioneedout) + { + n = dowrite(f->out, f->fd); + if (n < 0) { + f->error = fz_throw("ioerror in write: %s", strerror(errno)); + return -1; + } + + if (f->out->rp > f->out->bp) + f->error = fz_rewindbuffer(f->out); + else + f->error = fz_growbuffer(f->out); + if (f->error) + return -1; + } + + else if (reason == fz_iodone) + { + n = dowrite(f->out, f->fd); + if (n < 0) { + f->error = fz_throw("ioerror in write: %s", strerror(errno)); + return -1; + } + break; + } + + else { + f->error = reason; + return -1; + } + } + + return 0; +} + |