From ba470e9cfd4a2fd2b246228a04b6fdcc86a71be4 Mon Sep 17 00:00:00 2001 From: Chris Smowton Date: Mon, 6 Mar 2017 11:48:33 +0000 Subject: [PATCH] File-util: Windows compatibility fixes realpath isn't available, and Shlwapi.h is required. --- src/util/file_util.cpp | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/util/file_util.cpp b/src/util/file_util.cpp index d7bd637d442..65542dd325f 100644 --- a/src/util/file_util.cpp +++ b/src/util/file_util.cpp @@ -28,6 +28,7 @@ Date: January 2012 #include #include #include +#include #define chdir _chdir #define popen _popen #define pclose _pclose @@ -268,9 +269,19 @@ std::string fileutl_concatenate_file_paths(std::string const& left_path, std::string fileutl_absolute_path(std::string const& path) { - // TODO: portability - this implementation won't probably work on Windows... - std::vector buffer(10000,0); - return realpath(path.c_str(),&buffer.at(0)); +#if defined(WIN32) + DWORD buffer_length = GetFullPathName(path.c_str(), 0, nullptr, nullptr); + std::vector buffer(buffer_length); + DWORD actual_length = GetFullPathName(path.c_str(), buffer_length, &(buffer[0]), nullptr); + if(actual_length != buffer_length - 1) + throw "fileutl_absolute_path: GetFullPathName failed"; + return std::string(&(buffer[0])); +#else + char *absolute = realpath(path.c_str(), nullptr); + std::string ret(absolute); + free(absolute); + return ret; +#endif } std::string fileutl_normalise_path(std::string const& path)