📄 stringconv.cpp
字号:
#include "StdAfx.h"
#include "StringConv.h"
// CStringConv::CStringConv
//
// Default constructor
//
CStringConv::CStringConv(void)
: m_pszA (NULL),
m_pszW (NULL)
{
}
// CStringConv::CStringConv
//
// Constructs an object from an ANSI string
//
CStringConv::CStringConv(const char* pszA)
: m_pszA (NULL),
m_pszW (NULL)
{
SetString(pszA);
}
// CStringConv::CStringConv
//
// Constructs an object from a UNICODE string
//
CStringConv::CStringConv(const wchar_t* pszW)
: m_pszA (NULL),
m_pszW (NULL)
{
SetString(pszW);
}
// CStringConv::CStringConv
//
// Copy constructor (note that the arg is non const)
//
CStringConv::CStringConv(CStringConv &str)
{
SetString(str.GetStringW());
}
// CStringConv::~CStringConv
//
// Destructor
//
CStringConv::~CStringConv(void)
{
Clear();
}
// CStringConv::operator =
//
// Assigns an ANSI string
//
CStringConv& CStringConv::operator =(const char *pszA)
{
SetString(pszA);
return *this;
}
// CStringConv::operator =
//
// Assigns a UNICODE string
//
CStringConv& CStringConv::operator =(const wchar_t *pszW)
{
SetString(pszW);
return *this;
}
// CStringConv::operator =
//
// Copies two objects
//
CStringConv& CStringConv::operator =(CStringConv &str)
{
if(this != &str)
SetString(str.GetStringW());
return *this;
}
// CStringConv::Clear
//
// Clears the internal buffers
//
void CStringConv::Clear()
{
if(m_pszA != NULL)
delete [] m_pszA;
if(m_pszW != NULL)
delete [] m_pszW;
m_pszA = NULL;
m_pszW = NULL;
}
// CStringConv::SetString
//
// Sets a multi-byte character string
//
bool CStringConv::SetString(const char *pszA)
{
size_t nSize;
Clear();
nSize = strlen(pszA) + 1;
m_pszA = new char[nSize];
if(m_pszA == NULL)
return false;
strcpy(m_pszA, pszA);
return true;
}
// CStringConv::SetString
//
// Sets a UNICODE character string
//
bool CStringConv::SetString(const wchar_t *pszW)
{
size_t nSize;
Clear();
nSize = wcslen(pszW) + 1;
m_pszW = new wchar_t[nSize];
if(m_pszW == NULL)
return false;
wcscpy(m_pszW, pszW);
return true;
}
// CStringConv::GetStringA
//
// Gets the string as a muti-byte character string
//
const char* CStringConv::GetStringA()
{
if(m_pszA == NULL)
{
size_t nSizeA;
size_t nSizeW;
if(m_pszW == NULL)
return NULL;
nSizeW = wcslen(m_pszW);
nSizeA = wcstombs(NULL, m_pszW, nSizeW) + 1;
m_pszA = new char[nSizeA];
if(m_pszA == NULL)
return NULL;
memset(m_pszA, 0, nSizeA * sizeof(char));
wcstombs(m_pszA, m_pszW, nSizeW);
}
return m_pszA;
}
// CStringConv::GetStringW
//
// Gets ths string as a UNICODE string
//
const wchar_t* CStringConv::GetStringW()
{
if(m_pszW == NULL)
{
size_t nSizeA;
size_t nSizeW;
if(m_pszA == NULL)
return NULL;
nSizeA = strlen(m_pszA);
nSizeW = mbstowcs(NULL, m_pszA, nSizeA) + 1;
m_pszW = new wchar_t[nSizeW];
if(m_pszW == NULL)
return NULL;
memset(m_pszW, 0, nSizeW * sizeof(wchar_t));
mbstowcs(m_pszW, m_pszA, nSizeA);
}
return m_pszW;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -