ref: 1263787fc7e6578c31adc4651f786940e6a0aeeb
parent: ffbca88b7c4a766bc9a3192cf66b6b71610c431f
author: Fabian Greffrath <[email protected]>
date: Tue Mar 22 16:59:04 EDT 2022
M_DirName()/M_BaseName(): accept forward slash on Windows (#1286) * M_DirName()/M_BaseName(): accept forward slash on Windows Windows allows for both backward and forward slashes as path separators. So, if we cannot find the former, try the latter instead, but don't accept mixed forms. Actually, the latter is the prefered form when using Unix-like shells on Windows like e.g. Cygwin or MSYS2. Fixes: #1271 and https://github.com/fabiangreffrath/crispy-doom/issues/618 * update with implementation from Woof! * add trivial MIN/MAX macros to doomtype.h * Revert "add trivial MIN/MAX macros to doomtype.h" This reverts commit bdcea20510773bf006557bd4e1b0d579a2848834. * const char* correctness, spell out MAX() macro
--- a/src/m_misc.c
+++ b/src/m_misc.c
@@ -271,15 +271,22 @@
// and must be freed by the caller after use.
char *M_DirName(const char *path)
{
- char *p, *result;
+ char *result;
+ const char *pf, *pb;
- p = strrchr(path, DIR_SEPARATOR);
- if (p == NULL)
+ pf = strrchr(path, '/');
+#ifdef _WIN32
+ pb = strrchr(path, '\\');
+#else
+ pb = NULL;
+#endif
+ if (pf == NULL && pb == NULL)
{
return M_StringDuplicate(".");
}
else
{
+ const char *p = (pf > pb) ? pf : pb;
result = M_StringDuplicate(path);
result[p - path] = '\0';
return result;
@@ -291,15 +298,21 @@
// allocated.
const char *M_BaseName(const char *path)
{
- const char *p;
+ const char *pf, *pb;
- p = strrchr(path, DIR_SEPARATOR);
- if (p == NULL)
+ pf = strrchr(path, '/');
+#ifdef _WIN32
+ pb = strrchr(path, '\\');
+#else
+ pb = NULL;
+#endif
+ if (pf == NULL && pb == NULL)
{
return path;
}
else
{
+ const char *p = (pf > pb) ? pf : pb;
return p + 1;
}
}