strsep() function problem, please help

D

daniel

Hi everyone,

I'm trying to get this program compiled under Solaris. Unfortunately I
have little experience with C.

Solaris doesn't use the function strsep() anymore:


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

DESCRIPTION
The strsep() function returns the next token from the
string stringp which is delimited by delim. The token is
terminated with a `\0' character and stringp is updated to
point past the token.

RETURN VALUE
The strsep() function returns a pointer to the token, or
NULL if delim is not found in stringp.



I need to use a function that Solaris knows about. Tried to simply
substitute strsep() with strtok() which didn't work. The code which I
need to port is:

while (fgets(buf, sizeof(buf), desc) != NULL) {
w = buf;
if ((p = strsep(&w, " \t")) == NULL)
continue;
..
..
..

Any help would be greatly appreciated since google and everyone I know
couldn't help.

-Dan
 
T

thilbong

Try this code.

while (fgets(buf, sizeof(buf), desc) != NULL) {
w = buf;
strtok(w, "\t");
if ((p = strtok(NULL, " \t")) == NULL)
continue;
 
T

thilbong

Of course, strtok(w, "\t"); should be replaced with strtok(w, " \t"); in the
3rd line of my code.
 
M

Mark

thilbong said:
Of course, strtok(w, "\t"); should be replaced with strtok(w, " \t"); in
the 3rd line of my code.

Of course, your solution isn't really a solution regardless of the 3rd line!

while(fgets(buf, sizeof buf, desc)) {
if((w = strcspn(buf, " \t")) == NULL)
continue;
*w++ = 0;
p = buf;
...
}

Untested, but it should work.
Regards,
Mark
 
M

Mark

Mark said:
Of course, your solution isn't really a solution regardless of the 3rd
line!
And neither is mine!
should be: strpbrk() not strcspn() !
*oops* maybe I should have tested it first!

while(fgets(buf, sizeof buf, desc)) {
if((w = strpbrk(buf, " \t")) == NULL)
continue;
*w++ = 0;
p = buf;
...
}

Still untested ;) But much better...
Mark
 
L

Lawrence Kirby

Hi everyone,

I'm trying to get this program compiled under Solaris. Unfortunately I
have little experience with C.

Solaris doesn't use the function strsep() anymore:


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

DESCRIPTION
The strsep() function returns the next token from the
string stringp which is delimited by delim. The token is
terminated with a `\0' character and stringp is updated to
point past the token.

RETURN VALUE
The strsep() function returns a pointer to the token, or
NULL if delim is not found in stringp.



I need to use a function that Solaris knows about. Tried to simply
substitute strsep() with strtok() which didn't work. The code which I
need to port is:

strsep() isn't a standard C library function but common implementations
work differently to strtok(), which is the reason for it in the first
place. strtok() treats a sequence of delimeter characters as a single
separator field e.g. using \t delimiters field1\t\tfield2 is 2 fields.
strsep() would produce 3 i.e. with a zero length middle field. If you need
this functionality then you can't use strtok().

A simple solution would be to grab source code for strsep() from the Web,
it is a small function and a Google search of strsep source gives lots
of matches.

Lawrence
 
C

CBFalconer

I'm trying to get this program compiled under Solaris.
Unfortunately I have little experience with C.

Solaris doesn't use the function strsep() anymore:

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

DESCRIPTION
The strsep() function returns the next token from the
string stringp which is delimited by delim. The token is
terminated with a `\0' character and stringp is updated to
point past the token.

RETURN VALUE
The strsep() function returns a pointer to the token, or
NULL if delim is not found in stringp.

I need to use a function that Solaris knows about. Tried to
simply substitute strsep() with strtok() which didn't work. The
code which I need to port is:

while (fgets(buf, sizeof(buf), desc) != NULL) {
w = buf;
if ((p = strsep(&w, " \t")) == NULL)
continue;
.
.

Any help would be greatly appreciated since google and everyone
I know couldn't help.

It seems rather useless, since very few lines are going to use both
the space and a following tab as a token separator, which is what
you want according to your description of strsep. If that is the
logic you want look into strstr(), else look into strpbrk(), both
of which are standard functions.

I suspect what you really want is to find an occurrence of one or
more of those characters, after skipping leading occasions, and
that you don't want to use strtok because of it's re-entrancy
problems. You might want to simplify by making the delimiters any
whitespace, which will include \n and \f. So I would write a
routine to do this something like (untested):

/* This has the nuisance of strtok, it alters str content */
/* so it can't parse string constants, for example. However */
/* it doesn't have the reentrancy and threading problems */
char *sepstr(char* *str)
{
char *s = *str; /* scan off leading delims */
char *t; /* scan to next token */

while (isspace((unsigned char)*s)) s++;
if (!*s) return NULL;
t = s;
while (t && !isspace(unsigned char)*t) t++;
if (t) *t++ = '\0';
*str = t;
return s;
} /* untested */

I am not sure but that isspace is a C99 function; if so you may
have to build it out of the other is functions or first principles.

This may all be foolish because sscanf may also do the job for
you. However I rarely use the scanf family - it's too complicated
for me. Dan Pop would choose otherwise.
 
D

daniel

Thanky you very much for helping me out here, I was almost despairing.

So thanks for all the replies, the solution worked out very well!

Bye

-Dan
 
P

pete

Thanky you very much for helping me out here, I was almost despairing.

So thanks for all the replies, the solution worked out very well!

Bye

Too late, but here's what I got anyway:

#include <string.h>

char *str_sep(char **s1, const char *s2)
{
char *const p1 = *s1;

if (p1 != NULL) {
*s1 = strpbrk(p1, s2);
if (*s1 != NULL) {
*(*s1)++ = '\0';
}
}
return p1;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top