Originally Posted by
Queue
Dave_Sinkula, the code you posted would not be considered a FSM, right? If it is, what exactly made it so? And if not, what would you have to add so that it would be considered a FSM?
No. An FSM would have states, like first field, second field, etc. Then your foo might look at what state is in and do different things based on the state. Or perhaps foo is called for fields one and two, and some function bar is called for other fields. That sort of thing.
[edit]Kinda like this.
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))
{
int state = 0;
const char *end = text, *start = text;
while ( *start != '\0' )
{
while ( *end != '\0' && !isspace(*end) )
{
++end;
}
/*
* Do stuff.
*/
if ( state == 0 )
{
if ( !foo(start, end - start) )
{
state = 1;
}
}
if ( state == 1 )
{
printf("other state : \"%.*s\"\n", end - start, start);
}
/*
* 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"
other state : "string1"
other state : "string2"
other state : "string3"
*/