Menu

[r9]: / trunk / src / UnicodeUtils.cpp  Maximize  Restore  History

Download this file

169 lines (150 with data), 4.4 kB

  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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include "StdAfx.h"
#include "unicodeutils.h"
CUnicodeUtils::CUnicodeUtils(void)
{
}
CUnicodeUtils::~CUnicodeUtils(void)
{
}
#ifdef UNICODE
std::string CUnicodeUtils::StdGetUTF8(const wide_string& wide)
{
int len = (int)wide.size();
if (len==0)
return std::string();
int size = len*4;
char * narrow = new char[size];
int ret = WideCharToMultiByte(CP_UTF8, 0, wide.c_str(), len, narrow, size-1, NULL, NULL);
narrow[ret] = 0;
std::string sRet = std::string(narrow);
delete narrow;
return sRet;
}
wide_string CUnicodeUtils::StdGetUnicode(const std::string& multibyte)
{
int len = (int)multibyte.size();
if (len==0)
return wide_string();
int size = len*4;
wchar_t * wide = new wchar_t[size];
int ret = MultiByteToWideChar(CP_UTF8, 0, multibyte.c_str(), len, wide, size - 1);
wide[ret] = 0;
wide_string sRet = wide_string(wide);
delete wide;
return sRet;
}
#endif
std::string WideToMultibyte(const wide_string& wide)
{
char * narrow = new char[wide.length()*3+2];
BOOL defaultCharUsed;
int ret = (int)WideCharToMultiByte(CP_ACP, 0, wide.c_str(), (int)wide.size(), narrow, (int)wide.length()*3 - 1, ".", &defaultCharUsed);
narrow[ret] = 0;
std::string str = narrow;
delete[] narrow;
return str;
}
std::string WideToUTF8(const wide_string& wide)
{
char * narrow = new char[wide.length()*3+2];
int ret = (int)WideCharToMultiByte(CP_UTF8, 0, wide.c_str(), (int)wide.size(), narrow, (int)wide.length()*3 - 1, NULL, NULL);
narrow[ret] = 0;
std::string str = narrow;
delete[] narrow;
return str;
}
wide_string MultibyteToWide(const std::string& multibyte)
{
size_t length = multibyte.length();
if (length == 0)
return wide_string();
wchar_t * wide = new wchar_t[multibyte.length()*2+2];
if (wide == NULL)
return wide_string();
int ret = (int)MultiByteToWideChar(CP_ACP, 0, multibyte.c_str(), (int)multibyte.size(), wide, (int)length*2 - 1);
wide[ret] = 0;
wide_string str = wide;
delete[] wide;
return str;
}
wide_string UTF8ToWide(const std::string& multibyte)
{
size_t length = multibyte.length();
if (length == 0)
return wide_string();
wchar_t * wide = new wchar_t[length*2+2];
if (wide == NULL)
return wide_string();
int ret = (int)MultiByteToWideChar(CP_UTF8, 0, multibyte.c_str(), (int)multibyte.size(), wide, (int)length*2 - 1);
wide[ret] = 0;
wide_string str = wide;
delete[] wide;
return str;
}
#ifdef UNICODE
stdstring UTF8ToString(const std::string& string) {return UTF8ToWide(string);}
std::string StringToUTF8(const stdstring& string) {return WideToUTF8(string);}
#else
stdstring UTF8ToString(const std::string& string) {return WideToMultibyte(UTF8ToWide(string));}
std::string StringToUTF8(const stdstring& string) {return WideToUTF8(MultibyteToWide(string));}
#endif
#pragma warning(push)
#pragma warning(disable: 4200)
struct STRINGRESOURCEIMAGE
{
WORD nLength;
WCHAR achString[];
};
#pragma warning(pop) // C4200
int LoadStringEx(HINSTANCE hInstance, UINT uID, LPTSTR lpBuffer, int nBufferMax, WORD wLanguage)
{
const STRINGRESOURCEIMAGE* pImage;
const STRINGRESOURCEIMAGE* pImageEnd;
ULONG nResourceSize;
HGLOBAL hGlobal;
UINT iIndex;
#ifndef UNICODE
BOOL defaultCharUsed;
#endif
int ret;
if (lpBuffer == NULL)
return 0;
lpBuffer[0] = 0;
HRSRC hResource = FindResourceEx(hInstance, RT_STRING, MAKEINTRESOURCE(((uID>>4)+1)), wLanguage);
if (!hResource)
{
//try the default language before giving up!
hResource = FindResource(hInstance, MAKEINTRESOURCE(((uID>>4)+1)), RT_STRING);
if (!hResource)
return 0;
}
hGlobal = LoadResource(hInstance, hResource);
if (!hGlobal)
return 0;
pImage = (const STRINGRESOURCEIMAGE*)::LockResource(hGlobal);
if(!pImage)
return 0;
nResourceSize = ::SizeofResource(hInstance, hResource);
pImageEnd = (const STRINGRESOURCEIMAGE*)(LPBYTE(pImage)+nResourceSize);
iIndex = uID&0x000f;
while ((iIndex > 0) && (pImage < pImageEnd))
{
pImage = (const STRINGRESOURCEIMAGE*)(LPBYTE(pImage)+(sizeof(STRINGRESOURCEIMAGE)+(pImage->nLength*sizeof(WCHAR))));
iIndex--;
}
if (pImage >= pImageEnd)
return 0;
if (pImage->nLength == 0)
return 0;
#ifdef UNICODE
ret = pImage->nLength;
if (ret > nBufferMax)
ret = nBufferMax;
wcsncpy_s((wchar_t *)lpBuffer, nBufferMax, pImage->achString, ret);
lpBuffer[ret] = 0;
#else
ret = WideCharToMultiByte(CP_ACP, 0, pImage->achString, pImage->nLength, (LPSTR)lpBuffer, nBufferMax-1, ".", &defaultCharUsed);
lpBuffer[ret] = 0;
#endif
return ret;
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.