strsep

I have someone else's code, and my implementation doesen't have strsep() (apperanty it isn't common). What is an equivalent of p = strsep(&p, " \t");, where p is a char*? (I have GCC 4.4.3)
Last edited on
There is no direct replacement that I know of. You'll have to code one yourself. See http://www.mkssoftware.com/docs/man3/strsep.3.asp.

The closest is strtok(). You can probably factor a replacement easily using strtok() as base.
This one: http://www.gsp.com/cgi-bin/man.cgi?section=3&topic=strsep. Explains a bit better the behavior of the function, I think.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/* strsep.h

  Provides the 4.4BSD strsep(3) function for those that don't have it.

  Copyright 2011 Michael Thomas Greer
  Distributed under the Boost Software License, Version 1.0.
  ( See accompanying file LICENSE_1_0.txt or copy at
    http://www.boost.org/LICENSE_1_0.txt )

  Including this file modifies the std namespace in C++.

  Don't include this file if your compiler provides the strsep function in <string.h>.
  Make sure your build process tests for this and behaves accordingly!

*/
#ifdef __cplusplus
  #include <cstring>
  #define STD(x) std::x
  namespace std {
#else
  #include <string.h>
  #define STD(x) x
#endif

char* strsep( char** stringp, const char* delim )
  {
  char* result;

  if ((stringp == NULL) || (*stringp == NULL)) return NULL;

  result = *stringp;

  while (**stringp && !STD(strchr)( delim, **stringp )) ++*stringp;

  if (**stringp) *(*stringp)++ = '\0';
  else             *stringp    = NULL;

  return result;
  }

#ifdef __cplusplus
  } // namespace std;
#endif

#undef STD 

You should avoid strtok() and like functions.
http://linux.die.net/man/3/strsep

Good luck!
Topic archived. No new replies allowed.