Page 1 of 1

String and ...

Posted: Mon Sep 04, 2006 17:55
by Rakkar
I want to use CEGUI::String to contain a number as follows:

int a = 5;
CEGUI::String myString;
myString << 5;

I can tell by looking at the code and by trying that this is not supported. The sample just used sprintf for the FPS display, Before I go off and write my own thing, I was wondering how the CEGUI team does this? Do you also just use sprintf?

Also, suppose that I allow users in my game to choose their own names, and they happen to use %s in their name, such as "John the %s master". If I pass this to CEGUI::String, is it going to get confused and treat %s as a token?

Posted: Mon Sep 04, 2006 18:29
by lindquist
Take a look at the PropertyHelper class.

Posted: Tue Sep 05, 2006 06:08
by Rakkar
Ah I see, you are using sprintf. IMO It would have been better to just allow the user to pass a formatted string as a constructor argument such as

CEGUI::String myStr("%f", 5);

Regarding the second part of my question, I believe that it is safe to pass %s or other format specifiers to sprintf.

Posted: Tue Sep 05, 2006 12:16
by Rackle
Another approach is the following:

Code: Select all


template<class T> bool stringToValue(const char* s, T &value)
{
   std::istringstream i(s);
   return (i >> value);
}

template<class T> std::string valueToString(const T &value)
{
   std::ostringstream o;
   o << value;
   return o.str();
}