summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTor Andersson <tor@ghostscript.com>2004-10-07 04:15:51 +0200
committerTor Andersson <tor@ghostscript.com>2004-10-07 04:15:51 +0200
commit94a96e0ce2be0e32f59ea43f1166a884ca30222d (patch)
tree05d123cd5ea355a015978758eb3d83a4ab779b4b
parent59bd2f5bfc486b107c4bd689bd65ea7d23e2fad0 (diff)
downloadmupdf-94a96e0ce2be0e32f59ea43f1166a884ca30222d.tar.xz
openfile append mode. changed O_*ONLY to FZ_* enums.
-rw-r--r--filter/filec.c62
-rw-r--r--filter/filer.c2
-rw-r--r--filter/filew.c4
-rw-r--r--include/fitz/file.h6
-rw-r--r--mupdf/cmap.c2
-rw-r--r--mupdf/fontfile.c5
-rw-r--r--mupdf/open.c28
-rw-r--r--mupdf/repair.c8
-rw-r--r--mupdf/save.c6
-rw-r--r--mupdf/stream.c2
-rw-r--r--mupdf/xref.c2
-rw-r--r--test/pdfdebug.c2
-rw-r--r--test/showcmap.c2
13 files changed, 77 insertions, 54 deletions
diff --git a/filter/filec.c b/filter/filec.c
index a8a26008..ec81c873 100644
--- a/filter/filec.c
+++ b/filter/filec.c
@@ -15,21 +15,37 @@ fz_openfile(fz_file **filep, char *path, int mode)
fz_file *file;
int realmode;
int fd;
+ int n;
- assert(mode == O_RDONLY || mode == O_WRONLY || mode == O_APPEND);
+ assert(mode == FZ_READ || mode == FZ_WRITE || mode == FZ_APPEND);
- realmode = mode;
- if (mode == O_WRONLY)
+ realmode = 0;
+ if (mode == FZ_READ)
+ realmode = O_RDONLY;
+ if (mode == FZ_WRITE)
realmode = O_WRONLY | O_CREAT | O_TRUNC;
-
- file = *filep = fz_malloc(sizeof(fz_file));
- if (!file)
- return fz_outofmem;
+ if (mode == FZ_APPEND)
+ realmode = O_WRONLY;
fd = open(path, realmode, 0644);
if (fd == -1)
return fz_throw("ioerror: open '%s': %s", path, strerror(errno));
+ if (mode == FZ_APPEND)
+ {
+ mode = FZ_WRITE;
+ n = lseek(fd, 0, 2);
+ if (n == -1) {
+ error = fz_throw("ioerror: lseek: %s", strerror(errno));
+ close(fd);
+ return error;
+ }
+ }
+
+ file = *filep = fz_malloc(sizeof(fz_file));
+ if (!file)
+ return fz_outofmem;
+
file->mode = mode;
file->fd = fd;
file->depth = 0;
@@ -63,7 +79,7 @@ fz_openbuffer(fz_file **filep, fz_buffer *buf, int mode)
fz_error *error;
fz_file *file;
- assert(mode == O_RDONLY || mode == O_WRONLY);
+ assert(mode == FZ_READ || mode == FZ_WRITE);
file = *filep = fz_malloc(sizeof(fz_file));
if (!file)
@@ -75,7 +91,7 @@ fz_openbuffer(fz_file **filep, fz_buffer *buf, int mode)
file->error = nil;
file->filter = nil;
- if (mode == O_RDONLY)
+ if (mode == FZ_READ)
{
file->in = buf;
error = fz_newbuffer(&file->out, FZ_BUFSIZE);
@@ -104,7 +120,7 @@ fz_closefile(fz_file *file)
{
assert(file->depth == 0);
- if (file->mode != O_RDONLY)
+ if (file->mode == FZ_WRITE)
fz_flush(file);
if (file->error)
@@ -116,7 +132,7 @@ fz_closefile(fz_file *file)
if (file->fd == -1) /* open to buffer not file */
{
- if (file->mode == O_RDONLY)
+ if (file->mode == FZ_READ)
fz_freebuffer(file->out);
else
fz_freebuffer(file->in);
@@ -148,7 +164,7 @@ fz_pushfilter(fz_file *file, fz_filter *filter)
file->out = file->in;
file->in = buf;
- if (file->mode == O_RDONLY)
+ if (file->mode == FZ_READ)
{
file->out->rp = file->out->bp;
file->out->wp = file->out->bp;
@@ -167,7 +183,7 @@ fz_pushfilter(fz_file *file, fz_filter *filter)
else
{
- if (file->mode == O_RDONLY)
+ if (file->mode == FZ_READ)
{
error = fz_chainpipeline(&file->filter, file->filter, filter, file->out);
if (error)
@@ -208,7 +224,7 @@ fz_popfilter(fz_file *file)
assert(file->depth > 0);
- if (file->mode != O_RDONLY)
+ if (file->mode == FZ_WRITE)
fz_flush(file);
if (file->error)
@@ -229,7 +245,7 @@ fz_popfilter(fz_file *file)
}
else
{
- if (file->mode == O_RDONLY)
+ if (file->mode == FZ_READ)
{
fz_freebuffer(file->out);
fz_unchainpipeline(file->filter, &file->filter, &file->out);
@@ -245,15 +261,15 @@ fz_popfilter(fz_file *file)
}
int
-fz_seek(fz_file *f, int ofs)
+fz_seek(fz_file *f, int ofs, int whence)
{
int t;
int c;
- assert(f->mode == O_RDONLY);
-
if (f->filter)
{
+ assert(f->mode == FZ_READ && whence == 0);
+
if (ofs < fz_tell(f))
{
f->error = fz_throw("ioerror: cannot seek backwards in filter");
@@ -265,10 +281,10 @@ fz_seek(fz_file *f, int ofs)
if (c == EOF)
return -1;
}
- return 0;
+ return ofs;
}
- t = lseek(f->fd, ofs, 0);
+ t = lseek(f->fd, ofs, whence);
if (t == -1)
{
f->error = fz_throw("ioerror: lseek: %s", strerror(errno));
@@ -279,7 +295,7 @@ fz_seek(fz_file *f, int ofs)
f->out->wp = f->out->bp;
f->out->eof = 0;
- return 0;
+ return t;
}
int
@@ -289,7 +305,7 @@ fz_tell(fz_file *f)
if (f->filter)
{
- assert(f->mode == O_RDONLY);
+ assert(f->mode == FZ_READ);
return f->filter->count - (f->out->wp - f->out->rp);
}
@@ -300,7 +316,7 @@ fz_tell(fz_file *f)
return -1;
}
- if (f->mode == O_RDONLY)
+ if (f->mode == FZ_READ)
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
index 368f9dbe..7828fb0e 100644
--- a/filter/filer.c
+++ b/filter/filer.c
@@ -17,7 +17,7 @@ static int producedata(fz_file *f)
int produced;
int n;
- assert(f->mode == O_RDONLY);
+ assert(f->mode == FZ_READ);
assert(f->error == nil);
if (!f->filter)
diff --git a/filter/filew.c b/filter/filew.c
index fdf94043..53e3c120 100644
--- a/filter/filew.c
+++ b/filter/filew.c
@@ -81,7 +81,7 @@ fz_write(fz_file *f, char *buf, int n)
int i = 0;
int x;
- assert(f->mode == O_WRONLY);
+ assert(f->mode == FZ_WRITE);
assert(f->error == nil);
if (!f->filter)
@@ -177,7 +177,7 @@ fz_flush(fz_file *f)
fz_error *reason;
int n;
- assert(f->mode == O_WRONLY);
+ assert(f->mode == FZ_WRITE);
assert(f->error == nil);
f->in->eof = 1;
diff --git a/include/fitz/file.h b/include/fitz/file.h
index 2d6dc977..6179085a 100644
--- a/include/fitz/file.h
+++ b/include/fitz/file.h
@@ -1,8 +1,10 @@
typedef struct fz_file_s fz_file;
+enum { FZ_READ, FZ_WRITE, FZ_APPEND };
+
struct fz_file_s
{
- int mode; /* O_RDONLY or O_WRONLY */
+ int mode; /* FZ_READ or FZ_WRITE */
int fd;
int depth;
fz_filter *filter;
@@ -18,7 +20,7 @@ void fz_popfilter(fz_file *file);
void fz_closefile(fz_file *file);
fz_error *fz_ferror(fz_file *f);
-int fz_seek(fz_file *f, int ofs);
+int fz_seek(fz_file *f, int ofs, int whence);
int fz_tell(fz_file *f);
int fz_readbyte(fz_file *f);
diff --git a/mupdf/cmap.c b/mupdf/cmap.c
index 180e93c3..b043ab61 100644
--- a/mupdf/cmap.c
+++ b/mupdf/cmap.c
@@ -456,7 +456,7 @@ pdf_loadsystemcmap(fz_cmap **cmapp, char *name)
printf(" system cmap loading %s\n", path);
- error = fz_openfile(&file, path, O_RDONLY);
+ error = fz_openfile(&file, path, FZ_READ);
if (error)
goto cleanup;
diff --git a/mupdf/fontfile.c b/mupdf/fontfile.c
index 21a379b2..d8646a7e 100644
--- a/mupdf/fontfile.c
+++ b/mupdf/fontfile.c
@@ -47,6 +47,7 @@ static char *basepatterns[14] =
static fz_error *initfontlibs(void)
{
int fterr;
+ int maj, min, pat;
if (ftlib)
return nil;
@@ -55,6 +56,10 @@ static fz_error *initfontlibs(void)
if (fterr)
return fz_throw("freetype failed initialisation: 0x%x", fterr);
+ FT_Library_Version(ftlib, &maj, &min, &pat);
+ if (maj == 2 && min == 1 && pat < 7)
+ return fz_throw("freetype version too old: %d.%d.%d", maj, min, pat);
+
fclib = FcInitLoadConfigAndFonts();
if (!fclib)
return fz_throw("fontconfig failed initialisation");
diff --git a/mupdf/open.c b/mupdf/open.c
index 016cebf4..52ee4f29 100644
--- a/mupdf/open.c
+++ b/mupdf/open.c
@@ -17,7 +17,7 @@ loadversion(float *version, fz_file *file)
char buf[20];
int n;
- n = fz_seek(file, 0);
+ n = fz_seek(file, 0, 0);
if (n < 0)
return fz_ferror(file);
@@ -31,23 +31,23 @@ loadversion(float *version, fz_file *file)
}
static fz_error *
-readstartxref(int *ofs, int fd)
+readstartxref(int *ofs, fz_file *file)
{
unsigned char buf[1024];
int t, n;
int i;
- t = lseek(fd, 0, 2);
+ t = fz_seek(file, 0, 2);
if (t == -1)
- return fz_throw("ioerror in startxref: lseek: %s", strerror(errno));
+ return fz_ferror(file);
- t = lseek(fd, MAX(0, t - ((int)sizeof buf)), 0);
+ t = fz_seek(file, MAX(0, t - ((int)sizeof buf)), 0);
if (t == -1)
- return fz_throw("ioerror in startxref: lseek: %s", strerror(errno));
+ return fz_ferror(file);
- n = read(fd, buf, sizeof buf);
+ n = fz_read(file, buf, sizeof buf);
if (n == -1)
- return fz_throw("ioerror in startxref: read: %s", strerror(errno));
+ return fz_ferror(file);
for (i = n - 9; i >= 0; i--) {
if (memcmp(buf + i, "startxref", 9) == 0) {
@@ -95,7 +95,7 @@ readoldtrailer(fz_obj **objp, fz_file *file, unsigned char *buf, int cap)
t = fz_tell(file);
if (t < 0) return fz_ferror(file);
- n = fz_seek(file, t + 20 * len);
+ n = fz_seek(file, t + 20 * len, 0);
if (n < 0) return fz_ferror(file);
}
@@ -122,7 +122,7 @@ readtrailer(fz_obj **objp, fz_file *file, int ofs, unsigned char *buf, int cap)
int n;
int c;
- n = fz_seek(file, ofs);
+ n = fz_seek(file, ofs, 0);
if (n < 0)
return fz_ferror(file);
@@ -288,7 +288,7 @@ readxref(fz_obj **trailerp, pdf_xref *xref, int ofs, unsigned char *buf, int cap
int n;
int c;
- n = fz_seek(xref->file, ofs);
+ n = fz_seek(xref->file, ofs, 0);
if (n < 0)
return fz_ferror(xref->file);
@@ -392,7 +392,7 @@ pdf_readobjstm(pdf_xref *xref, int oid, int gid, unsigned char *buf, int cap)
ofsbuf[i] = atoi(buf);
}
- n = fz_seek(xref->file, first);
+ n = fz_seek(xref->file, first, 0);
if (n < 0)
{
error = fz_ferror(xref->file);
@@ -448,7 +448,7 @@ pdf_openxref(pdf_xref *xref, char *filename)
unsigned char buf[65536]; /* yeowch! */
- error = fz_openfile(&xref->file, filename, O_RDONLY);
+ error = fz_openfile(&xref->file, filename, FZ_READ);
if (error)
return error;
@@ -456,7 +456,7 @@ pdf_openxref(pdf_xref *xref, char *filename)
if (error)
return error;
- error = readstartxref(&xref->startxref, xref->file->fd);
+ error = readstartxref(&xref->startxref, xref->file);
if (error)
return error;
diff --git a/mupdf/repair.c b/mupdf/repair.c
index 0d6bffcb..d12277b6 100644
--- a/mupdf/repair.c
+++ b/mupdf/repair.c
@@ -69,11 +69,11 @@ parseobj(fz_file *file, unsigned char *buf, int cap, int *stmlen,
length = fz_dictgets(dict, "Length");
if (fz_isint(length))
{
- fz_seek(file, stmofs + fz_toint(length));
+ fz_seek(file, stmofs + fz_toint(length), 0);
tok = pdf_lex(file, buf, cap, &len);
if (tok == PDF_TENDSTREAM)
goto atobjend;
- fz_seek(file, stmofs);
+ fz_seek(file, stmofs, 0);
}
fz_read(file, buf, 9);
@@ -129,13 +129,13 @@ pdf_repairxref(pdf_xref *xref, char *filename)
if (!list)
return fz_outofmem;
- error = fz_openfile(&xref->file, filename, O_RDONLY);
+ error = fz_openfile(&xref->file, filename, FZ_READ);
if (error)
goto cleanup;
file = xref->file;
- n = fz_seek(file, 0);
+ n = fz_seek(file, 0, 0);
if (n < 0) {
error = fz_ferror(file);
goto cleanup;
diff --git a/mupdf/save.c b/mupdf/save.c
index 5d370286..4850f886 100644
--- a/mupdf/save.c
+++ b/mupdf/save.c
@@ -122,7 +122,7 @@ writecopy(fz_file *out, pdf_xref *xref, pdf_crypt *encrypt, int oid)
goto cleanup;
}
- fz_seek(xref->file, stmofs);
+ fz_seek(xref->file, stmofs, 0);
fz_pushfilter(xref->file, pipe);
if (encrypt)
@@ -195,7 +195,7 @@ pdf_saveincrementalpdf(pdf_xref *xref, char *path)
int startxref;
fz_obj *obj;
- error = fz_openfile(&out, path, O_APPEND);
+ error = fz_openfile(&out, path, FZ_APPEND);
if (error)
return error;
@@ -298,7 +298,7 @@ pdf_savepdf(pdf_xref *xref, char *path, pdf_crypt *encrypt)
if (!ofsbuf)
return fz_outofmem;
- error = fz_openfile(&out, path, O_WRONLY);
+ error = fz_openfile(&out, path, FZ_WRITE);
if (error)
{
fz_free(ofsbuf);
diff --git a/mupdf/stream.c b/mupdf/stream.c
index ab051865..379a80aa 100644
--- a/mupdf/stream.c
+++ b/mupdf/stream.c
@@ -198,7 +198,7 @@ pdf_openstream0(pdf_xref *xref, fz_obj *stmobj, int oid, int gid, int ofs)
if (error)
return error;
- ofs = fz_seek(xref->file, ofs);
+ ofs = fz_seek(xref->file, ofs, 0);
if (ofs < 0) {
fz_freefilter(filter);
return fz_ferror(xref->file);
diff --git a/mupdf/xref.c b/mupdf/xref.c
index a53f06e5..e1977ad6 100644
--- a/mupdf/xref.c
+++ b/mupdf/xref.c
@@ -400,7 +400,7 @@ pdf_loadobject0(fz_obj **objp, pdf_xref *xref, int oid, int gid, int *stmofs)
else if (x->type == 'n')
{
- n = fz_seek(xref->file, x->ofs);
+ n = fz_seek(xref->file, x->ofs, 0);
if (n < 0)
return fz_ferror(xref->file);
diff --git a/test/pdfdebug.c b/test/pdfdebug.c
index 1009849f..bf3de918 100644
--- a/test/pdfdebug.c
+++ b/test/pdfdebug.c
@@ -94,7 +94,7 @@ void copystream(pdf_xref *xref, fz_obj *stream, int ofs)
error = fz_newnullfilter(&filter, len);
if (error) fz_abort(error);
- fz_seek(xref->file, ofs);
+ fz_seek(xref->file, ofs, 0);
error = fz_pushfilter(xref->file, filter);
if (error) fz_abort(error);
diff --git a/test/showcmap.c b/test/showcmap.c
index 80575a4c..3e6e5e8e 100644
--- a/test/showcmap.c
+++ b/test/showcmap.c
@@ -7,7 +7,7 @@ int main(int argc, char **argv)
fz_cmap *cmap;
fz_file *file;
- err = fz_openfile(&file, argv[1], O_RDONLY);
+ err = fz_openfile(&file, argv[1], FZ_READ);
if (err)
fz_abort(err);