forked from pyrite-dev/milsko
add ui charset converter and stuff
This commit is contained in:
parent
2369b96091
commit
5ff982d106
9 changed files with 176 additions and 25 deletions
95
src/abstract/charset.c
Normal file
95
src/abstract/charset.c
Normal file
|
|
@ -0,0 +1,95 @@
|
|||
#include <Mw/Milsko.h>
|
||||
|
||||
#ifndef CP_UTF8
|
||||
#define CP_UTF8 65001
|
||||
#endif
|
||||
|
||||
char* MwACPToUTF8(const char* input){
|
||||
#if defined(_WIN32) && defined(MW_HAS_WCHAR)
|
||||
int mbbytes = (strlen(input) + 1) * 4;
|
||||
int wbytes = 0;
|
||||
int len;
|
||||
|
||||
wchar_t* wout;
|
||||
char* mbout = malloc(mbbytes);
|
||||
|
||||
wbytes = MultiByteToWideChar(CP_ACP, 0, input, strlen(input), NULL, 0) * sizeof(wchar_t);
|
||||
if(wbytes == 0){
|
||||
free(mbout);
|
||||
return MwStringDuplicate(input);
|
||||
}
|
||||
|
||||
wout = malloc(wbytes);
|
||||
len = wbytes / sizeof(wchar_t);
|
||||
|
||||
memset(wout, 0, wbytes);
|
||||
memset(mbout, 0, mbbytes);
|
||||
|
||||
len = MultiByteToWideChar(CP_ACP, 0, input, strlen(input), wout, len);
|
||||
if(len == 0){
|
||||
free(mbout);
|
||||
free(wout);
|
||||
return MwStringDuplicate(input);
|
||||
}
|
||||
|
||||
len = WideCharToMultiByte(CP_UTF8, 0, wout, len, mbout, mbbytes, 0, 0);
|
||||
if(len == 0){
|
||||
free(mbout);
|
||||
free(wout);
|
||||
return MwStringDuplicate(input);
|
||||
}
|
||||
|
||||
mbout[len] = 0;
|
||||
|
||||
free(wout);
|
||||
|
||||
return mbout;
|
||||
#else
|
||||
return MwStringDuplicate(input);
|
||||
#endif
|
||||
}
|
||||
|
||||
char* MwUTF8ToACP(const char* input){
|
||||
#if defined(_WIN32) && defined(MW_HAS_WCHAR)
|
||||
int mbbytes = (strlen(input) + 1) * 4;
|
||||
int wbytes = 0;
|
||||
int len;
|
||||
|
||||
wchar_t* wout;
|
||||
char* mbout = malloc(mbbytes);
|
||||
|
||||
wbytes = MultiByteToWideChar(CP_UTF8, 0, input, strlen(input), NULL, 0) * sizeof(wchar_t);
|
||||
if(wbytes == 0){
|
||||
free(mbout);
|
||||
return MwStringDuplicate(input);
|
||||
}
|
||||
|
||||
wout = malloc(wbytes);
|
||||
len = wbytes / sizeof(wchar_t);
|
||||
|
||||
memset(wout, 0, wbytes);
|
||||
memset(mbout, 0, mbbytes);
|
||||
|
||||
len = MultiByteToWideChar(CP_UTF8, 0, input, strlen(input), wout, len);
|
||||
if(len == 0){
|
||||
free(mbout);
|
||||
free(wout);
|
||||
return MwStringDuplicate(input);
|
||||
}
|
||||
|
||||
len = WideCharToMultiByte(CP_ACP, 0, wout, len, mbout, mbbytes, 0, 0);
|
||||
if(len == 0){
|
||||
free(mbout);
|
||||
free(wout);
|
||||
return MwStringDuplicate(input);
|
||||
}
|
||||
|
||||
mbout[len] = 0;
|
||||
|
||||
free(wout);
|
||||
|
||||
return mbout;
|
||||
#else
|
||||
return MwStringDuplicate(input);
|
||||
#endif
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue