diff --git a/mpw/mpw.cpp b/mpw/mpw.cpp index e975a79..d9d4a72 100644 --- a/mpw/mpw.cpp +++ b/mpw/mpw.cpp @@ -18,6 +18,7 @@ #include #include +#include #include #include @@ -116,7 +117,8 @@ namespace MPW std::string command = argv[0]; - std::replace(command.begin(), command.end(), '/', ':'); + command = ToolBox::UnixToMac(command); + //std::replace(command.begin(), command.end(), '/', ':'); argv[0] = basename(argv[0]); @@ -209,7 +211,8 @@ namespace MPW std::string tmp; std::string root(mpw); - std::replace(root.begin(), root.end(), '/', ':'); + root = ToolBox::UnixToMac(root); + //std::replace(root.begin(), root.end(), '/', ':'); if (root.back() != ':') root.push_back(':'); tmp = "MPW"; diff --git a/toolbox/toolbox.cpp b/toolbox/toolbox.cpp index 7f5fe9e..6d44157 100644 --- a/toolbox/toolbox.cpp +++ b/toolbox/toolbox.cpp @@ -307,7 +307,7 @@ namespace ToolBox { tmp.assign((char *)memoryPointer(address)); } - if (fname) std::replace(tmp.begin(), tmp.end(), ':', '/'); + if (fname) tmp = MacToUnix(tmp); return tmp; } @@ -322,7 +322,7 @@ namespace ToolBox { tmp.assign((char *)memoryPointer(address + 1), length); - if (fname) std::replace(tmp.begin(), tmp.end(), ':', '/'); + if (fname) tmp = MacToUnix(tmp); } @@ -356,5 +356,84 @@ namespace ToolBox { return true; } + /* + * MacOS basically does the absolute/relative paths backwards vs unix. + * + * file -> file + * :dir -> dir + * :dir:file -> dir/file + * volume:file -> /volume/file + */ + std::string UnixToMac(const std::string &path) + { + // ./..., //... etc. + std::string tmp; + int sep; + + if (path.empty()) return path; + + sep = path.find_first_of('/'); + + if (sep == path.npos) + { + // no sep -- relative file. treat as-is + return path; + } + if (sep == 0) + { + // absolute path -- drop the leading / + // '/' doesn't make sense. + tmp = path.substr(1); + } + else + { + // relative path. + tmp = '/'; + tmp.append(path); + } + + std::replace(tmp.begin(), tmp.end(), '/', ':'); + + return tmp; + } + + + std::string MacToUnix(const std::string &path) + { + std::string tmp; + int sep; + int slash; + + if (path.empty()) return path; + + sep = path.find_first_of(':'); + slash = path.find_first_of('/'); + + // if there's a / prior to the :, assume it's a unix prefix. + if (sep == path.npos) return path; + + if (slash == path.npos || slash > sep) + { + if (sep == 0) + { + // :directory... -> directory + tmp = path.substr(1); + } + else + { + // volume:path... + tmp = '/'; + tmp.append(path); + } + } + else // assume a unix prefix. + { + tmp = path; + } + + std::replace(tmp.begin(), tmp.end(), ':', '/'); + + return tmp; + } } \ No newline at end of file diff --git a/toolbox/toolbox.h b/toolbox/toolbox.h index 5b28665..a0a144c 100644 --- a/toolbox/toolbox.h +++ b/toolbox/toolbox.h @@ -29,6 +29,9 @@ namespace ToolBox bool WritePString(uint32_t address, const std::string &s); + + std::string UnixToMac(const std::string &path); + std::string MacToUnix(const std::string &path); }