diff --git a/core/imgread/gdi.cpp b/core/imgread/gdi.cpp index f975e86f8..3ab1d6ad5 100644 --- a/core/imgread/gdi.cpp +++ b/core/imgread/gdi.cpp @@ -1,9 +1,40 @@ #include "common.h" #include #include -#ifdef WIN32 -#include -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;itracks.push_back(t); }