Skip to content

Commit ba470e9

Browse files
smowtonpeterschrammel
authored andcommitted
File-util: Windows compatibility fixes
realpath isn't available, and Shlwapi.h is required.
1 parent b7c00a6 commit ba470e9

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

src/util/file_util.cpp

+14-3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Date: January 2012
2828
#include <io.h>
2929
#include <windows.h>
3030
#include <direct.h>
31+
#include <Shlwapi.h>
3132
#define chdir _chdir
3233
#define popen _popen
3334
#define pclose _pclose
@@ -268,9 +269,19 @@ std::string fileutl_concatenate_file_paths(std::string const& left_path,
268269

269270
std::string fileutl_absolute_path(std::string const& path)
270271
{
271-
// TODO: portability - this implementation won't probably work on Windows...
272-
std::vector<char> buffer(10000,0);
273-
return realpath(path.c_str(),&buffer.at(0));
272+
#if defined(WIN32)
273+
DWORD buffer_length = GetFullPathName(path.c_str(), 0, nullptr, nullptr);
274+
std::vector<char> buffer(buffer_length);
275+
DWORD actual_length = GetFullPathName(path.c_str(), buffer_length, &(buffer[0]), nullptr);
276+
if(actual_length != buffer_length - 1)
277+
throw "fileutl_absolute_path: GetFullPathName failed";
278+
return std::string(&(buffer[0]));
279+
#else
280+
char *absolute = realpath(path.c_str(), nullptr);
281+
std::string ret(absolute);
282+
free(absolute);
283+
return ret;
284+
#endif
274285
}
275286

276287
std::string fileutl_normalise_path(std::string const& path)

0 commit comments

Comments
 (0)