diff options
author | Michael J. Spencer <bigcheesegs@gmail.com> | 2010-11-29 22:28:51 +0000 |
---|---|---|
committer | Michael J. Spencer <bigcheesegs@gmail.com> | 2010-11-29 22:28:51 +0000 |
commit | dffde9964480f946d4308ce936b667b6c37b1059 (patch) | |
tree | 11cddd0a2725d0fd441afa52cf8e5c7c92e3ae01 /lib/Support/Windows/PathV2.inc | |
parent | 44c1bc14570e77630b5e6711ed42886aad848086 (diff) | |
download | external_llvm-dffde9964480f946d4308ce936b667b6c37b1059.zip external_llvm-dffde9964480f946d4308ce936b667b6c37b1059.tar.gz external_llvm-dffde9964480f946d4308ce936b667b6c37b1059.tar.bz2 |
Support: Add PathV2 implementation.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@120329 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Support/Windows/PathV2.inc')
-rw-r--r-- | lib/Support/Windows/PathV2.inc | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/lib/Support/Windows/PathV2.inc b/lib/Support/Windows/PathV2.inc new file mode 100644 index 0000000..9c15e26 --- /dev/null +++ b/lib/Support/Windows/PathV2.inc @@ -0,0 +1,71 @@ +//===- llvm/Support/Win32/PathV2.cpp - Windows Path Impl --------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file implements the Windows specific implementation of the PathV2 API. +// +//===----------------------------------------------------------------------===// + +//===----------------------------------------------------------------------===// +//=== WARNING: Implementation here must contain only generic Windows code that +//=== is guaranteed to work on *all* Windows variants. +//===----------------------------------------------------------------------===// + +#include "Windows.h" + +namespace llvm { +namespace sys { +namespace path { + +error_code current_path(SmallVectorImpl<char> &result) { + SmallVector<wchar_t, 128> cur_path; + cur_path.reserve(128); +retry_cur_dir: + DWORD len = ::GetCurrentDirectoryW(cur_path.capacity(), cur_path.data()); + + // A zero return value indicates a failure other than insufficient space. + if (len == 0) + return make_error_code(windows_error(::GetLastError())); + + // If there's insufficient space, the len returned is larger than the len + // given. + if (len > cur_path.capacity()) { + cur_path.reserve(len); + goto retry_cur_dir; + } + + cur_path.set_size(len); + // cur_path now holds the current directory in utf-16. Convert to utf-8. + + // Find out how much space we need. Sadly, this function doesn't return the + // size needed unless you tell it the result size is 0, which means you + // _always_ have to call it twice. + len = ::WideCharToMultiByte(CP_UTF8, NULL, + cur_path.data(), cur_path.size(), + result.data(), 0, + NULL, NULL); + + if (len == 0) + return make_error_code(windows_error(::GetLastError())); + + result.reserve(len); + result.set_size(len); + // Now do the actual conversion. + len = ::WideCharToMultiByte(CP_UTF8, NULL, + cur_path.data(), cur_path.size(), + result.data(), result.size(), + NULL, NULL); + if (len == 0) + return make_error_code(windows_error(::GetLastError())); + + return make_error_code(errc::success); +} + +} // end namespace path +} // end namespace sys +} // end namespace llvm |