The variable "start" is used to keep track of progress through the buffer.
// thread safe strtok. Also can do multiple buffers at a time.
// returned char* must be delete[]ed when finished.
static char* StrTok(const char* buffer, const char* delims, int& start)
{
bool exec = true;
int end = -1;
for (int i = start; buffer[i] != '\0' && exec; i++)
{
for (int j = 0; delims[j] != '\0'; j++)
{
if (buffer[i] == delims[j])
{
end = i;
if (end == start)
start++;
else
{
exec = false;
break;
}
}
}
}
if (end < start)
end = start+1;
char* token = NULL;
int len = end - start;
if (len > 0)
{
token = new char[len+1];
for (int i = start; i < end; i++)
token[i-start] = buffer[i];
token[len] = 0;
}
start = end;
return token;
};