ANSI String support

This commit is contained in:
Nomango 2018-02-28 19:17:15 +08:00
parent 22fba22dcc
commit d0733b01e2
12 changed files with 304 additions and 138 deletions

View File

@ -12,7 +12,7 @@ static bool s_bInitialized = false;
static e2d::String s_sAppName;
bool e2d::Game::init(LPCTSTR sTitle, UINT32 nWidth, UINT32 nHeight, LPCTSTR pIconID, LPCTSTR sAppname)
bool e2d::Game::init(const String & sTitle, UINT32 nWidth, UINT32 nHeight, LPCTSTR pIconID, const String & sAppname)
{
if (s_bInitialized)
{
@ -63,7 +63,7 @@ bool e2d::Game::init(LPCTSTR sTitle, UINT32 nWidth, UINT32 nHeight, LPCTSTR pIco
// 重设 Client 大小
Window::setSize(nWidth, nHeight);
// 设置 AppName
s_sAppName = (sAppname != nullptr) ? sAppname : Window::getTitle();
s_sAppName = sAppname.isEmpty() ? Window::getTitle() : sAppname;
// 标志初始化成功
s_bInitialized = true;

View File

@ -9,7 +9,7 @@ static HWND s_HWnd = nullptr;
static bool s_bShowConsole = false;
bool e2d::Window::__init(LPCTSTR sTitle, UINT32 nWidth, UINT32 nHeight, LPCTSTR pIconID /*= nullptr*/)
bool e2d::Window::__init(const String & sTitle, UINT32 nWidth, UINT32 nHeight, LPCTSTR pIconID /*= nullptr*/)
{
// ע˛á´°żÚŔŕ
WNDCLASSEX wcex = { sizeof(WNDCLASSEX) };

View File

@ -13,12 +13,12 @@ e2d::Image::Image()
{
}
e2d::Image::Image(LPCTSTR strFileName)
e2d::Image::Image(const String & strFileName)
{
this->loadFrom(strFileName);
}
e2d::Image::Image(LPCTSTR strFileName, double nClipX, double nClipY, double nClipWidth, double nClipHeight)
e2d::Image::Image(const String & strFileName, double nClipX, double nClipY, double nClipWidth, double nClipHeight)
{
this->loadFrom(strFileName);
this->clip(nClipX, nClipY, nClipWidth, nClipHeight);

View File

@ -1,5 +1,22 @@
#include "..\ecommon.h"
#include <iomanip>
#include <comutil.h>
#pragma comment(lib, "comsuppw.lib")
// 将 Unicode 字符串转化为 ANSI 字符串
static std::string ConvertWide2Ansi(const wchar_t* wstr)
{
std::string str = static_cast<char*>(_bstr_t(wstr));
return std::move(str);
}
// 将 ANSI 字符串转化为 Unicode 字符串
static std::wstring ConvertAnsi2Wide(const char* cstr)
{
std::wstring str = static_cast<wchar_t*>(_bstr_t(cstr));
return std::move(str);
}
e2d::String::String()
@ -12,6 +29,16 @@ e2d::String::String(const wchar_t *str)
{
}
e2d::String::String(const char *cstr)
: m_str(ConvertAnsi2Wide(cstr))
{
}
e2d::String::String(const std::string &str)
: m_str(ConvertAnsi2Wide(str.c_str()))
{
}
e2d::String::String(e2d::String && str)
{
m_str = std::move(str.m_str);
@ -38,6 +65,12 @@ e2d::String &e2d::String::operator=(const wchar_t *str)
return *this;
}
e2d::String & e2d::String::operator=(const char *cstr)
{
m_str = ConvertAnsi2Wide(cstr);
return *this;
}
e2d::String & e2d::String::operator=(const String &str)
{
m_str = str.m_str;
@ -50,6 +83,12 @@ e2d::String & e2d::String::operator=(const std::wstring &str)
return *this;
}
e2d::String & e2d::String::operator=(const std::string &str)
{
m_str = ConvertAnsi2Wide(str.c_str());
return *this;
}
bool e2d::String::operator==(const wchar_t *str)
{
if (str)
@ -62,6 +101,19 @@ bool e2d::String::operator==(const wchar_t *str)
}
}
bool e2d::String::operator==(const char *str)
{
if (str)
{
String temp(str);
return (m_str == temp.m_str);
}
else
{
return false;
}
}
bool e2d::String::operator ==(const e2d::String &str)
{
return m_str == str.m_str;
@ -84,6 +136,19 @@ bool e2d::String::operator!=(const wchar_t *str)
}
}
bool e2d::String::operator!=(const char *str)
{
if (str)
{
String temp(str);
return (m_str != temp.m_str);
}
else
{
return true;
}
}
bool e2d::String::operator!=(const e2d::String &str)
{
return m_str != str.m_str;
@ -106,10 +171,10 @@ e2d::String e2d::String::operator+(const wchar_t *str)
return std::move(temp);
}
e2d::String e2d::String::operator+(const wchar_t x)
e2d::String e2d::String::operator+(const char *str)
{
String temp;
temp.m_str = m_str + x;
String temp(str);
temp.m_str += m_str;
return std::move(temp);
}
@ -127,13 +192,6 @@ e2d::String e2d::String::operator+(const std::wstring &str)
return std::move(temp);
}
e2d::String e2d::operator+(const wchar_t ch, const e2d::String &str)
{
String temp;
temp.m_str = ch + str.m_str;
return std::move(temp);
}
e2d::String e2d::operator+(const wchar_t *str1, const e2d::String &str2)
{
String temp;
@ -141,6 +199,13 @@ e2d::String e2d::operator+(const wchar_t *str1, const e2d::String &str2)
return std::move(temp);
}
e2d::String e2d::operator+(const char *str1, const String &str2)
{
String temp(str1);
temp.m_str += str2.m_str;
return std::move(temp);
}
e2d::String e2d::operator+(const std::wstring &str1, const e2d::String &str2)
{
String temp;
@ -148,28 +213,58 @@ e2d::String e2d::operator+(const std::wstring &str1, const e2d::String &str2)
return std::move(temp);
}
e2d::String &e2d::String::operator+=(const wchar_t x)
{
m_str += x;
return *this;
}
e2d::String & e2d::String::operator+=(const wchar_t *str)
{
m_str += str;
return *this;
return (*this);
}
e2d::String &e2d::String::operator+=(const e2d::String &str)
e2d::String & e2d::String::operator+=(const char *str)
{
m_str += ::ConvertAnsi2Wide(str);
return (*this);
}
e2d::String & e2d::String::operator+=(const String &str)
{
m_str += str.m_str;
return *this;
return (*this);
}
e2d::String & e2d::String::operator+=(const std::wstring &str)
{
m_str += str;
return *this;
return (*this);
}
bool e2d::String::operator>(const String &str) const
{
return m_str > str.m_str;
}
bool e2d::String::operator>=(const String &str) const
{
return m_str >= str.m_str;
}
bool e2d::String::operator<(const String &str) const
{
return m_str < str.m_str;
}
bool e2d::String::operator<=(const String &str) const
{
return m_str <= str.m_str;
}
e2d::String & e2d::String::operator<<(const char * value)
{
return this->append(value);
}
e2d::String & e2d::String::operator<<(char * value)
{
return this->append(value);
}
e2d::String::operator const wchar_t*() const
@ -177,6 +272,16 @@ e2d::String::operator const wchar_t*() const
return m_str.c_str();
}
e2d::String::operator wchar_t*() const
{
return const_cast<wchar_t*>(m_str.c_str());
}
e2d::String::operator const char*() const
{
return ::ConvertWide2Ansi(m_str.c_str()).c_str();
}
e2d::String::operator bool() const
{
return getLength() != 0;
@ -204,12 +309,6 @@ unsigned int e2d::String::getHash() const
return hash;
}
std::wistream & e2d::operator>>(std::wistream &cin, e2d::String &str)
{
cin >> str.m_str;
return cin;
}
e2d::String e2d::String::toUpper() const
{
String str(*this);
@ -299,17 +398,52 @@ int e2d::String::findLastOf(const wchar_t ch) const
return index;
}
e2d::String & e2d::String::append(const wchar_t ch)
{
return (*this) += ch;
}
e2d::String & e2d::String::append(const wchar_t * str)
{
return (*this) += str;
m_str += str;
return *this;
}
e2d::String & e2d::String::append(const char * str)
{
m_str += ::ConvertAnsi2Wide(str);
return *this;
}
e2d::String & e2d::String::append(char * str)
{
m_str += ::ConvertAnsi2Wide(str);
return *this;
}
e2d::String & e2d::String::append(const e2d::String & str)
{
return (*this) += str;
m_str += str.m_str;
return (*this);
}
std::wostream & e2d::operator<<(std::wostream &cout, String &str)
{
cout << str.m_str;
return cout;
}
std::wistream & e2d::operator>>(std::wistream &cin, e2d::String &str)
{
cin >> str.m_str;
return cin;
}
std::ostream & e2d::operator<<(std::ostream &cout, String &str)
{
cout << ::ConvertWide2Ansi(str.m_str.c_str());
return cout;
}
std::istream & e2d::operator>>(std::istream &cin, e2d::String &str)
{
std::string temp;
cin >> temp;
str.m_str = ::ConvertAnsi2Wide(temp.c_str());
return cin;
}

View File

@ -12,13 +12,13 @@ e2d::Sprite::Sprite(Image * image)
loadFrom(image);
}
e2d::Sprite::Sprite(LPCTSTR imageFileName)
e2d::Sprite::Sprite(const String & imageFileName)
: m_pImage(nullptr)
{
loadFrom(imageFileName);
}
e2d::Sprite::Sprite(LPCTSTR imageFileName, double x, double y, double width, double height)
e2d::Sprite::Sprite(const String & imageFileName, double x, double y, double width, double height)
: m_pImage(nullptr)
{
loadFrom(imageFileName);
@ -42,7 +42,7 @@ void e2d::Sprite::loadFrom(Image * image)
}
}
void e2d::Sprite::loadFrom(LPCTSTR imageFileName)
void e2d::Sprite::loadFrom(const String & imageFileName)
{
loadFrom(new Image(imageFileName));
}

View File

@ -81,7 +81,7 @@ e2d::String e2d::File::getSaveFilePath(const String & title, const String & defE
{
// 弹出保存对话框
OPENFILENAME ofn = { 0 };
TCHAR strFilename[MAX_PATH] = { 0 }; // 用于接收文件名
wchar_t strFilename[MAX_PATH] = { 0 }; // 用于接收文件名
ofn.lStructSize = sizeof(OPENFILENAME); // 结构体大小
ofn.hwndOwner = Window::getHWnd(); // 窗口句柄
ofn.lpstrFilter = L"所有文件\0*.*\0\0"; // 设置过滤

View File

@ -10,13 +10,13 @@ using namespace e2d;
#define SAFE_DELETE_ARRAY(p) { if (p) { delete[] (p); (p)=nullptr; } }
#endif
inline bool TraceError(LPCTSTR sPrompt)
inline bool TraceError(wchar_t* sPrompt)
{
WARN_IF(true, "Music error: %s failed!", sPrompt);
return false;
}
inline bool TraceError(LPCTSTR sPrompt, HRESULT hr)
inline bool TraceError(wchar_t* sPrompt, HRESULT hr)
{
WARN_IF(true, "Music error: %s (%#X)", sPrompt, hr);
return false;
@ -40,7 +40,7 @@ Music::~Music()
_close();
}
bool Music::_open(LPCWSTR strFileName)
bool Music::_open(const String & strFileName)
{
if (m_bOpened)
{
@ -48,7 +48,7 @@ bool Music::_open(LPCWSTR strFileName)
return false;
}
if (strFileName == nullptr)
if (strFileName.isEmpty())
{
WARN_IF(true, L"Music::_open Invalid file name.");
return false;
@ -65,7 +65,7 @@ bool Music::_open(LPCWSTR strFileName)
wchar_t strFilePath[MAX_PATH];
if (!_findMediaFileCch(strFilePath, MAX_PATH, strFileName))
{
WARN_IF(true, L"Failed to find media file: %s", strFileName);
WARN_IF(true, L"Failed to find media file: %s", (const wchar_t*)strFileName);
return false;
}
@ -399,17 +399,17 @@ bool Music::_read(BYTE* pBuffer, DWORD dwSizeToRead)
return true;
}
bool Music::_findMediaFileCch(WCHAR* strDestPath, int cchDest, LPCWSTR strFilename)
bool Music::_findMediaFileCch(wchar_t* strDestPath, int cchDest, const String & strFilename)
{
bool bFound = false;
if (nullptr == strFilename || strFilename[0] == 0 || nullptr == strDestPath || cchDest < 10)
if (strFilename.isEmpty() || nullptr == strDestPath || cchDest < 10)
return false;
// Get the exe name, and exe path
WCHAR strExePath[MAX_PATH] = { 0 };
WCHAR strExeName[MAX_PATH] = { 0 };
WCHAR* strLastSlash = nullptr;
wchar_t strExePath[MAX_PATH] = { 0 };
wchar_t strExeName[MAX_PATH] = { 0 };
wchar_t* strLastSlash = nullptr;
GetModuleFileName(HINST_THISCOMPONENT, strExePath, MAX_PATH);
strExePath[MAX_PATH - 1] = 0;
strLastSlash = wcsrchr(strExePath, TEXT('\\'));
@ -431,13 +431,13 @@ bool Music::_findMediaFileCch(WCHAR* strDestPath, int cchDest, LPCWSTR strFilena
return true;
// Search all parent directories starting at .\ and using strFilename as the leaf name
WCHAR strLeafName[MAX_PATH] = { 0 };
wchar_t strLeafName[MAX_PATH] = { 0 };
wcscpy_s(strLeafName, MAX_PATH, strFilename);
WCHAR strFullPath[MAX_PATH] = { 0 };
WCHAR strFullFileName[MAX_PATH] = { 0 };
WCHAR strSearch[MAX_PATH] = { 0 };
WCHAR* strFilePart = nullptr;
wchar_t strFullPath[MAX_PATH] = { 0 };
wchar_t strFullFileName[MAX_PATH] = { 0 };
wchar_t strSearch[MAX_PATH] = { 0 };
wchar_t* strFilePart = nullptr;
GetFullPathName(L".", MAX_PATH, strFullPath, &strFilePart);
if (strFilePart == nullptr)

View File

@ -14,11 +14,11 @@ class Game
public:
// 初始化游戏
static bool init(
LPCTSTR sTitle, /* ´°¿Ú±êÌâ */
const String & sTitle, /* ´°¿Ú±êÌâ */
UINT32 nWidth, /* 窗口宽度 */
UINT32 nHeight, /* 窗口高度 */
LPCTSTR pIconID = nullptr, /* ´°¿Úͼ±ê */
LPCTSTR sAppname = nullptr /* AppName */
LPCTSTR pIconID = L"", /* ´°¿Úͼ±ê */
const String & sAppname = L"" /* AppName */
);
// 启动游戏
@ -89,7 +89,7 @@ public:
private:
// 初始化窗口
static bool __init(
LPCTSTR sTitle,
const String & sTitle,
UINT32 nWidth,
UINT32 nHeight,
LPCTSTR pIconID

View File

@ -99,47 +99,6 @@ public:
~String();
String& operator= (const wchar_t *);
String& operator= (const String &);
String& operator= (const std::wstring &);
bool operator== (const wchar_t *);
bool operator== (const String &);
bool operator== (const std::wstring &);
bool operator!= (const wchar_t *);
bool operator!= (const String &);
bool operator!= (const std::wstring &);
wchar_t &operator[] (int);
String operator+ (const wchar_t);
String operator+ (const wchar_t *);
String operator+ (const String &);
String operator+ (const std::wstring &);
friend String operator+ (const wchar_t, const String &);
friend String operator+ (const wchar_t*, const String &);
friend String operator+ (const std::wstring &, const String &);
String& operator+= (const wchar_t);
String& operator+= (const wchar_t *);
String& operator+= (const String &);
String& operator+= (const std::wstring &);
template<typename T>
String& operator<< (const T value)
{
std::wostringstream ss;
ss << value;
return (*this) += ss.str();
}
operator const wchar_t* () const;
operator bool () const;
friend std::wistream& operator>> (std::wistream &, String &);
// 判断字符串是否为空
bool isEmpty() const;
@ -149,30 +108,41 @@ public:
// 获取该字符串的散列值
unsigned int getHash() const;
// 后接字符
String &append(const wchar_t ch);
// 后接字符串
String& append(
const wchar_t *str
);
// 后接字符串
String &append(const wchar_t *str);
// 后接字符串
String &append(const String &str);
String& append(
const String &str
);
// 后接字符串
template<typename T>
String& append(const T &value)
{
return (*this) += value;
std::wostringstream ss;
ss << value;
m_str += ss.str();
return (*this);
}
// 获取裁剪字符串
String subtract(int offset, int count = -1) const;
String subtract(
int offset, /* 偏移量 */
int count = -1 /* 截取字符数量 */
) const;
// 获取字符串中第一个特定字符的下标
int findFirstOf(const wchar_t ch) const;
int findFirstOf(
const wchar_t ch
) const;
// 获取字符串中最后一个特定字符的下标
int findLastOf(const wchar_t ch) const;
int findLastOf(
const wchar_t ch
) const;
// 获取大写字符串
String toUpper() const;
@ -198,6 +168,68 @@ public:
return ss.str();
}
static String toString(const wchar_t * str) { return std::move(String(str)); }
static String toString(const char * str) { return std::move(String(str)); }
String& operator= (const wchar_t *);
String& operator= (const String &);
String& operator= (const std::wstring &);
bool operator== (const wchar_t *);
bool operator== (const String &);
bool operator== (const std::wstring &);
bool operator!= (const wchar_t *);
bool operator!= (const String &);
bool operator!= (const std::wstring &);
wchar_t &operator[] (int);
String operator+ (const wchar_t *);
String operator+ (const String &);
String operator+ (const std::wstring &);
friend String operator+ (const wchar_t*, const String &);
friend String operator+ (const std::wstring &, const String &);
String& operator+= (const wchar_t *);
String& operator+= (const String &);
String& operator+= (const std::wstring &);
bool operator> (const String &) const;
bool operator>= (const String &) const;
bool operator< (const String &) const;
bool operator<= (const String &) const;
template<typename T>
String& operator<< (const T value) { return this->append<>(value); }
operator const wchar_t* () const;
operator wchar_t* () const;
operator bool () const;
friend std::wostream& operator<< (std::wostream &, String &);
friend std::wistream& operator>> (std::wistream &, String &);
// 为 ANSI 作出的适应
String(const char *);
String(const std::string &);
operator const char* () const;
String& operator= (const char *);
String& operator= (const std::string &);
bool operator== (const char *);
bool operator!= (const char *);
String operator+ (const char *);
friend String operator+ (const char *, const String &);
String& operator+= (const char *);
String& operator<< (const char *);
String& operator<< (char *);
String& append(const char *);
String& append(char *);
friend std::ostream& operator<< (std::ostream &, String &);
friend std::istream& operator>> (std::istream &, String &);
private:
std::wstring m_str;
};
@ -501,12 +533,12 @@ public:
// 从本地文件中读取资源
Image(
LPCTSTR strFilePath /* 图片文件路径 */
const String & strFilePath /* 图片文件路径 */
);
// 从本地文件中读取资源
Image(
LPCTSTR strFilePath,/* 图片文件路径 */
const String & strFilePath,/* 图片文件路径 */
double nClipX, /* 裁剪位置 X 坐标 */
double nClipY, /* 裁剪位置 Y 坐标 */
double nClipWidth, /* 裁剪宽度 */

View File

@ -422,12 +422,12 @@ public:
// 从文件图片创建精灵
Sprite(
LPCTSTR imageFileName
const String & imageFileName
);
// 从文件图片创建精灵并裁剪
Sprite(
LPCTSTR imageFileName,
const String & imageFileName,
double x,
double y,
double width,
@ -443,7 +443,7 @@ public:
// 从本地文件加载图片
virtual void loadFrom(
LPCTSTR imageFileName
const String & imageFileName
);
// 裁剪图片
@ -472,25 +472,25 @@ public:
Text();
Text(
const String & text
const String & text /* 文字内容 */
);
Text(
Font * font
Font * font /* 字体样式 */
);
Text(
const String & text,
Font * font
const String & text,/* 文字内容 */
Font * font /* 字体样式 */
);
Text(
const String & text,
String fontFamily,
double fontSize = 22,
UINT32 color = Color::WHITE,
UINT32 fontWeight = FontWeight::REGULAR,
bool italic = false
const String & text, /* 文字内容*/
String fontFamily, /* 字体 */
double fontSize = 22, /* 字号 */
UINT32 color = Color::WHITE, /* 颜色 */
UINT32 fontWeight = FontWeight::REGULAR,/* 粗细值 */
bool italic = false /* 斜体 */
);
virtual ~Text();

View File

@ -242,7 +242,7 @@ protected:
virtual ~Music();
bool _open(LPCWSTR strFileName);
bool _open(const String & strFileName);
void _close();
@ -252,7 +252,7 @@ protected:
bool _read(BYTE* pBuffer, DWORD dwSizeToRead);
bool _findMediaFileCch(WCHAR* strDestPath, int cchDest, LPCWSTR strFilename);
bool _findMediaFileCch(wchar_t* strDestPath, int cchDest, const String & strFilename);
protected:
bool m_bOpened;