In case you or anyone else is still wondering about this, I have made another version with strchr to split the string. Here it is:
m_AllowedMaps.clear();
const char* allowedMaps = sConfig.GetStringDefault("Allowed.Maps", "").c_str();
// if we actually have a string...
if(strcmp(allowedMaps, "") != 0)
{
// next is a pointer to pos of next comma; prev is a pointer to last comma
const char* next;
// find first position of comma
next = strchr(allowedMaps, ',');
// no comma, insert only one entry
if(!next)
m_AllowedMaps.insert(atoi(allowedMaps));
else
{
// start at the beginning
const char* prev = allowedMaps;
// buffer for the argument
char* buffer;
// length of the fragment
size_t length;
// while we still have more
while(next != NULL)
{
// how big the string fragment will be; factor in null terminator (+ 1)
length = next - prev + 1;
// allocate enough room in the buffer to hold the string fragment
buffer = new char[length];
// copy the string fragment over
strncpy(buffer, prev, length);
// and don't forget the terminator
buffer[length - 1] = '\\0';
// insert the new entry into our set of banned maps
m_AllowedMaps.insert(atoi(buffer));
// set pos of last comma, + 1 char (i.e. the last comma) and get new pointer to next comma
prev = next + sizeof(char);
next = strchr(next + 1, ',');
// deallocate buffer to prevent memory leaks
delete buffer;
}
// once more for the last argument
// we already have this pointer set so use it
m_AllowedMaps.insert(atoi(prev));
}
}
In this case, it reads a string from the config and builds a set of map IDs (m_AllowedMaps is a std::set<uint32>) It has no problem with spacing, "0,530" and "0, 530" both work fine. I'm sure it could be easily made into a function.