4 #include "assman_impl.h"
6 static int add_fop(const char *prefix, int type, struct ass_fileops *fop);
7 static const char *match_prefix(const char *str, const char *prefix);
8 static int add_file(ass_file *file);
9 static int remove_file(ass_file *file);
11 #define DEF_FLAGS (1 << ASS_OPEN_FALLTHROUGH)
13 static unsigned int assflags = DEF_FLAGS;
14 static struct mount *mlist;
16 void ass_set_option(int opt, int val)
21 assflags &= ~(1 << opt);
25 int ass_get_option(int opt)
27 return assflags & (1 << opt);
30 int ass_add_path(const char *prefix, const char *path)
32 return add_fop(prefix, MOD_PATH, ass_alloc_path(path));
35 int ass_add_archive(const char *prefix, const char *arfile)
37 return add_fop(prefix, MOD_ARCHIVE, ass_alloc_archive(arfile));
40 int ass_add_url(const char *prefix, const char *url)
42 return add_fop(prefix, MOD_URL, ass_alloc_url(url));
45 int ass_add_user(const char *prefix, struct ass_fileops *fop)
47 return add_fop(prefix, MOD_USER, fop);
50 static int add_fop(const char *prefix, int type, struct ass_fileops *fop)
55 fprintf(stderr, "assman: failed to allocate asset source\n");
59 if(!(m = malloc(sizeof *m))) {
60 perror("assman: failed to allocate mount node");
64 if(!(m->prefix = malloc(strlen(prefix) + 1))) {
68 strcpy(m->prefix, prefix);
83 struct mount *m = mlist;
88 ass_free_path(m->fop);
91 ass_free_archive(m->fop);
105 ass_file *ass_fopen(const char *fname, const char *mode)
114 if(match_prefix(fname, m->prefix)) {
115 if((mfile = m->fop->open(fname, m->fop->udata))) {
116 if(!(file = malloc(sizeof *file))) {
117 perror("assman: ass_fopen failed to allocate file structure");
118 m->fop->close(mfile, m->fop->udata);
124 if(add_file(file) == -1) {
125 m->fop->close(mfile, m->fop->udata);
131 if(!(assflags & (1 << ASS_OPEN_FALLTHROUGH))) {
139 /* nothing matched, or failed to open, try the filesystem */
140 if((fp = fopen(fname, mode))) {
141 if(!(file = malloc(sizeof *file))) {
142 perror("assman: ass_fopen failed to allocate file structure");
149 if(add_file(file) == -1) {
159 static const char *match_prefix(const char *str, const char *prefix)
161 if(!prefix || !*prefix) return str; /* match on null or empty prefix */
164 if(*prefix++ != *str++) {
171 void ass_fclose(ass_file *fp)
174 fp->fop->close(fp->file, fp->fop->udata);
182 long ass_fseek(ass_file *fp, long offs, int whence)
185 return fp->fop->seek(fp->file, offs, whence, fp->fop->udata);
188 if(fseek(fp->file, offs, whence) == -1) {
191 return ftell(fp->file);
194 long ass_ftell(ass_file *fp)
196 return ass_fseek(fp, 0, SEEK_CUR);
199 size_t ass_fread(void *buf, size_t size, size_t count, ass_file *fp)
202 long res = fp->fop->read(fp->file, buf, size * count, fp->fop->udata);
203 if(res <= 0) return 0;
207 return fread(buf, size, count, fp->file);
211 /* --- convenience functions --- */
213 int ass_fgetc(ass_file *fp)
217 if(ass_fread(&c, 1, 1, fp) < 1) {
223 char *ass_fgets(char *s, int size, ass_file *fp)
230 for(i=0; i<size - 1; i++) {
231 if((c = ass_fgetc(fp)) == -1) {
239 return ptr == s ? 0 : s;
243 static int add_file(ass_file *file)
245 return -1; /* TODO */
248 static int remove_file(ass_file *file)
250 return -1; /* TODO */