IrrXML nodes to int

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
nander
Posts: 6
Joined: Fri Feb 26, 2010 6:35 pm

IrrXML nodes to int

Post by nander »

Hi,

I've got a xml file, example:
<root>
<child>
<value>21</value>
<value2>213</value>
<value3>4125</value>
<value4>2183</value>
</child>
</root>
I want to get the integer values of them.
Is there an easy way to do this?
Or more specifically, is there a way to convert const wchar_t* (or stringw, or stringc) to integer?
greenya
Posts: 1012
Joined: Sun Jan 21, 2007 1:46 pm
Location: Ukraine
Contact:

Re: IrrXML nodes to int

Post by greenya »

nander wrote: Or more specifically, is there a way to convert const wchar_t* (or stringw, or stringc) to integer?

Code: Select all

int _wtoi(const wchar_t *str);
(c) http://msdn.microsoft.com/en-us/library ... S.80).aspx
nander
Posts: 6
Joined: Fri Feb 26, 2010 6:35 pm

Re: IrrXML nodes to int

Post by nander »

greenya wrote:
nander wrote: Or more specifically, is there a way to convert const wchar_t* (or stringw, or stringc) to integer?

Code: Select all

int _wtoi(const wchar_t *str);
(c) http://msdn.microsoft.com/en-us/library ... S.80).aspx
I tried that one.. _wchar is not declared in this scope.. It seems to be only for MS. C++, not for linux (GCC). Or are they in a seperate library (apart from <string.h>
Tihtinen
Posts: 76
Joined: Fri Jan 08, 2010 3:12 pm
Location: Finland

Post by Tihtinen »

nander
Posts: 6
Joined: Fri Feb 26, 2010 6:35 pm

Post by nander »

I tried atoi.. Cannot convert error..
Doesn't atoi require a const char*, instead of a const wchar_t*?
For some reason it just doesn't seem to work nice.. Why on earth does getNodeData return a const char_t*? why not just a const char*?

I start with this:
core::stringw test;
test = xml->getNodeData();

//And I want to end with:
int integer = // ...//
What code should be placed in between?
greenya
Posts: 1012
Joined: Sun Jan 21, 2007 1:46 pm
Location: Ukraine
Contact:

Post by greenya »

If you have wchar_t* and you want to call atoi(), you may do next:

wchar_t* text = L"1234";
int value = atoi(stringc(text).c_str());
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

If you really must convert a wchar_t string to an integer, then you should either convert the wchar_t string to a char string, and then use atoi(), strtol(), strtoul() or strtod(), or you could write your own routine to do it directly. The solution would be trivial if you reformatted your xml to use xml attributes. A simple call to reader->getAttributeValueAsInt() would do the trick. An even better solution would be to use the Irrlicht IAttributes interface for reading and writing your xml data.

Travis
nander
Posts: 6
Joined: Fri Feb 26, 2010 6:35 pm

Post by nander »

vitek wrote:If you really must convert a wchar_t string to an integer, then you should either convert the wchar_t string to a char string, and then use atoi(), strtol(), strtoul() or strtod(), or you could write your own routine to do it directly. The solution would be trivial if you reformatted your xml to use xml attributes. A simple call to reader->getAttributeValueAsInt() would do the trick. An even better solution would be to use the Irrlicht IAttributes interface for reading and writing your xml data.

Travis
I'll try the getAttributeValueAsInt. I read of it before, but now I've made up my mind.. I first thought the XML would look terrible, but it doesn't look that bad. So I'll do it that way.. Thanks all.

Edit: Why does getAttributeValueAsInt return a const char*?
-- Solved it.. I missed a 'L'

Edit::
if (!strcmp("model", xml->getNodeName())) doesn't work for some reason (http://www.ambiera.com/irrxml/docu/index.html)
It gives an error (converting const wchar_t* to const char*).
I used to use Irrlicht 1.6.. I never ran into any wchar errors.. Why on earth do all those functions exit wchar variables?
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

nander wrote:if (!strcmp("model", xml->getNodeName())) doesn't work for some reason (http://www.ambiera.com/irrxml/docu/index.html)
It gives an error (converting const wchar_t* to const char*).
I used to use Irrlicht 1.6.. I never ran into any wchar errors.. Why on earth do all those functions exit wchar variables?
The function strcmp() is declared like this...

Code: Select all

extern "C" {
  int strcmp (const char* s1, const char* s2);
}
That means that the function expects to take two arguments of type const char*.

When you call the function like this...

Code: Select all

strcmp("model", xml->getNodeName())
the first argument is of the correct type, but the second argument is of type const wchar_t*. The types const char* and const wchar_t* are incompatible, so you get a compile error. The simple solution is to use wcscmp() which is fairly portable (you must include wchar.h).

Travis
anirul
Posts: 30
Joined: Wed Oct 07, 2009 6:03 am
Location: Geneva
Contact:

STL?

Post by anirul »

I know that STL is not suppose to be used with irrlicht but I usually do that:

Code: Select all

#include <sstream>
#include <string>
[...]
// your pointer to a wchar_t string
wchar_t* p;
// your return int
int i;
// convert STL style
{
   std::wstringstream ss(p);
   ss >> i;
}
If I didn't do any typo this should work on any OS sporting STL in the world. Some people will argue about performance issue but if you read XML files just once, who care!
Post Reply