summaryrefslogtreecommitdiff
path: root/src/win32
diff options
context:
space:
mode:
authorVicent Martí <vicent@github.com>2013-02-01 15:57:31 -0800
committerVicent Martí <vicent@github.com>2013-02-01 15:57:31 -0800
commite963166019de3dca72d18bd4ee8f81519e97fdf3 (patch)
treedd79c4cc5fdadd74665014881abe337681d5d872 /src/win32
parentdb37d3de7957a9189d29c64dfc0830e93084202e (diff)
parentc70455c75e30bf2a4cc5abdf4229d12d0e6cf159 (diff)
downloadlibgit2-e963166019de3dca72d18bd4ee8f81519e97fdf3.tar.gz
Merge pull request #1303 from csware/win32_consistent_error_encoding
Win32: Make sure error messages are consistently UTF-8 encoded
Diffstat (limited to 'src/win32')
-rw-r--r--src/win32/error.c41
-rw-r--r--src/win32/error.h13
2 files changed, 54 insertions, 0 deletions
diff --git a/src/win32/error.c b/src/win32/error.c
new file mode 100644
index 000000000..3851ff099
--- /dev/null
+++ b/src/win32/error.c
@@ -0,0 +1,41 @@
+/*
+ * Copyright (C) the libgit2 contributors. All rights reserved.
+ *
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
+ * a Linking Exception. For full terms see the included COPYING file.
+ */
+
+#include "common.h"
+#include "error.h"
+
+char *git_win32_get_error_message(DWORD error_code)
+{
+ LPWSTR lpMsgBuf = NULL;
+
+ if (!error_code)
+ return NULL;
+
+ if (FormatMessageW(
+ FORMAT_MESSAGE_ALLOCATE_BUFFER |
+ FORMAT_MESSAGE_FROM_SYSTEM |
+ FORMAT_MESSAGE_IGNORE_INSERTS,
+ NULL, error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
+ (LPWSTR)&lpMsgBuf, 0, NULL)) {
+ int utf8_size = WideCharToMultiByte(CP_UTF8, 0, lpMsgBuf, -1, NULL, 0, NULL, NULL);
+
+ char *lpMsgBuf_utf8 = git__malloc(utf8_size * sizeof(char));
+ if (lpMsgBuf_utf8 == NULL) {
+ LocalFree(lpMsgBuf);
+ return NULL;
+ }
+ if (!WideCharToMultiByte(CP_UTF8, 0, lpMsgBuf, -1, lpMsgBuf_utf8, utf8_size, NULL, NULL)) {
+ LocalFree(lpMsgBuf);
+ git__free(lpMsgBuf_utf8);
+ return NULL;
+ }
+
+ LocalFree(lpMsgBuf);
+ return lpMsgBuf_utf8;
+ }
+ return NULL;
+}
diff --git a/src/win32/error.h b/src/win32/error.h
new file mode 100644
index 000000000..12947a2e6
--- /dev/null
+++ b/src/win32/error.h
@@ -0,0 +1,13 @@
+/*
+ * Copyright (C) the libgit2 contributors. All rights reserved.
+ *
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
+ * a Linking Exception. For full terms see the included COPYING file.
+ */
+
+#ifndef INCLUDE_git_win32_error_h__
+#define INCLUDE_git_win32_error_h__
+
+extern char *git_win32_get_error_message(DWORD error_code);
+
+#endif