<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: strtok in Operating System - HP-UX</title>
    <link>https://community.hpe.com/t5/operating-system-hp-ux/strtok/m-p/2833053#M721241</link>
    <description>I am not building an expression evaluator here, I just have a string with *different* delims that I need to parse *and* depending on the delim used, my program must take different actions. Therefore, I don't think I need to go the Yacc route, although I would like to study about it. All I know about Yacc is the nameJ Where would I start to learn about it.&lt;BR /&gt;&lt;BR /&gt;Back to my problem:&lt;BR /&gt;I was looking for something likes Java's StringTokenizer where there is an option to return the delims as tokens, that's all.&lt;BR /&gt;&lt;BR /&gt;Regular Expressions.... I am very comfortable with regex, in fact in Perl I would do this:&lt;BR /&gt;&lt;BR /&gt;@values = grep { length } split m!([ ()*+,/])!;&lt;BR /&gt;&lt;BR /&gt;@values would get all non-zero length tokens, including the delims (notice the capturing parenthesis).&lt;BR /&gt;&lt;BR /&gt;I have not used any regex in C, and I will certainly give it thought. For start, does C have a split function or better how would you write the above line in C?&lt;BR /&gt;&lt;BR /&gt;In the mean time, for simplicity I think I would go with Jean's simple approach and do it in C.&lt;BR /&gt;&lt;BR /&gt;Nevertheless, the regex route is cool:)&lt;BR /&gt;</description>
    <pubDate>Fri, 25 Oct 2002 12:50:25 GMT</pubDate>
    <dc:creator>Leslie Chaim</dc:creator>
    <dc:date>2002-10-25T12:50:25Z</dc:date>
    <item>
      <title>strtok</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/strtok/m-p/2833047#M721235</link>
      <description>Is there a C function to tokenize a string and get the token values. If I use strtok() I cannot know what was the last token. Is there anything else out there?&lt;BR /&gt;&lt;BR /&gt;Thanks,</description>
      <pubDate>Thu, 24 Oct 2002 20:18:31 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/strtok/m-p/2833047#M721235</guid>
      <dc:creator>Leslie Chaim</dc:creator>
      <dc:date>2002-10-24T20:18:31Z</dc:date>
    </item>
    <item>
      <title>Re: strtok</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/strtok/m-p/2833048#M721236</link>
      <description>Actually strtok(s1,s2) will do the job. The 1st time that you invoke the function you pass in a non-NULL value for s1. It returns a pointer to the first token separated by at least one the the chars in s2. You then call strtok again but this time with s1 set to (char *) NULL. You repeat the process until strtok() returns NULL (meaning your original s1 has been completely parsed. You are then ready for a new s1.&lt;BR /&gt;&lt;BR /&gt;strtok() works for simple tokenizing but for more complex tasks, you can use lex to generate a lexical engine and for really complex tasks, it's time to use yacc.&lt;BR /&gt;</description>
      <pubDate>Thu, 24 Oct 2002 20:41:02 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/strtok/m-p/2833048#M721236</guid>
      <dc:creator>A. Clay Stephenson</dc:creator>
      <dc:date>2002-10-24T20:41:02Z</dc:date>
    </item>
    <item>
      <title>Re: strtok</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/strtok/m-p/2833049#M721237</link>
      <description>But..&lt;BR /&gt;&lt;BR /&gt;Here is what I try to do.&lt;BR /&gt;&lt;BR /&gt;char *sep = "()*+,/ ";&lt;BR /&gt;&lt;BR /&gt;char *str = "a * b + c/foo(x,y,z)";&lt;BR /&gt;&lt;BR /&gt;strtok will give me the following tokens:&lt;BR /&gt;a&lt;BR /&gt;b&lt;BR /&gt;c&lt;BR /&gt;foo&lt;BR /&gt;x&lt;BR /&gt;y&lt;BR /&gt;z&lt;BR /&gt;&lt;BR /&gt;But I want this:&lt;BR /&gt;a&lt;BR /&gt;*&lt;BR /&gt;b&lt;BR /&gt;+&lt;BR /&gt;c&lt;BR /&gt;/&lt;BR /&gt;foo&lt;BR /&gt;(&lt;BR /&gt;,&lt;BR /&gt;x&lt;BR /&gt;,&lt;BR /&gt;y&lt;BR /&gt;,&lt;BR /&gt;z&lt;BR /&gt;)&lt;BR /&gt;----------------------&lt;BR /&gt;&lt;BR /&gt;In other words, I want to use a function where I know WHAT was the delimiting token.&lt;BR /&gt;&lt;BR /&gt;Or must I write my own tokenizer.&lt;BR /&gt;&lt;BR /&gt;Thanks</description>
      <pubDate>Thu, 24 Oct 2002 20:56:40 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/strtok/m-p/2833049#M721237</guid>
      <dc:creator>Leslie Chaim</dc:creator>
      <dc:date>2002-10-24T20:56:40Z</dc:date>
    </item>
    <item>
      <title>Re: strtok</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/strtok/m-p/2833050#M721238</link>
      <description>That's beyond the ability of strtok(). You could change the rules to require whitespace between tokens and then strtok() would work but if you need to do actual expression evaluation then it's time to use yacc. Yacc will allow you to build an interpreter or compiler about as complex as you wish.</description>
      <pubDate>Thu, 24 Oct 2002 21:38:22 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/strtok/m-p/2833050#M721238</guid>
      <dc:creator>A. Clay Stephenson</dc:creator>
      <dc:date>2002-10-24T21:38:22Z</dc:date>
    </item>
    <item>
      <title>Re: strtok</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/strtok/m-p/2833051#M721239</link>
      <description>I've been dealing with parsing strings lately.&lt;BR /&gt;If I had to do this now I would use regular expressiong.&lt;BR /&gt;They are not so hard to learn, and flexible enough.&lt;BR /&gt;man rexexec&lt;BR /&gt;man regcomp&lt;BR /&gt;&lt;BR /&gt;Don't forget regfree at the end. ;)&lt;BR /&gt;&lt;BR /&gt;Adam&lt;BR /&gt;</description>
      <pubDate>Fri, 25 Oct 2002 09:08:00 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/strtok/m-p/2833051#M721239</guid>
      <dc:creator>Adam J Markiewicz</dc:creator>
      <dc:date>2002-10-25T09:08:00Z</dc:date>
    </item>
    <item>
      <title>Re: strtok</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/strtok/m-p/2833052#M721240</link>
      <description>Hi,&lt;BR /&gt;&lt;BR /&gt;I also don't think that you can do it using strtok only ... I also noticed that spaces have a special meaning. If you don't have time to invest, I wrote this which uses a "mirror" string.&lt;BR /&gt;&lt;BR /&gt;Regards,&lt;BR /&gt;&lt;BR /&gt;Jean-Louis.&lt;BR /&gt;&lt;BR /&gt;#include &lt;STDIO.H&gt;&lt;BR /&gt;#include &lt;STRINGS.H&gt;&lt;BR /&gt;&lt;BR /&gt;main (argc, argv)&lt;BR /&gt;int argc;&lt;BR /&gt;char *argv[];&lt;BR /&gt;{&lt;BR /&gt;  char *sep = "()*+,/ "; &lt;BR /&gt;  char *str = "a * b + c/foo(x,y,z)"; &lt;BR /&gt;  char *v;&lt;BR /&gt;  char *p;&lt;BR /&gt;  char o[BUFSIZ];&lt;BR /&gt;  int first;&lt;BR /&gt;&lt;BR /&gt;  v=str;&lt;BR /&gt;  p=o;&lt;BR /&gt;  while (*v)&lt;BR /&gt;  {&lt;BR /&gt;    if (*v != ' ')&lt;BR /&gt;    {&lt;BR /&gt;      *p=*v;&lt;BR /&gt;      p++;&lt;BR /&gt;    }&lt;BR /&gt;    v++;&lt;BR /&gt;  }&lt;BR /&gt;  *p='\0';&lt;BR /&gt;  p=o;&lt;BR /&gt;  printf("string &amp;lt;%s&amp;gt;\ntokens &amp;lt;%s&amp;gt;\nnewstr&amp;lt;%s&amp;gt;\n\n", str, sep, o);&lt;BR /&gt;  v=strtok(str,sep);&lt;BR /&gt;  first=1;&lt;BR /&gt;  do {&lt;BR /&gt;    if (first)&lt;BR /&gt;    {&lt;BR /&gt;      first=0;&lt;BR /&gt;      printf("&amp;lt;%s&amp;gt; first pass\n", v);&lt;BR /&gt;    }&lt;BR /&gt;    else&lt;BR /&gt;    {&lt;BR /&gt;      printf("&amp;lt;%s&amp;gt; token &amp;lt;%c&amp;gt;\n", v, *(p-1));&lt;BR /&gt;    }&lt;BR /&gt;    p+=strlen(v)+1;&lt;BR /&gt;    v=strtok(NULL,sep);&lt;BR /&gt;  } while(*v);&lt;BR /&gt;}&lt;BR /&gt;&lt;/STRINGS.H&gt;&lt;/STDIO.H&gt;</description>
      <pubDate>Fri, 25 Oct 2002 10:30:27 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/strtok/m-p/2833052#M721240</guid>
      <dc:creator>Jean-Louis Phelix</dc:creator>
      <dc:date>2002-10-25T10:30:27Z</dc:date>
    </item>
    <item>
      <title>Re: strtok</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/strtok/m-p/2833053#M721241</link>
      <description>I am not building an expression evaluator here, I just have a string with *different* delims that I need to parse *and* depending on the delim used, my program must take different actions. Therefore, I don't think I need to go the Yacc route, although I would like to study about it. All I know about Yacc is the nameJ Where would I start to learn about it.&lt;BR /&gt;&lt;BR /&gt;Back to my problem:&lt;BR /&gt;I was looking for something likes Java's StringTokenizer where there is an option to return the delims as tokens, that's all.&lt;BR /&gt;&lt;BR /&gt;Regular Expressions.... I am very comfortable with regex, in fact in Perl I would do this:&lt;BR /&gt;&lt;BR /&gt;@values = grep { length } split m!([ ()*+,/])!;&lt;BR /&gt;&lt;BR /&gt;@values would get all non-zero length tokens, including the delims (notice the capturing parenthesis).&lt;BR /&gt;&lt;BR /&gt;I have not used any regex in C, and I will certainly give it thought. For start, does C have a split function or better how would you write the above line in C?&lt;BR /&gt;&lt;BR /&gt;In the mean time, for simplicity I think I would go with Jean's simple approach and do it in C.&lt;BR /&gt;&lt;BR /&gt;Nevertheless, the regex route is cool:)&lt;BR /&gt;</description>
      <pubDate>Fri, 25 Oct 2002 12:50:25 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/strtok/m-p/2833053#M721241</guid>
      <dc:creator>Leslie Chaim</dc:creator>
      <dc:date>2002-10-25T12:50:25Z</dc:date>
    </item>
    <item>
      <title>Re: strtok</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/strtok/m-p/2833054#M721242</link>
      <description>Unluckilly it's not so easy as in perl. Mainly because you are limited to the number of subexpressions found inside. Actually they are checked inside expression pattern rather then the string itself.&lt;BR /&gt;&lt;BR /&gt;As I think more about it I come to the conclusion that afterall you would end ap with the iterating loop, so in your case mayby simple Jean-Louis (the winner of the month!) approach would be more practical.&lt;BR /&gt;&lt;BR /&gt;However if you were interested in regex in C check the &lt;REGEX.H&gt;&lt;BR /&gt;&lt;BR /&gt;But one warning:&lt;BR /&gt;Don't remove spaces at the beginning. I think they should be ignored later. If you had two strings without operator they should be intepreted separately (and probably later considered semantical error). If you just remove spaces they will just concatenate into single string and you won't detect it.&lt;BR /&gt;&lt;BR /&gt;Example:&lt;BR /&gt;&lt;BR /&gt;"a + b + c d" -&amp;gt; "a+b+cd"&lt;BR /&gt;&lt;BR /&gt;&lt;/REGEX.H&gt;</description>
      <pubDate>Fri, 25 Oct 2002 15:18:25 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/strtok/m-p/2833054#M721242</guid>
      <dc:creator>Adam J Markiewicz</dc:creator>
      <dc:date>2002-10-25T15:18:25Z</dc:date>
    </item>
    <item>
      <title>Re: strtok</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/strtok/m-p/2833055#M721243</link>
      <description>Thank you all for you inputs.&lt;BR /&gt;As an academic, it would be interesting to see how many lines of C will be needed to do what the above Perl does in one line.</description>
      <pubDate>Fri, 25 Oct 2002 15:29:16 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/strtok/m-p/2833055#M721243</guid>
      <dc:creator>Leslie Chaim</dc:creator>
      <dc:date>2002-10-25T15:29:16Z</dc:date>
    </item>
    <item>
      <title>Re: strtok</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/strtok/m-p/2833056#M721244</link>
      <description>Its me again.&lt;BR /&gt;I was thinking about good regular expresions to be defined and I found few potential traps you can fall into, so I decided to write.&lt;BR /&gt;&lt;BR /&gt;You have to treat operators differently from tre names. For the names good regex seems to be:&lt;BR /&gt;"[a-zA-Z_][a-zA-Z0-9_]*"&lt;BR /&gt;of course, if you think about C.&lt;BR /&gt;But for the operators its not so nice. Should they only be single characters? If so what about operators like "&amp;lt;=" (of course, if you respect them).&lt;BR /&gt;But you cannot collect all operators automatically, because in an expression like "a*(b+c)"  the first operator you will get is "*(".&lt;BR /&gt;&lt;BR /&gt;So, I'm affraid, operators should be defined more strictly, and for names I would use the abore regexp. The rest I would do in a loop with cases.&lt;BR /&gt;&lt;BR /&gt;So, the conclusion is I would do this with regexp. Perl and (sorry) Jean-Louis sollutions could generate you errors.&lt;BR /&gt;&lt;BR /&gt;Good luck&lt;BR /&gt;&lt;BR /&gt;Adam&lt;BR /&gt;</description>
      <pubDate>Mon, 28 Oct 2002 09:23:43 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/strtok/m-p/2833056#M721244</guid>
      <dc:creator>Adam J Markiewicz</dc:creator>
      <dc:date>2002-10-28T09:23:43Z</dc:date>
    </item>
    <item>
      <title>Re: strtok</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/strtok/m-p/2833057#M721245</link>
      <description>Ok so here is what I did with my_strtok(), please voice your critques.&lt;BR /&gt;&lt;BR /&gt;Regards,&lt;BR /&gt;Leslie&lt;BR /&gt;&lt;BR /&gt;#include &lt;STRINGS.H&gt;&lt;BR /&gt;#include &lt;STDIO.H&gt;&lt;BR /&gt;&lt;BR /&gt;#define TOKEN_DELIMITER              100&lt;BR /&gt;#define TOKEN_TEXT                   200&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;char * my_strtok (char *text, char *sep)&lt;BR /&gt;{&lt;BR /&gt;&lt;BR /&gt;    static char&lt;BR /&gt; *p,&lt;BR /&gt;        *this_token;&lt;BR /&gt;    &lt;BR /&gt;    static short&lt;BR /&gt; next_token_type;&lt;BR /&gt;&lt;BR /&gt;    size_t&lt;BR /&gt; token_length;&lt;BR /&gt;&lt;BR /&gt;    static long&lt;BR /&gt; current_token_max_len = 50;   /* Should be good for most tokens */&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;    if ( this_token == NULL )&lt;BR /&gt;    {&lt;BR /&gt; fprintf (stderr, "Info: Calling malloc\n");&lt;BR /&gt;&lt;BR /&gt; if ( !(this_token = (char *) malloc ( sizeof (char) * current_token_max_len)) )&lt;BR /&gt; {&lt;BR /&gt;           perror ("my_strtok() malloc failed");&lt;BR /&gt;    return NULL;&lt;BR /&gt; }&lt;BR /&gt;    }&lt;BR /&gt;&lt;BR /&gt;    if ( text != NULL )&lt;BR /&gt;    {&lt;BR /&gt;        p = text;   /* Let 'p' point to the text given */&lt;BR /&gt;&lt;BR /&gt; if ( strchr (sep, *p) )&lt;BR /&gt; {&lt;BR /&gt;     next_token_type = TOKEN_DELIMITER;&lt;BR /&gt; }&lt;BR /&gt; else&lt;BR /&gt; {&lt;BR /&gt;     next_token_type = TOKEN_TEXT;&lt;BR /&gt; }&lt;BR /&gt;    }&lt;BR /&gt;&lt;BR /&gt;    if ( next_token_type == TOKEN_TEXT )&lt;BR /&gt;    {&lt;BR /&gt; token_length = strcspn (p, sep);&lt;BR /&gt;&lt;BR /&gt; next_token_type = TOKEN_DELIMITER;&lt;BR /&gt;    }&lt;BR /&gt;    else&lt;BR /&gt;    {&lt;BR /&gt; token_length = strspn (p, sep);&lt;BR /&gt;&lt;BR /&gt; if ( token_length == 1 )&lt;BR /&gt; {&lt;BR /&gt;    next_token_type = TOKEN_TEXT;&lt;BR /&gt; }&lt;BR /&gt; else if ( token_length &amp;gt; 1 )&lt;BR /&gt; {&lt;BR /&gt;     token_length = 1;&lt;BR /&gt;     next_token_type = TOKEN_DELIMITER;&lt;BR /&gt; }&lt;BR /&gt;    }&lt;BR /&gt;&lt;BR /&gt;    if ( token_length == 0 )&lt;BR /&gt;    {&lt;BR /&gt; if (this_token) free (this_token);&lt;BR /&gt; return NULL;&lt;BR /&gt;    }&lt;BR /&gt;&lt;BR /&gt;    if ( token_length &amp;gt;= current_token_max_len )&lt;BR /&gt;    {&lt;BR /&gt; fprintf (stderr, "Info: Calling realloc\n");&lt;BR /&gt;&lt;BR /&gt; if ( !(this_token = (char *) realloc (this_token, sizeof (char) * (token_length + 1))) )&lt;BR /&gt; {&lt;BR /&gt;           perror ("my_strtok() realloc failed");&lt;BR /&gt;&lt;BR /&gt;    if (this_token) free (this_token);&lt;BR /&gt;    return NULL;&lt;BR /&gt; }&lt;BR /&gt;    }&lt;BR /&gt;&lt;BR /&gt;    strncpy (this_token, p, token_length);&lt;BR /&gt;&lt;BR /&gt;    *(this_token + token_length) = '\0';&lt;BR /&gt;&lt;BR /&gt;    p += token_length;&lt;BR /&gt;&lt;BR /&gt;    return this_token;&lt;BR /&gt;}&lt;BR /&gt;&lt;/STDIO.H&gt;&lt;/STRINGS.H&gt;</description>
      <pubDate>Thu, 31 Oct 2002 20:29:39 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/strtok/m-p/2833057#M721245</guid>
      <dc:creator>Leslie Chaim</dc:creator>
      <dc:date>2002-10-31T20:29:39Z</dc:date>
    </item>
    <item>
      <title>Re: strtok</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/strtok/m-p/2833058#M721246</link>
      <description>Here is my_strtok in an attachment.&lt;BR /&gt;&lt;BR /&gt;If we could only edit our posts:(</description>
      <pubDate>Thu, 31 Oct 2002 20:31:58 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/strtok/m-p/2833058#M721246</guid>
      <dc:creator>Leslie Chaim</dc:creator>
      <dc:date>2002-10-31T20:31:58Z</dc:date>
    </item>
    <item>
      <title>Re: strtok</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/strtok/m-p/2833059#M721247</link>
      <description>If you're interested in regexp version I can think about.&lt;BR /&gt;&lt;BR /&gt;I'm affraid I don't like your code. &lt;BR /&gt;I don't see the point in the way hou handle dynamic memory.&lt;BR /&gt;Appart from it I see some bugs.&lt;BR /&gt;&lt;BR /&gt;Actually I would do it from the scratch rather than remaster that code.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;But I have to warn you: I like the things to mbe done precisely, so my code will be more complecated.&lt;BR /&gt;&lt;BR /&gt;I'll think of it in the neares future if you still need it.&lt;BR /&gt;&lt;BR /&gt;Adam&lt;BR /&gt;</description>
      <pubDate>Mon, 04 Nov 2002 19:45:50 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/strtok/m-p/2833059#M721247</guid>
      <dc:creator>Adam J Markiewicz</dc:creator>
      <dc:date>2002-11-04T19:45:50Z</dc:date>
    </item>
  </channel>
</rss>

