src/Platform-Posix.cpp
changeset 251 ca99a81745fe
parent 92 e3974e7360e9
child 252 1dee2330aa78
     1.1 --- a/src/Platform-Posix.cpp	Sat Dec 29 00:55:38 2012 -0800
     1.2 +++ b/src/Platform-Posix.cpp	Tue Jan 01 15:34:07 2013 -0800
     1.3 @@ -17,6 +17,9 @@
     1.4   * JITCraft. If not, see http://www.gnu.org/licenses/.
     1.5   */
     1.6  #include <sys/mman.h>
     1.7 +#include <limits.h>
     1.8 +#include <string.h>
     1.9 +#include <stdlib.h>
    1.10  #include "Platform.h"
    1.11  
    1.12  using namespace ke;
    1.13 @@ -99,3 +102,55 @@
    1.14      OS::UnmapMemory(address, size);
    1.15  }
    1.16  
    1.17 +bool
    1.18 +OS::GetAbsolutePath(const char *path, AutoFree<char> &chars)
    1.19 +{
    1.20 +    chars = (char *)calloc(PATH_MAX, sizeof(char));
    1.21 +    if (!*chars)
    1.22 +        return false;
    1.23 +
    1.24 +    char *ptr = realpath(path, *chars);
    1.25 +    if (!ptr)
    1.26 +        return false;
    1.27 +
    1.28 +    return ptr == *chars;
    1.29 +}
    1.30 +
    1.31 +bool
    1.32 +OS::SplitPath(const char *path, AutoFree<char> &base, AutoFree<char> &name)
    1.33 +{
    1.34 +    // dirname() and basename() pretty much suck.
    1.35 +    size_t len = strlen(path);
    1.36 +
    1.37 +    // Skip trailing slashes.
    1.38 +    while (len > 1 && (path[len - 1] == '/'))
    1.39 +        len--;
    1.40 +
    1.41 +    for (size_t i = len - 1; i < len; i--) {
    1.42 +        if (path[i] == '/') {
    1.43 +            name = (char *)calloc(len - i, sizeof(char));
    1.44 +            if (!*name)
    1.45 +                return false;
    1.46 +
    1.47 +            strcpy(*name, &path[i + 1]);
    1.48 +
    1.49 +            base = (char *)calloc(i + 1, sizeof(char));
    1.50 +            if (!*base)
    1.51 +                return false;
    1.52 +
    1.53 +            memcpy(*base, path, i);
    1.54 +            (*base)[i] = '\0';
    1.55 +            return true;
    1.56 +        }
    1.57 +    }
    1.58 +    
    1.59 +    // The path should have been absolute, which contains at least one separator.
    1.60 +    return false;
    1.61 +}
    1.62 +
    1.63 +const char *
    1.64 +OS::PathSeparator()
    1.65 +{
    1.66 +    return "/";
    1.67 +}
    1.68 +