gdhelpers.c 1.2 KB

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