summaryrefslogtreecommitdiffstats
path: root/llvm/lib/Support/Windows
diff options
context:
space:
mode:
authorMichael J. Spencer <bigcheesegs@gmail.com>2010-12-01 19:32:01 +0000
committerMichael J. Spencer <bigcheesegs@gmail.com>2010-12-01 19:32:01 +0000
commit9fc1d9dcc3490b45cca703139e80cd05059c8ef2 (patch)
tree77e192ca8563c51f2617526311fad70e39438321 /llvm/lib/Support/Windows
parent8d73cac4ba7c2e8dd3a3d4f9e68c1033f8fdd26a (diff)
downloadbcm5719-llvm-9fc1d9dcc3490b45cca703139e80cd05059c8ef2.tar.gz
bcm5719-llvm-9fc1d9dcc3490b45cca703139e80cd05059c8ef2.zip
Support/FileSystem: Add copy_file implementation. Not tests yet because the
file creation APIs aren't implemented. llvm-svn: 120593
Diffstat (limited to 'llvm/lib/Support/Windows')
-rw-r--r--llvm/lib/Support/Windows/PathV2.inc57
1 files changed, 57 insertions, 0 deletions
diff --git a/llvm/lib/Support/Windows/PathV2.inc b/llvm/lib/Support/Windows/PathV2.inc
index 9c15e26b79d..cb80731c1c3 100644
--- a/llvm/lib/Support/Windows/PathV2.inc
+++ b/llvm/lib/Support/Windows/PathV2.inc
@@ -18,6 +18,36 @@
#include "Windows.h"
+using namespace llvm;
+
+namespace {
+ error_code UTF8ToUTF16(const StringRef &utf8,
+ SmallVectorImpl<wchar_t> &utf16) {
+ int len = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS,
+ utf8.begin(), utf8.size(),
+ utf16.begin(), 0);
+
+ if (len == 0)
+ return make_error_code(windows_error(::GetLastError()));
+
+ utf16.reserve(len + 1);
+ utf16.set_size(len);
+
+ len = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS,
+ utf8.begin(), utf8.size(),
+ utf16.begin(), utf16.size());
+
+ if (len == 0)
+ return make_error_code(windows_error(::GetLastError()));
+
+ // Make utf16 null terminated.
+ utf16.push_back(0);
+ utf16.pop_back();
+
+ return make_error_code(errc::success);
+ }
+}
+
namespace llvm {
namespace sys {
namespace path {
@@ -67,5 +97,32 @@ retry_cur_dir:
}
} // end namespace path
+
+namespace fs {
+
+error_code copy_file(const Twine &from, const Twine &to, copy_option copt) {
+ // Get arguments.
+ SmallString<128> from_storage;
+ SmallString<128> to_storage;
+ StringRef f = from.toStringRef(from_storage);
+ StringRef t = to.toStringRef(to_storage);
+
+ // Convert to utf-16.
+ SmallVector<wchar_t, 128> wide_from;
+ SmallVector<wchar_t, 128> wide_to;
+ if (error_code ec = UTF8ToUTF16(f, wide_from)) return ec;
+ if (error_code ec = UTF8ToUTF16(t, wide_to)) return ec;
+
+ // Copy the file.
+ BOOL res = ::CopyFileW(wide_from.begin(), wide_to.begin(),
+ copt != copy_option::overwrite_if_exists);
+
+ if (res == 0)
+ return make_error_code(windows_error(::GetLastError()));
+
+ return make_error_code(errc::success);
+}
+
+} // end namespace fs
} // end namespace sys
} // end namespace llvm
OpenPOWER on IntegriCloud