1 : #ifdef HAVE_CONFIG_H
2 : #include "config.h"
3 : #endif
4 :
5 : #include "gd.h"
6 : #include "gdhelpers.h"
7 : #include <stdlib.h>
8 : #include <string.h>
9 :
10 : /* TBB: gd_strtok_r is not portable; provide an implementation */
11 :
12 : #define SEP_TEST (separators[*((unsigned char *) s)])
13 :
14 : char *
15 : gd_strtok_r (char *s, char *sep, char **state)
16 5 : {
17 : char separators[256];
18 : char *start;
19 5 : char *result = 0;
20 5 : memset (separators, 0, sizeof (separators));
21 15 : while (*sep)
22 : {
23 5 : separators[*((unsigned char *) sep)] = 1;
24 5 : sep++;
25 : }
26 5 : if (!s)
27 : {
28 : /* Pick up where we left off */
29 0 : s = *state;
30 : }
31 5 : start = s;
32 : /* 1. EOS */
33 5 : if (!(*s))
34 : {
35 0 : *state = s;
36 0 : return 0;
37 : }
38 : /* 2. Leading separators, if any */
39 5 : if (SEP_TEST)
40 : {
41 : do
42 : {
43 0 : s++;
44 : }
45 0 : while (SEP_TEST);
46 : /* 2a. EOS after separators only */
47 0 : if (!(*s))
48 : {
49 0 : *state = s;
50 0 : return 0;
51 : }
52 : }
53 : /* 3. A token */
54 5 : result = s;
55 : do
56 : {
57 : /* 3a. Token at end of string */
58 228 : if (!(*s))
59 : {
60 5 : *state = s;
61 5 : return result;
62 : }
63 223 : s++;
64 : }
65 223 : while (!SEP_TEST);
66 : /* 4. Terminate token and skip trailing separators */
67 0 : *s = '\0';
68 : do
69 : {
70 0 : s++;
71 : }
72 0 : while (SEP_TEST);
73 : /* 5. Return token */
74 0 : *state = s;
75 0 : return result;
76 : }
|