string or stringw, stringc with TinyXML

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
elfuz
Posts: 14
Joined: Wed Oct 13, 2010 10:20 am

string or stringw, stringc with TinyXML

Post by elfuz »

Hi all

I've looked all over these forums but i cannot find a clear answer...

I'm migrating my project from HGE to IRRLICHT, however what i cannot seem to figure out currently is how to use the strings correctly

this is what i used for HGE and string

Code: Select all

string SceneManager::XMLString(TiXmlElement *XMLNode, const string &node, string defaultValue)
{
	const char* tmp;
	if(XMLNode->FirstChildElement(node.c_str())) {
        tmp = XMLNode->FirstChildElement(node.c_str())->GetText();
		if (tmp == NULL) return defaultValue; else return string(tmp);
	} else {
	     return defaultValue;
	}
}
however if i want to use this on irrlicht, how should i implement this function so it can read all kinds of data from the xml file. This can be

a. a texture name
b. an object name
c. an object command
d. text (in unicode)


but if i use the stringw (which seems unicode-wise most interesting for the text). Will i be able to pass that string to irrlicht's texture loader ?

Also i notice that sometimes L"sometext" is used... What does the L stand for?

greetings
el Fuz
Luben
Posts: 568
Joined: Sun Oct 09, 2005 10:12 am
Location: #irrlicht @freenode

Post by Luben »

L"Some text" makes the string a wide string, using wchar_t's instead of char's. Meaning it's more unicode friendly*
If you load data into a stringw, it's fairly easy to make a simple conversion into a stringc / char*.

Code: Select all

stringw text(L"Text");
stringc temp(text);
char *ptr = temp.c_str();
*not really afaik, but more internationalistic than chars at least. In VS one can use L"text" for 'unicode' and 'wide char'-builds. Someone with more expert knowledge may fill us in on the details.
Nalin
Posts: 194
Joined: Thu Mar 30, 2006 12:34 am
Location: Lacey, WA, USA
Contact:

Post by Nalin »

Code: Select all

const char* text = "Some text";
const wchar_t* wtext = L"Some text";
The L"" creates a wchar_t string.

Now, if you are trying to do unicode, you will need to use my custom patches and build a unicode/wchar_t version of Irrlicht from source:
http://irrlicht.sourceforge.net/phpBB2/ ... hp?t=37296

The reason is that wchar_t is not portable. On Windows, it is a 16-bit value. On Linux, it is a 32-bit value. The Windows API views wchar_t strings as UTF-16 encoded strings. Converting UTF-16 to UTF-8 is not that simple. Luben's example IS how you would normally convert between stringw and stringc in Irrlicht, but it fails when you are storing actual unicode data in your strings. That is the whole point of my patches: to create a new string class, ustring, that stores a unicode string and can convert to UTF-8 encoded stringc and UTF-16 encoded stringw.

But, the other requirement is to compile Irrlicht using the unicode character set, and to enable the _IRR_WCHAR_FILESYSTEM define in IrrCompileConfig.h. Enabling both of those will result in Irrlicht calling the wchar_t versions of the Windows API. Load up your file names in a ustring, call the toWCHAR_s() function, and pass it to Irrlicht. It should work properly then.

So, there you have it. If you don't care about unicode and internationalization, and you must pass a stringc to Irrlicht, just use Luben's method to convert your stringw into a stringc.
Post Reply