2022-05-08 02:32:29 +02:00
|
|
|
/*****************************************************************************\
|
|
|
|
Snes9x - Portable Super Nintendo Entertainment System (TM) emulator.
|
|
|
|
This file is licensed under the Snes9x License.
|
|
|
|
For further information, consult the LICENSE file in the root directory.
|
|
|
|
\*****************************************************************************/
|
|
|
|
|
2022-05-06 23:17:03 +02:00
|
|
|
#include <cstring>
|
|
|
|
#include <string>
|
2022-05-08 02:29:40 +02:00
|
|
|
|
|
|
|
#include "display.h"
|
|
|
|
#include "fscompat.h"
|
|
|
|
#include "port.h"
|
|
|
|
#include "memmap.h"
|
2022-05-06 23:17:03 +02:00
|
|
|
|
|
|
|
using std::string;
|
|
|
|
|
|
|
|
bool SplitPath::ext_is(const string &other)
|
|
|
|
{
|
|
|
|
if (strcasecmp(ext.c_str(), other.c_str()) == 0)
|
|
|
|
return true;
|
|
|
|
if (other[0] != '.' && (strcasecmp(other.c_str(), &(ext.c_str()[1])) == 0))
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string makepath(const SplitPath &path)
|
|
|
|
{
|
|
|
|
return makepath(path.drive, path.dir, path.stem, path.ext);
|
|
|
|
}
|
|
|
|
|
2022-05-08 02:29:40 +02:00
|
|
|
std::string S9xGetFilename(string filename, string ext, enum s9x_getdirtype dirtype)
|
|
|
|
{
|
|
|
|
auto path = splitpath(filename);
|
|
|
|
auto dir = S9xGetDirectory(dirtype);
|
|
|
|
return makepath(path.drive, dir, path.stem, ext);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string S9xGetFilename(string ext, enum s9x_getdirtype dirtype)
|
|
|
|
{
|
|
|
|
return S9xGetFilename(Memory.ROMFilename, ext, dirtype);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string S9xBasename(std::string filename)
|
|
|
|
{
|
|
|
|
auto path = splitpath(filename);
|
|
|
|
return path.stem + path.ext;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string S9xBasenameNoExt(std::string filename)
|
|
|
|
{
|
|
|
|
return splitpath(filename).stem;
|
|
|
|
}
|
|
|
|
|
2022-05-06 23:17:03 +02:00
|
|
|
#if __cplusplus >= 201703L
|
|
|
|
#include <filesystem>
|
|
|
|
namespace fs = std::filesystem;
|
|
|
|
|
|
|
|
SplitPath splitpath(string str)
|
|
|
|
{
|
|
|
|
SplitPath output{};
|
2023-03-14 12:26:03 +01:00
|
|
|
fs::path path(fs::u8path(str));
|
2022-05-06 23:17:03 +02:00
|
|
|
|
|
|
|
if (path.has_root_name())
|
2023-03-14 12:26:03 +01:00
|
|
|
output.drive = path.root_name().u8string();
|
2022-05-06 23:17:03 +02:00
|
|
|
|
|
|
|
if (path.has_filename())
|
|
|
|
{
|
2023-03-14 12:26:03 +01:00
|
|
|
output.stem = path.stem().u8string();
|
|
|
|
output.ext = path.extension().u8string();
|
2022-05-06 23:17:03 +02:00
|
|
|
path.remove_filename();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!path.empty())
|
2023-03-14 12:26:03 +01:00
|
|
|
output.dir = path.u8string();
|
2022-05-06 23:17:03 +02:00
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
string makepath(const string &drive, const string &dir, const string &stem, const string &ext)
|
|
|
|
{
|
2022-06-15 02:09:51 +02:00
|
|
|
auto dot_position = ext.find('.');
|
|
|
|
|
|
|
|
if (dot_position == string::npos)
|
|
|
|
{
|
2023-03-14 12:26:03 +01:00
|
|
|
fs::path path(fs::u8path(drive));
|
|
|
|
path = path / fs::u8path(dir) / fs::u8path(stem);
|
2022-06-15 02:09:51 +02:00
|
|
|
path.replace_extension(ext);
|
2023-03-14 12:26:03 +01:00
|
|
|
return path.u8string();
|
2022-06-15 02:09:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
auto filename = stem + ext;
|
2023-03-14 12:26:03 +01:00
|
|
|
fs::path path(fs::u8path(drive));
|
|
|
|
path = path / fs::u8path(dir) / fs::u8path(filename);
|
|
|
|
return path.u8string();
|
2022-05-06 23:17:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
constexpr auto npos = std::string::npos;
|
|
|
|
|
|
|
|
SplitPath splitpath(string path)
|
|
|
|
{
|
|
|
|
SplitPath output{};
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
if (path.length() > 2 && path[1] == ':')
|
|
|
|
{
|
|
|
|
output.drive = path.substr(0, 2);
|
|
|
|
path = path.substr(2);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
auto slash = path.rfind(SLASH_CHAR);
|
|
|
|
auto dot = path.rfind('.');
|
|
|
|
|
|
|
|
if (dot != npos && slash != npos && dot < slash)
|
|
|
|
{
|
|
|
|
dot = npos;
|
|
|
|
}
|
|
|
|
else if (dot != npos)
|
|
|
|
{
|
|
|
|
output.ext = path.substr(dot);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (slash != npos)
|
|
|
|
{
|
|
|
|
output.dir = path.substr(0, slash + 1);
|
|
|
|
output.stem = path.substr(slash + 1, dot - slash - 1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
output.stem = path.substr(0, dot);
|
|
|
|
}
|
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2022-06-15 02:09:51 +02:00
|
|
|
string makepath(const string &drive, const string &dir, const string &stem, const string &ext)
|
2022-05-06 23:17:03 +02:00
|
|
|
{
|
|
|
|
string output;
|
|
|
|
|
|
|
|
if (!drive.empty())
|
|
|
|
{
|
|
|
|
output += drive + ":";
|
|
|
|
if (!dir.empty() && dir[0] != SLASH_CHAR)
|
|
|
|
output += SLASH_CHAR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!dir.empty())
|
|
|
|
{
|
|
|
|
output += dir;
|
|
|
|
if (output[output.length() - 1] != SLASH_CHAR)
|
|
|
|
output += SLASH_CHAR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!stem.empty())
|
|
|
|
{
|
|
|
|
output += stem;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ext.empty())
|
|
|
|
{
|
2022-06-15 02:09:51 +02:00
|
|
|
if (ext.find('.') == string::npos)
|
2022-05-06 23:17:03 +02:00
|
|
|
output += '.';
|
|
|
|
output += ext;
|
|
|
|
}
|
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
void _splitpath(const char *path, char *drive, char *dir, char *fname, char *ext)
|
|
|
|
{
|
|
|
|
char *slash = strrchr((char *)path, SLASH_CHAR);
|
|
|
|
char *dot = strrchr((char *)path, '.');
|
|
|
|
|
|
|
|
*drive = '\0';
|
|
|
|
|
|
|
|
if (dot && slash && dot < slash)
|
|
|
|
{
|
|
|
|
dot = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!slash)
|
|
|
|
{
|
|
|
|
*dir = '\0';
|
|
|
|
strcpy(fname, path);
|
|
|
|
|
|
|
|
if (dot)
|
|
|
|
{
|
|
|
|
fname[dot - path] = '\0';
|
|
|
|
strcpy(ext, dot + 1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*ext = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
strcpy(dir, path);
|
|
|
|
dir[slash - path] = '\0';
|
|
|
|
strcpy(fname, slash + 1);
|
|
|
|
|
|
|
|
if (dot)
|
|
|
|
{
|
|
|
|
fname[(dot - slash) - 1] = '\0';
|
|
|
|
strcpy(ext, dot + 1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*ext = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void _makepath(char *path, const char *drive, const char *dir, const char *fname, const char *ext)
|
|
|
|
{
|
|
|
|
if (dir && *dir)
|
|
|
|
{
|
|
|
|
strcpy(path, dir);
|
|
|
|
strcat(path, "/");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
*path = '\0';
|
|
|
|
|
|
|
|
strcat(path, fname);
|
|
|
|
|
|
|
|
if (ext && *ext)
|
|
|
|
{
|
2022-06-15 02:09:51 +02:00
|
|
|
if (!strchr(ext, '.'))
|
2022-05-06 23:17:03 +02:00
|
|
|
strcat(path, ".");
|
|
|
|
strcat(path, ext);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|