Jump to content
  • Advertisement
Sign in to follow this  
brcolow

Separating One Into Three

This topic is 5454 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey there, I have a RGB value in an XML file like so:
<lightcolor>255 255 255</lightcolor>
When I get the data as a const char * I then want to split it up into three separate variables. I tried using sscanf but that produced whacky results. How can I do this? Thanks :)

Share this post


Link to post
Share on other sites
Advertisement
Step through the string and break it up into other strings. As terminators use the space, "<" and ">".

You should get this array:
[0] lightcolor
[1] 255
[2] 255
[3] 255
[4] /lightcolor

Now you know that when string[0]=="lightcolor", you have your rgb elements in string[1/2/3]...

Share this post


Link to post
Share on other sites
I think you would want to parse out the number section first, by doining a substr from ">" to "</".
Then you could use a stringstream to extract 3 numbers.

Share this post


Link to post
Share on other sites
I already parsed out the XML tags..but what is a stringstream? Or, how would I "step through the string"?

Thanks

Share this post


Link to post
Share on other sites
Im a low level programmer at heart, so I use C primarily.

When I say step through the string, I actually mean checking each char.

Basicly you have an array of "char*", other strings you'll fill up as you step through your "primary" string.

Whenever you find a non-terminator character, you place that character into the current sub string, else, add another substring.

In pseudo-code, something along these lines:


UINT t;
UINT css=0; // Current sub-string
CString substrings[100];

for(t=0; t< len(XMLstring); t++)
{
if(XMLstring[t] == terminator)
{
css++;
}
else
{
substring[css] += XMLstring[t];
};
};



Now its just a question of figuring out which substrings are numbers and convert them (to integer, float, etc...).

Share this post


Link to post
Share on other sites
stringstream is an I/O stream in the STL that uses a string for storage (another stream in the STL for example is fstream that uses a file for storage). For detailed info on stringstream check out MSDN, but in short it allows you to do formatted reading/writing with a string. This is basically what you want:

char *color_string = "10 20 30"; // Get this from your XML
std::stringstream color_stream(color_string);
int r,g,b;
color_stream >> r >> g >> b;


As the result r=10, b=20 and g=30.

Share this post


Link to post
Share on other sites
The C way looks completely uneccassarily too much work. But, the stringstream method gives me a ton of errors :(

Share this post


Link to post
Share on other sites
strtok()


int r, g, b;
char * colorstring;
r = atoi(strtok(colorstring, " "));
g = atoi(strtok(NULL, " "));
b = atoi(strtok(NULL, " "));


Note that you should check to see if strtok returns null and catch that...

Share this post


Link to post
Share on other sites
Quote:
Original post by brcolow
The C way looks completely uneccassarily too much work. But, the stringstream method gives me a ton of errors :(

I probably should have mentioned, you need to
#include <stringstream>

Share this post


Link to post
Share on other sites
Quote:
Original post by Ximmer
strtok()


int r, g, b;
char * colorstring;
r = atoi(strtok(colorstring, " "));
g = atoi(strtok(NULL, " "));
b = atoi(strtok(NULL, " "));


Note that you should check to see if strtok returns null and catch that...


Thank you, but, how can I convert some const char* to char* because my string is originally a const char* and I can't change that.
Thanks :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!