gdhelpers.c 1.2 KB

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