src/Platform-Windows.cpp
changeset 251 ca99a81745fe
parent 233 bbb498ccb4ac
     1.1 --- a/src/Platform-Windows.cpp	Sat Dec 29 00:55:38 2012 -0800
     1.2 +++ b/src/Platform-Windows.cpp	Tue Jan 01 15:34:07 2013 -0800
     1.3 @@ -17,9 +17,10 @@
     1.4   * SourcePawn. If not, see http://www.gnu.org/licenses/.
     1.5   */
     1.6  
     1.7 -// :TODO: move into utility-style include
     1.8  #define WIN32_LEAN_AND_MEAN
     1.9  #include <Windows.h>
    1.10 +#include <direct.h>
    1.11 +#include <sys/stat.h>
    1.12  #include "Platform.h"
    1.13  
    1.14  using namespace ke;
    1.15 @@ -109,3 +110,78 @@
    1.16  {
    1.17      OS::UnmapMemory(address, size);
    1.18  }
    1.19 +
    1.20 +bool
    1.21 +OS::GetAbsolutePath(const char *path, AutoFree<char> &chars)
    1.22 +{
    1.23 +    char try1[MAX_PATH];
    1.24 +    DWORD result = GetFullPathName(path, sizeof(try1), try1, NULL);
    1.25 +    if (result <= 0)
    1.26 +        return false;
    1.27 +
    1.28 +    // +1 because if result < MAX_PATH, it doesn't include the null terminator.
    1.29 +    chars = (char *)calloc(result + 1, sizeof(char));
    1.30 +    if (!*chars)
    1.31 +        return false;
    1.32 +
    1.33 +    if (result <= MAX_PATH) {
    1.34 +        strcpy(*chars, try1);
    1.35 +        return true;
    1.36 +    }
    1.37 +
    1.38 +    // We should get the same output on the next try.
    1.39 +    return GetFullPathName(path, result, *chars, NULL) == result - 1;
    1.40 +}
    1.41 +
    1.42 +bool
    1.43 +OS::SplitPath(const char *path, AutoFree<char> &base, AutoFree<char> &name)
    1.44 +{
    1.45 +    size_t len = strlen(path);
    1.46 +
    1.47 +    // Skip trailing slashes.
    1.48 +    while (len > 1 && (path[len - 1] == '/' || path[len - 1] == '\\'))
    1.49 +        len--;
    1.50 +
    1.51 +    for (size_t i = len - 1; i < len; i--) {
    1.52 +        if (path[i] == '\\' || path[i] == '/') {
    1.53 +            name = (char *)calloc(len - i, sizeof(char));
    1.54 +            if (!*name)
    1.55 +                return false;
    1.56 +
    1.57 +            strcpy(*name, &path[i + 1]);
    1.58 +
    1.59 +            base = (char *)calloc(i + 1, sizeof(char));
    1.60 +            if (!*base)
    1.61 +                return false;
    1.62 +
    1.63 +            memcpy(*base, path, i);
    1.64 +            (*base)[i] = '\0';
    1.65 +            return true;
    1.66 +        }
    1.67 +    }
    1.68 +    
    1.69 +    // The path should have been absolute, which contains at least one separator.
    1.70 +    return false;
    1.71 +}
    1.72 +
    1.73 +const char *
    1.74 +OS::PathSeparator()
    1.75 +{
    1.76 +    return "\\";
    1.77 +}
    1.78 +
    1.79 +OS::PathType
    1.80 +OS::InspectPath(const char *path)
    1.81 +{
    1.82 +    struct _stat s;
    1.83 +    int result = _stat(path, &s);
    1.84 +    if (result != 0)
    1.85 +        return OS::Path_Invalid;
    1.86 +
    1.87 +    if (s.st_mode & _S_IFDIR)
    1.88 +        return OS::Path_Folder;
    1.89 +    if (s.st_mode & _S_IFREG)
    1.90 +        return OS::Path_File;
    1.91 +
    1.92 +    return OS::Path_Invalid;
    1.93 +}