mirror of
https://github.com/owntone/owntone-server.git
synced 2025-02-26 12:59:19 -05:00
89 lines
2.3 KiB
C
89 lines
2.3 KiB
C
/* Reentrant string tokenizer. Generic version.
|
|
Copyright (C) 1991,1996-1999,2001,2004 Free Software Foundation, Inc.
|
|
This file is part of the GNU C Library.
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation; either version 2, or (at your option)
|
|
any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License along
|
|
with this program; if not, write to the Free Software Foundation,
|
|
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
# include "config.h"
|
|
#endif
|
|
|
|
#ifndef HAVE_STRTOK_R
|
|
|
|
#include <stdio.h>
|
|
|
|
/* Parse S into tokens separated by characters in DELIM.
|
|
If S is NULL, the saved pointer in SAVE_PTR is used as
|
|
the next starting point. For example:
|
|
char s[] = "-abc-=-def";
|
|
char *sp;
|
|
x = strtok_r(s, "-", &sp); // x = "abc", sp = "=-def"
|
|
x = strtok_r(NULL, "-=", &sp); // x = "def", sp = NULL
|
|
x = strtok_r(NULL, "=", &sp); // x = NULL
|
|
// s = "abc\0-def\0"
|
|
*/
|
|
char *strtok_r(char *s, const char *delim, char **last)
|
|
{
|
|
char *spanp;
|
|
int c, sc;
|
|
char *tok;
|
|
|
|
if (s == NULL && (s = *last) == NULL) {
|
|
return NULL;
|
|
}
|
|
|
|
/*
|
|
* Skip (span) leading delimiters (s += strspn(s, delim), sort of).
|
|
*/
|
|
cont:
|
|
c = *s++;
|
|
for (spanp = (char *)delim; (sc = *spanp++) != 0; ) {
|
|
if (c == sc) {
|
|
goto cont;
|
|
}
|
|
}
|
|
|
|
if (c == 0) { /* no non-delimiter characters */
|
|
*last = NULL;
|
|
return NULL;
|
|
}
|
|
tok = s - 1;
|
|
|
|
/*
|
|
* Scan token (scan for delimiters: s += strcspn(s, delim), sort of).
|
|
* Note that delim must have one NUL; we stop if we see that, too.
|
|
*/
|
|
for (;;) {
|
|
c = *s++;
|
|
spanp = (char *)delim;
|
|
do {
|
|
if ((sc = *spanp++) == c) {
|
|
if (c == 0) {
|
|
s = NULL;
|
|
}
|
|
else {
|
|
char *w = s - 1;
|
|
*w = '\0';
|
|
}
|
|
*last = s;
|
|
return tok;
|
|
}
|
|
}
|
|
while (sc != 0);
|
|
}
|
|
/* NOTREACHED */
|
|
}
|
|
#endif
|