gdi: Cleanup path code, use C++ strings

This commit is contained in:
Stefanos Kornilios Mitsis Poiitidis 2018-04-11 11:40:15 +02:00
parent 052c5c1cb3
commit 4300951b51
1 changed files with 38 additions and 19 deletions

View File

@ -1,9 +1,40 @@
#include "common.h"
#include <ctype.h>
#include <sstream>
#ifdef WIN32
#include <BaseTsd.h>
typedef SSIZE_T ssize_t;
// given file/name.ext or file\name.ext returns file/ or file\, depending on the platform
// given name.ext returns ./ or .\, depending on the platform
string OS_dirname(string file)
{
#if HOST_OS == OS_WINDOWS
const char sep = '\\';
#else
const char sep = '/';
#endif
size_t last_slash = file.find_last_of(sep);
if (last_slash == string::npos)
{
string local_dir = ".";
local_dir += sep;
return local_dir;
}
return file.substr(0, last_slash + 1);
}
#if 0 // TODO: Move this to some tests, make it platform agnostic
namespace {
struct OS_dirname_Test {
OS_dirname_Test() {
verify(OS_dirname("local/path") == "local/");
verify(OS_dirname("local/path/two") == "local/path/");
verify(OS_dirname("local/path/three\\a.exe") == "local/path/");
verify(OS_dirname("local.ext") == "./");
}
} test;
}
#endif
Disc* load_gdi(const char* file)
@ -35,21 +66,9 @@ Disc* load_gdi(const char* file)
gdi >> iso_tc;
printf("\nGDI : %d tracks\n",iso_tc);
// FIXME: Data loss if buffer is too small
char path[512];
strncpy(path, file, sizeof(path));
path[sizeof(path) - 1] = '\0';
string basepath = OS_dirname(file);
ssize_t len = strlen(path);
while (len>=0)
{
if (path[len]=='\\' || path[len]=='/')
break;
len--;
}
len++;
char* pathptr=&path[len];
u32 TRACK=0,FADS=0,CTRL=0,SSIZE=0;
s32 OFFSET=0;
for (u32 i=0;i<iso_tc;i++)
@ -95,8 +114,8 @@ Disc* load_gdi(const char* file)
if (SSIZE!=0)
{
strcpy(pathptr, track_filename.c_str());
t.file = new RawTrackFile(core_fopen(path),OFFSET,t.StartFAD,SSIZE);
string path = basepath + track_filename;
t.file = new RawTrackFile(core_fopen(path.c_str()),OFFSET,t.StartFAD,SSIZE);
}
disc->tracks.push_back(t);
}