Hm. I dunno about a full-fledged FSM for this, I've never written a complex parser. Some ideas...
Code:
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int myfunction(const char *text, int len)
{
const char *comma = strchr(text, ',');
if ( comma == NULL )
{
return 0;
}
printf("\"%.*s\"\n", len, text);
fflush(stdout);
return 1;
}
void parse(const char *text, int (*foo)(const char *, int len))
{
const char *end = text, *start = text;
while ( *start != '\0' )
{
while ( *end != '\0' && !isspace(*end) )
{
++end;
}
/*
* Do stuff.
*/
if ( !foo(start, end - start) )
{
break;
}
/*
* Move the start and end pointers.
*/
start = end;
while ( *start != '\0' && isspace(*start) )
{
++start;
}
end = start;
}
}
int main(void)
{
const char line[] = "user1,user2 group1,group2,group3 string1 string2 string3";
parse(line, myfunction);
return 0;
}
/* my output
"user1,user2"
"group1,group2,group3"
*/
Some example of checking for a comma.