diff --git a/core/imgread/gdi.cpp b/core/imgread/gdi.cpp index c6cf5207a..2cc246c78 100644 --- a/core/imgread/gdi.cpp +++ b/core/imgread/gdi.cpp @@ -1,6 +1,65 @@ #include "common.h" #include #include +#include + +// 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); +} + +// On windows, transform / to \\ +// On linux, transform \\ to / +string normalize_path_separator(string path) +{ + #if HOST_OS == OS_WINDOWS + std::replace( path.begin(), path.end(), '/', '\\'); + #else + std::replace( path.begin(), path.end(), '\\', '/'); + #endif + + return path; +} + +#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") == "./"); + } + } test1; + + struct normalize_path_separator_Test { + normalize_path_separator_Test() { + verify(normalize_path_separator("local/path") == "local/path"); + verify(normalize_path_separator("local\\path") == "local/path"); + verify(normalize_path_separator("local\\path\\") == "local/path/"); + verify(normalize_path_separator("\\local\\path\\") == "/local/path/"); + verify(normalize_path_separator("loc\\al\\pa\\th") == "loc/al/pa/th"); + } + } test2; +} +#endif Disc* load_gdi(const char* file) { @@ -31,21 +90,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); - size_t len = strlen(path); - - while (len>2) - { - 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); } diff --git a/tests/files/test_gdis/a/cs.gdi b/tests/files/test_gdis/a/cs.gdi new file mode 100644 index 000000000..faca45760 --- /dev/null +++ b/tests/files/test_gdis/a/cs.gdi @@ -0,0 +1,4 @@ +3 +1 0 4 2352 tracks\cs01.bin 0 +2 450 0 2352 tracks\cs02.raw 0 +3 45000 4 2352 tracks\cs03.bin 0 diff --git a/tests/files/test_gdis/a/tracks/cs01.bin b/tests/files/test_gdis/a/tracks/cs01.bin new file mode 100644 index 000000000..e69de29bb diff --git a/tests/files/test_gdis/a/tracks/cs02.raw b/tests/files/test_gdis/a/tracks/cs02.raw new file mode 100644 index 000000000..e69de29bb diff --git a/tests/files/test_gdis/a/tracks/cs03.bin b/tests/files/test_gdis/a/tracks/cs03.bin new file mode 100644 index 000000000..e69de29bb diff --git a/tests/files/test_gdis/b/cs.gdi b/tests/files/test_gdis/b/cs.gdi new file mode 100644 index 000000000..5562ee8c8 --- /dev/null +++ b/tests/files/test_gdis/b/cs.gdi @@ -0,0 +1,4 @@ +3 +1 0 4 2352 cs01.bin 0 +2 450 0 2352 cs02.raw 0 +3 45000 4 2352 cs03.bin 0 diff --git a/tests/files/test_gdis/b/cs01.bin b/tests/files/test_gdis/b/cs01.bin new file mode 100644 index 000000000..e69de29bb diff --git a/tests/files/test_gdis/b/cs02.raw b/tests/files/test_gdis/b/cs02.raw new file mode 100644 index 000000000..e69de29bb diff --git a/tests/files/test_gdis/b/cs03.bin b/tests/files/test_gdis/b/cs03.bin new file mode 100644 index 000000000..e69de29bb diff --git a/tests/files/test_gdis/c/cs.gdi b/tests/files/test_gdis/c/cs.gdi new file mode 100644 index 000000000..73563f35e --- /dev/null +++ b/tests/files/test_gdis/c/cs.gdi @@ -0,0 +1,4 @@ +3 +1 0 4 2352 tracks/cs01.bin 0 +2 450 0 2352 tracks/cs02.raw 0 +3 45000 4 2352 tracks/cs03.bin 0 diff --git a/tests/files/test_gdis/c/tracks/cs01.bin b/tests/files/test_gdis/c/tracks/cs01.bin new file mode 100644 index 000000000..e69de29bb diff --git a/tests/files/test_gdis/c/tracks/cs02.raw b/tests/files/test_gdis/c/tracks/cs02.raw new file mode 100644 index 000000000..e69de29bb diff --git a/tests/files/test_gdis/c/tracks/cs03.bin b/tests/files/test_gdis/c/tracks/cs03.bin new file mode 100644 index 000000000..e69de29bb