Back to index

php5  5.3.10
gdhelpers.c
Go to the documentation of this file.
00001 #ifdef HAVE_CONFIG_H
00002 #include "config.h"
00003 #endif
00004 
00005 #include "gd.h"
00006 #include "gdhelpers.h"
00007 #include <stdlib.h>
00008 #include <string.h>
00009 
00010 /* TBB: gd_strtok_r is not portable; provide an implementation */
00011 
00012 #define SEP_TEST (separators[*((unsigned char *) s)])
00013 
00014 char *
00015 gd_strtok_r (char *s, char *sep, char **state)
00016 {
00017   char separators[256];
00018   char *start;
00019   char *result = 0;
00020   memset (separators, 0, sizeof (separators));
00021   while (*sep)
00022     {
00023       separators[*((unsigned char *) sep)] = 1;
00024       sep++;
00025     }
00026   if (!s)
00027     {
00028       /* Pick up where we left off */
00029       s = *state;
00030     }
00031   start = s;
00032   /* 1. EOS */
00033   if (!(*s))
00034     {
00035       *state = s;
00036       return 0;
00037     }
00038   /* 2. Leading separators, if any */
00039   if (SEP_TEST)
00040     {
00041       do
00042        {
00043          s++;
00044        }
00045       while (SEP_TEST);
00046       /* 2a. EOS after separators only */
00047       if (!(*s))
00048        {
00049          *state = s;
00050          return 0;
00051        }
00052     }
00053   /* 3. A token */
00054   result = s;
00055   do
00056     {
00057       /* 3a. Token at end of string */
00058       if (!(*s))
00059        {
00060          *state = s;
00061          return result;
00062        }
00063       s++;
00064     }
00065   while (!SEP_TEST);
00066   /* 4. Terminate token and skip trailing separators */
00067   *s = '\0';
00068   do
00069     {
00070       s++;
00071     }
00072   while (SEP_TEST);
00073   /* 5. Return token */
00074   *state = s;
00075   return result;
00076 }