]> git.draconx.ca Git - liblbx.git/commitdiff
liblbx: Start conversion to public/private structure bits.
authorNick Bowler <draconx@users.sourceforge.net>
Sat, 14 May 2011 01:03:17 +0000 (21:03 -0400)
committerNick Bowler <draconx@users.sourceforge.net>
Sat, 14 May 2011 01:08:26 +0000 (21:08 -0400)
This sort of interface is really a lot nicer, and avoids stupid
accessor functions.

src/lbx.c
src/lbx.h

index 720cf077881dc6861473b7438de0659cfaf7b09c..1d92f469d9b8c357003f61c05c381969ab61ea5e 100644 (file)
--- a/src/lbx.c
+++ b/src/lbx.c
@@ -31,7 +31,9 @@
 #define LBX_MAGIC    0x0000fead
 #define LBX_HDR_SIZE 8
 
-struct lbx_state {
+struct lbx_priv {
+       struct lbx pub;
+
        char *name;
 
        const struct lbx_file_ops *fops;
@@ -40,22 +42,21 @@ struct lbx_state {
 
        struct lbx_file_state *last_file;
 
-       unsigned short nfiles;
        unsigned long offsets[];
 };
 
 struct lbx_file_state {
        unsigned long base, limit, offset;
-       struct lbx_state *lbx;
+       struct lbx_priv *lbx;
        int eof;
 };
 
-static struct lbx_state *lbx_init(unsigned char hdr[static LBX_HDR_SIZE])
+static struct lbx_priv *lbx_init(unsigned char hdr[static LBX_HDR_SIZE])
 {
        unsigned short nfiles  = unpack_16_le(hdr+0);
        unsigned long  magic   = unpack_32_le(hdr+2);
        unsigned short version = unpack_16_le(hdr+6);
-       struct lbx_state *lbx;
+       struct lbx_priv *lbx;
 
        if (magic != LBX_MAGIC) {
                lbx_error_raise(LBX_EMAGIC);
@@ -68,8 +69,8 @@ static struct lbx_state *lbx_init(unsigned char hdr[static LBX_HDR_SIZE])
                return NULL;
        }
 
-       *lbx = (struct lbx_state) {
-               .nfiles = nfiles,
+       *lbx = (struct lbx_priv) {
+               .pub = { .nfiles = nfiles, },
        };
 
        return lbx;
@@ -85,11 +86,11 @@ static char *str_dup(const char *s)
        return buf;
 }
 
-struct lbx_state *lbx_open(void *f, const struct lbx_file_ops *fops,
-                           int (*destructor)(void *), const char *name)
+struct lbx *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 = NULL;
+       struct lbx_priv *lbx = NULL;
        char *dupname = NULL;
 
        dupname = str_dup(name);
@@ -113,7 +114,7 @@ struct lbx_state *lbx_open(void *f, const struct lbx_file_ops *fops,
        lbx->fops = fops;
        lbx->f    = f;
 
-       for (unsigned i = 0; i <= lbx->nfiles; i++) {
+       for (unsigned i = 0; i <= lbx->pub.nfiles; i++) {
                unsigned char buf[4];
 
                if (fops->read(buf, sizeof buf, f) != sizeof buf) {
@@ -125,7 +126,7 @@ struct lbx_state *lbx_open(void *f, const struct lbx_file_ops *fops,
                lbx->offsets[i] = unpack_32_le(buf);
        }
 
-       return lbx;
+       return &lbx->pub;
 err:
        free(dupname);
        free(lbx);
@@ -158,7 +159,7 @@ static char *last_component(const char *name)
        return c+1;
 }
 
-struct lbx_state *lbx_fopen(const char *file)
+struct lbx *lbx_fopen(const char *file)
 {
        const char *name = last_component(file);
        struct lbx_pipe_state *p;
@@ -181,17 +182,18 @@ struct lbx_state *lbx_fopen(const char *file)
        return lbx_open(p, &lbx_pipe_fops, pipe_close, name);
 }
 
-size_t lbx_numfiles(struct lbx_state *lbx)
+size_t lbx_numfiles(struct lbx *pub)
 {
-       return lbx->nfiles;
+       return pub->nfiles;
 }
 
 int
-lbx_file_stat(struct lbx_state *lbx, unsigned fileno, struct lbx_statbuf *buf)
+lbx_file_stat(struct lbx *pub, unsigned fileno, struct lbx_statbuf *buf)
 {
+       struct lbx_priv *lbx = (struct lbx_priv *)pub;
        static char str[256]; /* FIXME */
 
-       if (fileno >= lbx->nfiles) {
+       if (fileno >= lbx->pub.nfiles) {
                lbx_error_raise(LBX_ENOENT);
                buf->name = NULL;
                return -1;
@@ -203,8 +205,9 @@ lbx_file_stat(struct lbx_state *lbx, unsigned fileno, struct lbx_statbuf *buf)
        return 0;
 }
 
-int lbx_close(struct lbx_state *lbx)
+int lbx_close(struct lbx *pub)
 {
+       struct lbx_priv *lbx = (struct lbx_priv *)pub;
        int rc = 0;
 
        if (lbx && lbx->dtor)
@@ -215,11 +218,12 @@ int lbx_close(struct lbx_state *lbx)
        return rc;
 }
 
-struct lbx_file_state *lbx_file_open(struct lbx_state *lbx, unsigned fileno)
+struct lbx_file_state *lbx_file_open(struct lbx *pub, unsigned fileno)
 {
+       struct lbx_priv *lbx = (struct lbx_priv *)pub;
        struct lbx_file_state *state;
 
-       if (fileno >= lbx->nfiles) {
+       if (fileno >= lbx->pub.nfiles) {
                lbx_error_raise(LBX_ENOENT);
                return NULL;
        }
index 6b7ca02ba4a609f41a732819d1e9cb36fa3e2669..a7be05876b2d8b5c0ea62717f60f8e5ae9976d17 100644 (file)
--- a/src/lbx.h
+++ b/src/lbx.h
@@ -25,9 +25,13 @@ extern const struct lbx_file_ops lbx_pipe_fops;
 extern const struct lbx_file_ops lbx_arch_fops;
 
 /* Opaque */
-typedef struct lbx_state LBX;
+typedef struct lbx LBX;
 typedef struct lbx_file_state LBXfile;
 
+struct lbx {
+       unsigned nfiles;
+};
+
 struct lbx_statbuf {
        const char *name;
        size_t size;