X-Git-Url: http://git.draconx.ca/gitweb/liblbx.git/blobdiff_plain/e4d75d5e84c051b75bf6a81072734da22c44cb21..fc8dd8be3744680d93c490227be4db8628377d3c:/src/image.c diff --git a/src/image.c b/src/image.c index 5f3a6e3..78cb8a5 100644 --- a/src/image.c +++ b/src/image.c @@ -159,9 +159,41 @@ struct lbx_image *lbximg_open(void *f, const struct lbx_file_ops *fops, return img; } -struct lbx_image *lbximg_fopen(FILE *f) +static int pipe_close(void *f) { - return lbximg_open(f, &lbx_default_fops, NULL); + struct lbx_pipe_state *p = f; + int rc; + + rc = fclose(p->f); + free(p); + return rc; +} + +static int file_close(void *f) +{ + return fclose((FILE *)f); +} + +struct lbx_image *lbximg_fopen(const char *file) +{ + struct lbx_pipe_state *p; + FILE *f; + + f = fopen(file, "rb"); + if (!f) + return NULL; + + if (fseek(f, 0, SEEK_CUR) == 0) + return lbximg_open(f, &lbx_default_fops, file_close); + + p = malloc(sizeof *p); + if (!p) { + fclose(f); + return NULL; + } + + *p = (struct lbx_pipe_state) { .f = f }; + return lbximg_open(p, &lbx_pipe_fops, pipe_close); } static int _lbx_drawrow(int first, struct lbx_image *img)