Esempio n. 1
0
/**
 * @brief Convert string value to desired type.
 * @param [in, out] value Value to convert.
 * @param [in] nType Type to convert to.
 * @return true if conversion succeeded, false otherwise.
 * @note Only supports converting to integer and boolean at the moment.
 * @todo Add other conversions (float?).
  */
bool COption::ConvertString(varprop::VariantValue & value, varprop::VT_TYPE nType)
{
	String svalue = value.GetString();
	switch(nType)
	{
	case varprop::VT_INT:
		// Convert string to integer
		{
			int val=0;
			if (!GetAsInt(svalue.c_str(), val))
				return false;
			value.SetInt(val);
			return true;
		}
	case varprop::VT_BOOL:
		// Convert string to boolean
		{
			std::transform(svalue.begin(), svalue.end(), svalue.begin(), ::tolower);
			if (svalue == _T("1") || !svalue.compare(_T("yes"))
				|| !svalue.compare(_T("true")))
			{
				value.SetBool(true);
				return true;
			}
			if (svalue == _T("0") || !svalue.compare(_T("no"))
				|| !svalue.compare(_T("false")))
			{
				value.SetBool(false);
				return true;
			}
			return false;
		}
	}
	return false;
}