String and ...

For help with general CEGUI usage:
- Questions about the usage of CEGUI and its features, if not explained in the documentation.
- Problems with the CMAKE configuration or problems occuring during the build process/compilation.
- Errors or unexpected behaviour.

Moderators: CEGUI MVP, CEGUI Team

Rakkar
Not too shy to talk
Not too shy to talk
Posts: 34
Joined: Tue Aug 15, 2006 02:49

String and ...

Postby Rakkar » Mon Sep 04, 2006 17:55

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?

User avatar
lindquist
CEGUI Team (Retired)
Posts: 770
Joined: Mon Jan 24, 2005 21:20
Location: Copenhagen, Denmark

Postby lindquist » Mon Sep 04, 2006 18:29

Take a look at the PropertyHelper class.

Rakkar
Not too shy to talk
Not too shy to talk
Posts: 34
Joined: Tue Aug 15, 2006 02:49

Postby Rakkar » Tue Sep 05, 2006 06:08

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.

Rackle
CEGUI Team (Retired)
Posts: 534
Joined: Mon Jan 16, 2006 11:59
Location: Montréal

Postby Rackle » Tue Sep 05, 2006 12:16

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();
}


Return to “Help”

Who is online

Users browsing this forum: No registered users and 32 guests