summaryrefslogtreecommitdiff
path: root/source/fitz
diff options
context:
space:
mode:
authorRobin Watts <robin.watts@artifex.com>2015-03-21 00:18:27 +0000
committerRobin Watts <robin.watts@artifex.com>2015-05-14 13:29:56 +0100
commitdeb9de94ac7fed8c98ebc5d93e844cc149a8cf86 (patch)
treecb07ba20a6b3d399c3adea8c10b4c91243ddc3fb /source/fitz
parent987969ac033151810aade57d45c3d968dbf83d60 (diff)
downloadmupdf-deb9de94ac7fed8c98ebc5d93e844cc149a8cf86.tar.xz
Move away from file descriptors to FILE *'s.
Diffstat (limited to 'source/fitz')
-rw-r--r--source/fitz/stream-open.c33
-rw-r--r--source/fitz/stream-prog.c36
2 files changed, 34 insertions, 35 deletions
diff --git a/source/fitz/stream-open.c b/source/fitz/stream-open.c
index 6994293e..10d94a09 100644
--- a/source/fitz/stream-open.c
+++ b/source/fitz/stream-open.c
@@ -60,7 +60,7 @@ fz_drop_stream(fz_context *ctx, fz_stream *stm)
typedef struct fz_file_stream_s
{
- int file;
+ FILE *file;
unsigned char buffer[4096];
} fz_file_stream;
@@ -69,7 +69,7 @@ static int next_file(fz_context *ctx, fz_stream *stm, int n)
fz_file_stream *state = stm->state;
/* n is only a hint, that we can safely ignore */
- n = read(state->file, state->buffer, sizeof(state->buffer));
+ n = fread(state->buffer, 1, sizeof(state->buffer), state->file);
if (n < 0)
fz_throw(ctx, FZ_ERROR_GENERIC, "read error: %s", strerror(errno));
stm->rp = state->buffer;
@@ -84,10 +84,10 @@ static int next_file(fz_context *ctx, fz_stream *stm, int n)
static void seek_file(fz_context *ctx, fz_stream *stm, int offset, int whence)
{
fz_file_stream *state = stm->state;
- int n = lseek(state->file, offset, whence);
+ int n = fseek(state->file, offset, whence);
if (n < 0)
- fz_throw(ctx, FZ_ERROR_GENERIC, "cannot lseek: %s", strerror(errno));
- stm->pos = n;
+ fz_throw(ctx, FZ_ERROR_GENERIC, "cannot seek: %s", strerror(errno));
+ stm->pos = ftell(state->file);
stm->rp = state->buffer;
stm->wp = state->buffer;
}
@@ -95,18 +95,18 @@ static void seek_file(fz_context *ctx, fz_stream *stm, int offset, int whence)
static void close_file(fz_context *ctx, void *state_)
{
fz_file_stream *state = state_;
- int n = close(state->file);
+ int n = fclose(state->file);
if (n < 0)
fz_warn(ctx, "close error: %s", strerror(errno));
fz_free(ctx, state);
}
fz_stream *
-fz_open_fd(fz_context *ctx, int fd)
+fz_open_file_ptr(fz_context *ctx, FILE *file)
{
fz_stream *stm;
fz_file_stream *state = fz_malloc_struct(ctx, fz_file_stream);
- state->file = fd;
+ state->file = file;
fz_try(ctx)
{
@@ -125,34 +125,35 @@ fz_open_fd(fz_context *ctx, int fd)
fz_stream *
fz_open_file(fz_context *ctx, const char *name)
{
+ FILE *f;
#if defined(_WIN32) || defined(_WIN64)
char *s = (char*)name;
wchar_t *wname, *d;
- int c, fd;
+ int c;
d = wname = fz_malloc(ctx, (strlen(name)+1) * sizeof(wchar_t));
while (*s) {
s += fz_chartorune(&c, s);
*d++ = c;
}
*d = 0;
- fd = _wopen(wname, O_BINARY | O_RDONLY, 0);
+ f = _wfopen(wname, L"rb");
fz_free(ctx, wname);
#else
- int fd = open(name, O_BINARY | O_RDONLY, 0);
+ f = fopen(name, "rb");
#endif
- if (fd == -1)
+ if (f == NULL)
fz_throw(ctx, FZ_ERROR_GENERIC, "cannot open %s", name);
- return fz_open_fd(ctx, fd);
+ return fz_open_file_ptr(ctx, f);
}
#if defined(_WIN32) || defined(_WIN64)
fz_stream *
fz_open_file_w(fz_context *ctx, const wchar_t *name)
{
- int fd = _wopen(name, O_BINARY | O_RDONLY, 0);
- if (fd == -1)
+ FILE *f = _wfopen(name, L"rb");
+ if (f == NULL)
fz_throw(ctx, FZ_ERROR_GENERIC, "cannot open file %ls", name);
- return fz_open_fd(ctx, fd);
+ return fz_open_file_ptr(ctx, f);
}
#endif
diff --git a/source/fitz/stream-prog.c b/source/fitz/stream-prog.c
index c7054bcb..9911ddf6 100644
--- a/source/fitz/stream-prog.c
+++ b/source/fitz/stream-prog.c
@@ -19,7 +19,7 @@ show_progress(int av, int pos)
typedef struct prog_state
{
- int fd;
+ FILE *file;
int length;
int available;
int bps;
@@ -55,7 +55,7 @@ static int next_prog(fz_context *ctx, fz_stream *stm, int len)
}
}
- n = (len > 0 ? read(ps->fd, buf, len) : 0);
+ n = (len > 0 ? fread(buf, 1, (unsigned int)len, ps->file) : 0);
if (n < 0)
fz_throw(ctx, FZ_ERROR_GENERIC, "read error: %s", strerror(errno));
stm->rp = ps->buffer + stm->pos;
@@ -69,7 +69,6 @@ static int next_prog(fz_context *ctx, fz_stream *stm, int len)
static void seek_prog(fz_context *ctx, fz_stream *stm, int offset, int whence)
{
prog_state *ps = (prog_state *)stm->state;
- int n;
/* Simulate more data having arrived */
if (ps->available < ps->length)
@@ -106,19 +105,16 @@ static void seek_prog(fz_context *ctx, fz_stream *stm, int offset, int whence)
}
}
- n = lseek(ps->fd, offset, whence);
- if (n < 0)
- fz_throw(ctx, FZ_ERROR_GENERIC, "cannot lseek: %s", strerror(errno));
- stm->pos = n;
+ if (fseek(ps->file, offset, whence) != 0)
+ fz_throw(ctx, FZ_ERROR_GENERIC, "cannot seek: %s", strerror(errno));
+ stm->pos = offset;
stm->wp = stm->rp;
}
static void close_prog(fz_context *ctx, void *state)
{
prog_state *ps = (prog_state *)state;
- int n = close(ps->fd);
- if (n < 0)
- fz_warn(ctx, "close error: %s", strerror(errno));
+ fclose(ps->file);
fz_free(ctx, state);
}
@@ -137,19 +133,20 @@ static int meta_prog(fz_context *ctx, fz_stream *stm, int key, int size, void *p
}
fz_stream *
-fz_open_fd_progressive(fz_context *ctx, int fd, int bps)
+fz_open_file_ptr_progressive(fz_context *ctx, FILE *file, int bps)
{
fz_stream *stm;
prog_state *state;
state = fz_malloc_struct(ctx, prog_state);
- state->fd = fd;
+ state->file = file;
state->bps = bps;
state->start_time = clock();
state->available = 0;
- state->length = lseek(state->fd, 0, SEEK_END);
- lseek(state->fd, 0, SEEK_SET);
+ fseek(state->file, 0, SEEK_END);
+ state->length = ftell(state->file);
+ fseek(state->file, 0, SEEK_SET);
fz_try(ctx)
{
@@ -169,22 +166,23 @@ fz_open_fd_progressive(fz_context *ctx, int fd, int bps)
fz_stream *
fz_open_file_progressive(fz_context *ctx, const char *name, int bps)
{
+ FILE *f;
#if defined(_WIN32) || defined(_WIN64)
char *s = (char*)name;
wchar_t *wname, *d;
- int c, fd;
+ int c;
d = wname = fz_malloc(ctx, (strlen(name)+1) * sizeof(wchar_t));
while (*s) {
s += fz_chartorune(&c, s);
*d++ = c;
}
*d = 0;
- fd = _wopen(wname, O_BINARY | O_RDONLY, 0);
+ f = _wfopen(wname, L"rb");
fz_free(ctx, wname);
#else
- int fd = open(name, O_BINARY | O_RDONLY, 0);
+ f = fopen(name, "rb");
#endif
- if (fd == -1)
+ if (f == NULL)
fz_throw(ctx, FZ_ERROR_GENERIC, "cannot open %s", name);
- return fz_open_fd_progressive(ctx, fd, bps);
+ return fz_open_file_ptr_progressive(ctx, f, bps);
}