From f19c280566277f341901d86fe3f383268c5d1706 Mon Sep 17 00:00:00 2001 From: Jari Vetoniemi Date: Thu, 17 Feb 2022 04:34:18 +0900 Subject: physfs-serve: simplify extension replacement --- jni/physfs-serve-sdl-wrap.c | 40 ++++++++++++++++++++-------------------- 1 file 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 #include #include +#include static void ignore_ret(bool b, ...) {}; #define ignore_ret(x) ignore_ret(true, x); @@ -116,29 +117,28 @@ content_type(struct physfs_serve *serve, const char *path) { return physfs_default_content_type(serve, 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; } -- cgit v1.2.3-70-g09d2