summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTor Andersson <tor.andersson@artifex.com>2011-04-04 01:06:30 +0200
committerTor Andersson <tor.andersson@artifex.com>2011-04-04 01:06:30 +0200
commit4d125739c1ac42d629360a7e2700410e788028c7 (patch)
treea6cb6812fcde435c79ab646b3496c075e4e0a094
parentfc5ff7b56e0e0797570eba81a39e13d40aaccb40 (diff)
downloadmupdf-4d125739c1ac42d629360a7e2700410e788028c7.tar.xz
Change how files are opened with fz_openfd/file/filew.
-rw-r--r--apps/pdfapp.c2
-rw-r--r--fitz/fitz.h5
-rw-r--r--fitz/stm_open.c43
-rw-r--r--mupdf/cmapdump.c10
-rw-r--r--mupdf/pdf_xref.c7
-rw-r--r--xps/xps_tiff.c9
6 files changed, 52 insertions, 24 deletions
diff --git a/apps/pdfapp.c b/apps/pdfapp.c
index 0faff3b3..a9cb698a 100644
--- a/apps/pdfapp.c
+++ b/apps/pdfapp.c
@@ -108,7 +108,7 @@ static void pdfapp_open_pdf(pdfapp_t *app, char *filename, int fd)
* Open PDF and load xref table
*/
- file = fz_openfile(fd);
+ file = fz_openfd(fd);
error = pdf_openxrefwithstream(&app->xref, file, nil);
if (error)
pdfapp_error(app, fz_rethrow(error, "cannot open document '%s'", filename));
diff --git a/fitz/fitz.h b/fitz/fitz.h
index d79e280e..216c7264 100644
--- a/fitz/fitz.h
+++ b/fitz/fitz.h
@@ -515,8 +515,11 @@ struct fz_stream_s
unsigned char buf[4096];
};
-fz_stream *fz_openfile(int file);
+fz_stream *fz_openfd(int file);
+fz_stream *fz_openfile(const char *filename);
+fz_stream *fz_openfilew(const wchar_t *filename); /* only on win32 */
fz_stream *fz_openbuffer(fz_buffer *buf);
+fz_stream *fz_openmemory(unsigned char *data, int len);
void fz_close(fz_stream *stm);
fz_stream *fz_newstream(void*, int(*)(fz_stream*, unsigned char*, int), void(*)(fz_stream *));
diff --git a/fitz/stm_open.c b/fitz/stm_open.c
index 6f1160d8..c7cf8409 100644
--- a/fitz/stm_open.c
+++ b/fitz/stm_open.c
@@ -78,7 +78,7 @@ static void closefile(fz_stream *stm)
}
fz_stream *
-fz_openfile(int fd)
+fz_openfd(int fd)
{
fz_stream *stm;
int *state;
@@ -92,6 +92,26 @@ fz_openfile(int fd)
return stm;
}
+fz_stream *
+fz_openfile(const char *name)
+{
+ int fd = open(name, O_BINARY | O_RDONLY, 0);
+ if (fd == -1)
+ return nil;
+ return fz_openfd(fd);
+}
+
+#ifdef _WIN32
+fz_stream *
+fz_openfilew(const wchar_t *name)
+{
+ int fd = _wopen(name, O_BINARY | O_RDONLY, 0);
+ if (fd == -1)
+ return nil;
+ return fz_openfd(fd);
+}
+#endif
+
/* Memory stream */
static int readbuffer(fz_stream *stm, unsigned char *buf, int len)
@@ -113,7 +133,8 @@ static void seekbuffer(fz_stream *stm, int offset, int whence)
static void closebuffer(fz_stream *stm)
{
- fz_dropbuffer(stm->state);
+ if (stm->state)
+ fz_dropbuffer(stm->state);
}
fz_stream *
@@ -133,3 +154,21 @@ fz_openbuffer(fz_buffer *buf)
return stm;
}
+
+fz_stream *
+fz_openmemory(unsigned char *data, int len)
+{
+ fz_stream *stm;
+
+ stm = fz_newstream(nil, readbuffer, closebuffer);
+ stm->seek = seekbuffer;
+
+ stm->bp = data;
+ stm->rp = data;
+ stm->wp = data + len;
+ stm->ep = data + len;
+
+ stm->pos = len;
+
+ return stm;
+}
diff --git a/mupdf/cmapdump.c b/mupdf/cmapdump.c
index 411d1d11..1a587ac7 100644
--- a/mupdf/cmapdump.c
+++ b/mupdf/cmapdump.c
@@ -51,7 +51,6 @@ main(int argc, char **argv)
char name[256];
char *realname;
int i, k;
- int fd;
if (argc < 3)
{
@@ -89,14 +88,9 @@ main(int argc, char **argv)
strcpy(name, realname);
clean(name);
- fd = open(argv[i], O_BINARY | O_RDONLY, 0666);
- if (fd < 0)
- {
+ fi = fz_openfile(argv[i]);
+ if (!fi)
fz_throw("cmapdump: could not open input file '%s'\n", argv[i]);
- return 1;
- }
-
- fi = fz_openfile(fd);
error = pdf_parsecmap(&cmap, fi);
if (error)
diff --git a/mupdf/pdf_xref.c b/mupdf/pdf_xref.c
index 64d92cc7..2e98c97e 100644
--- a/mupdf/pdf_xref.c
+++ b/mupdf/pdf_xref.c
@@ -951,16 +951,15 @@ pdf_openxref(pdf_xref **xrefp, char *filename, char *password)
fz_error error;
pdf_xref *xref;
fz_stream *file;
- int fd;
- fd = open(filename, O_BINARY | O_RDONLY);
- if (fd < 0)
+ file = fz_openfile(filename);
+ if (!file)
return fz_throw("cannot open file '%s': %s", filename, strerror(errno));
- file = fz_openfile(fd);
error = pdf_openxrefwithstream(&xref, file, password);
if (error)
return fz_rethrow(error, "cannot load document '%s'", filename);
+
fz_close(file);
*xrefp = xref;
diff --git a/xps/xps_tiff.c b/xps/xps_tiff.c
index 8aabb084..c4fdbfcd 100644
--- a/xps/xps_tiff.c
+++ b/xps/xps_tiff.c
@@ -358,7 +358,6 @@ xps_expand_tiff_colormap(struct tiff *tiff)
static int
xps_decode_tiff_strips(struct tiff *tiff)
{
- fz_buffer buf;
fz_stream *stm;
int error;
@@ -454,14 +453,8 @@ xps_decode_tiff_strips(struct tiff *tiff)
for (i = 0; i < rlen; i++)
rp[i] = bitrev[rp[i]];
- /* create a fz_buffer on the stack */
- buf.refs = 2;
- buf.data = rp;
- buf.len = rlen;
- buf.cap = rlen;
-
/* the strip decoders will close this */
- stm = fz_openbuffer(&buf);
+ stm = fz_openmemory(rp, rlen);
switch (tiff->compression)
{