commit 1263787fc7e6578c31adc4651f786940e6a0aeeb
Author: Fabian Greffrath <fabian@greffrath.com>
AuthorDate: Tue Mar 22 20:59:04 2022 +0100
Commit: GitHub <noreply@github.com>
CommitDate: Tue Mar 22 20:59:04 2022 +0100
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
---
src/m_misc.c | 25 +++++++++++++++++++------
1 file changed, 19 insertions(+), 6 deletions(-)
diff --git a/src/m_misc.c b/src/m_misc.c
index 6aa97b6e..67b03b18 100644
--- a/src/m_misc.c
+++ b/src/m_misc.c
@@ -271,15 +271,22 @@ boolean M_StrToInt(const char *str, int *result)
// 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 @@ char *M_DirName(const char *path)
// 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;
}
}