/* ******************************************************************************* * Copyright (c) 1996 Martin Poole * ******************************************************************************* ** ** WARNING !! WARNING !! WARNING !! WARNING !! WARNING !! WARNING !! ** ** Any changes to be made to this file should first be checked with ** mplib1 source control for library integrity. ** ** mplib1 source control can be reached at mplib1@quatermass.co.uk ** * * $Source: /home/cvs/cvsroot/onelan/onelan/src/mplib1/libsrc/mpstrtok.c,v $ * $Author: mpoole $ * $Date: 2002/10/07 09:37:39 $ * $Revision: 1.2 $ * Purpose : * ******************************************************************************* * * Change History * * $Log: mpstrtok.c,v $ * Revision 1.2 2002/10/07 09:37:39 mpoole * Initial checkin of mplib1-3.1.0 * * Revision 1.1 2002/10/07 09:36:57 mpoole * Initial checkin of mplib1-3.1.0 * * ******************************************************************************* */ #ident "$Header: /home/cvs/cvsroot/onelan/onelan/src/mplib1/libsrc/mpstrtok.c,v 1.2 2002/10/07 09:37:39 mpoole Exp $" /* ------------------------------------------------------------------ Include files ------------------------------------------------------------------ */ #include #include #include #include #include /* ------------------------------------------------------------------ defines ------------------------------------------------------------------ */ struct strtok { char *str_st; char *str_end; char *str_nxt; }; /* ------------------------------------------------------------------ Code starts here ------------------------------------------------------------------ */ char * mpstrtok( char *str, void **ftok, const char *seps ) { struct strtok *tok= *ftok; char *cp,*cp2; if (str) { /* first call, set things up and give first details */ tok = (struct strtok *)malloc(sizeof(struct strtok)); *ftok = tok; if (tok == NULL) return(NULL); tok->str_st = str; tok->str_end = str + strlen(str); tok->str_nxt = str; }else if (tok==NULL) return(NULL); if (seps==NULL) { /* early termination, free memory */ free(tok); *ftok = NULL; return(NULL); } /* skip leading seperators */ cp = tok->str_nxt; while (*cp && strchr(seps,*cp)) cp++; /* have we gone too far ? */ if (cp >= tok->str_end) { free(tok); *ftok = NULL; return(NULL); } /* now mark the end of the string */ cp2 = cp; while (*cp2 && strchr(seps,*cp2)==NULL) cp2++; tok->str_nxt = cp2 + ((*cp2)? 1 : 0); *cp2 = '\0'; return(cp); } /* -- End of File -- */