Note: this isn't the usual typedef
basic_string<TCHAR> tstring; that's commonly used in programs. It
has functions added, so the tutorial may not work in cases other than
the tstring class I created.
Updates:
August 29, 2010. Copy constructor updated for vector compatibility.
Operator+ and append() functions also fixed
Sept 5, 2010: == and != fixed for TCHAR
Noted side-effect: a lot of functions seem to return a space at the end.
tstring: my enhanced version of the classic tstring. Perhaps I should
have named it something different to help people avoid confusion.
However, tstring is not a standard C++ type, even though it's a
commonly-used name. tstring has everything that basic_string has, so if
you know how to use regular C++ strings, you should feel mostly at home
here. You can find the source code here.
header:
#ifdef _UNICODE
#define tcout wcout
#define tcin
wcin
#else
#define tcout cout
#define tcin
cin
#endif
class tstring : public basic_string<TCHAR>
{
public:
// added constructors to help with replacing
implementations that were just typedef basic_string<TCHAR>
tstring;
tstring(){};
tstring(const tstring &in) { assign(in); };
// tstring(tstring &in) { assign(in); };
tstring(string &in) { assign(in); };
tstring(wstring &in) { assign(in); };
tstring(char *in) { assign(in); };
tstring(wchar_t *in) { assign(in); };
// assignment operators for the various string types.
tstring &assign(string &_str);
tstring &assign(wstring &_wstr);
tstring &assign(tstring &_tstr);
tstring &assign(wchar_t *wc_str);
tstring &assign(char *c_str);
// Eats an UTF c-style string. This is a special case
compared to an ANSI string
tstring &assignUTF8(char *c_str);
tstring &assignUTF8Compostie(char *c_str);
// added for STL compatibility
tstring &assign(const tstring &_tstr);
// operator=, which basically is the same as the .assign()
function
tstring &operator=(const tstring &rhs);
tstring &operator=(tstring &rhs);
tstring &operator=(string &rhs);
tstring &operator=(wstring &rhs);
tstring &operator=(char *rhs);
tstring &operator=(wchar_t *rhs);
// append recreated. I mostly want it to work on TCHAR and
tstrings for now.
// I may add c string and wide strings later
tstring &append(const tstring &_tstr);
tstring &append(const TCHAR *tc);
// operator +=, which makes operator + easier
tstring &operator+=(const tstring &rhs);
tstring &operator+=(const TCHAR *rhs);
// operator+, adds two tstrings together
tstring operator+(const tstring &rhs);
tstring operator+(const TCHAR *rhs);
// operator== to detect equivalency
bool operator==(const TCHAR *rhs);
bool operator==(const tstring &rhs);
// operator!= to detect non-equiv.
bool operator!=(const TCHAR *rhs);
bool operator!=(const tstring &rhs);
// fill the output with the versions appropiate to what
you are requesting. maxlen is the maxiumum amount of characters to copy.
//
// mbc_str is multibyte C string / wc_str is wide
character string
void mbc_str(char *output, size_t maxlen);
void wc_str (wchar_t *output, size_t maxlen);
// multibyte c++ string.
void mbcpp_str(string *out);
// Wide c++ string
void wcpp_str(wstring *out);
};
My test code. As you can tell, I created the assign() functions
first,
followed by operator=, then the special return types.
#include
"../../../Common/tstring.h"
#include <iostream>
using std::endl;
#include <string>
using std::string;
using std::wstring;
#include <iostream>
using std::cout;
using std::cin;
using std::wcout;
using std::wcin;
int main()
{
// test all 6 string
types native to Win32 C++
tstring test;
tstring ttest; ttest.assign("Meow?");
string strtest = "Hallo!";
wstring wstest = L"'sup!";
char pebbles[] = "My pebbles!";
char cstr[16];
wchar_t wcstr[16];
// test the assign functions
test.assign(TEXT("TCHAR Moo!"));
tcout << test << endl;
test.assign("Multibyte Moo!");
tcout << test << endl;
test.assign(L"UNICODE Moo!");
tcout << test << endl;
test.assign(strtest);
tcout << test << endl;
test.assign(wstest);
tcout << test << endl;
test.assign(ttest);
tcout << test << endl;
// test operator=
test.assign("ERROR!"); // I want it to say "Error!" in
case I mess this up since I know that assign() works
test =
ttest; // but
want to make it say "Meow?" if it works
tcout << test << endl;
test.assign("No error!");
test =
test;
// this shouldn't make the compiler/executable explode
tcout << test << endl;
// now test the taking in of text using operator=
test = L"Fred";
tcout << test << endl;
test = "Wilma";
tcout << test << endl;
test = TEXT("Barney");
tcout << test << endl;
test = pebbles;
tcout << test << endl;
// check the returned char * and wchar_t *. It should be
exactly the same
test.wc_str(wcstr, 16);
wcout << wcstr << endl;
test.mbc_str(cstr, 16);
cout << cstr << endl;
// check the returned string and wstring
test = L"Open sesame!";
test.mbcpp_str(&strtest);
cout << strtest << endl;
test = "Whee dogie!";
test.wcpp_str(&wstest);
wcout << wstest << endl;
// and here's c_str() in action, which outputs the TCHAR
string
tcout << test.c_str() << endl;
tcout << "Enter a thingy: ";
tcin >> test;
tcout << test << endl;
// using this to check on something. I needed to check stl
string to see if it put a
// space at the end because that's what was happening with
tstring
strtest = "str";
strtest.append("test");
cout << strtest << endl;
strtest = "dance";
strtest = strtest + "dance";
strtest = strtest + strtest;
cout << strtest << endl;
// append test. I only want to append tstring and TCHARs
to the string
// and the default append works, although there might be a
space at the end
//
// There should be no space between the two strings
test = TEXT("I have a");
test.append( TEXT("cat named Biff"));
tcout << test << endl;
// this will check for an extra space. Result: there is an
extra space
// The period should be removed
test = TEXT("He looks at my eyes and says.");
test.resize(test.size() - 1);
test.append( TEXT("Meow meow meow"));
tcout << test << endl;
// operator += test.
test = "ow!";
ttest = L"Break it down!";
test += ttest;
tcout << test << endl;
// operator+ test 1
test = "Stop; ";
ttest = L"Hammer Time!";
tcout << (test + ttest) << endl;
// operator+ test #2
test = "123";
ttest = TEXT("456");
test = test + ttest;
tcout << test << endl;
test = "123";
test = test + test;
tcout << test << endl;
test = "123";
ttest = "123";
cout << endl;
// test the != operator with tstring. Should not work
tcout << test << TEXT("!=") <<
ttest << endl;
if ( test != ttest )
{
tcout << TEXT("Equal/incorrect
result!") << endl;
}
else
{
tcout << TEXT("Not equal
detected/Correct result") << endl;
}
cout << endl;
// test the == operator with tstring
tcout << test << TEXT("==") <<
ttest << endl;
if ( test == ttest )
{
tcout << TEXT("Equal/correct
result!") << endl;
}
else
{
tcout << TEXT("Equal/Incorrect
result") << endl;
}
cout << endl;
ttest = "Never going to let you down.";
// test the != operator with tstring. Should work
if ( test != ttest )
{
tcout << test << TEXT("!=")
<< ttest << endl;
tcout << TEXT("Not equal/correct
result!") << endl;
}
else
{
tcout << TEXT("Not equal/Detected
==") << endl;
}
cout << endl;
// test the != operator with TCHAR *. Didn't work before
if ( test != TEXT("Mmm... donut!") )
{
tcout << test << TEXT("!=")
<< TEXT("Mmm... donut!") << endl;
tcout << TEXT("Not equal/correct
result!") << endl;
}
else
{
tcout << TEXT("Not equal/Detected
==") << endl;
}
cout << endl;
// test the != operator with TCHAR *.
tcout << test << TEXT("!=") <<
TEXT("123") << endl;
test.assign(TEXT("123"));
if ( test != TEXT("123") )
{
tcout << TEXT("!=
detected/incorrect result!") << endl;
}
else
{
tcout << TEXT("!= not
detected/They are Equal/Correct result detected") << endl;
}
cout << endl;
// test the == operator with TCHAR *
tcout << test << TEXT("==") <<
TEXT("123") << endl;
if ( test == TEXT("123") )
{
tcout << TEXT("Equal/correct
result!") << endl;
}
else
{
tcout << TEXT("Equal/Detected !=")
<< endl;
}
cout << endl;
system("pause");
return 42;
}
Like twStringStuff, feel free to use my code in your projects. The
source code IS copyrighted, but if you make enhancements and reupload
it, feel free to and credit yourself for your work, even if you use a
nickname like "d00d" to do so.