You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
152 lines
3.7 KiB
152 lines
3.7 KiB
#include "util.h"
|
|
|
|
|
|
string ConvertToString(System::String^ str)
|
|
{
|
|
char* p = (char*)(void*)System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(str);
|
|
return p;
|
|
}
|
|
System::String ^ convert_to_cstring(const char *p)
|
|
{
|
|
return System::Runtime::InteropServices::Marshal::PtrToStringAnsi((System::IntPtr)(char *)p);
|
|
}
|
|
bool isTypeOf(tag_t objtag, const char * type_name) {
|
|
tag_t type = NULLTAG;
|
|
TCTYPE_ask_object_type(objtag, &type);
|
|
tag_t item_type = NULLTAG;
|
|
TCTYPE_find_type(type_name, "", &item_type);
|
|
bool is_type = false;
|
|
if (item_type != NULLTAG) {
|
|
logical isok = FALSE;
|
|
TCTYPE_is_type_of(type, item_type, &isok);
|
|
if (isok) {
|
|
is_type = true;
|
|
}
|
|
}
|
|
return is_type;
|
|
}
|
|
void split(std::string s, const char* delim, std::vector<std::string>* ret)
|
|
{
|
|
size_t last = 0;
|
|
size_t index = s.find(delim, last);
|
|
int size = strlen(delim);
|
|
while (index != std::string::npos) {
|
|
ret->push_back(s.substr(last, index - last));
|
|
last = index + size;
|
|
index = s.find(delim, last);
|
|
}
|
|
if (index - last > 0) {
|
|
ret->push_back(s.substr(last, index - last));
|
|
}
|
|
}
|
|
|
|
const char* newGUID()
|
|
{
|
|
static char buf[64] = { 0 };
|
|
GUID guid;
|
|
if (S_OK == ::CoCreateGuid(&guid))
|
|
{
|
|
_snprintf(buf, sizeof(buf)
|
|
, "%08X-%04X-%04x-%02X%02X-%02X%02X%02X%02X%02X%02X"
|
|
, guid.Data1
|
|
, guid.Data2
|
|
, guid.Data3
|
|
, guid.Data4[0], guid.Data4[1]
|
|
, guid.Data4[2], guid.Data4[3], guid.Data4[4], guid.Data4[5]
|
|
, guid.Data4[6], guid.Data4[7]
|
|
);
|
|
}
|
|
return (const char*)buf;
|
|
|
|
}
|
|
|
|
#ifdef _WIN32
|
|
#include <windows.h>
|
|
string GbkToUtf8(const char *src_str) {
|
|
int len = MultiByteToWideChar(CP_ACP, 0, src_str, -1, NULL, 0);
|
|
wchar_t* wstr = new wchar_t[len + 1];
|
|
memset(wstr, 0, len + 1);
|
|
MultiByteToWideChar(CP_ACP, 0, src_str, -1, wstr, len);
|
|
len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);
|
|
char* str = new char[len + 1];
|
|
memset(str, 0, len + 1);
|
|
WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str, len, NULL, NULL);
|
|
string strTemp = str;
|
|
if (wstr) delete[] wstr;
|
|
if (str) delete[] str;
|
|
return strTemp;
|
|
}
|
|
|
|
string Utf8ToGbk(const char *src_str) {
|
|
int len = MultiByteToWideChar(CP_UTF8, 0, src_str, -1, NULL, 0);
|
|
wchar_t* wszGBK = new wchar_t[len + 1];
|
|
memset(wszGBK, 0, len * 2 + 2);
|
|
MultiByteToWideChar(CP_UTF8, 0, src_str, -1, wszGBK, len);
|
|
len = WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, NULL, 0, NULL, NULL);
|
|
char* szGBK = new char[len + 1];
|
|
memset(szGBK, 0, len + 1);
|
|
WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, szGBK, len, NULL, NULL);
|
|
string strTemp(szGBK);
|
|
if (wszGBK) delete[] wszGBK;
|
|
if (szGBK) delete[] szGBK;
|
|
return strTemp;
|
|
}
|
|
#else
|
|
#include <iconv.h>
|
|
|
|
int GbkToUtf8(char *str_str, size_t src_len, char *dst_str, size_t dst_len) {
|
|
iconv_t cd;
|
|
char **pin = &str_str;
|
|
char **pout = &dst_str;
|
|
|
|
cd = iconv_open("utf8", "gbk");
|
|
if (cd == 0)
|
|
return -1;
|
|
memset(dst_str, 0, dst_len);
|
|
if (iconv(cd, pin, &src_len, pout, &dst_len) == -1)
|
|
return -1;
|
|
iconv_close(cd);
|
|
*pout = '\0';
|
|
|
|
return 0;
|
|
}
|
|
|
|
int Utf8ToGbk(char *src_str, size_t src_len, char *dst_str, size_t dst_len) {
|
|
iconv_t cd;
|
|
char **pin = &src_str;
|
|
char **pout = &dst_str;
|
|
|
|
cd = iconv_open("gbk", "utf8");
|
|
if (cd == 0)
|
|
return -1;
|
|
memset(dst_str, 0, dst_len);
|
|
if (iconv(cd, pin, &src_len, pout, &dst_len) == -1)
|
|
return -1;
|
|
iconv_close(cd);
|
|
*pout = '\0';
|
|
|
|
return 0;
|
|
}
|
|
|
|
|
|
#endif
|
|
|
|
const wchar_t* stringToWchar(const std::string& str)
|
|
{
|
|
LPCSTR pszSrc = str.c_str();
|
|
int nLen = MultiByteToWideChar(CP_ACP, 0, pszSrc, -1, NULL, 0);
|
|
if (nLen == 0)
|
|
return std::wstring(L"").c_str();
|
|
|
|
wchar_t* pwszDst = new wchar_t[nLen];
|
|
if (!pwszDst)
|
|
return std::wstring(L"").c_str();
|
|
|
|
MultiByteToWideChar(CP_ACP, 0, pszSrc, -1, pwszDst, nLen);
|
|
std::wstring wstr(pwszDst);
|
|
delete[] pwszDst;
|
|
pwszDst = NULL;
|
|
|
|
return wstr.c_str();
|
|
}
|