diff options
-rw-r--r-- | include/mupdf/fitz/stream.h | 12 | ||||
-rw-r--r-- | source/fitz/stream-open.c | 33 | ||||
-rw-r--r-- | source/fitz/stream-prog.c | 36 |
3 files changed, 40 insertions, 41 deletions
diff --git a/include/mupdf/fitz/stream.h b/include/mupdf/fitz/stream.h index fd9dea18..003e1bbe 100644 --- a/include/mupdf/fitz/stream.h +++ b/include/mupdf/fitz/stream.h @@ -20,7 +20,7 @@ typedef struct fz_stream_s fz_stream; fz_open_file: Open the named file and wrap it in a stream. filename: Path to a file. On non-Windows machines the filename should - be exactly as it would be passed to open(2). On Windows machines, the + be exactly as it would be passed to fopen(2). On Windows machines, the path should be UTF-8 encoded so that non-ASCII characters can be represented. Other platforms do the encoding as standard anyway (and in most cases, particularly for MacOS and Linux, the encoding they @@ -28,7 +28,7 @@ typedef struct fz_stream_s fz_stream; */ fz_stream *fz_open_file(fz_context *ctx, const char *filename); -fz_stream *fz_open_fd_progressive(fz_context *ctx, int fd, int bps); +fz_stream *fz_open_file_ptr_progressive(fz_context *ctx, FILE *file, int bps); fz_stream *fz_open_file_progressive(fz_context *ctx, const char *filename, int bps); /* @@ -37,20 +37,20 @@ fz_stream *fz_open_file_progressive(fz_context *ctx, const char *filename, int b This function is only available when compiling for Win32. filename: Wide character path to the file as it would be given - to _wopen(). + to _wfopen(). */ fz_stream *fz_open_file_w(fz_context *ctx, const wchar_t *filename); /* - fz_open_fd: Wrap an open file descriptor in a stream. + fz_open_file: Wrap an open file descriptor in a stream. file: An open file descriptor supporting bidirectional seeking. The stream will take ownership of the file descriptor, so it may not be modified or closed after the call - to fz_open_fd. When the stream is closed it will also close + to fz_open_file_ptr. When the stream is closed it will also close the file descriptor. */ -fz_stream *fz_open_fd(fz_context *ctx, int file); +fz_stream *fz_open_file_ptr(fz_context *ctx, FILE *file); /* fz_open_memory: Open a block of memory as a stream. 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); } |