diff -r 0998a43cc155 -r ca99a81745fe src/Platform-Posix.cpp --- a/src/Platform-Posix.cpp Sat Dec 29 00:55:38 2012 -0800 +++ b/src/Platform-Posix.cpp Tue Jan 01 15:34:07 2013 -0800 @@ -17,6 +17,9 @@ * JITCraft. If not, see http://www.gnu.org/licenses/. */ #include +#include +#include +#include #include "Platform.h" using namespace ke; @@ -99,3 +102,55 @@ OS::UnmapMemory(address, size); } +bool +OS::GetAbsolutePath(const char *path, AutoFree &chars) +{ + chars = (char *)calloc(PATH_MAX, sizeof(char)); + if (!*chars) + return false; + + char *ptr = realpath(path, *chars); + if (!ptr) + return false; + + return ptr == *chars; +} + +bool +OS::SplitPath(const char *path, AutoFree &base, AutoFree &name) +{ + // dirname() and basename() pretty much suck. + size_t len = strlen(path); + + // Skip trailing slashes. + while (len > 1 && (path[len - 1] == '/')) + len--; + + for (size_t i = len - 1; i < len; i--) { + if (path[i] == '/') { + name = (char *)calloc(len - i, sizeof(char)); + if (!*name) + return false; + + strcpy(*name, &path[i + 1]); + + base = (char *)calloc(i + 1, sizeof(char)); + if (!*base) + return false; + + memcpy(*base, path, i); + (*base)[i] = '\0'; + return true; + } + } + + // The path should have been absolute, which contains at least one separator. + return false; +} + +const char * +OS::PathSeparator() +{ + return "/"; +} +