FROM : János
DATE : Thu Apr 05 20:49:38 2007
On Apr 5, 2007, at 2:03 PM, Peter Ilberg wrote:
> In your test case, strtok is returning NULL on its last iteration
> through
> the loop. strstr doesn't like NULL as an input argument. You avoid the
> problem in your longer test case by explicity jumping out of the loop
> after processing "-m". Try rewriting the loop this way:
>
> flg = (char *)cflags;
> tokenval = (char *)delim;
> for (token = strtok(flg,delim); token; token = strtok
> (NULL,delim)) {
> if ( strstr( token,"-t") != NULL) {
> tokenval = strtok(NULL,delim);
> code = tokenval;
> }
> [...other options...]
> if ( strstr( token,"-m") != NULL) {
> mr = 1;
> }
> fprintf(stderr, "token=%s tokenval=%s\n", token,tokenval);
> }
>
> If all of your options are single letters, you can rewrite the loop
> body:
>
> if (token[0] == '-')
> switch (token[1]) {
> case 't':
> tokenval = strtok(NULL,delim);
> code = tokenval;
> break;
> [...other options...]
> case 'm':
> mr = 1;
> break;
> }
>
> --- Peter
> _______________________________________________
Peter,
That is it.
/I found out in the meantime as I tried to debug/
Thanks a lot,
János
P.S. Almost all C books I read say that I should use switch just
with int values, so I was afraid to do so although originally that
was my first choice._______________________________________________
MacOSX-dev mailing list
<email_removed>
http://www.omnigroup.com/mailman/listinfo/macosx-dev
DATE : Thu Apr 05 20:49:38 2007
On Apr 5, 2007, at 2:03 PM, Peter Ilberg wrote:
> In your test case, strtok is returning NULL on its last iteration
> through
> the loop. strstr doesn't like NULL as an input argument. You avoid the
> problem in your longer test case by explicity jumping out of the loop
> after processing "-m". Try rewriting the loop this way:
>
> flg = (char *)cflags;
> tokenval = (char *)delim;
> for (token = strtok(flg,delim); token; token = strtok
> (NULL,delim)) {
> if ( strstr( token,"-t") != NULL) {
> tokenval = strtok(NULL,delim);
> code = tokenval;
> }
> [...other options...]
> if ( strstr( token,"-m") != NULL) {
> mr = 1;
> }
> fprintf(stderr, "token=%s tokenval=%s\n", token,tokenval);
> }
>
> If all of your options are single letters, you can rewrite the loop
> body:
>
> if (token[0] == '-')
> switch (token[1]) {
> case 't':
> tokenval = strtok(NULL,delim);
> code = tokenval;
> break;
> [...other options...]
> case 'm':
> mr = 1;
> break;
> }
>
> --- Peter
> _______________________________________________
Peter,
That is it.
/I found out in the meantime as I tried to debug/
Thanks a lot,
János
P.S. Almost all C books I read say that I should use switch just
with int values, so I was afraid to do so although originally that
was my first choice._______________________________________________
MacOSX-dev mailing list
<email_removed>
http://www.omnigroup.com/mailman/listinfo/macosx-dev
| Related mails | Author | Date |
|---|---|---|
| János | Apr 5, 19:10 | |
| Norman Gray | Apr 5, 19:23 | |
| Peter Ilberg | Apr 5, 20:03 | |
| János | Apr 5, 20:49 | |
| Andy Armstrong | Apr 5, 20:57 | |
| Uli Kusterer | Apr 6, 12:26 | |
| Koryn Grant | Apr 11, 08:35 | |
| Uli Kusterer | Apr 11, 11:27 | |
| George Warner | Apr 11, 18:10 | |
| Graham J Lee | Apr 11, 18:40 | |
| János | Apr 11, 18:57 | |
| Koryn Grant | Apr 11, 21:36 |






Cocoa mail archive

