Back to index

tetex-bin  3.0
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 *result = 0;
00019   memset (separators, 0, sizeof (separators));
00020   while (*sep)
00021     {
00022       separators[*((unsigned char *) sep)] = 1;
00023       sep++;
00024     }
00025   if (!s)
00026     {
00027       /* Pick up where we left off */
00028       s = *state;
00029     }
00030   /* 1. EOS */
00031   if (!(*s))
00032     {
00033       *state = s;
00034       return 0;
00035     }
00036   /* 2. Leading separators, if any */
00037   if (SEP_TEST)
00038     {
00039       do
00040        {
00041          s++;
00042        }
00043       while (SEP_TEST);
00044       /* 2a. EOS after separators only */
00045       if (!(*s))
00046        {
00047          *state = s;
00048          return 0;
00049        }
00050     }
00051   /* 3. A token */
00052   result = s;
00053   do
00054     {
00055       /* 3a. Token at end of string */
00056       if (!(*s))
00057        {
00058          *state = s;
00059          return result;
00060        }
00061       s++;
00062     }
00063   while (!SEP_TEST);
00064   /* 4. Terminate token and skip trailing separators */
00065   *s = '\0';
00066   do
00067     {
00068       s++;
00069     }
00070   while (SEP_TEST);
00071   /* 5. Return token */
00072   *state = s;
00073   return result;
00074 }
00075 
00076 void *
00077 gdCalloc (size_t nmemb, size_t size)
00078 {
00079   return calloc (nmemb, size);
00080 }
00081 
00082 void *
00083 gdMalloc (size_t size)
00084 {
00085   return malloc (size);
00086 }
00087 
00088 void *
00089 gdRealloc (void *ptr, size_t size)
00090 {
00091   return realloc (ptr, size);
00092 }
00093 
00094 BGD_DECLARE(void) gdFree (void *ptr)
00095 {
00096   free (ptr);
00097 }