| iMatix home page
| << | < | > | >>
SFL Logo SFL
Version 2.11

 

getstrfld

#include "sflstr.h"
char *
getstrfld (
    char *strbuf,
    int fldno,
    int ofset,
    char *sep,
    char *retstr)

Synopsis

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

Source Code - (sflstr.c)

{
   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;
}

| << | < | > | >> iMatix Copyright © 1996-2000 iMatix Corporation