2007-01-25 12:57:44 +00:00
|
|
|
#include "Platform.h"
|
|
|
|
#include "winchar.h"
|
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
#include <stdexcept>
|
|
|
|
|
|
|
|
using std::runtime_error;
|
|
|
|
|
2007-01-25 13:04:52 +00:00
|
|
|
WCHAR *winchar_fromansi(const char* s, unsigned int codepage/*=CP_ACP*/)
|
|
|
|
{
|
|
|
|
int l = MultiByteToWideChar(codepage, 0, s, -1, 0, 0);
|
2007-01-25 12:57:44 +00:00
|
|
|
if (l == 0)
|
|
|
|
throw runtime_error("Unicode conversion failed");
|
|
|
|
|
|
|
|
WCHAR *ws = new WCHAR[l + 1];
|
|
|
|
|
2007-01-25 13:04:52 +00:00
|
|
|
if (MultiByteToWideChar(codepage, 0, s, -1, ws, l + 1) == 0)
|
2007-01-25 12:57:44 +00:00
|
|
|
throw runtime_error("Unicode conversion failed");
|
|
|
|
|
|
|
|
return ws;
|
|
|
|
}
|
|
|
|
|
2007-01-25 13:04:52 +00:00
|
|
|
char *winchar_toansi(const WCHAR* ws, unsigned int codepage/*=CP_ACP*/)
|
2007-01-25 12:57:44 +00:00
|
|
|
{
|
2007-01-25 13:04:52 +00:00
|
|
|
int l = WideCharToMultiByte(codepage, 0, ws, -1, 0, 0, 0, 0);
|
2007-01-25 12:57:44 +00:00
|
|
|
if (l == 0)
|
|
|
|
throw runtime_error("Unicode conversion failed");
|
|
|
|
|
|
|
|
char *s = new char[l + 1];
|
|
|
|
|
2007-01-25 13:04:52 +00:00
|
|
|
if (WideCharToMultiByte(codepage, 0, ws, -1, s, l + 1, 0, 0) == 0)
|
2007-01-25 12:57:44 +00:00
|
|
|
throw runtime_error("Unicode conversion failed");
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
WCHAR *winchar_strcpy(WCHAR *ws1, const WCHAR *ws2)
|
|
|
|
{
|
|
|
|
WCHAR *ret = ws1;
|
|
|
|
|
|
|
|
while (*ws2)
|
|
|
|
{
|
|
|
|
*ws1++ = *ws2++;
|
|
|
|
}
|
|
|
|
|
|
|
|
*ws1 = 0;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
WCHAR *winchar_strncpy(WCHAR *ws1, const WCHAR *ws2, size_t n)
|
|
|
|
{
|
|
|
|
WCHAR *ret = ws1;
|
|
|
|
|
|
|
|
while (n && *ws2)
|
|
|
|
{
|
|
|
|
*ws1++ = *ws2++;
|
|
|
|
n--;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (n--)
|
|
|
|
{
|
|
|
|
*ws1++ = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t winchar_strlen(WCHAR *ws)
|
|
|
|
{
|
|
|
|
size_t len = 0;
|
|
|
|
|
|
|
|
while (*ws++)
|
|
|
|
{
|
|
|
|
len++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
int winchar_strcmp(const WCHAR *ws1, const WCHAR *ws2)
|
|
|
|
{
|
2007-01-25 14:07:29 +00:00
|
|
|
int diff = 0;
|
2007-01-25 12:57:44 +00:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2007-01-25 14:07:29 +00:00
|
|
|
diff = static_cast<int>(*ws1) - static_cast<int>(*ws2);
|
2007-01-25 12:57:44 +00:00
|
|
|
}
|
2007-01-25 14:07:29 +00:00
|
|
|
while (*ws1++ && *ws2++ && !diff);
|
2007-01-25 12:57:44 +00:00
|
|
|
|
2007-01-25 14:07:29 +00:00
|
|
|
return diff;
|
2007-01-25 12:57:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int winchar_stoi(const WCHAR *ws)
|
|
|
|
{
|
|
|
|
char *s = winchar_toansi(ws);
|
|
|
|
|
|
|
|
int ret = atoi(s);
|
|
|
|
|
|
|
|
delete [] s;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|