soui 5.0.0.1
Soui5 Doc
 
Loading...
Searching...
No Matches
strcpcvt.cpp
1#include "string/strcpcvt.h"
2
3SNSBEGIN
4
5SStringW SStrCpCvt::CvtW2W( const SStringW &str,unsigned int)
6{
7 return str;
8}
9
10SStringA SStrCpCvt::CvtA2A( const SStringA & str,unsigned int cpFrom/*=CP_UTF8*/,unsigned int cpTo/*=CP_ACP*/ )
11{
12 if(cpTo==cpFrom)
13 return str;
14 SStringW strw=CvtA2W(str,cpFrom);
15 return CvtW2A(strw,cpTo);
16}
17
18SStringW SStrCpCvt::CvtA2W( const SStringA & str,unsigned int cp/*=CP_ACP*/,unsigned int cp2/*=0*/ )
19{
20 UNREFERENCED_PARAMETER(cp2);
21 wchar_t szBuf[1024];
22 int nRet=MultiByteToWideChar(cp,0,str,str.GetLength(),szBuf,1024);
23 if(nRet>0)
24 {
25 return SStringW(szBuf,nRet);
26 }
27 if(GetLastError()==ERROR_INSUFFICIENT_BUFFER)
28 {
29 int nRet=MultiByteToWideChar(cp,0,str,str.GetLength(),NULL,0);
30 if(nRet>0)
31 {
32 wchar_t *pBuf=new wchar_t[nRet];
33 MultiByteToWideChar(cp,0,str,str.GetLength(),pBuf,nRet);
34 SStringW strRet(pBuf,nRet);
35 delete []pBuf;
36 return strRet;
37 }
38 }
39 return L"";
40}
41
42SStringA SStrCpCvt::CvtW2A( const SStringW & str,unsigned int cp/*=CP_ACP*/ )
43{
44 char szBuf[1024];
45 int nRet=WideCharToMultiByte(cp,0,str,str.GetLength(),szBuf,1024,NULL,NULL);
46 if(nRet>0) return SStringA(szBuf,nRet);
47 if(GetLastError()==ERROR_INSUFFICIENT_BUFFER)
48 {
49 int nRet=WideCharToMultiByte(cp,0,str,str.GetLength(),NULL,0,NULL,NULL);
50 if(nRet>0)
51 {
52 char *pBuf=new char[nRet];
53 WideCharToMultiByte(cp,0,str,str.GetLength(),pBuf,nRet,NULL,NULL);
54 SStringA strRet(pBuf,nRet);
55 delete []pBuf;
56 return strRet;
57 }
58 }
59 return "";
60}
61
62
63SNSEND
static SStringA CvtA2A(const SStringA &str, unsigned int cpFrom=CP_UTF8, unsigned int cpTo=0)
Converts a multi-byte string from one code page to another multi-byte code page.
Definition strcpcvt.cpp:10
static SStringA CvtW2A(const SStringW &str, unsigned int cp=0)
Converts a wide string (UTF-16) to a multi-byte string (ANSI or specified code page).
Definition strcpcvt.cpp:42
static SStringW CvtA2W(const SStringA &str, unsigned int cp=0, unsigned int cp2=0)
Converts a multi-byte string (ANSI or specified code page) to a wide string (UTF-16).
Definition strcpcvt.cpp:18
static SStringW CvtW2W(const SStringW &str, unsigned int cp=0)
Converts a wide string (UTF-16) to another wide string (UTF-16) with a specified code page.
Definition strcpcvt.cpp:5
A class representing an ASCII string.
Definition sstringa.h:96
int GetLength() SCONST
Retrieves the length of the string.
A class representing an ASCII string.
Definition sstringw.h:96
int GetLength() SCONST
Retrieves the length of the string.