Sign in to follow this  

Converting from string to bool etc

This topic is 3717 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

I have been doing a little programming recently based on this article http://ati.amd.com/developer/gdc/AtiGDC02Vlachos.PDF basically I am loading some data from a text file and one of the things i have in it is: Lighting true to signify lighting should be enabled. However since i dont read in say bool true i cant right now think of a way to tell the program that it is bool. I have a class that is a list of propertys and they have a union to be set as float, int, bool etc which works for most things except that when creating a property i normally set the type. Here is a function that I'd normally use when setting a value
bool Property::SetUnknownValue(String const &value)
{
	//not done vectors or matrices yet
	bool returnValue = true;
	switch(m_Type)
	{
		case INT:
			*m_Data.m_Int = atoi(value.c_str());
			break;
		case FLOAT:
			*m_Data.m_Float = atof(value.c_str());
			break;
		case STRING:
			*m_Data.m_String = value.c_str();
			break;
		case BOOL:
			*m_Data.m_Bool = (value.compare("true") == 0) || (value.compare("TRUE") == 0);
			break;
		default:
			returnValue = false;
			break;
	}

	return returnValue;
}



This normally works but notice it relies on you knowing the type. So basically can i load in without knowing the type or will i have to put in the file bool Lighting true or simmilar, not all that disasterous really lol.

Share this post


Link to post
Share on other sites
You can evaluate the string value to check what type that would be, depending on the format. This is kinda like the Lex representation:

integer: ('-' | '+')?number+
float: integer?'.'number+ | integer('e' | 'E')integer
bool: "true" | "false"
string: .

Integers are composed of 1 or 0 of one of '-' or '+' followed by at least 1 number.
Floats are composed of 1 or 0 integer followed by '.' followed by at least 1 number; or an integer followed by 'e' or 'E' followed by another integer.
Bools are composed by one of those 2 strings.
String is everything else.

This leaves to the fact a "true" string would never be read as a string, even if you wanted to. You would have to explicitally set it to read as a string.

Share this post


Link to post
Share on other sites
You could:

a) Infer the type by using use regex or some other pattern matching technique to determine what type the field is. You might require that strings be enclosed in "" and "'s in the string need to be escaped to make your life easier.
b) Require strong typing in your data files.
c) Given that your data structure has room for the different types a property might have, just store something for everything it might be.
1) Store the string.
2) if conversion to bool succeeds, store a bool.
3) if conversion to short succeeds, store a short
4) etc...

Look at boost::any and boost::lexical_cast. They provide powerful tools for what you're doing.

Share this post


Link to post
Share on other sites
A lot more work then with no benefit?

hmm maybe it would be best just to declare the type when writing in the text file.

If you read the article, do you think ATI had the files like they presented or do you think they added type declerations?

Share this post


Link to post
Share on other sites

This topic is 3717 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this