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 * gd_strtok_r(char * s,char * sep,char ** state)15gd_strtok_r (char *s, char *sep, char **state) 16 { 17 char separators[256]; 18 char *start; 19 char *result = 0; 20 memset (separators, 0, sizeof (separators)); 21 while (*sep) 22 { 23 separators[*((unsigned char *) sep)] = 1; 24 sep++; 25 } 26 if (!s) 27 { 28 /* Pick up where we left off */ 29 s = *state; 30 } 31 start = s; 32 /* 1. EOS */ 33 if (!(*s)) 34 { 35 *state = s; 36 return 0; 37 } 38 /* 2. Leading separators, if any */ 39 if (SEP_TEST) 40 { 41 do 42 { 43 s++; 44 } 45 while (SEP_TEST); 46 /* 2a. EOS after separators only */ 47 if (!(*s)) 48 { 49 *state = s; 50 return 0; 51 } 52 } 53 /* 3. A token */ 54 result = s; 55 do 56 { 57 /* 3a. Token at end of string */ 58 if (!(*s)) 59 { 60 *state = s; 61 return result; 62 } 63 s++; 64 } 65 while (!SEP_TEST); 66 /* 4. Terminate token and skip trailing separators */ 67 *s = '\0'; 68 do 69 { 70 s++; 71 } 72 while (SEP_TEST); 73 /* 5. Return token */ 74 *state = s; 75 return result; 76 } 77