]> git.proxmox.com Git - libgit2.git/blob - src/win32/utf-conv.c
Fix missing prototype warning in utf-conv.c
[libgit2.git] / src / win32 / utf-conv.c
1 /*
2 * Copyright (C) 2009-2012 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 #include "git2/windows.h"
11
12 /*
13 * Default codepage value
14 */
15 static int _active_codepage = CP_UTF8;
16
17 void gitwin_set_codepage(unsigned int codepage)
18 {
19 _active_codepage = codepage;
20 }
21
22 unsigned int gitwin_get_codepage(void)
23 {
24 return _active_codepage;
25 }
26
27 void gitwin_set_utf8(void)
28 {
29 _active_codepage = CP_UTF8;
30 }
31
32 wchar_t* gitwin_to_utf16(const char* str)
33 {
34 wchar_t* ret;
35 int cb;
36
37 if (!str) {
38 return NULL;
39 }
40
41 cb = strlen(str) * sizeof(wchar_t);
42 if (cb == 0) {
43 ret = (wchar_t*)git__malloc(sizeof(wchar_t));
44 ret[0] = 0;
45 return ret;
46 }
47
48 /* Add space for null terminator */
49 cb += sizeof(wchar_t);
50
51 ret = (wchar_t*)git__malloc(cb);
52
53 if (MultiByteToWideChar(_active_codepage, 0, str, -1, ret, cb) == 0) {
54 git__free(ret);
55 ret = NULL;
56 }
57
58 return ret;
59 }
60
61 int gitwin_append_utf16(wchar_t *buffer, const char *str, size_t len)
62 {
63 return MultiByteToWideChar(_active_codepage, 0, str, -1, buffer, len);
64 }
65
66 char* gitwin_from_utf16(const wchar_t* str)
67 {
68 char* ret;
69 int cb;
70
71 if (!str) {
72 return NULL;
73 }
74
75 cb = wcslen(str) * sizeof(char);
76 if (cb == 0) {
77 ret = (char*)git__malloc(sizeof(char));
78 ret[0] = 0;
79 return ret;
80 }
81
82 /* Add space for null terminator */
83 cb += sizeof(char);
84
85 ret = (char*)git__malloc(cb);
86
87 if (WideCharToMultiByte(_active_codepage, 0, str, -1, ret, cb, NULL, NULL) == 0) {
88 git__free(ret);
89 ret = NULL;
90 }
91
92 return ret;
93
94 }