summaryrefslogtreecommitdiff
path: root/lib/strtok_r.c
blob: cead029a2a662f359bb833dd6434c1a47211c864 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*
 * written for xine project, 2004-2006
 *
 * public domain replacement function for strtok_r()
 *
 */

#include "config.h"

#include <stddef.h>
#include <string.h>

char *xine_private_strtok_r(char *s, const char *delim, char **ptrptr) {
  char *next;
  size_t toklen, cutlen;

  /* first or next call */
  if (s) *ptrptr = s;
  else s = *ptrptr;

  /* end of searching */
  if (!s || s == '\0') return NULL;

  /* cut the initial garbage */
  cutlen = strspn(s, delim);
  s = s + cutlen;

  /* pointer before next token */
  if ((toklen = strcspn(s, delim)) == 0) {
    *ptrptr = NULL;
    return NULL;
  }
  next = s + toklen;

  /* cut current token */
  *next = '\0';

  /* prepare next call */
  *ptrptr = next + 1;

  /* return the token */
  return s;
}