diff options
author | Carlos Martín Nieto <carlos@cmartin.tk> | 2011-11-18 22:45:56 +0100 |
---|---|---|
committer | Carlos Martín Nieto <carlos@cmartin.tk> | 2011-11-18 22:45:56 +0100 |
commit | bdd31dd5e832126b2f22fccbe244a1106c241ab0 (patch) | |
tree | b08a2ad0fad57131563aa8c071221bea241128c1 /src/win32/utf-conv.c | |
parent | 277b7efe493887081ce1dafd91199d0ee9f676c9 (diff) | |
parent | e4c93a392763a006d11e1c1dd01c12f85498dad5 (diff) | |
download | libgit2-error-handling.tar.gz |
Merge branch 'development' into error-handlingerror-handling
The code in this branch has been modified so it works with the global
state introduced in development.
Diffstat (limited to 'src/win32/utf-conv.c')
-rw-r--r-- | src/win32/utf-conv.c | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/src/win32/utf-conv.c b/src/win32/utf-conv.c new file mode 100644 index 000000000..b41c78f92 --- /dev/null +++ b/src/win32/utf-conv.c @@ -0,0 +1,88 @@ +/* + * Copyright (C) 2009-2011 the libgit2 contributors + * + * 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 "utf-conv.h" + +/* + * Default codepage value + */ +static int _active_codepage = CP_UTF8; + +void gitwin_set_codepage(unsigned int codepage) +{ + _active_codepage = codepage; +} + +unsigned int gitwin_get_codepage(void) +{ + return _active_codepage; +} + +void gitwin_set_utf8(void) +{ + _active_codepage = CP_UTF8; +} + +wchar_t* gitwin_to_utf16(const char* str) +{ + wchar_t* ret; + int cb; + + if (!str) { + return NULL; + } + + cb = strlen(str) * sizeof(wchar_t); + if (cb == 0) { + ret = (wchar_t*)git__malloc(sizeof(wchar_t)); + ret[0] = 0; + return ret; + } + + /* Add space for null terminator */ + cb += sizeof(wchar_t); + + ret = (wchar_t*)git__malloc(cb); + + if (MultiByteToWideChar(_active_codepage, 0, str, -1, ret, cb) == 0) { + git__free(ret); + ret = NULL; + } + + return ret; +} + +char* gitwin_from_utf16(const wchar_t* str) +{ + char* ret; + int cb; + + if (!str) { + return NULL; + } + + cb = wcslen(str) * sizeof(char); + if (cb == 0) { + ret = (char*)git__malloc(sizeof(char)); + ret[0] = 0; + return ret; + } + + /* Add space for null terminator */ + cb += sizeof(char); + + ret = (char*)git__malloc(cb); + + if (WideCharToMultiByte(_active_codepage, 0, str, -1, ret, cb, NULL, NULL) == 0) { + git__free(ret); + ret = NULL; + } + + return ret; + +} |