diff options
| author | Nicholas Rodrigues Lordello <[email protected]> | 2026-10-01 10:19:27 +0200 |
|---|---|---|
| committer | Nicholas Rodrigues Lordello <[email protected]> | 2026-10-01 10:19:27 +0200 |
| commit | 36213f7dfd59ab0f3e99b88e14b653723ca37d64 (patch) | |
| tree | 0200ff70f34b4183cb45351776f0dd53e0fb839b /src | |
| parent | 38417203681f89ad2d0aae20d8c3aa16c07e786a (diff) | |
Refactor Out Path Utilities
Move path utilities (basename, joining, etc.) to a separate module.
`-h` now prints the basename of arg0 instead of arg0. Avoid some
macros and replace with a small helper function.
Diffstat (limited to 'src')
| -rw-r--r-- | src/cart.c | 28 | ||||
| -rw-r--r-- | src/cart.h | 4 | ||||
| -rw-r--r-- | src/path.c | 110 | ||||
| -rw-r--r-- | src/path.h | 28 | ||||
| -rw-r--r-- | src/picow.c | 97 |
5 files changed, 204 insertions, 63 deletions
@@ -2,6 +2,7 @@ #include <SDL3/SDL_surface.h> #include "cart.h" +#include "path.h" #include "sysroot.h" #define STBI_ONLY_PNG @@ -26,34 +27,15 @@ static int stbi__mul2shorts_valid(int, int) __attribute__((unused)); #define PATH_SEP '/' #endif -static bool name_strip_extension(char name[PW_NAME_MAX], char const *src, - char const *ext) { - size_t slen = SDL_strlen(src); - size_t elen = SDL_strlen(ext); - - if (slen < elen) { - return false; - } - - size_t len = slen - elen; - if (len >= PW_NAME_MAX || SDL_strcmp(src + len, ext)) { - return false; - } - - SDL_strlcpy(name, src, len + 1); - return true; -} - static bool cart_file_path(struct pw_cart_file *file, char const *dir, char const *filename, char const *ext) { - size_t n = - SDL_snprintf(file->path, PW_PATH_MAX, "%s%c%s", dir, PATH_SEP, filename); - if (n >= PW_PATH_MAX) { - SDL_SetError("cart path exceeds maximum length"); + if (!pw_pathjoin(file->path, PW_PATH_MAX, dir, filename, NULL) || + !pw_stripext(file->name, PW_NAME_MAX, filename, ext)) { + SDL_SetError("invalid cart file name %s", filename); return false; } - return name_strip_extension(file->name, filename, ext); + return true; } static int cmp_cart_entry(void const *a, void const *b) { @@ -4,10 +4,10 @@ #include <SDL3/SDL_render.h> /** The maximum supported PICO-8 cart path size. */ -#define PW_PATH_MAX 992 +#define PW_PATH_MAX 960 /** The maximum supported PICO-8 cart name size. */ -#define PW_NAME_MAX 32 +#define PW_NAME_MAX 64 /** The size of a PICO-8 ROM. */ #define PW_ROM_SIZE 32768 diff --git a/src/path.c b/src/path.c new file mode 100644 index 0000000..2651b45 --- /dev/null +++ b/src/path.c @@ -0,0 +1,110 @@ +#include <SDL3/SDL_error.h> + +#include "path.h" + +#ifdef SDL_PLATFORM_WINDOWS +#define PATH_SEP '\\' +#else +#define PATH_SEP '/' +#endif + +char const *pw_basename(char const *path) { + if (!path) { + SDL_InvalidParamError("path"); + return NULL; + } + + char const *base = SDL_strrchr(path, PATH_SEP); + if (!base) { + base = path; + } else { + base++; + } + + return base; +} + +bool pw_stripext(char *name, size_t sz, char const *path, char const *ext) { + if (!name) { + SDL_InvalidParamError("name"); + return false; + } + if (!sz) { + SDL_InvalidParamError("sz"); + return false; + } + if (!path) { + SDL_InvalidParamError("path"); + return false; + } + if (!ext) { + SDL_InvalidParamError("ext"); + return false; + } + + size_t plen = SDL_strlen(path); + size_t elen = SDL_strlen(ext); + size_t nlen = plen - elen; + if (plen < elen || SDL_strcmp(path + nlen, ext)) { + SDL_SetError("path does not have extension"); + return false; + } + + if (nlen >= sz) { + SDL_SetError("buffer is too small for name"); + return false; + } + + SDL_strlcpy(name, path, nlen + 1); + return true; +} + +bool pw_pathjoin(char *path, size_t sz, ...) { + if (!path) { + SDL_InvalidParamError("path"); + return false; + } + if (!sz) { + SDL_InvalidParamError("sz"); + return false; + } + + va_list ap; + va_start(ap, sz); + char const *start = path; + for (char const *segment = va_arg(ap, char const *); segment; + segment = va_arg(ap, char const *)) { + if (!*segment) { + continue; + } + + if (path != start) { + if (!(--sz)) { + break; + } + *(path++) = PATH_SEP; + } + + path[0] = '\0'; + size_t n = SDL_strlcat(path, segment, sz); + if (n >= sz) { + sz = 0; + break; + } + + path += n; + sz -= n; + } + va_end(ap); + + if (!sz) { + SDL_SetError("buffer is too small for final path"); + return false; + } + if (path == start) { + SDL_SetError("empty path"); + return false; + } + + return true; +} diff --git a/src/path.h b/src/path.h new file mode 100644 index 0000000..096434b --- /dev/null +++ b/src/path.h @@ -0,0 +1,28 @@ +#ifndef PW_PATH_H +#define PW_PATH_H + +#include <SDL3/SDL_stdinc.h> + +/** + * Returns the basename of `path` or `NULL` if `path` is `NULL`. + */ +char const *pw_basename(char const *path); + +/** + * Strips the extension from `path` into `name`. Returns the `false` if `name` + * is not large enough. + * + * `name` must not overlap with `path`. + */ +bool pw_stripext(char *name, size_t sz, char const *path, char const *ext); + +/** + * Joins multiple components into `path`. returns `false` if `path` is not + * large enough to fit all of the components. + * + * `path` must not overap with any of the segments, a final `NULL` segment must + * be passed to signal end of arguments. + */ +bool pw_pathjoin(char *path, size_t sz, ...); + +#endif diff --git a/src/picow.c b/src/picow.c index 0e9eb97..9faa524 100644 --- a/src/picow.c +++ b/src/picow.c @@ -7,14 +7,39 @@ #include <wamr/wamr.h> #include "cart.h" +#include "path.h" #include "sysroot.h" static void usage(char const *name) { - // clang-format off - printf("Usage: %s [-c <carts>] [-v[v[v]]]\n", name); - printf(" -c <carts> Carts directory. Defaults to 'carts'.\n"); - printf(" -v[v[v]] Increasingly verbose logging.\n"); - // clang-format on + printf("Usage: %s [-c <carts>] [-v[v[v]]]\n" + " -c <carts> Carts directory. Defaults to 'carts'.\n" + " -v[v[v]] Increasingly verbose logging.\n", + pw_basename(name)); +} + +static SDL_AppResult failf(char const *fmt, ...) { + va_list ap; + char *msg; + va_start(ap, fmt); + int n = SDL_vasprintf(&msg, fmt, ap); + va_end(ap); + + if (n <= 0) { + msg = "unspecified failure"; + } + + char const *err = SDL_GetError(); + if (err) { + SDL_LogCritical(0, "%s: %s", msg, err); + } else { + SDL_LogCritical(0, "%s", msg); + } + + if (n > 0) { + SDL_free(msg); + } + + return SDL_APP_FAILURE; } enum pw_button { @@ -69,10 +94,6 @@ struct pw_appstate { enum pw_scene scene2; }; -#define FAIL(msg, ...) \ - (SDL_LogCritical(0, msg ". %s", ##__VA_ARGS__, SDL_GetError()), \ - SDL_APP_FAILURE) - static SDL_Texture *init_pxtexture(SDL_Renderer *renderer, int w, int h) { SDL_Texture *texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA32, SDL_TEXTUREACCESS_STREAMING, w, h); @@ -169,7 +190,7 @@ static SDL_AppResult update_target_rect(struct pw_appstate *state, int w, } if (!update_pxtexture_target(texture, &state->target, w, h)) { - return FAIL("failed to update texture target"); + return failf("failed to update texture target"); } return SDL_APP_CONTINUE; @@ -192,7 +213,7 @@ static SDL_AppResult add_gamepad(struct pw_appstate *state, SDL_Gamepad *gamepad = SDL_OpenGamepad(event->which); if (gamepad == NULL) { - return FAIL("failed to open gamepad"); + return failf("failed to open gamepad"); } SDL_LogDebug(0, "connected gamepad for player %zu", i + 1); @@ -383,7 +404,7 @@ static SDL_AppResult scene_cart_selection(struct pw_appstate *state) { if (state->cartidx != state->cartidx2) { state->cartidx = state->cartidx2; if (!pw_cart_load(&state->carts->entries[state->cartidx], state->cart)) { - return FAIL("failed to load cart png"); + return failf("failed to load cart png"); } } @@ -401,7 +422,7 @@ static SDL_AppResult scene_game_init(struct pw_appstate *state) { wasm_runtime_instantiate(state->module, 1024, 0, err, sizeof err); if (!state->instance) { SDL_SetError("%s", err); - return FAIL("failed to instantiate PICO-8 WebAssembly module"); + return failf("failed to instantiate PICO-8 WebAssembly module"); } wasm_function_inst_t ctor = wasm_runtime_lookup_function( @@ -415,17 +436,17 @@ static SDL_AppResult scene_game_init(struct pw_appstate *state) { wasm_function_inst_t mix = wasm_runtime_lookup_function(state->instance, "_mix_0"); if (!ctor || !start || !stackalloc || !dyncall || !mix) { - return FAIL("could not required exports"); + return failf("could not required exports"); } wasm_exec_env_t exec = wasm_runtime_create_exec_env(state->instance, 1024); if (exec == NULL) { - return FAIL("failed to create execution environment"); + return failf("failed to create execution environment"); } wasm_runtime_set_user_data(exec, state); if (!wasm_runtime_call_wasm(exec, ctor, 0, NULL)) { - return FAIL("error calling main"); + return failf("error calling main"); } uint32_t arg0; @@ -433,13 +454,13 @@ static SDL_AppResult scene_game_init(struct pw_appstate *state) { char const value[] = "./pico8"; uint32_t args[] = {(uint32_t)sizeof(value)}; if (!wasm_runtime_call_wasm(exec, stackalloc, 1, args)) { - return FAIL("error allocating argument 0"); + return failf("error allocating argument 0"); } arg0 = args[0]; char *target = wasm_runtime_addr_app_to_native(state->instance, arg0); if (!target) { - return FAIL("invalid argument 0 address"); + return failf("invalid argument 0 address"); } SDL_memcpy(target, value, sizeof(value)); @@ -450,13 +471,13 @@ static SDL_AppResult scene_game_init(struct pw_appstate *state) { uint32_t const value[] = {arg0, 0}; uint32_t args[] = {(uint32_t)sizeof(value)}; if (!wasm_runtime_call_wasm(exec, stackalloc, 1, args)) { - return FAIL("error allocating arguments array"); + return failf("error allocating arguments array"); } argv = args[0]; char *target = wasm_runtime_addr_app_to_native(state->instance, argv); if (!target) { - return FAIL("invalid arguments array address"); + return failf("invalid arguments array address"); } SDL_memcpy(target, value, sizeof(value)); @@ -466,14 +487,14 @@ static SDL_AppResult scene_game_init(struct pw_appstate *state) { { uint32_t args[] = {1, argv}; if (!wasm_runtime_call_wasm(exec, start, 2, args)) { - return FAIL("error calling main"); + return failf("error calling main"); } if (args[0]) { - return FAIL("main returned exit code"); + return failf("main returned exit code"); } } if (!state->mainfn) { - return FAIL("main loop function not set"); + return failf("main loop function not set"); } state->exec = exec; @@ -486,23 +507,23 @@ static SDL_AppResult scene_game(struct pw_appstate *state) { { uint32_t args[] = {state->mainfn}; if (!wasm_runtime_call_wasm(state->exec, state->dyncall, 1, args)) { - return FAIL("error calling main loop function"); + return failf("error calling main loop function"); } } { uint32_t args[] = {42, 1337}; if (!wasm_runtime_call_wasm(state->exec, state->mix, 2, args)) { - return FAIL("error calling audio mixer"); + return failf("error calling audio mixer"); } if (!args[0]) { - return FAIL("audio mixing failed %d %d", args[0], args[1]); + return failf("audio mixing failed %d %d", args[0], args[1]); } } void *fb; int pitch; if (!SDL_LockTexture(state->framebuffer, NULL, &fb, &pitch)) { - return FAIL("failed to lock framebuffer"); + return failf("failed to lock framebuffer"); } blit_framebuffer(state, (uint32_t *)fb); SDL_UnlockTexture(state->framebuffer); @@ -534,7 +555,7 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char **argv) { *appstate = state; if (!SDL_InitSubSystem(SDL_INIT_GAMEPAD)) { - return FAIL("failed to initialize SDL"); + return failf("failed to initialize SDL"); } char *carts = "carts"; @@ -566,30 +587,30 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char **argv) { SDL_WINDOW_HIGH_PIXEL_DENSITY | SDL_WINDOW_RESIZABLE, &state->window, &state->renderer)) { - return FAIL("failed to create window"); + return failf("failed to create window"); } if (!SDL_SetWindowMinimumSize(state->window, 160, 205)) { - return FAIL("failed to configure minimum window size"); + return failf("failed to configure minimum window size"); } state->framebuffer = init_pxtexture(state->renderer, 128, 128); if (!state->framebuffer) { - return FAIL("failed to create framebuffer"); + return failf("failed to create framebuffer"); } state->cart = pw_cart_init(state->renderer); if (!state->cart) { - return FAIL("failed to initialize cart"); + return failf("failed to initialize cart"); } state->carts = pw_carts_list(carts); if (!state->carts) { - return FAIL("failed to read carts directory"); + return failf("failed to read carts directory"); } else if (!state->carts->count) { SDL_LogWarn(0, "empty or missing carts directory"); } else if (!pw_cart_load(&state->carts->entries[0], state->cart)) { - return FAIL("failed to load startup cart"); + return failf("failed to load startup cart"); } struct RuntimeInitArgs init = { @@ -600,7 +621,7 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char **argv) { }; if (!wasm_runtime_full_init(&init)) { SDL_SetError("unspecified error"); - return FAIL("failed to initialize the WebAssembly runtime"); + return failf("failed to initialize the WebAssembly runtime"); } struct NativeSymbol env[] = { @@ -608,7 +629,7 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char **argv) { }; if (!wasm_runtime_register_natives_raw("env", env, SDL_arraysize(env))) { SDL_SetError("unspecified error"); - return FAIL("failed to register host functions"); + return failf("failed to register host functions"); } char err[1024]; @@ -616,7 +637,7 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char **argv) { err, sizeof err); if (state->module == NULL) { SDL_SetError("%s", err); - return FAIL("failed to load the PICO-8 WebAssembly module"); + return failf("failed to load the PICO-8 WebAssembly module"); } struct NativeGlobal genv[] = {}; @@ -625,7 +646,7 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char **argv) { !wasm_runtime_link_globals(state->module, "global", genv, SDL_arraysize(genv))) { SDL_SetError("unspecified error"); - return FAIL("failed to register host globals"); + return failf("failed to register host globals"); } state->scene = PW_SCENE_CART_SELECTION; |
