summaryrefslogtreecommitdiff
path: root/source/fitz/stream-open.c
diff options
context:
space:
mode:
authorTor Andersson <tor.andersson@artifex.com>2017-08-17 15:01:09 +0200
committerTor Andersson <tor.andersson@artifex.com>2017-11-01 13:08:56 +0100
commitf595e889b91a674eb94db7ca4d832da54f5194cd (patch)
tree273b4a7cc465477e428d0c628a19a61301baaae0 /source/fitz/stream-open.c
parent2910531c99e80bede06d2f1460459e8f6ce79961 (diff)
downloadmupdf-f595e889b91a674eb94db7ca4d832da54f5194cd.tar.xz
Use int64_t for public file API offsets.
Don't mess with conditional compilation with LARGEFILE -- always expose 64-bit file offsets in our public API.
Diffstat (limited to 'source/fitz/stream-open.c')
-rw-r--r--source/fitz/stream-open.c68
1 files changed, 40 insertions, 28 deletions
diff --git a/source/fitz/stream-open.c b/source/fitz/stream-open.c
index d7cff535..ffd61b66 100644
--- a/source/fitz/stream-open.c
+++ b/source/fitz/stream-open.c
@@ -1,3 +1,9 @@
+#define _LARGEFILE_SOURCE
+#ifndef _FILE_OFFSET_BITS
+#define _FILE_OFFSET_BITS 64
+#endif
+
+#include "mupdf/fitz.h"
#include "fitz-imp.h"
#include <string.h>
@@ -7,7 +13,12 @@
int
fz_file_exists(fz_context *ctx, const char *path)
{
- FILE *file = fz_fopen(path, "rb");
+ FILE *file;
+#if defined(_WIN32) || defined(_WIN64)
+ file = fz_fopen_utf8(path, "rb");
+#else
+ file = fopen(path, "rb");
+#endif
if (file)
fclose(file);
return !!file;
@@ -66,6 +77,9 @@ fz_drop_stream(fz_context *ctx, fz_stream *stm)
/* File stream */
+// TODO: WIN32: HANDLE CreateFileW(), etc.
+// TODO: POSIX: int creat(), read(), write(), lseeko, etc.
+
typedef struct fz_file_stream_s
{
FILE *file;
@@ -82,20 +96,28 @@ static int next_file(fz_context *ctx, fz_stream *stm, size_t n)
fz_throw(ctx, FZ_ERROR_GENERIC, "read error: %s", strerror(errno));
stm->rp = state->buffer;
stm->wp = state->buffer + n;
- stm->pos += (fz_off_t)n;
+ stm->pos += (int64_t)n;
if (n == 0)
return EOF;
return *stm->rp++;
}
-static void seek_file(fz_context *ctx, fz_stream *stm, fz_off_t offset, int whence)
+static void seek_file(fz_context *ctx, fz_stream *stm, int64_t offset, int whence)
{
fz_file_stream *state = stm->state;
- fz_off_t n = fz_fseek(state->file, offset, whence);
+#if defined(_WIN32) || defined(_WIN64)
+ int64_t n = _fseeki64(state->file, offset, whence);
+#else
+ int64_t n = fseeko(state->file, offset, whence);
+#endif
if (n < 0)
fz_throw(ctx, FZ_ERROR_GENERIC, "cannot seek: %s", strerror(errno));
- stm->pos = fz_ftell(state->file);
+#if defined(_WIN32) || defined(_WIN64)
+ stm->pos = _ftelli64(state->file);
+#else
+ stm->pos = ftello(state->file);
+#endif
stm->rp = state->buffer;
stm->wp = state->buffer;
}
@@ -109,7 +131,7 @@ static void close_file(fz_context *ctx, void *state_)
fz_free(ctx, state);
}
-fz_stream *
+static fz_stream *
fz_open_file_ptr(fz_context *ctx, FILE *file)
{
fz_stream *stm;
@@ -125,35 +147,25 @@ fz_open_file_ptr(fz_context *ctx, FILE *file)
fz_stream *
fz_open_file(fz_context *ctx, const char *name)
{
- FILE *f;
+ FILE *file;
#if defined(_WIN32) || defined(_WIN64)
- char *s = (char*)name;
- wchar_t *wname, *d;
- int c;
- d = wname = fz_malloc(ctx, (strlen(name)+1) * sizeof(wchar_t));
- while (*s) {
- s += fz_chartorune(&c, s);
- *d++ = c;
- }
- *d = 0;
- f = _wfopen(wname, L"rb");
- fz_free(ctx, wname);
+ file = fz_fopen_utf8(name, "rb");
#else
- f = fz_fopen(name, "rb");
+ file = fopen(name, "rb");
#endif
- if (f == NULL)
+ if (file == NULL)
fz_throw(ctx, FZ_ERROR_GENERIC, "cannot open %s: %s", name, strerror(errno));
- return fz_open_file_ptr(ctx, f);
+ return fz_open_file_ptr(ctx, file);
}
#if defined(_WIN32) || defined(_WIN64)
fz_stream *
fz_open_file_w(fz_context *ctx, const wchar_t *name)
{
- FILE *f = _wfopen(name, L"rb");
- if (f == NULL)
+ FILE *file = _wfopen(name, L"rb");
+ if (file == NULL)
fz_throw(ctx, FZ_ERROR_GENERIC, "cannot open file %ls: %s", name, strerror(errno));
- return fz_open_file_ptr(ctx, f);
+ return fz_open_file_ptr(ctx, file);
}
#endif
@@ -164,9 +176,9 @@ static int next_buffer(fz_context *ctx, fz_stream *stm, size_t max)
return EOF;
}
-static void seek_buffer(fz_context *ctx, fz_stream *stm, fz_off_t offset, int whence)
+static void seek_buffer(fz_context *ctx, fz_stream *stm, int64_t offset, int whence)
{
- fz_off_t pos = stm->pos - (stm->wp - stm->rp);
+ int64_t pos = stm->pos - (stm->wp - stm->rp);
/* Convert to absolute pos */
if (whence == 1)
{
@@ -202,7 +214,7 @@ fz_open_buffer(fz_context *ctx, fz_buffer *buf)
stm->rp = buf->data;
stm->wp = buf->data + buf->len;
- stm->pos = (fz_off_t)buf->len;
+ stm->pos = (int64_t)buf->len;
return stm;
}
@@ -218,7 +230,7 @@ fz_open_memory(fz_context *ctx, const unsigned char *data, size_t len)
stm->rp = (unsigned char *)data;
stm->wp = (unsigned char *)data + len;
- stm->pos = (fz_off_t)len;
+ stm->pos = (int64_t)len;
return stm;
}