]> git.draconx.ca Git - liblbx.git/blobdiff - src/image.c
liblbx: Update lbximg_fopen to work like lbx_fopen.
[liblbx.git] / src / image.c
index 5f3a6e320c51f62010d12f24ac8fac28a0d9bd63..78cb8a5d38c50ce41fae3c778a6daab0d0bdc7d2 100644 (file)
@@ -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)