summaryrefslogtreecommitdiff
path: root/fitz/stm_open.c
diff options
context:
space:
mode:
Diffstat (limited to 'fitz/stm_open.c')
-rw-r--r--fitz/stm_open.c43
1 files changed, 23 insertions, 20 deletions
diff --git a/fitz/stm_open.c b/fitz/stm_open.c
index 74346f73..aa99451b 100644
--- a/fitz/stm_open.c
+++ b/fitz/stm_open.c
@@ -1,13 +1,13 @@
#include "fitz.h"
fz_stream *
-fz_new_stream(void *state,
+fz_new_stream(fz_context *ctx, void *state,
int(*read)(fz_stream *stm, unsigned char *buf, int len),
void(*close)(fz_stream *stm))
{
fz_stream *stm;
- stm = fz_malloc(sizeof(fz_stream));
+ stm = fz_malloc(ctx, sizeof(fz_stream));
stm->refs = 1;
stm->error = 0;
@@ -26,6 +26,7 @@ fz_new_stream(void *state,
stm->read = read;
stm->close = close;
stm->seek = NULL;
+ stm->ctx = ctx;
return stm;
}
@@ -40,12 +41,14 @@ fz_keep_stream(fz_stream *stm)
void
fz_close(fz_stream *stm)
{
+ if (stm == NULL)
+ return;
stm->refs --;
if (stm->refs == 0)
{
if (stm->close)
stm->close(stm);
- fz_free(stm);
+ fz_free(stm->ctx, stm);
}
}
@@ -55,7 +58,7 @@ static int read_file(fz_stream *stm, unsigned char *buf, int len)
{
int n = read(*(int*)stm->state, buf, len);
if (n < 0)
- return fz_throw("read error: %s", strerror(errno));
+ return fz_error_make("read error: %s", strerror(errno));
return n;
}
@@ -63,7 +66,7 @@ static void seek_file(fz_stream *stm, int offset, int whence)
{
int n = lseek(*(int*)stm->state, offset, whence);
if (n < 0)
- fz_warn("cannot lseek: %s", strerror(errno));
+ fz_warn(stm->ctx, "cannot lseek: %s", strerror(errno));
stm->pos = n;
stm->rp = stm->bp;
stm->wp = stm->bp;
@@ -73,42 +76,42 @@ static void close_file(fz_stream *stm)
{
int n = close(*(int*)stm->state);
if (n < 0)
- fz_warn("close error: %s", strerror(errno));
- fz_free(stm->state);
+ fz_warn(stm->ctx, "close error: %s", strerror(errno));
+ fz_free(stm->ctx, stm->state);
}
fz_stream *
-fz_open_fd(int fd)
+fz_open_fd(fz_context *ctx, int fd)
{
fz_stream *stm;
int *state;
- state = fz_malloc(sizeof(int));
+ state = fz_malloc(ctx, sizeof(int));
*state = fd;
- stm = fz_new_stream(state, read_file, close_file);
+ stm = fz_new_stream(ctx, state, read_file, close_file);
stm->seek = seek_file;
return stm;
}
fz_stream *
-fz_open_file(const char *name)
+fz_open_file(fz_context *ctx, const char *name)
{
int fd = open(name, O_BINARY | O_RDONLY, 0);
if (fd == -1)
- return NULL;
- return fz_open_fd(fd);
+ fz_throw(ctx, "Failed to open %s", name);
+ return fz_open_fd(ctx, fd);
}
#ifdef _WIN32
fz_stream *
-fz_open_file_w(const wchar_t *name)
+fz_open_file_w(fz_context *ctx, const wchar_t *name)
{
int fd = _wopen(name, O_BINARY | O_RDONLY, 0);
if (fd == -1)
return NULL;
- return fz_open_fd(fd);
+ return fz_open_fd(ctx, fd);
}
#endif
@@ -134,15 +137,15 @@ static void seek_buffer(fz_stream *stm, int offset, int whence)
static void close_buffer(fz_stream *stm)
{
if (stm->state)
- fz_drop_buffer(stm->state);
+ fz_drop_buffer(stm->ctx, stm->state);
}
fz_stream *
-fz_open_buffer(fz_buffer *buf)
+fz_open_buffer(fz_context *ctx, fz_buffer *buf)
{
fz_stream *stm;
- stm = fz_new_stream(fz_keep_buffer(buf), read_buffer, close_buffer);
+ stm = fz_new_stream(ctx, fz_keep_buffer(buf), read_buffer, close_buffer);
stm->seek = seek_buffer;
stm->bp = buf->data;
@@ -156,11 +159,11 @@ fz_open_buffer(fz_buffer *buf)
}
fz_stream *
-fz_open_memory(unsigned char *data, int len)
+fz_open_memory(fz_context *ctx, unsigned char *data, int len)
{
fz_stream *stm;
- stm = fz_new_stream(NULL, read_buffer, close_buffer);
+ stm = fz_new_stream(ctx, NULL, read_buffer, close_buffer);
stm->seek = seek_buffer;
stm->bp = data;