This commit was generated by cvs2svn to compensate for changes in r2,
which included commits to RCS files with non-trunk default branches. git-svn-id: svn://svn.cc65.org/cc65/trunk@3 b7a2c559-68d2-44c3-8de9-860c34a00d81
This commit is contained in:
77
libsrc/common/strtok.c
Normal file
77
libsrc/common/strtok.c
Normal file
@@ -0,0 +1,77 @@
|
||||
/*
|
||||
* strtok.c
|
||||
*
|
||||
* Ullrich von Bassewitz, 11.12.1998
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include <string.h>
|
||||
|
||||
|
||||
|
||||
/*****************************************************************************/
|
||||
/* Data */
|
||||
/*****************************************************************************/
|
||||
|
||||
|
||||
|
||||
/* Memory location that holds the last input */
|
||||
static char* Last = 0;
|
||||
|
||||
|
||||
|
||||
/*****************************************************************************/
|
||||
/* Code */
|
||||
/*****************************************************************************/
|
||||
|
||||
|
||||
|
||||
char* strtok (char* s1, const char* s2)
|
||||
{
|
||||
char c;
|
||||
char* start;
|
||||
|
||||
/* Use the stored location if called with a NULL pointer */
|
||||
if (s1 == 0) {
|
||||
s1 = Last;
|
||||
}
|
||||
|
||||
/* If s1 is empty, there are no more tokens. Return 0 in this case. */
|
||||
if (*s1 == '\0') {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Search the address of the first element in s1 that equals none
|
||||
* of the characters in s2.
|
||||
*/
|
||||
while ((c = *s1) && strchr (s2, c) != 0) {
|
||||
++s1;
|
||||
}
|
||||
if (c == '\0') {
|
||||
/* No more tokens found */
|
||||
Last = s1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Remember the start of the token */
|
||||
start = s1;
|
||||
|
||||
/* Search for the end of the token */
|
||||
while ((c = *s1) && strchr (s2, c) == 0) {
|
||||
++s1;
|
||||
}
|
||||
if (c == '\0') {
|
||||
/* Last element */
|
||||
Last = s1;
|
||||
} else {
|
||||
*s1 = '\0';
|
||||
Last = s1 + 1;
|
||||
}
|
||||
|
||||
/* Return the start of the token */
|
||||
return start;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user