diff options
author | Philip Kelley <phkelley@hotmail.com> | 2014-04-19 18:05:31 -0400 |
---|---|---|
committer | Philip Kelley <phkelley@hotmail.com> | 2014-04-19 21:50:44 -0400 |
commit | c2c8161541e54689926ec1f463569d5d1b975503 (patch) | |
tree | d3d1f7096c6449a9d9e19747508ad08cc910d8fe /tests/clar_libgit2.c | |
parent | bfc50f83f829afe52c10f615c4f7efa478a83098 (diff) | |
download | libgit2-c2c8161541e54689926ec1f463569d5d1b975503.tar.gz |
Win32: UTF-8 <-> WCHAR conversion overhaul
Diffstat (limited to 'tests/clar_libgit2.c')
-rw-r--r-- | tests/clar_libgit2.c | 53 |
1 files changed, 23 insertions, 30 deletions
diff --git a/tests/clar_libgit2.c b/tests/clar_libgit2.c index 90e53c5e6..6f6143dad 100644 --- a/tests/clar_libgit2.c +++ b/tests/clar_libgit2.c @@ -59,47 +59,40 @@ void cl_git_rewritefile(const char *path, const char *content) char *cl_getenv(const char *name) { - git_win32_path name_utf16; - DWORD alloc_len; - wchar_t *value_utf16; - char *value_utf8; + wchar_t *wide_name, *wide_value; + char *utf8_value = NULL; + DWORD value_len; - git_win32_path_from_c(name_utf16, name); - alloc_len = GetEnvironmentVariableW(name_utf16, NULL, 0); - if (alloc_len <= 0) - return NULL; + cl_assert(git__utf8_to_16_alloc(&wide_name, name) >= 0); - cl_assert(value_utf16 = git__calloc(alloc_len, sizeof(wchar_t))); + value_len = GetEnvironmentVariableW(wide_name, NULL, 0); - GetEnvironmentVariableW(name_utf16, value_utf16, alloc_len); - - alloc_len = alloc_len * 4 + 1; /* worst case UTF16->UTF8 growth */ - cl_assert(value_utf8 = git__calloc(alloc_len, 1)); - - git__utf16_to_8(value_utf8, alloc_len, value_utf16); - - git__free(value_utf16); + if (value_len) { + cl_assert(wide_value = git__malloc(value_len * sizeof(wchar_t))); + cl_assert(GetEnvironmentVariableW(wide_name, wide_value, value_len)); + cl_assert(git__utf16_to_8_alloc(&utf8_value, wide_value) >= 0); + git__free(wide_value); + } - return value_utf8; + git__free(wide_name); + return utf8_value; } int cl_setenv(const char *name, const char *value) { - git_win32_path name_utf16; - git_win32_path value_utf16; + wchar_t *wide_name, *wide_value; - git_win32_path_from_c(name_utf16, name); + cl_assert(git__utf8_to_16_alloc(&wide_name, name) >= 0); if (value) { - git_win32_path_from_c(value_utf16, value); - cl_assert(SetEnvironmentVariableW(name_utf16, value_utf16)); + cl_assert(git__utf8_to_16_alloc(&wide_value, value) >= 0); + cl_assert(SetEnvironmentVariableW(wide_name, wide_value)); } else { /* Windows XP returns 0 (failed) when passing NULL for lpValue when - * lpName does not exist in the environment block. This behavior - * seems to have changed in later versions. Don't check return value - * of SetEnvironmentVariable when passing NULL for lpValue. - */ - SetEnvironmentVariableW(name_utf16, NULL); + * lpName does not exist in the environment block. This behavior + * seems to have changed in later versions. Don't check the return value + * of SetEnvironmentVariable when passing NULL for lpValue. */ + SetEnvironmentVariableW(wide_name, NULL); } return 0; @@ -115,8 +108,8 @@ int cl_rename(const char *source, const char *dest) git_win32_path dest_utf16; unsigned retries = 1; - git_win32_path_from_c(source_utf16, source); - git_win32_path_from_c(dest_utf16, dest); + cl_assert(git_win32_path_from_utf8(source_utf16, source) >= 0); + cl_assert(git_win32_path_from_utf8(dest_utf16, dest) >= 0); while (!MoveFileW(source_utf16, dest_utf16)) { /* Only retry if the error is ERROR_ACCESS_DENIED; |