Back to index

plt-scheme  4.2.1
gdhelpers.c
Go to the documentation of this file.
00001 
00002 #ifdef HAVE_CONFIG_H
00003 #include "config.h"
00004 #endif
00005 
00006 #include "gd.h"
00007 #include "gdhelpers.h"
00008 #include <stdlib.h>
00009 #include <string.h>
00010 
00011 /* TBB: gd_strtok_r is not portable; provide an implementation */
00012 
00013 #define SEP_TEST (separators[*((unsigned char *) s)])
00014 
00015 char *
00016 gd_strtok_r (char *s, char *sep, char **state)
00017 {
00018   char separators[256];
00019   char *start;
00020   char *result = 0;
00021   memset (separators, 0, sizeof (separators));
00022   while (*sep)
00023     {
00024       separators[*((unsigned char *) sep)] = 1;
00025       sep++;
00026     }
00027   if (!s)
00028     {
00029       /* Pick up where we left off */
00030       s = *state;
00031     }
00032   start = s;
00033   /* 1. EOS */
00034   if (!(*s))
00035     {
00036       *state = s;
00037       return 0;
00038     }
00039   /* 2. Leading separators, if any */
00040   if (SEP_TEST)
00041     {
00042       do
00043        {
00044          s++;
00045        }
00046       while (SEP_TEST);
00047       /* 2a. EOS after separators only */
00048       if (!(*s))
00049        {
00050          *state = s;
00051          return 0;
00052        }
00053     }
00054   /* 3. A token */
00055   result = s;
00056   do
00057     {
00058       /* 3a. Token at end of string */
00059       if (!(*s))
00060        {
00061          *state = s;
00062          return result;
00063        }
00064       s++;
00065     }
00066   while (!SEP_TEST);
00067   /* 4. Terminate token and skip trailing separators */
00068   *s = '\0';
00069   do
00070     {
00071       s++;
00072     }
00073   while (SEP_TEST);
00074   /* 5. Return token */
00075   *state = s;
00076   return result;
00077 }
00078 
00079 void *
00080 gdCalloc (size_t nmemb, size_t size)
00081 {
00082   return calloc (nmemb, size);
00083 }
00084 
00085 void *
00086 gdMalloc (size_t size)
00087 {
00088   return malloc (size);
00089 }
00090 
00091 void *
00092 gdRealloc (void *ptr, size_t size)
00093 {
00094   return realloc (ptr, size);
00095 }
00096 
00097 void
00098 gdFree (void *ptr)
00099 {
00100   free (ptr);
00101 }