Jan Pechanec wrote:
> On Mon, 14 Jan 2008, [EMAIL PROTECTED] wrote:
>
> hi Frank,
>
> interestingly, the mailing list software added some spaces, the
> original subject was this:
>
> parsing "xxx=yyy;abc=efg;nnn=1 2 3;..."-like option string
>
>
>> strtok()
>> A sequence of calls to strtok() breaks the string pointed to
>> by s1 into a sequence of tokens, each of which is delimited
>> by a byte from the string pointed to by s2. The first call
>> in the sequence has s1 as its first argument, and is fol-
>> lowed by calls with a null pointer as their first argument.
>> The separator string pointed to by s2 can be different from
>> call to call.
>> [ ... ]
>>
>
> good idea, I used strtok() some time ago but forgot about that.
> However, I would have to reparse the token using strchr(3c) and look for
> possible '=' there. So yes, it would be very helpful but I still hope for a
> solution that is "more complete" for me.
>
What I would do is use strtok_r, and then you can use a nested structure:
char *l1, *l2, *nvp, *name, *value;
for (nvp = strtok_r(str, ";", &l1); nvp != NULL; nvp = strtok_r(NULL,
';', &l1)) {
name = strtok_r(nvp, "=", &l2);
value = strtok_r(NULL, "", &l2);
}
The only possible problem with this approach, is that it will skip cases
like ";;" (an empty name-value pair), and it can't parse "name==value"
properly. (It will see the value as 'value', rather than '=value'.) I
once had a version of strtok that did not collapse multiple delimiters.
I.e. if a token delimiter appeared twice in a row, it would see an empty
string as a token value. There are cases where this kind of parsing is
useful.
-- Garrett
> many URIs use name=value definitions separated by semicolons, I
> think it's quite a common specification so I still believe it's already
> present somewhere.
>
> thanks, Jan.
>
>
_______________________________________________
opensolaris-code mailing list
[email protected]
http://mail.opensolaris.org/mailman/listinfo/opensolaris-code