[En-Nut-Discussion] Using STRTOK_R

peter.scandrett at transport.alstom.com peter.scandrett at transport.alstom.com
Wed Aug 6 09:09:56 CEST 2003


Hi all,

A note of warning about using STRTOK_R. Just like STRTOK, STRTOK_R 
destroys the string being parsed, so if the string is precious, make a 
copy of it.

BTW, I've been using STRTOK_R, STRSEP_R and STRSEP_RS for several years 
now, on this and other platforms, and have not found a problem. Unlike 
STRTOK, these functions do not internally save where the parsing has got 
up to. That's your job. You have to pass the string each time. Harald's 
example below is a good one.

In case you're wondering how they work, let us assume our parse string is 
",ABC,,,DEF," and the search string is "-,+" (hyphen comma plus).

STRTOK_R will return ABC on the first call, DEF on the second, and either 
NULL or an empty string on the third. (I've forgotten which one it is, but 
it's the same as STRTOK.)

STRSEP_R (short for STRing SEParator) will return an empty string on the 
first, third and fourth calls, ABC on the second call, DEF on the fifth 
call, and either NULL or an empty string on the sixth. In other words, it 
goes serarator by separator.

STRSEP_RS is identical to STRSEP_R, except it also returns the seperator 
encountered. This can be useful in parsing MicroSoft's CSV structure where 
a comma can be in the data, and double quotes are added around the whole 
field. Thus the following is valid
        111,"2,2",333
where the first three cells contain 111, 2,2 (2 comma 2) and 333.

Enjoy,
Peter S

Peter Scandrett
Engineering Systems Department
ALSTOM Australia Limited
3 Bridge Street, Pymble, 2073, Australia
Phone (+612) 94 88 49 11
Fax (+612) 94 88 49 00
peter.scandrett at transport.alstom.com




Harald Kipp <harald.kipp at egnite.de>
Sent by: en-nut-discussion-admin at egnite.de
05-08-2003 07:03 pm
Please respond to en-nut-discussion

 
        To:     en-nut-discussion at egnite.de
        cc:     (bcc: Peter Scandrett/AUMIL01/Transport/ALSTOM)
        Subject:        Re: [En-Nut-Discussion] any code in lib for parsing POST  tokens in 
req_query?


Damian,

The next release will include a form sample:

/*
  * CGI Sample: Proccessing a form.
  *
  * This routine must have been registered by NutRegisterCgi() and is
  * automatically called by NutHttpProcessRequest() when the client
  * request the URL 'cgi-bin/form.cgi'.
  *
  * Thanks to Tom Boettger, who provided this sample for ICCAVR.
  */
int ShowForm(FILE * stream, REQUEST * req)
{
     static prog_char html_head[] = "<HTML><BODY><BR><H1>Form 
Result</H1><BR><BR>";
     static prog_char html_body[] = "<BR><BR><p><a 
href=\"../index.html\">return to main</a></BODY></HTML></p>";

     NutHttpSendHeaderTop(stream, req, 200, "Ok");
     NutHttpSendHeaderBot(stream, html_mt, -1);

     /* Send HTML header. */
     fputs_P(html_head, stream);

     if (req->req_query) {
#ifdef __IMAGECRAFT__
         char *param1;
         char *param2;
         char *param3;

         /*
          * Extract the parameters. Note, that it is potentially dangerous
          * to use strtok in multithreaded applications. There's no 
problem
          * here, because we can be sure that there will by no thread 
switch
          * between the first and the last call to strtok. Otherwise we
          * must use strtok_r().
          */
         param1 = strtok(req->req_query, "=");
         param1 = strtok(NULL, "&");
         param2 = strtok(NULL, "=");
         param2 = strtok(NULL, "&");
         param3 = strtok(NULL, "=");
         param3 = strtok(NULL, "&");

         /* Send the parameters back to the client. */
         fputs("Param1=", stream);
         fputs(param1, stream);
         fputs("<BR>Param2=", stream);
         fputs(param2, stream);
         fputs("<BR>Param3=", stream);
         fputs(param3, stream);
#else
         /*
          * There's no strtok in the GCC library. So we take the chance to
          * demonstrate Peter Scandrett's strtok_r().
          */
         char *qp;
         char *c[3];
         char *p[3];
         u_char i;

         /* Extract 3 parameters. */
         qp = req->req_query;
         for(i = 0; i < 3; i++) {
             c[i] = strtok_r(&qp, "=");
             p[i] = strtok_r(&qp, "&");
         }

         /* Send the parameters back to the client. */
         for(i = 0; i < 3; i++) {
             fprintf_P(stream, PSTR("%s: %s<BR>\r\n"), c[i], p[i]);
         }
#endif
     }

     fputs_P(html_body, stream);
     fflush(stream);

     return 0;
}

_______________________________________________
En-Nut-Discussion mailing list
En-Nut-Discussion at egnite.de
http://www.egnite.de/mailman/listinfo/en-nut-discussion


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.egnite.de/pipermail/en-nut-discussion/attachments/20030806/2330dea5/attachment.html>


More information about the En-Nut-Discussion mailing list