diff options
author | Jari Vetoniemi <jari.vetoniemi@indooratlas.com> | 2022-02-17 04:34:18 +0900 |
---|---|---|
committer | Jari Vetoniemi <jari.vetoniemi@indooratlas.com> | 2022-02-17 04:34:18 +0900 |
commit | f19c280566277f341901d86fe3f383268c5d1706 (patch) | |
tree | bf07c4cc2752cc1348015ad8131006c612b024ae | |
parent | 9bd3c08e9c2826e48167b4a1ce7280873cf83bbd (diff) |
physfs-serve: simplify extension replacement
-rw-r--r-- | jni/physfs-serve-sdl-wrap.c | 40 |
1 files changed, 20 insertions, 20 deletions
diff --git a/jni/physfs-serve-sdl-wrap.c b/jni/physfs-serve-sdl-wrap.c index c0de9df..92b2290 100644 --- a/jni/physfs-serve-sdl-wrap.c +++ b/jni/physfs-serve-sdl-wrap.c @@ -4,6 +4,7 @@ #include <unistd.h> #include <string.h> #include <stdio.h> +#include <assert.h> static void ignore_ret(bool b, ...) {}; #define ignore_ret(x) ignore_ret(true, x); @@ -117,28 +118,27 @@ content_type(struct physfs_serve *serve, const char *path) { } static bool +replace_ext(char buf[], const size_t bufsz, const char *old, const char *new) { + if (!physfs_ends_with(buf, old)) return false; + size_t len = strlen(buf); + assert(len >= strlen(old)); + len -= strlen(old); + assert(bufsz >= len); + snprintf(buf + len, bufsz - len, "%s", new); + return true; +} + +static bool path_rewrite(struct physfs_serve *serve, char buf[], const size_t bufsz, struct PHYSFS_Stat *st) { if (physfs_default_path_rewrite(serve, buf, bufsz, st)) return true; - if (physfs_ends_with(buf, ".rpgmvm")) { - buf[strlen(buf) - 1] = 'o'; - if (st->filetype == -1 && PHYSFS_stat(buf, st)) return false; - } else if (physfs_ends_with(buf, ".rpgmvo")) { - buf[strlen(buf) - 1] = 'm'; - if (st->filetype == -1 && PHYSFS_stat(buf, st)) return false; - } else if (physfs_ends_with(buf, ".webm")) { - buf[strlen(buf) - 1] = 0; - buf[strlen(buf) - 2] = '4'; - buf[strlen(buf) - 3] = 'p'; - buf[strlen(buf) - 4] = 'm'; - if (st->filetype == -1 && PHYSFS_stat(buf, st)) return false; - } else if (physfs_ends_with(buf, ".mp4")) { - buf[strlen(buf) + 2] = 0; - buf[strlen(buf) + 1] = 'm'; - buf[strlen(buf)] = 'b'; - buf[strlen(buf) - 1] = 'e'; - buf[strlen(buf) - 2] = 'w'; - if (st->filetype == -1 && PHYSFS_stat(buf, st)) return false; - } + if (st->filetype != -1) return false; + if (replace_ext(buf, bufsz, ".rpgmvm", ".rpgmvo")) return PHYSFS_stat(buf, st); + else if (replace_ext(buf, bufsz, ".rpgmvo", ".rpgmvm")) return PHYSFS_stat(buf, st); + else if (replace_ext(buf, bufsz, ".webm", ".mp4")) return PHYSFS_stat(buf, st); + else if (replace_ext(buf, bufsz, ".mp4", ".webm")) return PHYSFS_stat(buf, st); + else if (replace_ext(buf, bufsz, ".png", ".rpgmvp")) return PHYSFS_stat(buf, st); + else if (replace_ext(buf, bufsz, ".ogg", ".rpgmvo")) return PHYSFS_stat(buf, st); + else if (replace_ext(buf, bufsz, ".mp4", ".rpgmvm")) return PHYSFS_stat(buf, st); return false; } |