X-Git-Url: http://git.draconx.ca/gitweb/liblbx.git/blobdiff_plain/91689c5ef8174cc65e1f3ce31f9869778b2af671..f8295b32a98df79caee16a25a1dcf3a7a678e409:/src/image.c diff --git a/src/image.c b/src/image.c index 5f3a6e3..cc8d54b 100644 --- a/src/image.c +++ b/src/image.c @@ -1,7 +1,7 @@ /* * 2ooM: The Master of Orion II Reverse Engineering Project * Library for working with LBX image files. - * Copyright (C) 2006-2008 Nick Bowler + * Copyright (C) 2006-2010 Nick Bowler * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -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)