]> git.draconx.ca Git - liblbx.git/blobdiff - src/lbx.c
liblbx: Rename lbx_stat to lbx_file_stat.
[liblbx.git] / src / lbx.c
index 77a150a7c229c5b65bdc237e5e79d2238aca540f..0e26315fc8652acdc922a7f526140d5288463ab9 100644 (file)
--- a/src/lbx.c
+++ b/src/lbx.c
@@ -1,7 +1,7 @@
 /*
  *  2ooM: The Master of Orion II Reverse Engineering Project
  *  Library for working with LBX archive 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
@@ -36,7 +36,7 @@
 int lbx_errno = 0;
 
 struct lbx_state {
-       const char *name;
+       char *name;
 
        const struct lbx_file_ops *fops;
        int (*dtor)(void *handle);
@@ -79,23 +79,40 @@ static struct lbx_state *lbx_init(unsigned char hdr[static LBX_HDR_SIZE])
        return lbx;
 }
 
+static char *str_dup(const char *s)
+{
+       char *buf;
+
+       buf = malloc(strlen(s)+1);
+       if (buf)
+               strcpy(buf, s);
+       return buf;
+}
+
 struct lbx_state *lbx_open(void *f, const struct lbx_file_ops *fops,
                            int (*destructor)(void *), const char *name)
 {
        unsigned char hdr_buf[LBX_HDR_SIZE];
-       struct lbx_state *lbx;
+       struct lbx_state *lbx = NULL;
+       char *dupname = NULL;
+
+       dupname = str_dup(name);
+       if (!dupname) {
+               lbx_errno = -errno;
+               goto err;
+       }
 
        if (fops->read(hdr_buf, sizeof hdr_buf, f) != sizeof hdr_buf) {
                lbx_errno = -errno;
-               return NULL;
+               goto err;
        }
 
        lbx = lbx_init(hdr_buf);
        if (!lbx)
-               return NULL;
+               goto err;
 
+       lbx->name = dupname;
        lbx->dtor = destructor;
-       lbx->name = name;
        lbx->fops = fops;
        lbx->f    = f;
 
@@ -106,19 +123,66 @@ struct lbx_state *lbx_open(void *f, const struct lbx_file_ops *fops,
                        lbx_errno = -errno;
                        if (fops->eof(f))
                                lbx_errno = LBX_EEOF;
-                       free(lbx);
-                       return NULL;
+                       goto err;
                }
 
                lbx->offsets[i] = unpack_32_le(buf);
        }
 
        return lbx;
+err:
+       free(dupname);
+       free(lbx);
+       return NULL;
+}
+
+static int file_close(void *f)
+{
+       return fclose((FILE *)f);
+}
+
+static int pipe_close(void *f)
+{
+       struct lbx_pipe_state *p = f;
+       int rc;
+
+       rc = fclose(p->f);
+       free(p);
+       return rc;
+}
+
+static char *last_component(const char *name)
+{
+       char *c;
+
+       /* TODO: Handle other path separators. */
+       c = strrchr(name, '/');
+       if (!c)
+               return (char *)name;
+       return c+1;
 }
 
-struct lbx_state *lbx_fopen(FILE *f, const char *name)
+struct lbx_state *lbx_fopen(const char *file)
 {
-       return lbx_open(f, &lbx_default_fops, NULL, name);
+       const char *name = last_component(file);
+       struct lbx_pipe_state *p;
+       FILE *f;
+
+       f = fopen(file, "rb");
+       if (!f)
+               return NULL;
+
+       if (fseek(f, 0, SEEK_CUR) == 0)
+               return lbx_open(f, &lbx_default_fops, file_close, name);
+
+       p = malloc(sizeof *p);
+       if (!p) {
+               fclose(f);
+               return NULL;
+       }
+
+       *p = (struct lbx_pipe_state) { .f = f };
+       return lbx_open(p, &lbx_pipe_fops, pipe_close, name);
 }
 
 size_t lbx_numfiles(struct lbx_state *lbx)
@@ -126,19 +190,20 @@ size_t lbx_numfiles(struct lbx_state *lbx)
        return lbx->nfiles;
 }
 
-int lbx_stat(struct lbx_state *lbx, size_t index, struct lbx_statbuf *buf)
+int
+lbx_file_stat(struct lbx_state *lbx, unsigned fileno, struct lbx_statbuf *buf)
 {
        static char str[256]; /* FIXME */
 
-       if (index >= lbx->nfiles) {
+       if (fileno >= lbx->nfiles) {
                buf->name = NULL;
                lbx_errno = LBX_ERANGE;
                return -1;
        }
 
-       snprintf(str, sizeof str, "%s.%03zu", lbx->name, index);
+       snprintf(str, sizeof str, "%s.%03u", lbx->name, fileno);
        buf->name = str;
-       buf->size = lbx->offsets[index+1] - lbx->offsets[index];
+       buf->size = lbx->offsets[fileno+1] - lbx->offsets[fileno];
        return 0;
 }
 
@@ -148,6 +213,7 @@ int lbx_close(struct lbx_state *lbx)
 
        if (lbx && lbx->dtor)
                rc = lbx->dtor(lbx->f);
+       free(lbx->name);
        free(lbx);
 
        return rc;
@@ -228,6 +294,8 @@ int lbx_file_seek(struct lbx_file_state *f, long offset, int whence)
        f->lbx->last_file = NULL;
        if (fops->seek(f->lbx->f, f->base + pos, SEEK_SET) != 0)
                return -1;
+
+       f->offset = pos;
        f->lbx->last_file = f;
        f->eof = 0;