X-Git-Url: http://git.draconx.ca/gitweb/liblbx.git/blobdiff_plain/9d7bdff315a9fbe3ed27266df09fe9bc8de2b605..f8295b32a98df79caee16a25a1dcf3a7a678e409:/src/lbx.c diff --git a/src/lbx.c b/src/lbx.c index 05da0b4..5dc14fd 100644 --- 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 @@ -136,9 +136,53 @@ err: return NULL; } -struct lbx_state *lbx_fopen(FILE *f, const char *name) +static int file_close(void *f) { - return lbx_open(f, &lbx_default_fops, NULL, name); + 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(const char *file) +{ + 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) @@ -249,6 +293,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;