Skip to content

Commit

Permalink
Add a PathsClass function to extract the filename from a path.
Browse files Browse the repository at this point in the history
  • Loading branch information
isojalka authored and OmniBlade committed Jun 14, 2024
1 parent 6233416 commit 2de948c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 19 deletions.
25 changes: 6 additions & 19 deletions common/mixfile.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@
#include "shastraw.h"
#include "wwstd.h"
#include "rndstraw.h"

#ifndef _WIN32
#include <libgen.h> // For basename()
#endif
#include "paths.h"

#ifndef _MAX_PATH
#define _MAX_PATH PATH_MAX
Expand Down Expand Up @@ -528,28 +525,18 @@ template <class T, class TCRC> MixFileClass<T, TCRC>* MixFileClass<T, TCRC>::Fin
{
MixFileClass<T, TCRC>* 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
** filename specified. The filename specified won't have a path attached and
** 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();
Expand Down
1 change: 1 addition & 0 deletions common/paths.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '\\';
Expand Down
9 changes: 9 additions & 0 deletions common/paths_posix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <sys/types.h>
#include <unistd.h>
#include <vector>
#include <libgen.h>

#if defined(__linux__)
#include <linux/limits.h>
Expand Down Expand Up @@ -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');
Expand Down
12 changes: 12 additions & 0 deletions common/paths_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down

0 comments on commit 2de948c

Please sign in to comment.