]> git.draconx.ca Git - liblbx.git/commitdiff
liblbx: Parameterise I/O functions for image handling.
authorNick Bowler <nbowler@draconx.ca>
Thu, 4 Feb 2010 16:51:12 +0000 (11:51 -0500)
committerNick Bowler <nbowler@draconx.ca>
Mon, 8 Feb 2010 17:00:54 +0000 (12:00 -0500)
This finally allows us to kill the horrible _lbx_fseek function.

src/Makefile.inc
src/image.c
src/image.h
src/lbximg.c
src/misc.c [deleted file]
src/misc.h

index a8772d183ffaf5777ff73593d1acbb3ef5fdc7be..8b39a5203cd067775facadda425c02af9cc12fce 100644 (file)
@@ -10,7 +10,7 @@ lbx_HEADERS = src/lbx.h src/image.h
 noinst_HEADERS += src/misc.h src/tools.h src/pack.h
 
 lib_LTLIBRARIES  += liblbx.la
-liblbx_la_SOURCES = src/misc.c src/lbx.c src/fops.c src/image.c src/pack.c
+liblbx_la_SOURCES = src/lbx.c src/fops.c src/image.c src/pack.c
 
 bin_PROGRAMS    += lbxtool
 lbxtool_SOURCES  = src/lbxtool.c
index 9bc4985942c69411228d85b7e05451c26a135279..f98238e49edb0f79395551d14ed4c7c9e66599fb 100644 (file)
@@ -43,8 +43,11 @@ struct lbx_image {
        unsigned short frames, leadin;
        unsigned short palstart, palcount;
 
-       FILE *f;
-       long foff, paloff;
+       const struct lbx_file_ops *fops;
+       int (*dtor)(void *handle);
+       void *f;
+
+       long paloff;
 
        int currentframe;
        int currentx, currenty;
@@ -79,15 +82,15 @@ static struct lbx_image *lbximg_init(unsigned char hdr[static HDR_LEN])
        return img;
 }
 
-struct lbx_image *lbximg_fopen(FILE *f)
+struct lbx_image *lbximg_open(void *f, const struct lbx_file_ops *fops,
+                              int (*destructor)(void *))
 {
        unsigned char hdr_buf[HDR_LEN];
        struct lbx_image *img;
-       size_t rc;
 
-       if (fread(hdr_buf, 1, sizeof hdr_buf, f) != sizeof hdr_buf) {
+       if (fops->read(hdr_buf, sizeof hdr_buf, f) != sizeof hdr_buf) {
                lbx_errno = -errno;
-               if (feof(f))
+               if (fops->eof(f))
                        lbx_errno = LBX_EEOF;
                return NULL;
        }
@@ -97,7 +100,8 @@ struct lbx_image *lbximg_fopen(FILE *f)
                return NULL;
 
        img->f    = f;
-       img->foff = sizeof hdr_buf;
+       img->fops = fops;
+       img->dtor = destructor;
 
        /*
         * DEBUG ONLY.  These assertions exist to catch otherwise valid image
@@ -113,24 +117,23 @@ struct lbx_image *lbximg_fopen(FILE *f)
        for (unsigned i = 0; i <= img->frames; i++) {
                unsigned char buf[4];
 
-               if (fread(buf, 1, sizeof buf, f) != sizeof buf) {
+               if (fops->read(buf, sizeof buf, f) != sizeof buf) {
                        lbx_errno = -errno;
-                       if (feof(f))
+                       if (fops->eof(f))
                                lbx_errno = LBX_EEOF;
                        free(img);
                        return NULL;
                }
 
                img->offsets[i] = unpack_32_le(buf);
-               img->foff += 4;
        }
 
        if (img->flags & FLAG_PALETTE) {
                unsigned char buf[4];
 
-               if (fread(buf, 1, sizeof buf, f) != sizeof buf) {
+               if (fops->read(buf, sizeof buf, f) != sizeof buf) {
                        lbx_errno = -errno;
-                       if (feof(f))
+                       if (fops->eof(f))
                                lbx_errno = LBX_EEOF;
                        free(img);
                        return NULL;
@@ -138,8 +141,7 @@ struct lbx_image *lbximg_fopen(FILE *f)
 
                img->palstart = unpack_16_le(buf+0);
                img->palcount = unpack_16_le(buf+2);
-               img->foff    += sizeof buf;
-               img->paloff   = img->foff;
+               img->paloff   = fops->tell(f);
 
                if (img->palstart + img->palcount > 256) {
                        lbx_errno = LBX_EFORMAT;
@@ -151,6 +153,11 @@ struct lbx_image *lbximg_fopen(FILE *f)
        return img;
 }
 
+struct lbx_image *lbximg_fopen(FILE *f)
+{
+       return lbximg_open(f, &lbx_default_fops, NULL);
+}
+
 static int _lbx_drawrow(int first, struct lbx_image *img)
 {
        unsigned short type, count, yval, xval;
@@ -161,9 +168,8 @@ static int _lbx_drawrow(int first, struct lbx_image *img)
        assert(img->framedata);
        assert(img->mask);
 
-       if (fread(buf, 1, sizeof buf, img->f) != sizeof buf)
+       if (img->fops->read(buf, sizeof buf, img->f) != sizeof buf)
                goto readerr;
-       img->foff += 4;
        type = unpack_16_le(buf+0);
 
        if (first) {
@@ -177,9 +183,8 @@ static int _lbx_drawrow(int first, struct lbx_image *img)
                if (yval == 1000)
                        return 1;
 
-               if (fread(buf, 1, sizeof buf, img->f) != sizeof buf)
+               if (img->fops->read(buf, sizeof buf, img->f) != sizeof buf)
                        goto readerr;
-               img->foff += 4;
                count = unpack_16_le(buf+0);
 
                xval = unpack_16_le(buf+2);
@@ -214,26 +219,22 @@ static int _lbx_drawrow(int first, struct lbx_image *img)
        memset(&img->mask[img->currenty][img->currentx], 1, count);
 
        pos = &img->framedata[img->currenty][img->currentx];
-       rc  = fread(pos, 1, count, img->f);
+       rc  = img->fops->read(pos, count, img->f);
        img->currentx += rc;
-       img->foff     += rc;
 
        if (rc < count)
                goto readerr;
 
        if (count % 2) {
-               if (fread(buf, 1, 1, img->f) != 1)
+               if (img->fops->read(buf, 1, img->f) != 1)
                        goto readerr;
-               img->foff += 1;
        }
 
        return 0;
 readerr:
-       if (feof(img->f)) {
+       lbx_errno = -errno;
+       if (img->fops->eof(img->f))
                lbx_errno = LBX_EEOF;
-       } else {
-               lbx_errno = -errno;
-       }
        return -1;
 }
 
@@ -300,8 +301,10 @@ unsigned char **lbximg_getframe(struct lbx_image *img, int frame)
        if (img->currentframe != frame) {
                int rc, first = 1;
 
-               if (_lbx_fseek(img->f, &img->foff, img->offsets[frame]) == -1)
+               if (img->fops->seek(img->f, img->offsets[frame], SEEK_SET)) {
+                       lbx_errno = -errno;
                        return NULL;
+               }
 
                do {
                        rc = _lbx_drawrow(first, img);
@@ -309,7 +312,7 @@ unsigned char **lbximg_getframe(struct lbx_image *img, int frame)
                                return NULL;
                        first = 0;
 
-                       if (!rc && img->foff > img->offsets[frame+1]) {
+                       if (img->fops->tell(img->f) > img->offsets[frame+1]) {
                                lbx_errno = LBX_EFORMAT;
                                return NULL;
                        }
@@ -357,12 +360,13 @@ lbximg_getpalette(struct lbx_image *img, struct lbx_colour palette[static 256])
        if (!(img->flags & FLAG_PALETTE))
                return 0;
 
-       if (_lbx_fseek(img->f, &img->foff, img->paloff) == -1)
+       if (img->fops->seek(img->f, img->paloff, SEEK_SET)) {
+               lbx_errno = -errno;
                return -1;
+       }
 
        for (i = 0; i < img->palcount; i++) {
-               rc = fread(entry, 1, sizeof entry, img->f);
-               img->foff += rc;
+               rc = img->fops->read(entry, sizeof entry, img->f);
 
                if (rc < sizeof entry) {
                        goto readerr;
@@ -382,7 +386,7 @@ lbximg_getpalette(struct lbx_image *img, struct lbx_colour palette[static 256])
 
        return 0;
 readerr:
-       lbx_errno = feof(img->f) ? LBX_EEOF : -errno;
+       lbx_errno = img->fops->eof(img->f) ? LBX_EEOF : -errno;
        return -1;
 }
 
@@ -410,23 +414,27 @@ unsigned char **lbximg_getmask(struct lbx_image *img)
        return img->mask;
 }
 
-void lbximg_close(struct lbx_image *img)
+int lbximg_close(struct lbx_image *img)
 {
-       if (!img) return;
+       int rc = 0;
 
-       if (img->framedata) {
-               free(img->framedata[0]);
-               free(img->framedata);
-       }
+       if (img) {
+               if (img->framedata) {
+                       free(img->framedata[0]);
+                       free(img->framedata);
+               }
 
-       if (img->mask) {
-               free(img->mask[0]);
-               free(img->mask);
-       }
+               if (img->mask) {
+                       free(img->mask[0]);
+                       free(img->mask);
+               }
+
+               if (img && img->dtor) {
+                       rc = img->dtor(img->f);
+               }
 
-       if (img->f) {
-               fclose(img->f);
+               free(img);
        }
 
-       free(img);
+       return rc;
 }
index 7f70b434550cd6c589d5c6e886bf4e95de862017..5369e0aafbed4affb7ba4e0319664a0b3ee98f60 100644 (file)
@@ -4,6 +4,7 @@
 #include <stdio.h>
 
 typedef struct lbx_image LBX_IMG;
+struct lbx_file_ops;
 
 struct lbx_colour {
        unsigned char red;
@@ -18,8 +19,11 @@ struct lbx_imginfo {
        int looping;
 };
 
+LBX_IMG *lbximg_open(void *f, const struct lbx_file_ops *fops,
+                     int (*destructor)(void *));
 LBX_IMG *lbximg_fopen(FILE *f);
-void     lbximg_close(LBX_IMG *img);
+int      lbximg_close(LBX_IMG *img);
+
 unsigned char **lbximg_getframe(LBX_IMG *img, int frame);
 unsigned char **lbximg_getmask(LBX_IMG *img);
 
index fe7ab584fefc7bab8b5685bdf5ed9d58a2e789ed..7827a87751685e487201c5590d679193700dece8 100644 (file)
@@ -364,11 +364,11 @@ err:
 
 int main(int argc, char **argv)
 {
-       int mode = MODE_NONE;
-       FILE *inf = stdin, *palf = NULL, *overf = NULL;
+       int mode = MODE_NONE, opt, rc = EXIT_FAILURE;
+       struct lbx_pipe_state state = { .f = stdin };
+       FILE *palf = NULL, *overf = NULL;
        const char *name = "stdin";
        LBX_IMG *img;
-       int opt;
 
        static const char *sopts = "idvf:p:O:V";
        static const struct option lopts[] = {
@@ -407,8 +407,7 @@ int main(int argc, char **argv)
                        name = strrchr(optarg, '/');
                        name = name ? name+1 : optarg;
 
-                       inf = fopen(optarg, "rb");
-                       if (!inf) {
+                       if (!freopen(optarg, "rb", state.f)) {
                                errmsg("failed to open %s: %m\n", optarg);
                                return EXIT_FAILURE;
                        }
@@ -448,7 +447,11 @@ int main(int argc, char **argv)
                return EXIT_FAILURE;
        }
 
-       img = lbximg_fopen(inf);
+       if (fseek(state.f, 0, SEEK_CUR) == 0)
+               img = lbximg_open(state.f, &lbx_default_fops, NULL);
+       else
+               img = lbximg_open(&state, &lbx_pipe_fops, NULL);
+
        if (!img) {
                errmsg("failed to open image: %s.\n", lbx_strerror());
                return EXIT_FAILURE;
@@ -466,13 +469,10 @@ int main(int argc, char **argv)
 
        switch (mode) {
        case MODE_DECODE:
-               if (decode(img, palf, overf, &argv[optind])) {
-                       lbximg_close(img);
-                       return EXIT_FAILURE;
-               }
+               rc = decode(img, palf, overf, &argv[optind]);
                break;
        }
 
        lbximg_close(img);
-       return EXIT_SUCCESS;
+       return rc;
 }
diff --git a/src/misc.c b/src/misc.c
deleted file mode 100644 (file)
index 74cc77a..0000000
+++ /dev/null
@@ -1,63 +0,0 @@
-/*
- *  2ooM: The Master of Orion II Reverse Engineering Project
- *  Miscellaneous common routines for liblbx.
- *  Copyright (C) 2006-2008 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
- *  the Free Software Foundation, either version 3 of the License, or
- *  (at your option) any later version.
- *
- *  This program is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *  GNU General Public License for more details.
- *
- *  You should have received a copy of the GNU General Public License
- *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
- */
-#ifdef HAVE_CONFIG_H
-#      include "config.h"
-#endif
-
-#include <stdio.h>
-#include <errno.h>
-
-#include "misc.h"
-#include "lbx.h"
-
-int _lbx_fseek(FILE *f, long *current, size_t offset)
-{
-       static unsigned char oblivion[1024];
-       long dist;
-
-       if (*current < offset) {
-               dist = offset - *current;
-       } else if (*current > offset) {
-               dist = -(long)(*current - offset);
-       } else {
-               return 0;
-       }
-
-       if (fseek(f, dist, SEEK_CUR) == 0) {
-               *current += dist;
-       } else if (*current < offset) {
-               while (dist) {
-                       size_t rc, amt = MIN(sizeof oblivion, dist);
-                       rc = fread(oblivion, 1, amt, f);
-                       *current += rc;
-                       dist -= rc;
-                       if (rc < amt) {
-                               if (feof(f))
-                                       lbx_errno = LBX_EEOF;
-                               else
-                                       lbx_errno = -errno;
-                               return -1;
-                       }
-               }
-       } else {
-               lbx_errno = -errno;
-               return -1;
-       }
-       return 0;
-}
index af7298d95fad80a0b16425c0c75236dc3db7cf49..839908e3e81d3f124c6d087b7a19ab4c1786c2f0 100644 (file)
 #      define _lbx_assert(expr) ((expr) ? 1 : 0)
 #endif
 
-/** _lbx_fseek()
- * Seeks the specified file to the specified offset, given that the file is
- * currently positioned at *current.  If the file does not support seeking,
- * and offset > *current, perform the seek by reading (and discarding) data.
- */
-int _lbx_fseek(FILE *f, long *current, size_t offset);
-
 #endif