当前位置: 首页 > 工具软件 > tstring > 使用案例 >

一个简单的支持unicode的tstring类

东郭鹤龄
2023-12-01

一个简单的支持unicode的tstring类

TfxString.h 

#pragma once
#include <string>
#include <locale.h>
#include <tchar.h>

#ifdef _WINDLL
	#ifdef TFXCOMN_EXPORTS
		#define TFXCOMN_API_STRING __declspec(dllexport)
	#else
		#define TFXCOMN_API_STRING __declspec(dllimport)
	#endif
#else
	#define TFXCOMN_API_STRING
#endif

#ifndef _UNICODE
	#define tstring std::string
#else
	#define tstring std::wstring
#endif

TFXCOMN_API_STRING tstring Tfxs2tFillHighByte(std::string& str);
TFXCOMN_API_STRING std::string Tfxt2sDropHighByte(tstring& str);

TFXCOMN_API_STRING std::string Tfxws2s(std::wstring& str, TCHAR* szLocale = _T("chs"));
TFXCOMN_API_STRING std::wstring Tfxs2ws(std::string& str, TCHAR* szLocale = _T("chs"));
TFXCOMN_API_STRING std::string Tfxt2s(tstring& str, TCHAR* szLocale = _T("chs"));
TFXCOMN_API_STRING std::wstring Tfxt2ws(tstring& str, TCHAR* szLocale = _T("chs"));
TFXCOMN_API_STRING tstring Tfxs2t(std::string& str, TCHAR* szLocale = _T("chs"));


TfxString.cpp 

#include "stdafx.h"
#include "TfxString.h"

#ifdef UNDER_CE
#include <windows.h>
#endif

#pragma warning(disable: 4996)
#ifdef UNDER_CE
	#ifndef _UNICODE
		#define _tsetlocale setlocale
	#else
		#define _tsetlocale _wsetlocale
	#endif
#endif

tstring Tfxs2tFillHighByte(std::string& str)
{
#ifdef _UNICODE
	int nLen = str.size();
	wchar_t* pwChar = new wchar_t[nLen + 1];
	for (int i = 0; i < nLen; i++)
	{
		pwChar[i] = str[i];
		memset((char*)&pwChar[i] + 1, 0, 1);
	}
	pwChar[nLen] = 0;
	std::wstring strRet = pwChar;
	delete []pwChar;
	return strRet;
#else
	return str;
#endif
}

std::string Tfxt2sDropHighByte(tstring& str)
{
#ifdef _UNICODE
	int nLen = str.size();
	// 	char* pChar = new char[nLen + 1];
	std::string strRet;
	for (int i = 0; i < nLen; i++)
	{
		strRet += (char)str[i];
	}
	// 	strRet += (char)0;
	// 	pChar[nLen] = 0;
	// 	delete []pChar;
	return strRet;
#else
	return str;
#endif
}

std::string Tfxws2s(std::wstring& str, TCHAR* szLocale)
{
#ifndef UNDER_CE
	tstring strCurLocale = _tsetlocale(LC_ALL, NULL);
	_tsetlocale(LC_ALL, szLocale);
	const wchar_t* pSource = str.c_str();
	size_t szDestSize = 2 * str.size() + 1;
	char* pDest = new char[szDestSize];
	memset(pDest, 0, szDestSize);
	wcstombs(pDest, pSource, szDestSize);
	std::string strResult = pDest;
	delete []pDest;
	_tsetlocale(LC_ALL, strCurLocale.c_str());
	return strResult;
#else
	UINT uiCodePage = CP_ACP;
	if (!_tcscmp(szLocale, _T("utf8")))
	{
		uiCodePage = CP_UTF8;
	}
	int nLen = ::WideCharToMultiByte(uiCodePage, 0, str.c_str(), -1, NULL, 0, NULL, NULL);
	if (nLen == 0)
	{
		return "";
	}
	char* pResult = new char[nLen];
	::WideCharToMultiByte(uiCodePage, 0, str.c_str(), -1, pResult, nLen, NULL, NULL);
	std::string strResult = pResult;
	delete []pResult;
	return strResult;
#endif
}

std::wstring Tfxs2ws(std::string& str, TCHAR* szLocale)
{
#ifndef UNDER_CE
	tstring strCurLocale = _tsetlocale(LC_ALL, NULL);
	_tsetlocale(LC_ALL, szLocale);
	const char* pSource = str.c_str();
	size_t szDestSize = str.size() + 1;
	wchar_t* pDest = new wchar_t[szDestSize];
	wmemset(pDest, 0, szDestSize);
	mbstowcs(pDest, pSource, szDestSize);
	std::wstring strResult = pDest;
	delete []pDest;
	_tsetlocale(LC_ALL, strCurLocale.c_str());
	return strResult;
#else
	UINT uiCodePage = CP_ACP;
	if (!_tcscmp(szLocale, _T("utf8")))
	{
		uiCodePage = CP_UTF8;
	}
	int nLen = ::MultiByteToWideChar(uiCodePage, 0, str.c_str(), -1, NULL, 0);
	if (nLen == 0)
	{
		return _T("");
	}
	wchar_t* pResult = new wchar_t[nLen];
	::MultiByteToWideChar(uiCodePage, 0, str.c_str(), -1, pResult, nLen);
	std::wstring strResult = pResult;
	delete []pResult;
	return strResult;
#endif
}

std::string Tfxt2s(tstring& str, TCHAR* szLocale)
{
#ifdef _UNICODE
	return Tfxws2s(str, szLocale);
#else
	return str;
#endif
}

std::wstring Tfxt2ws(tstring& str, TCHAR* szLocale)
{
#ifdef _UNICODE
	return str;
#else
	return Tfxs2ws(str, szLocale);
#endif
}

tstring Tfxs2t(std::string& str, TCHAR* szLocale)
{
#ifdef _UNICODE
	return Tfxs2ws(str, szLocale);
#else
	return str;
#endif
}

tstring Tfxws2t(std::wstring& str, TCHAR* szLocale)
{
#ifdef _UNICODE
	return str;
#else
	return Tfxws2s(str, szLocale);
#endif
}



 类似资料: