I will show you code that extracts values from a comma delimited string. This may help you. I use this in my SpaceX prototype to extract vectors from XML config files.
Code:
template <typename T> bool XMLUtils::ExtractValuesFromString(const std::string & text,const std::string & delim,T *pValues)
{
bool result = false;
size_t pos = 0;
size_t pos2 = text.find(delim,pos);
int count = 0;
std::string value;
while (pos2 != std::string::npos)
{
result = true; //Found at least 1 of the specified delimiters
if (pos == 0)
{
value = text.substr(pos,(pos2 - pos));
}
else
{
value = text.substr(pos + 1,(pos2 - pos));
}
pValues[count] = static_cast<T>(atof(value.c_str()));
++count;
pos = pos2;
pos2 = text.find(delim,pos + 1);
}
size_t length = text.length();
std::string last = text.substr(pos + 1,text.length() - (pos + 1));
pValues[count] = static_cast<T>(atof(last.c_str()));
return result;
}
Forgive the template code. It is there so that I can use the function for int, float and what have you. Pay attention to how I'm using find to find all the specified delimiters. Forget about the sub string extraction portion as well as the data type stuff.
Here is some code that uses the function:
Code:
const D3DXCOLOR XMLUtils::ExtractColorFromString(const std::string &text)
{
D3DXCOLOR result = D3DXCOLOR(0.0f,0.0f,0.0f,0.0f);
float values[4];
if (ExtractValuesFromString<float>(text,",",values))
{
result = D3DXCOLOR(values[0],values[1],values[2],values[3]);
}
return result;
}