]> git.draconx.ca Git - liblbx.git/blobdiff - src/image.c
license: Fix copyright years in source files.
[liblbx.git] / src / image.c
index 5f3a6e320c51f62010d12f24ac8fac28a0d9bd63..cc8d54bf290cd44bf237048bb44e3096b57af0b5 100644 (file)
@@ -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)