]> git.draconx.ca Git - liblbx.git/blobdiff - src/lbx.c
liblbx: Use unconditional includes of <config.h>.
[liblbx.git] / src / lbx.c
index c2db14636d6a1e3fc93b05f97c087f42cb3f59a5..720cf077881dc6861473b7438de0659cfaf7b09c 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
  *  You should have received a copy of the GNU General Public License
  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
-#ifdef HAVE_CONFIG_H
-#      include "config.h"
-#endif
-
+#include <config.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 
 #include "pack.h"
 #include "misc.h"
+#include "error.h"
 #include "lbx.h"
 
 #define LBX_MAGIC    0x0000fead
 #define LBX_HDR_SIZE 8
 
-int lbx_errno = 0;
-
 struct lbx_state {
        char *name;
 
@@ -62,13 +58,13 @@ static struct lbx_state *lbx_init(unsigned char hdr[static LBX_HDR_SIZE])
        struct lbx_state *lbx;
 
        if (magic != LBX_MAGIC) {
-               lbx_errno = -LBX_EMAGIC;
+               lbx_error_raise(LBX_EMAGIC);
                return NULL;
        }
 
        lbx = malloc(sizeof *lbx + sizeof lbx->offsets[0] * (nfiles+1));
        if (!lbx) {
-               lbx_errno = -errno;
+               lbx_error_raise(LBX_ENOMEM);
                return NULL;
        }
 
@@ -98,12 +94,13 @@ struct lbx_state *lbx_open(void *f, const struct lbx_file_ops *fops,
 
        dupname = str_dup(name);
        if (!dupname) {
-               lbx_errno = -errno;
+               lbx_error_raise(LBX_ENOMEM);
                goto err;
        }
 
        if (fops->read(hdr_buf, sizeof hdr_buf, f) != sizeof hdr_buf) {
-               lbx_errno = -errno;
+               if (fops->eof(f))
+                       lbx_error_raise(LBX_EEOF);
                goto err;
        }
 
@@ -120,9 +117,8 @@ struct lbx_state *lbx_open(void *f, const struct lbx_file_ops *fops,
                unsigned char buf[4];
 
                if (fops->read(buf, sizeof buf, f) != sizeof buf) {
-                       lbx_errno = -errno;
                        if (fops->eof(f))
-                               lbx_errno = LBX_EEOF;
+                               lbx_error_raise(LBX_EEOF);
                        goto err;
                }
 
@@ -190,19 +186,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) {
+               lbx_error_raise(LBX_ENOENT);
                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;
 }
 
@@ -223,19 +220,18 @@ struct lbx_file_state *lbx_file_open(struct lbx_state *lbx, unsigned fileno)
        struct lbx_file_state *state;
 
        if (fileno >= lbx->nfiles) {
-               lbx_errno = LBX_ERANGE;
+               lbx_error_raise(LBX_ENOENT);
                return NULL;
        }
 
        lbx->last_file = NULL;
        if (lbx->fops->seek(lbx->f, lbx->offsets[fileno], SEEK_SET) != 0) {
-               lbx_errno = -errno;
                return NULL;
        }
 
        state = malloc(sizeof *state);
        if (!state) {
-               lbx_errno = -errno;
+               lbx_error_raise(LBX_ENOMEM);
                return NULL;
        }
 
@@ -317,19 +313,3 @@ void lbx_file_close(struct lbx_file_state *f)
                f->lbx->last_file = NULL;
        free(f);
 }
-
-const char *lbx_strerror(void)
-{
-       if (lbx_errno < 0)
-               return strerror(-lbx_errno);
-
-       switch (lbx_errno) {
-       case LBX_ESUCCESS: return "Success";
-       case LBX_EMAGIC:   return "Bad magic number";
-       case LBX_EEOF:     return "Unexpected end-of-file";
-       case LBX_ERANGE:   return "Index out of range";
-       case LBX_EFORMAT:  return "Invalid file format";
-       }
-
-       return "Unknown error";
-}