From 2de948c62fdaeb6e8233cb6a54fdc2f66a6c8ff6 Mon Sep 17 00:00:00 2001 From: Mark Olsen Date: Mon, 6 Mar 2023 15:03:57 +0000 Subject: [PATCH] Add a PathsClass function to extract the filename from a path. --- common/mixfile.h | 25 ++++++------------------- common/paths.h | 1 + common/paths_posix.cpp | 9 +++++++++ common/paths_win.cpp | 12 ++++++++++++ 4 files changed, 28 insertions(+), 19 deletions(-) diff --git a/common/mixfile.h b/common/mixfile.h index 3a4f3604..109c35f4 100644 --- a/common/mixfile.h +++ b/common/mixfile.h @@ -29,10 +29,7 @@ #include "shastraw.h" #include "wwstd.h" #include "rndstraw.h" - -#ifndef _WIN32 -#include // For basename() -#endif +#include "paths.h" #ifndef _MAX_PATH #define _MAX_PATH PATH_MAX @@ -528,11 +525,6 @@ template MixFileClass* MixFileClass::Fin { MixFileClass* ptr = MixList.First(); while (ptr->Is_Valid()) { -#ifdef _WIN32 - char path[_MAX_PATH]; - char name[_MAX_FNAME]; - char ext[_MAX_EXT]; - /* ** Strip the drive and path (if present) off of the filename ** in the mixfile list. This enables a simple comparison to the @@ -540,16 +532,11 @@ template MixFileClass* MixFileClass::Fin ** the full pathname in the mixfile list WILL have a path attached. Hence, this ** stripping of the path is necessary. */ - _splitpath(ptr->Filename, NULL, NULL, name, ext); - _makepath(path, NULL, NULL, name, ext); -#else - char buff[PATH_MAX]; - char* path = nullptr; - strncpy(buff, ptr->Filename, PATH_MAX); - buff[PATH_MAX - 1] = '\0'; - path = basename(buff); -#endif - if (stricmp(path, filename) == 0) { + + std::string path; + path = Paths.Get_Filename(ptr->Filename); + + if (stricmp(path.c_str(), filename) == 0) { return (ptr); } ptr = ptr->Next(); diff --git a/common/paths.h b/common/paths.h index d016a997..4b045d95 100644 --- a/common/paths.h +++ b/common/paths.h @@ -40,6 +40,7 @@ class PathsClass static bool Create_Directory(const char* path); static bool Is_Absolute(const char* path); static std::string Concatenate_Paths(const char* path1, const char* path2); + static std::string Get_Filename(const char* path); #ifdef _WIN32 constexpr static char SEP = '\\'; diff --git a/common/paths_posix.cpp b/common/paths_posix.cpp index 030fee12..195e6f4a 100644 --- a/common/paths_posix.cpp +++ b/common/paths_posix.cpp @@ -21,6 +21,7 @@ #include #include #include +#include #if defined(__linux__) #include @@ -245,6 +246,14 @@ std::string PathsClass::Concatenate_Paths(const char* path1, const char* path2) return std::string(path1) + SEP + path2; } +std::string PathsClass::Get_Filename(const char* path) +{ + char buff[PATH_MAX]; + strncpy(buff, path, PATH_MAX); + buff[PATH_MAX - 1] = '\0'; + return std::string(basename(buff)); +} + std::string PathsClass::Argv_Path(const char* cmd_arg) { std::string ret(PATH_MAX, '\0'); diff --git a/common/paths_win.cpp b/common/paths_win.cpp index f2a42df6..69b3795f 100644 --- a/common/paths_win.cpp +++ b/common/paths_win.cpp @@ -151,6 +151,18 @@ std::string PathsClass::Concatenate_Paths(const char* path1, const char* path2) return std::string(path1) + SEP + path2; } +std::string PathsClass::Get_Filename(const char* path) +{ + char temppath[_MAX_PATH]; + char name[_MAX_FNAME]; + char ext[_MAX_EXT]; + + _splitpath(path, NULL, NULL, name, ext); + _makepath(temppath, NULL, NULL, name, ext); + + return std::string(temppath); +} + std::string PathsClass::Argv_Path(const char* cmd_arg) { TCHAR base_buff[MAX_PATH];