summaryrefslogtreecommitdiff
path: root/attache/str_utils.cpp
blob: cadb7b82be5291725fe4efb161460a2a9a5569ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include "common.h"
#include "str_utils.h"

int code_page = CP_ACP;

void set_codepage() {
	if(ServiceExists(MS_LANGPACK_GETCODEPAGE))
		code_page = CallService(MS_LANGPACK_GETCODEPAGE, 0, 0);
}

char *w2u(const wchar_t *ws) {
	if(ws) {
		int size = WideCharToMultiByte(CP_UTF8, 0, ws, -1, 0, 0, 0, 0);
		char *buff = (char *)malloc(size);
		WideCharToMultiByte(CP_UTF8, 0, ws, -1, buff, 2048, 0, 0);
		return buff;
	} else 
		return 0;
}

wchar_t *u2w(const char *utfs) {
	if(utfs) {
		int size = MultiByteToWideChar(CP_UTF8, 0, utfs, -1, 0, 0);
		wchar_t *buff = (wchar_t *)malloc(size * sizeof(wchar_t));
		MultiByteToWideChar(CP_UTF8, 0, utfs, -1, buff, size);
		return buff;
	} else
		return 0;
}

wchar_t *a2w(const char *as) {
	int code_page = CP_ACP;
	if(ServiceExists(MS_LANGPACK_GETCODEPAGE)) code_page = CallService(MS_LANGPACK_GETCODEPAGE, 0, 0);
	int size = MultiByteToWideChar(code_page, 0, as, -1, 0, 0);
	wchar_t *buff = (wchar_t *)malloc(size * sizeof(wchar_t));
	MultiByteToWideChar(code_page, 0, as, -1, buff, size);
	return buff;
}

char *w2a(const wchar_t *ws) {
	int code_page = CP_ACP;
	if(ServiceExists(MS_LANGPACK_GETCODEPAGE)) code_page = CallService(MS_LANGPACK_GETCODEPAGE, 0, 0);
	int size = WideCharToMultiByte(code_page, 0, ws, -1, 0, 0, 0, 0);
	char *buff = (char *)malloc(size);
	WideCharToMultiByte(code_page, 0, ws, -1, buff, 2048, 0, 0);
	return buff;
}

char *t2a(const TCHAR *ts) {
#ifdef _UNICODE
	return w2a(ts);
#else
	return _strdup(ts);
#endif
}

TCHAR *a2t(const char *as) {
#ifdef _UNICODE
	return a2w(as);
#else
	return _strdup(as);
#endif
}

TCHAR *u2t(const char *utfs) {
#ifdef _UNICODE
	return u2w(utfs);
#else
	wchar_t *ws = u2w(utfs);
	char *ret = w2a(ws);
	free(ws);
	return ret;
#endif
}

char *t2u(const TCHAR *ts) {
#ifdef _UNICODE
	return w2u(ts);
#else
	wchar_t *ws = a2w(ts);
	char *ret = w2u(ws);
	free(ws);
	return ret;
#endif
}

char *u2a(const char *utfs) {
	wchar_t *ws = u2w(utfs);
	char *ret = w2a(ws);
	free(ws);
	return ret;
}

char *a2u(const char *as) {
	wchar_t *ws = a2w(as);
	char *ret = w2u(ws);
	free(ws);
	return ret;
}