]> git.proxmox.com Git - libgit2.git/blob - src/win32/utf-conv.c
Merge remote-tracking branch 'nulltoken/topic/oid-generation' into development
[libgit2.git] / src / win32 / utf-conv.c
1 /*
2 * Copyright (C) 2009-2011 the libgit2 contributors
3 *
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
6 */
7
8 #include "common.h"
9 #include "utf-conv.h"
10
11 /*
12 * Default codepage value
13 */
14 static int _active_codepage = CP_UTF8;
15
16 void gitwin_set_codepage(unsigned int codepage)
17 {
18 _active_codepage = codepage;
19 }
20
21 unsigned int gitwin_get_codepage(void)
22 {
23 return _active_codepage;
24 }
25
26 void gitwin_set_utf8(void)
27 {
28 _active_codepage = CP_UTF8;
29 }
30
31 wchar_t* gitwin_to_utf16(const char* str)
32 {
33 wchar_t* ret;
34 int cb;
35
36 if (!str) {
37 return NULL;
38 }
39
40 cb = strlen(str) * sizeof(wchar_t);
41 if (cb == 0) {
42 ret = (wchar_t*)git__malloc(sizeof(wchar_t));
43 ret[0] = 0;
44 return ret;
45 }
46
47 /* Add space for null terminator */
48 cb += sizeof(wchar_t);
49
50 ret = (wchar_t*)git__malloc(cb);
51
52 if (MultiByteToWideChar(_active_codepage, 0, str, -1, ret, cb) == 0) {
53 git__free(ret);
54 ret = NULL;
55 }
56
57 return ret;
58 }
59
60 char* gitwin_from_utf16(const wchar_t* str)
61 {
62 char* ret;
63 int cb;
64
65 if (!str) {
66 return NULL;
67 }
68
69 cb = wcslen(str) * sizeof(char);
70 if (cb == 0) {
71 ret = (char*)git__malloc(sizeof(char));
72 ret[0] = 0;
73 return ret;
74 }
75
76 /* Add space for null terminator */
77 cb += sizeof(char);
78
79 ret = (char*)git__malloc(cb);
80
81 if (WideCharToMultiByte(_active_codepage, 0, str, -1, ret, cb, NULL, NULL) == 0) {
82 git__free(ret);
83 ret = NULL;
84 }
85
86 return ret;
87
88 }