|
| iMatix home page | << | < | > | >> |
SFLVersion 2.11 |
#include "sflstr.h"
char *
getstrfld (
char *strbuf,
int fldno,
int ofset,
char *sep,
char *retstr)
Gets a sub-string from a formated string. nice strtok replacement. usage: char strarray[] = { "123,456,789,abc" }; char strretbuff[4]; getstrfld (strarray, 2, 0, ",", strretbuff); This would return the string "789" and place it also in strretbuff. Returns a NULL if fldno is out of range, else returns a pointer to head of the buffer. Submitted by Scott Beasley jscottb@infoave.com
{
char *offset, *strptr;
int curfld;
ASSERT (strbuf);
ASSERT (sep);
ASSERT (retstr);
offset = strptr = (char *)NULL;
curfld = 0;
strbuf += ofset;
while (*strbuf)
{
strptr = !offset ? strbuf : offset;
offset = strpbrk ((!offset ? strbuf : offset), sep);
if (offset)
offset++;
else if (curfld != fldno)
{
*retstr = (char)NULL;
break;
}
if (curfld == fldno)
{
strncpy (retstr, strptr,
(int)(!offset ? strlen (strptr)+ 1 :
(int)(offset - strptr)));
if (offset)
retstr[offset - strptr - 1] = 0;
break;
}
curfld++;
}
return retstr;
}
| | << | < | > | >> |
|