asscat example
authorJohn Tsiombikas <nuclear@member.fsf.org>
Sun, 23 Sep 2018 05:49:24 +0000 (08:49 +0300)
committerJohn Tsiombikas <nuclear@member.fsf.org>
Sun, 23 Sep 2018 05:49:24 +0000 (08:49 +0300)
examples/asscat/Makefile [new file with mode: 0644]
examples/asscat/asscat.c [new file with mode: 0644]
src/assman.c
src/mod_path.c
src/mod_url.c

diff --git a/examples/asscat/Makefile b/examples/asscat/Makefile
new file mode 100644 (file)
index 0000000..6a41b75
--- /dev/null
@@ -0,0 +1,13 @@
+obj = asscat.o
+bin = asscat
+root = ../..
+
+CFLAGS = -pedantic -Wall -g -I$(root)/src
+LDFLAGS = -L$(root) -Wl,-rpath=$(root) -lassman
+
+$(bin): $(obj)
+       $(CC) -o $@ $(obj) $(LDFLAGS)
+
+.PHONY: clean
+clean:
+       rm -f $(obj) $(bin)
diff --git a/examples/asscat/asscat.c b/examples/asscat/asscat.c
new file mode 100644 (file)
index 0000000..799b6b0
--- /dev/null
@@ -0,0 +1,62 @@
+#include <stdio.h>
+#include <string.h>
+#include "assman.h"
+
+void print_usage(const char *argv0);
+
+int main(int argc, char **argv)
+{
+       int i, rdsz;
+       const char *prefix = 0;
+       ass_file *fp;
+       static char buf[1024];
+
+       for(i=1; i<argc; i++) {
+               if(argv[i][0] == '-') {
+                       if(strcmp(argv[i], "-prefix") == 0) {
+                               prefix = argv[++i];
+
+                       } else if(strcmp(argv[i], "-path") == 0) {
+                               ass_add_path(prefix, argv[++i]);
+
+                       } else if(strcmp(argv[i], "-archive") == 0) {
+                               ass_add_archive(prefix, argv[++i]);
+
+                       } else if(strcmp(argv[i], "-url") == 0) {
+                               ass_add_url(prefix, argv[++i]);
+
+                       } else if(strcmp(argv[i], "-help") == 0 || strcmp(argv[i], "-h") == 0) {
+                               print_usage(argv[0]);
+                               return 0;
+
+                       } else {
+                               fprintf(stderr, "invalid option: %s\n", argv[i]);
+                               return 1;
+                       }
+
+               } else {
+                       if(!(fp = ass_fopen(argv[i], "rb"))) {
+                               fprintf(stderr, "failed to open asset: %s: %s\n", argv[i], strerror(ass_errno));
+                               return 1;
+                       }
+                       while((rdsz = ass_fread(buf, 1, sizeof buf, fp)) > 0) {
+                               fwrite(buf, 1, rdsz, stdout);
+                       }
+                       fflush(stdout);
+                       ass_fclose(fp);
+               }
+       }
+
+       return 0;
+}
+
+void print_usage(const char *argv0)
+{
+       printf("Usage: %s [options] <file 1> <file 2> ... <file n>\n");
+       printf("Options:\n");
+       printf(" -prefix <prefix>   sets the path prefix to match for subsequent asset sources\n");
+       printf(" -path <path>       filesystem asset source\n");
+       printf(" -archive <archive> archive asset source\n");
+       printf(" -url <url>         url asset source\n");
+       printf(" -h,-help           print usage and exit\n");
+}
index 2e263e6..735cc16 100644 (file)
@@ -1,6 +1,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <errno.h>
 #include "assman_impl.h"
 
 static int add_fop(const char *prefix, int type, struct ass_fileops *fop);
@@ -108,11 +109,12 @@ ass_file *ass_fopen(const char *fname, const char *mode)
        void *mfile;
        ass_file *file;
        FILE *fp;
+       const char *after_prefix;
 
        m = mlist;
        while(m) {
-               if(match_prefix(fname, m->prefix)) {
-                       if((mfile = m->fop->open(fname, m->fop->udata))) {
+               if((after_prefix = match_prefix(fname, m->prefix))) {
+                       if((mfile = m->fop->open(after_prefix, m->fop->udata))) {
                                if(!(file = malloc(sizeof *file))) {
                                        perror("assman: ass_fopen failed to allocate file structure");
                                        m->fop->close(mfile, m->fop->udata);
@@ -139,6 +141,7 @@ ass_file *ass_fopen(const char *fname, const char *mode)
        /* nothing matched, or failed to open, try the filesystem */
        if((fp = fopen(fname, mode))) {
                if(!(file = malloc(sizeof *file))) {
+                       ass_errno = errno;
                        perror("assman: ass_fopen failed to allocate file structure");
                        fclose(fp);
                        return 0;
@@ -153,6 +156,7 @@ ass_file *ass_fopen(const char *fname, const char *mode)
                }
                return file;
        }
+       ass_errno = errno;
        return 0;
 }
 
index 1a680e6..fa01776 100644 (file)
@@ -53,12 +53,12 @@ void ass_free_path(struct ass_fileops *fop)
 
 static void *fop_open(const char *fname, void *udata)
 {
-       const char *prefix = (char*)udata;
+       const char *asspath = (char*)udata;
        char *path;
        FILE *fp;
 
-       path = alloca(strlen(prefix) + strlen(fname) + 2);
-       sprintf(path, "%s/%s", path, fname);
+       path = alloca(strlen(asspath) + strlen(fname) + 2);
+       sprintf(path, "%s/%s", asspath, fname);
 
        if(!(fp = fopen(path, "rb"))) {
                ass_errno = errno;
index f254b68..c8c62cb 100644 (file)
@@ -66,5 +66,13 @@ static long fop_read(void *fp, void *buf, long size, void *udata)
 }
 
 #else
-int ass_mod_url_disabled;
+struct ass_fileops *ass_alloc_url(const char *url)
+{
+       fprintf(stderr, "assman: compiled without URL asset source support\n");
+       return 0;
+}
+
+void ass_free_url(struct ass_fileops *fop)
+{
+}
 #endif