diff --git a/linux/src/open_file_dialog.cc b/linux/src/open_file_dialog.cc index 45513ce..368112b 100644 --- a/linux/src/open_file_dialog.cc +++ b/linux/src/open_file_dialog.cc @@ -1,4 +1,5 @@ #include "open_file_dialog.h" +#include "compat.h" #include #include #include @@ -68,11 +69,11 @@ std::vector open_file_dialog(const std::string &title, const std::s return result; } -int open_file_dialog(char *rom_name) { +int open_file_dialog(char *rom_filepath) { std::vector fileTypes = {"ch8", "CH8", "chip-8", "CHIP-8", "Chip-8"}; const char* defaultDir = ""; // unify behavior: let OS choose last-used/home std::vector files = open_file_dialog("Chip8", defaultDir, fileTypes); if (files.empty()) return 1; - snprintf(rom_name, 256, "%s", files[0].c_str()); + snprintf(rom_filepath, PATH_MAX, "%s", files[0].c_str()); return 0; } diff --git a/macos/src/open_file_dialog.mm b/macos/src/open_file_dialog.mm index 2e5e422..adf60c8 100644 --- a/macos/src/open_file_dialog.mm +++ b/macos/src/open_file_dialog.mm @@ -1,4 +1,5 @@ #import "open_file_dialog.h" +#import "compat.h" #import #import #include @@ -43,11 +44,11 @@ return fileList; } -int open_file_dialog(char *rom_name) { +int open_file_dialog(char *rom_filepath) { std::vector fileTypes = {"ch8", "CH8", "chip-8", "CHIP-8", "Chip-8"}; const char* defaultDir = ""; // unify behavior: let OS choose last-used/home std::vector files = open_file_dialog("Chip8", defaultDir, fileTypes); if (files.empty()) return 1; - snprintf(rom_name, 256, "%s", files[0].c_str()); + snprintf(rom_filepath, PATH_MAX, "%s", files[0].c_str()); return 0; } diff --git a/shared/chip8.c b/shared/chip8.c index d472b34..08a7199 100644 --- a/shared/chip8.c +++ b/shared/chip8.c @@ -7,6 +7,7 @@ #include "profiles.h" #include "sha256.h" #include "toast.h" +#include "compat.h" #include "open_file_dialog.h" #include #include @@ -193,10 +194,10 @@ int chip8_load_rom(const char *rom_filepath) { } else { /* load ROM from GUI */ - char new_rom_name[PATH_MAX]; - open_file_dialog(new_rom_name) ? + char new_rom_filepath[PATH_MAX]; + open_file_dialog(new_rom_filepath) ? printf("User aborted the open file dialog.\n") : - chip8_load_rom(new_rom_name); + chip8_load_rom(new_rom_filepath); /* flip GUI toggle */ gui.load_rom_flag = 0; diff --git a/shared/chip8.h b/shared/chip8.h index be65d98..2ef4f5e 100644 --- a/shared/chip8.h +++ b/shared/chip8.h @@ -5,6 +5,7 @@ extern "C" { #endif +#include "compat.h" #include "bootrom.h" // Generated at build time from roms/Kiwi8_logo_2.ch8 #include "quirks.h" #include @@ -62,7 +63,7 @@ struct chip8 { unsigned int rom_size; /* rom profile tracking */ - char rom_filename[256]; /* basename of currently loaded ROM */ + char rom_filename[FILENAME_MAX]; /* basename of currently loaded ROM */ int rom_loaded; /* 1 if user ROM loaded, 0 if bootrom */ /* registers */ diff --git a/shared/compat.h b/shared/compat.h new file mode 100644 index 0000000..9feeb8c --- /dev/null +++ b/shared/compat.h @@ -0,0 +1,28 @@ +#ifndef COMPAT_H +#define COMPAT_H + +#include + +// Handle missing PATH_MAX on some systems +#if defined(__APPLE__) + #include +#elif defined(__linux__) + #include +#elif defined(_WIN32) + #include + #ifndef PATH_MAX + #define PATH_MAX MAX_PATH + #endif +#endif + +#ifndef PATH_MAX + #define PATH_MAX 4096 +#endif +#ifndef FILENAME_MAX + #define FILENAME_MAX 256 +#endif +#ifndef LINE_MAX + #define LINE_MAX 2048 +#endif + +#endif // COMPAT_H diff --git a/shared/open_file_dialog.h b/shared/open_file_dialog.h index a0837b3..bcfabcb 100644 --- a/shared/open_file_dialog.h +++ b/shared/open_file_dialog.h @@ -5,23 +5,10 @@ extern "C" { #endif -#ifdef __APPLE__ -#include /* PATH_MAX */ -#endif - -#ifdef _WIN32 -#include /* MAX_PATH */ -#define PATH_MAX MAX_PATH -#endif - -#ifdef __linux__ -#include /* PATH_MAX */ -#endif - -int open_file_dialog(char *rom_name); +int open_file_dialog(char *rom_filepath); #ifdef __cplusplus } #endif -#endif +#endif // OPEN_FILE_DIALOG_H diff --git a/shared/profiles.c b/shared/profiles.c index 2fd080f..b00d1f7 100644 --- a/shared/profiles.c +++ b/shared/profiles.c @@ -1,4 +1,5 @@ #include "profiles.h" +#include "compat.h" #define STB_DS_IMPLEMENTATION #include "stb_ds.h" #include "sha256.h" @@ -13,7 +14,7 @@ static struct { sha256_hash_t key; struct profile value; } *profile_map = NULL; /* Track which path we loaded profiles.ini from */ -static char loaded_profiles_path[512] = ""; +static char loaded_profiles_path[FILENAME_MAX] = ""; /* Quirk field descriptor table — generated from QUIRK_FIELDS X-macro in quirks.h */ static const struct { @@ -58,7 +59,7 @@ static void parse_profiles_ini(void) { FILE *file = fopen(loaded_profiles_path, "r"); if (!file) return; - char line[512]; + char line[LINE_MAX]; sha256_hash_t current_sha256 = {0}; int has_current = 0; struct profile current_profile; @@ -164,7 +165,7 @@ static int resolve_profiles_path(void) { } /* Build search paths relative to executable */ - char search_paths[2][512]; + char search_paths[2][LINE_MAX]; char *base_path = SDL_GetBasePath(); if (!base_path) { printf("Warning: Could not determine executable path. Trying current directory.\n"); diff --git a/shared/profiles.h b/shared/profiles.h index 59d6602..687d5e6 100644 --- a/shared/profiles.h +++ b/shared/profiles.h @@ -8,12 +8,13 @@ extern "C" { #include #include "quirks.h" #include "sha256.h" +#include "compat.h" typedef struct { uint8_t bytes[32]; } sha256_hash_t; struct profile { sha256_hash_t sha256; - char rom_name[256]; + char rom_name[FILENAME_MAX]; struct quirks quirks; }; diff --git a/shared/usage.h b/shared/usage.h index 17d98e7..db40a55 100644 --- a/shared/usage.h +++ b/shared/usage.h @@ -1,6 +1,10 @@ #ifndef USAGE_H #define USAGE_H +#ifdef __cplusplus +extern "C" { +#endif + /* Centralized usage/help text for CLI and GUI */ static const char *USAGE_TEXT = "Usage: Kiwi8 [options] [rom_file]\n" @@ -12,4 +16,8 @@ static const char *USAGE_TEXT = "\n" "Note: Quirks are configured per-ROM via profiles.ini or GUI.\n"; +#ifdef __cplusplus +} +#endif + #endif /* USAGE_H */ diff --git a/windows/src/open_file_dialog.cc b/windows/src/open_file_dialog.cc index b85e087..459715f 100644 --- a/windows/src/open_file_dialog.cc +++ b/windows/src/open_file_dialog.cc @@ -1,17 +1,18 @@ #include "open_file_dialog.h" +#include "compat.h" #include #include #include #include -int open_file_dialog(char *rom_name, char *filters) { +int open_file_dialog(char *rom_filepath, char *filters) { /* open file dialogue */ - char cwd[MAX_PATH]; - GetCurrentDirectory(MAX_PATH, cwd); + char cwd[PATH_MAX]; + GetCurrentDirectory(PATH_MAX, cwd); OPENFILENAME ofn; - char szFile[MAX_PATH]; + char szFile[PATH_MAX]; /* open a file name */ ZeroMemory( &ofn , sizeof( ofn)); @@ -35,10 +36,10 @@ int open_file_dialog(char *rom_name, char *filters) { return 1; } - strcpy(rom_name, szFile); + strcpy(rom_filepath, szFile); return 0; } -int open_file_dialog(char *rom_name) { - return open_file_dialog(rom_name, "Chip8\0*.ch8\0All\0*.*\0"); +int open_file_dialog(char *rom_filepath) { + return open_file_dialog(rom_filepath, "Chip8\0*.ch8\0All\0*.*\0"); }