diff options
author | Edward Thomson <ethomson@microsoft.com> | 2015-04-29 18:12:51 -0400 |
---|---|---|
committer | Edward Thomson <ethomson@microsoft.com> | 2015-05-01 12:31:44 -0400 |
commit | cd39e4e2f3cc27a2976c9a8e0058f9e32d1f1c8f (patch) | |
tree | 09d8dc56bccd21b408ce049e13276d16d6493fd4 /src/win32/buffer.c | |
parent | f63a1b729bc74e1e72f80f75843b8f2042f3f81f (diff) | |
download | libgit2-cd39e4e2f3cc27a2976c9a8e0058f9e32d1f1c8f.tar.gz |
git_buf_put_w: introduce utf16->utf8 conversion
Diffstat (limited to 'src/win32/buffer.c')
-rw-r--r-- | src/win32/buffer.c | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/src/win32/buffer.c b/src/win32/buffer.c new file mode 100644 index 000000000..74950189e --- /dev/null +++ b/src/win32/buffer.c @@ -0,0 +1,55 @@ +/* + * 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 "buffer.h" +#include "../buffer.h" +#include "utf-conv.h" + +GIT_INLINE(int) handle_wc_error(void) +{ + if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) + errno = ENAMETOOLONG; + else + errno = EINVAL; + + return -1; +} + +int git_buf_put_w(git_buf *buf, const wchar_t *string_w, size_t len_w) +{ + int utf8_len, utf8_write_len; + size_t new_size; + + if (!len_w) + return 0; + + assert(string_w); + + /* Measure the string necessary for conversion */ + if ((utf8_len = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, string_w, len_w, NULL, 0, NULL, NULL)) == 0) + return 0; + + assert(utf8_len > 0); + + GITERR_CHECK_ALLOC_ADD(&new_size, buf->size, (size_t)utf8_len); + GITERR_CHECK_ALLOC_ADD(&new_size, new_size, 1); + + if (git_buf_grow(buf, new_size) < 0) + return -1; + + if ((utf8_write_len = WideCharToMultiByte( + CP_UTF8, WC_ERR_INVALID_CHARS, string_w, len_w, &buf->ptr[buf->size], utf8_len, NULL, NULL)) == 0) + return handle_wc_error(); + + assert(utf8_write_len == utf8_len); + + buf->size += utf8_write_len; + buf->ptr[buf->size] = '\0'; + return 0; +} + |