strtok_r.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /***************************************************************************
  19. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  20. ***************************************************************************
  21. * *
  22. * Project Name : G *
  23. * *
  24. * $Archive:: /G/wwlib/strtok_r.cpp $*
  25. * *
  26. * $Author:: Neal_k $*
  27. * *
  28. * $Modtime:: 4/03/00 11:43a $*
  29. * *
  30. * $Revision:: 1 $*
  31. * *
  32. *-------------------------------------------------------------------------*
  33. * Functions: *
  34. * strtok_r -- POSIX replacement for strtok (no internal static) *
  35. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  36. #include "strtok_r.h"
  37. #include <string.h>
  38. #include <stdio.h>
  39. //
  40. // Replacement for strtok() that doesn't use a static to
  41. // store the current position. The name comes from the
  42. // POSIX threadsafe version of strtok (r = reentrant). The
  43. // user provided var lasts is used in place of the static.
  44. //
  45. // Yes the Windows version of strtok is already threadsafe,
  46. // but the fact that you can't call a function that uses strtok()
  47. // during a series of strtok() calls is really annoying.
  48. //
  49. #ifndef _UNIX
  50. char *strtok_r(char *strptr, const char *delimiters, char **lasts)
  51. {
  52. if (strptr)
  53. *lasts=strptr;
  54. if ((*lasts)[0]==0) // 0 length string?
  55. return(NULL);
  56. //
  57. // Note: strcspn & strspn are both called, they're opposites
  58. //
  59. int dstart=strcspn(*lasts, delimiters); // find first char of string in delimiters
  60. if (dstart == 0) // string starts with a delimiter
  61. {
  62. int dend=strspn(*lasts, delimiters); // find last char of string NOT in delimiters
  63. *lasts+=dend;
  64. if ((*lasts)[0]==0) // 0 length string?
  65. return(NULL);
  66. dstart=strcspn(*lasts, delimiters);
  67. }
  68. char *retval=*lasts;
  69. if ((*lasts)[dstart]==0) // is this the last token?
  70. *lasts+=dstart;
  71. else // at least one more token to go...
  72. {
  73. (*lasts)[dstart]=0; // null out the end
  74. *lasts+=(dstart+1); // advance pointer
  75. }
  76. return(retval);
  77. }
  78. #endif