diff --git a/data/dynos_mgr_pack.cpp b/data/dynos_mgr_pack.cpp index 9bd54ad92..e60ccf410 100644 --- a/data/dynos_mgr_pack.cpp +++ b/data/dynos_mgr_pack.cpp @@ -151,7 +151,7 @@ PackData* DynOS_Pack_Add(const SysPath& aPath) { const char* displayName = aPath.c_str(); const char* ctoken = displayName; while (*ctoken != '\0') { - if (*ctoken == '/' || *ctoken == '\\') { + if (*ctoken == *PATH_SEPARATOR || *ctoken == *PATH_SEPARATOR_ALT) { if (*(ctoken + 1) != '\0') { displayName = (ctoken + 1); } diff --git a/src/pc/lua/smlua.c b/src/pc/lua/smlua.c index cd2502050..d8f2619c7 100644 --- a/src/pc/lua/smlua.c +++ b/src/pc/lua/smlua.c @@ -359,7 +359,7 @@ void smlua_init(void) { } // skip loading scripts in subdirectories - if (strchr(file->relativePath, '/') != NULL || strchr(file->relativePath, '\\') != NULL) { + if (strchr(file->relativePath, *PATH_SEPARATOR) != NULL || strchr(file->relativePath, *PATH_SEPARATOR_ALT) != NULL) { continue; } diff --git a/src/pc/lua/smlua_require.c b/src/pc/lua/smlua_require.c index c331a0570..c99db7513 100644 --- a/src/pc/lua/smlua_require.c +++ b/src/pc/lua/smlua_require.c @@ -121,7 +121,7 @@ static int smlua_custom_require(lua_State* L) { return 0; } - if (path_ends_with(moduleName, "/") || path_ends_with(moduleName, "\\")) { + if (path_ends_with(moduleName, PATH_SEPARATOR) || path_ends_with(moduleName, PATH_SEPARATOR_ALT)) { LOG_LUA_LINE("cannot require a directory"); return 0; } diff --git a/src/pc/lua/smlua_utils.c b/src/pc/lua/smlua_utils.c index a680fe2b0..3ccda2a7f 100644 --- a/src/pc/lua/smlua_utils.c +++ b/src/pc/lua/smlua_utils.c @@ -849,7 +849,7 @@ void smlua_logline(void) { if (strlen(src) < SYS_MAX_PATH) { int slashCount = 0; for (const char* p = src + strlen(src); p > src; --p) { - if (*p == '/' || *p == '\\') { + if (*p == *PATH_SEPARATOR || *p == *PATH_SEPARATOR_ALT) { if (++slashCount == 2) { folderStart = p + 1; break; diff --git a/src/pc/lua/utils/smlua_misc_utils.c b/src/pc/lua/utils/smlua_misc_utils.c index 9e90a45fb..faa10c1ae 100644 --- a/src/pc/lua/utils/smlua_misc_utils.c +++ b/src/pc/lua/utils/smlua_misc_utils.c @@ -622,8 +622,8 @@ LuaTable get_mod_files(struct Mod* mod, OPTIONAL const char* subDirectory) { normalize_path(normalizedSubDir); size_t subDirLen = strlen(normalizedSubDir); - if (subDirLen > 0 && subDirLen + 1 < SYS_MAX_PATH && normalizedSubDir[subDirLen - 1] != '/') { - strcat(normalizedSubDir, "/"); + if (subDirLen > 0 && subDirLen + 1 < SYS_MAX_PATH && normalizedSubDir[subDirLen - 1] != *PATH_SEPARATOR) { + strcat(normalizedSubDir, PATH_SEPARATOR); subDirLen = strlen(normalizedSubDir); } diff --git a/src/pc/mods/mods_utils.c b/src/pc/mods/mods_utils.c index 6e8c2bcd9..e09df3089 100644 --- a/src/pc/mods/mods_utils.c +++ b/src/pc/mods/mods_utils.c @@ -144,7 +144,7 @@ bool mod_file_create_directories(struct Mod* mod, struct ModFile* modFile) { char* p = path; u16 index = 0; while (*p != '\0') { - if (*p == '/' || *p == '\\') { + if (*p == *PATH_SEPARATOR || *p == *PATH_SEPARATOR_ALT) { if (snprintf(tmpPath, index + 1, "%s", path) < 0) { } if (!fs_sys_dir_exists(tmpPath)) { fs_sys_mkdir(tmpPath); @@ -210,11 +210,9 @@ void normalize_path(char* path) { // replace slashes char* p = path; while (*p) { -#if defined(_WIN32) - if (*p == '/') { *p = '\\'; } -#else - if (*p == '\\') { *p = '/'; } -#endif + if (*p == *PATH_SEPARATOR_ALT) { + *p = *PATH_SEPARATOR; + } p++; } } @@ -227,7 +225,7 @@ char* path_basename(char* path) { char* base = path; while (*path != '\0') { if (*(path + 1) != '\0') { - if (*path == '\\' || *path == '/') { + if (*path == *PATH_SEPARATOR || *path == *PATH_SEPARATOR_ALT) { base = path + 1; } } @@ -251,7 +249,7 @@ void path_get_folder(char* path, char* outpath) { int path_depth(const char* path) { int depth = 0; for (; *path; path++) { - if (*path == '/' || *path == '\\') { + if (*path == *PATH_SEPARATOR || *path == *PATH_SEPARATOR_ALT) { depth++; } } @@ -262,7 +260,7 @@ void resolve_relative_path(const char* base, const char* path, char* output) { char combined[SYS_MAX_PATH] = ""; // If path is absolute, copy as is. Otherwise, combine base and relative path - if (path[0] == '/' || path[0] == '\\') { + if (path[0] == *PATH_SEPARATOR || path[0] == *PATH_SEPARATOR_ALT) { snprintf(combined, sizeof(combined), "%s", path); } else { snprintf(combined, sizeof(combined), "%s/%s", base, path); @@ -272,7 +270,7 @@ void resolve_relative_path(const char* base, const char* path, char* output) { int tokenCount = 0; // Tokenize path by separators - char* token = strtok(combined, "/\\"); + char* token = strtok(combined, PATH_SEPARATOR PATH_SEPARATOR_ALT); while (token && tokenCount < 64) { if (strcmp(token, "..") == 0) { // Pop last token to go up a directory @@ -283,7 +281,7 @@ void resolve_relative_path(const char* base, const char* path, char* output) { tokens[tokenCount++] = token; } - token = strtok(NULL, "/\\"); + token = strtok(NULL, PATH_SEPARATOR PATH_SEPARATOR_ALT); } output[0] = '\0'; @@ -291,7 +289,7 @@ void resolve_relative_path(const char* base, const char* path, char* output) { // Build output path from tokens for (int i = 0; i < tokenCount; i++) { if (i > 0) { - strncat(output, "/", SYS_MAX_PATH - strlen(output) - 1); + strncat(output, PATH_SEPARATOR, SYS_MAX_PATH - strlen(output) - 1); } strncat(output, tokens[i], SYS_MAX_PATH - strlen(output) - 1); } @@ -308,8 +306,8 @@ bool directory_sanity_check(struct dirent* dir, char* dirPath, char* outPath) { if (!fs_sys_filename_is_portable(dir->d_name)) { return false; } // skip anything that contains \ or / - if (strchr(dir->d_name, '/') != NULL) { return false; } - if (strchr(dir->d_name, '\\') != NULL) { return false; } + if (strchr(dir->d_name, *PATH_SEPARATOR) != NULL) { return false; } + if (strchr(dir->d_name, *PATH_SEPARATOR_ALT) != NULL) { return false; } // skip anything that starts with . if (dir->d_name[0] == '.') { return false; } diff --git a/src/pc/platform.c b/src/pc/platform.c index 185b85bd9..7daeb178e 100644 --- a/src/pc/platform.c +++ b/src/pc/platform.c @@ -56,8 +56,8 @@ const char *sys_file_extension(const char *fpath) { } const char *sys_file_name(const char *fpath) { - const char *sep1 = strrchr(fpath, '/'); - const char *sep2 = strrchr(fpath, '\\'); + const char *sep1 = strrchr(fpath, *PATH_SEPARATOR); + const char *sep2 = strrchr(fpath, *PATH_SEPARATOR_ALT); const char *sep = sep1 > sep2 ? sep1 : sep2; if (!sep) return fpath; return sep + 1; @@ -325,7 +325,7 @@ const char *sys_user_path(void) { // strip the trailing separator const unsigned int len = strlen(path); - if (path[len-1] == '/' || path[len-1] == '\\') { path[len-1] = 0; } + if (path[len-1] == *PATH_SEPARATOR || path[len-1] == *PATH_SEPARATOR_ALT) { path[len-1] = 0; } return path; } diff --git a/src/pc/platform.h b/src/pc/platform.h index c05094ad4..e043c6ab5 100644 --- a/src/pc/platform.h +++ b/src/pc/platform.h @@ -7,6 +7,14 @@ /* platform-specific functions and whatnot */ +#ifdef _WIN32 +#define PATH_SEPARATOR "\\" +#define PATH_SEPARATOR_ALT "/" +#else +#define PATH_SEPARATOR "/" +#define PATH_SEPARATOR_ALT "\\" +#endif + #define SYS_MAX_PATH 4096 // crossplatform impls of misc stuff