/*
* 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
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)