Last active
December 17, 2015 11:59
-
-
Save johnhmj/5606492 to your computer and use it in GitHub Desktop.
String, MultiByteToWideChar & WideCharToMultiByte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "W32String.h" | |
// | |
#define _BufSize 1024 | |
using std::wcout; | |
using std::endl; | |
void Pause(void); | |
// | |
int main(int argc, char* argv[]) | |
{ | |
_wsetlocale(LC_ALL, L"cht"); | |
W32String a, b("ansi臺灣中文(zh-tw)"), c(L"unicode臺灣中文(zh-tw)"); | |
wcout << "a == b: " << ((a == b)?L"true":L"false") << endl; | |
wcout << "b != c: " << ((b != c)?L"true":L"false") << endl; | |
wcout << "a = " << ((a.empty())?L"empty":L"not empty") << endl; | |
a = b.GetString(); | |
wcout << "a = b.GetString(), a = " << a.GetStringW() << endl; | |
a = c.GetStringW(); | |
wcout << "a = c.GetStringW(), a = " << a.GetStringW() << endl; | |
W32String d(a); | |
wcout << "d(a), a = " << a.GetStringW() << endl; | |
d = b; | |
wcout << "d = b, a = " << a.GetStringW() << endl; | |
a = a + b; | |
a += c; | |
wcout << "a = " << a.GetStringW() << endl; | |
Pause(); | |
return 0; | |
} | |
void Pause(void) | |
{ | |
using std::cin; | |
using std::cout; | |
using std::endl; | |
cout << "press any key to continue..."; | |
cin.clear(); | |
cin.ignore(_BufSize, '\n'); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "W32String.h" | |
// | |
W32String::W32String() | |
{ | |
this->m_string = NULL; | |
this->m_wstring = NULL; | |
this->m_temp = NULL; | |
this->m_size = 0; | |
} | |
W32String::W32String(const char* s) | |
{ | |
if (this->m_string == s) | |
{ | |
return; | |
} | |
if (s == NULL) | |
{ | |
return; | |
} | |
unsigned int _count = 0; | |
for (; s[_count] != NULL; _count ++); | |
_count ++; | |
this->m_string = NULL; | |
this->m_string = new char[_count]; | |
memcpy(this->m_string, s, _count); | |
int _wcount = MultiByteToWideChar(_CodePage, 0x00000000, this->m_string, -1, NULL, 0); | |
if (_wcount == 0) | |
{ | |
delete [] this->m_string; | |
this->m_string = NULL; | |
return; | |
} | |
this->m_wstring = new wchar_t[_wcount]; | |
MultiByteToWideChar(_CodePage, 0x00000000, this->m_string, -1, this->m_wstring, _wcount); | |
this->m_size = this->size(this->m_wstring); | |
this->m_temp = NULL; | |
} | |
W32String::W32String(const wchar_t* s) | |
{ | |
if (this->m_wstring == s) | |
{ | |
return; | |
} | |
if (s == NULL) | |
{ | |
return; | |
} | |
unsigned int _wcount = 0; | |
for (; s[_wcount] != NULL; _wcount ++); | |
_wcount ++; | |
this->m_string = NULL; | |
this->m_wstring = new wchar_t[_wcount]; | |
wmemcpy(this->m_wstring, s, _wcount); | |
int _count = WideCharToMultiByte(_CodePage, 0x00000000, this->m_wstring, -1, NULL, 0, NULL, NULL); | |
if (_count == 0) | |
{ | |
delete [] this->m_wstring; | |
this->m_wstring = NULL; | |
return; | |
} | |
this->m_string = new char[_count]; | |
WideCharToMultiByte(_CodePage, 0x00000000, this->m_wstring, -1, this->m_string, _count, NULL, NULL); | |
this->m_size = this->size(this->m_wstring); | |
this->m_temp = NULL; | |
} | |
W32String::W32String(const W32String& c) | |
{ | |
if (this == &c) | |
{ | |
return; | |
} | |
this->m_temp = NULL; | |
this->m_size = c.size(); | |
if (this->m_size == 0) | |
{ | |
this->m_string = NULL; | |
this->m_wstring = NULL; | |
return; | |
} | |
unsigned int _count = 0; | |
for (; c.m_string[_count] != NULL; _count ++); | |
_count ++; | |
unsigned int _wcount = 0; | |
for (; c.m_wstring[_wcount] != NULL; _wcount ++); | |
_wcount ++; | |
this->m_string = new char[_count]; | |
this->m_wstring = new wchar_t[_wcount]; | |
memcpy(this->m_string, c.m_string, _count); | |
wmemcpy(this->m_wstring, c.m_wstring, _wcount); | |
} | |
W32String::~W32String() | |
{ | |
if (this->m_string != NULL) | |
{ | |
delete [] this->m_string; | |
this->m_string = NULL; | |
} | |
if (this->m_wstring != NULL) | |
{ | |
delete [] this->m_wstring; | |
this->m_wstring = NULL; | |
} | |
if (this->m_temp != NULL) | |
{ | |
delete this->m_temp; | |
this->m_temp = NULL; | |
} | |
this->m_size = 0; | |
} | |
W32String& W32String::operator=(const W32String& c) | |
{ | |
if (this == &c) | |
{ | |
return *this; | |
} | |
if (this->m_string != NULL) | |
{ | |
delete [] this->m_string; | |
} | |
if (this->m_wstring != NULL) | |
{ | |
delete [] this->m_wstring; | |
} | |
this->m_size = c.size(); | |
if (this->m_size == 0) | |
{ | |
this->m_string = NULL; | |
this->m_wstring = NULL; | |
this->m_size = 0; | |
return *this; | |
} | |
unsigned int _count = 0; | |
for (; c.m_string[_count] != NULL; _count ++); | |
_count ++; | |
unsigned int _wcount = 0; | |
for (; c.m_wstring[_wcount] != NULL; _wcount ++); | |
_wcount ++; | |
this->m_string = new char[_count]; | |
this->m_wstring = new wchar_t[_wcount]; | |
memcpy(this->m_string, c.m_string, _count); | |
wmemcpy(this->m_wstring, c.m_wstring, _wcount); | |
return *this; | |
} | |
W32String& W32String::operator=(const char* s) | |
{ | |
if (this->m_string == s) | |
{ | |
return *this; | |
} | |
if (s == NULL) | |
{ | |
return *this; | |
} | |
if (this->m_string != NULL) | |
{ | |
delete [] this->m_string; | |
} | |
if (this->m_wstring != NULL) | |
{ | |
delete [] this->m_wstring; | |
} | |
unsigned int _count = 0; | |
for (; s[_count] != NULL; _count ++); | |
_count ++; | |
this->m_string = NULL; | |
this->m_string = new char[_count]; | |
memcpy(this->m_string, s, _count); | |
int _wcount = MultiByteToWideChar(_CodePage, 0x00000000, this->m_string, -1, NULL, 0); | |
if (_wcount == 0) | |
{ | |
delete [] this->m_string; | |
this->m_string = NULL; | |
return *this; | |
} | |
this->m_wstring = new wchar_t[_wcount]; | |
MultiByteToWideChar(_CodePage, 0x00000000, this->m_string, -1, this->m_wstring, _wcount); | |
this->m_size = this->size(this->m_wstring); | |
return *this; | |
} | |
W32String& W32String::operator=(const wchar_t* s) | |
{ | |
if (this->m_wstring == s) | |
{ | |
return *this; | |
} | |
if (s == NULL) | |
{ | |
return *this; | |
} | |
if (this->m_string != NULL) | |
{ | |
delete [] this->m_string; | |
} | |
if (this->m_wstring != NULL) | |
{ | |
delete [] this->m_wstring; | |
} | |
unsigned int _wcount = 0; | |
for (; s[_wcount] != NULL; _wcount ++); | |
_wcount ++; | |
this->m_string = NULL; | |
this->m_wstring = new wchar_t[_wcount]; | |
wmemcpy(this->m_wstring, s, _wcount); | |
int _count = WideCharToMultiByte(_CodePage, 0x00000000, this->m_wstring, -1, NULL, 0, NULL, NULL); | |
if (_count == 0) | |
{ | |
delete [] this->m_wstring; | |
this->m_wstring = NULL; | |
return *this; | |
} | |
this->m_string = new char[_count]; | |
WideCharToMultiByte(_CodePage, 0x00000000, this->m_wstring, -1, this->m_string, _count, NULL, NULL); | |
this->m_size = this->size(this->m_wstring); | |
return *this; | |
} | |
W32String& W32String::operator+(const char* s) | |
{ | |
if (this->m_string == s) | |
{ | |
return *this; | |
} | |
if (s == NULL) | |
{ | |
return *this; | |
} | |
if (this->m_temp == NULL) | |
{ | |
this->m_temp = new W32String; | |
} | |
*(this->m_temp) = s; | |
unsigned int _wcount = 1, _wci = 0, _wcj = 0; | |
for (; this->m_wstring[_wci] != NULL; _wci ++); | |
for (; this->m_temp->m_wstring[_wcj] != NULL; _wcj ++); | |
_wcount += _wci + _wcj; | |
wchar_t* _wstr = new wchar_t[_wcount]; | |
wmemcpy(_wstr, this->m_wstring, _wci); | |
wmemcpy((_wstr + _wci), this->m_temp->m_wstring, (_wcj + 1)); | |
*(this->m_temp) = _wstr; | |
return *(this->m_temp); | |
} | |
W32String& W32String::operator+(const wchar_t* s) | |
{ | |
if (this->m_wstring == s) | |
{ | |
return *this; | |
} | |
if (s == NULL) | |
{ | |
return *this; | |
} | |
if (this->m_temp == NULL) | |
{ | |
this->m_temp = new W32String; | |
} | |
unsigned int _wcount = 1, _wci = 0, _wcj = 0; | |
for (; this->m_wstring[_wci] != NULL; _wci ++); | |
for (; s[_wcj] != NULL; _wcj ++); | |
_wcount += _wci + _wcj; | |
wchar_t* _wstr = new wchar_t[_wcount]; | |
wmemcpy(_wstr, this->m_wstring, _wci); | |
wmemcpy((_wstr + _wci), s, (_wcj + 1)); | |
*(this->m_temp) = _wstr; | |
return *(this->m_temp); | |
} | |
W32String& W32String::operator+(const W32String& c) | |
{ | |
if (this == &c) | |
{ | |
return *this; | |
} | |
if (this->m_temp == NULL) | |
{ | |
this->m_temp = new W32String; | |
} | |
unsigned int _wcount = 1, _wci = 0, _wcj = 0; | |
for (; this->m_wstring[_wci] != NULL; _wci ++); | |
for (; c.m_wstring[_wcj] != NULL; _wcj ++); | |
_wcount += _wci + _wcj; | |
wchar_t* _wstr = new wchar_t[_wcount]; | |
wmemcpy(_wstr, this->m_wstring, _wci); | |
wmemcpy((_wstr + _wci), c.m_wstring, (_wcj + 1)); | |
*(this->m_temp) = _wstr; | |
return *(this->m_temp); | |
} | |
W32String& W32String::operator+=(const char* s) | |
{ | |
if (this->m_string == s) | |
{ | |
return *this; | |
} | |
if (s == NULL) | |
{ | |
return *this; | |
} | |
if (this->m_size == 0) | |
{ | |
*this = s; | |
return *this; | |
} | |
W32String _this = *this; | |
*this = _this + s; | |
return *this; | |
} | |
W32String& W32String::operator+=(const wchar_t* s) | |
{ | |
if (this->m_wstring == s) | |
{ | |
return *this; | |
} | |
if (s == NULL) | |
{ | |
return *this; | |
} | |
if (this->m_size == 0) | |
{ | |
*this = s; | |
return *this; | |
} | |
W32String _this = *this; | |
*this = _this + s; | |
return *this; | |
} | |
W32String& W32String::operator+=(const W32String& c) | |
{ | |
if (this == &c) | |
{ | |
return *this; | |
} | |
if (this->m_size == 0) | |
{ | |
*this = c; | |
return *this; | |
} | |
W32String _this = *this; | |
*this = _this + c; | |
return *this; | |
} | |
bool W32String::operator==(const char* s) | |
{ | |
if (this->m_string == s) | |
{ | |
return true; | |
} | |
if (this->m_size == 0) | |
{ | |
return false; | |
} | |
if (s == NULL) | |
{ | |
return false; | |
} | |
for (unsigned int _i = 0; this->m_string[_i] != NULL; _i ++) | |
{ | |
if (this->m_string[_i] != s[_i]) | |
{ | |
return false; | |
} | |
} | |
return true; | |
} | |
bool W32String::operator==(const wchar_t* s) | |
{ | |
if (this->m_wstring == s) | |
{ | |
return true; | |
} | |
if (this->m_size == 0) | |
{ | |
return false; | |
} | |
if (s == NULL) | |
{ | |
return false; | |
} | |
for (unsigned int _i = 0; this->m_wstring[_i] != NULL; _i ++) | |
{ | |
if (this->m_wstring[_i] != s[_i]) | |
{ | |
return false; | |
} | |
} | |
return true; | |
} | |
bool W32String::operator==(const W32String& c) | |
{ | |
if (this == &c) | |
{ | |
return true; | |
} | |
if (this->m_size != c.m_size) | |
{ | |
return false; | |
} | |
if (this->m_size == 0) | |
{ | |
return false; | |
} | |
for (unsigned int _i = 0; this->m_wstring[_i] != NULL; _i ++) | |
{ | |
if (this->m_wstring[_i] != c.m_wstring[_i]) | |
{ | |
return false; | |
} | |
} | |
return true; | |
} | |
bool W32String::operator!=(const char* s) | |
{ | |
if (this->m_string == s) | |
{ | |
return !true; | |
} | |
if (this->m_size == 0) | |
{ | |
return !false; | |
} | |
if (s == NULL) | |
{ | |
return !false; | |
} | |
for (unsigned int _i = 0; this->m_string[_i] != NULL; _i ++) | |
{ | |
if (this->m_string[_i] != s[_i]) | |
{ | |
return !false; | |
} | |
} | |
return !true; | |
} | |
bool W32String::operator!=(const wchar_t* s) | |
{ | |
if (this->m_wstring == s) | |
{ | |
return !true; | |
} | |
if (this->m_size == 0) | |
{ | |
return !false; | |
} | |
if (s == NULL) | |
{ | |
return !false; | |
} | |
for (unsigned int _i = 0; this->m_wstring[_i] != NULL; _i ++) | |
{ | |
if (this->m_wstring[_i] != s[_i]) | |
{ | |
return !false; | |
} | |
} | |
return !true; | |
} | |
bool W32String::operator!=(const W32String& c) | |
{ | |
if (this == &c) | |
{ | |
return !true; | |
} | |
if (this->m_size != c.m_size) | |
{ | |
return !false; | |
} | |
if (this->m_size == 0) | |
{ | |
return !false; | |
} | |
for (unsigned int _i = 0; this->m_wstring[_i] != NULL; _i ++) | |
{ | |
if (this->m_wstring[_i] != c.m_wstring[_i]) | |
{ | |
return !false; | |
} | |
} | |
return !true; | |
} | |
char* W32String::GetString(void) const | |
{ | |
return this->m_string; | |
} | |
wchar_t* W32String::GetStringW(void) const | |
{ | |
return this->m_wstring; | |
} | |
unsigned int W32String::size(void) const | |
{ | |
return this->m_size; | |
} | |
bool W32String::empty(void) const | |
{ | |
if (this->m_size == 0) | |
{ | |
return true; | |
} | |
return false; | |
} | |
unsigned int W32String::size(const wchar_t* s) | |
{ | |
if (s == NULL) | |
{ | |
return 0; | |
} | |
unsigned int _size = 0; | |
for (; s[_size] != NULL; _size ++); | |
return _size; | |
} | |
void W32String::clear(void) | |
{ | |
if (this->m_string != NULL) | |
{ | |
delete [] this->m_string; | |
this->m_string = NULL; | |
} | |
if (this->m_wstring != NULL) | |
{ | |
delete [] this->m_wstring; | |
this->m_wstring = NULL; | |
} | |
if (this->m_temp != NULL) | |
{ | |
delete this->m_temp; | |
this->m_temp = NULL; | |
} | |
this->m_size = 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <Windows.h> | |
// | |
#ifndef _W32STRING_H_ | |
#define _W32STRING_H_ | |
// | |
#define _CodePage CP_ACP | |
// | |
// String class | |
class W32String | |
{ | |
public: | |
W32String(); | |
virtual ~W32String(); | |
W32String(const char* s); | |
W32String(const wchar_t* s); | |
W32String(const W32String& c); | |
W32String& operator=(const char* s); | |
W32String& operator=(const wchar_t* s); | |
W32String& operator=(const W32String& c); | |
W32String& operator+(const char* s); | |
W32String& operator+(const wchar_t* s); | |
W32String& operator+(const W32String& c); | |
W32String& operator+=(const char* s); | |
W32String& operator+=(const wchar_t* s); | |
W32String& operator+=(const W32String& c); | |
bool operator==(const char* s); | |
bool operator==(const wchar_t* s); | |
bool operator==(const W32String& c); | |
bool operator!=(const char* s); | |
bool operator!=(const wchar_t* s); | |
bool operator!=(const W32String& c); | |
char* GetString(void) const; | |
wchar_t* GetStringW(void) const; | |
unsigned int size(void) const; | |
bool empty(void) const; | |
void clear(void); | |
private: | |
char* m_string; | |
wchar_t* m_wstring; | |
unsigned int m_size; | |
unsigned int size(const wchar_t* s); | |
W32String* m_temp; | |
}; | |
// | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment