2 assfile - library for accessing assets with an fopen/fread-like interface
3 Copyright (C) 2018 John Tsiombikas <nuclear@member.fsf.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>.
30 #include "assfile_impl.h"
33 static void *fop_open(const char *fname, void *udata);
34 static void fop_close(void *fp, void *udata);
35 static long fop_seek(void *fp, long offs, int whence, void *udata);
36 static long fop_read(void *fp, void *buf, long size, void *udata);
39 struct ass_fileops *ass_alloc_path(const char *path)
42 struct ass_fileops *fop;
44 if(!(fop = malloc(sizeof *fop))) {
47 if(!(p = malloc(strlen(path) + 1))) {
56 while(p > (char*)fop->udata && (p[-1] == '/' || isspace(p[-1]))) p--;
60 fop->close = fop_close;
66 void ass_free_path(struct ass_fileops *fop)
71 static void *fop_open(const char *fname, void *udata)
73 const char *asspath = (char*)udata;
77 path = alloca(strlen(asspath) + strlen(fname) + 2);
78 sprintf(path, "%s/%s", asspath, fname);
80 if(!(fp = fopen(path, "rb"))) {
87 static void fop_close(void *fp, void *udata)
92 static long fop_seek(void *fp, long offs, int whence, void *udata)
94 fseek(fp, offs, whence);
98 static long fop_read(void *fp, void *buf, long size, void *udata)
100 return fread(buf, 1, size, fp);