#include "libut/ut.h" int UT_stridx(char *string, char *strary[]);
This function looks to see if the given string appears in the strary array of strings. If it does, the index in strary is returned. Otherwise, -1 is returned.
The comparison is by string equality, not by pointer equality.
This is useful for testing a string against a list of allowed values, particularly when the index into the allowed values is a state variable.
char *in, *modes[] = { "wait", "run", "stop" }; int i; in = "stop"; /* imagine this is user input */ i = UT_stridx( in, modes); if (i != -1) UT_LOG(Info, "Mode set to %d (%s)", i, modes[i]); /* logs "Mode set to 2 (stop)" */
Troy D. Hanson <thanson@users.sourceforge.net>