Lost Password?


Go Back   CodeCall Programming Forum > Software Development > C and C++

C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-23-2007, 12:24 PM
Salrandin Salrandin is offline
Newbie
 
Join Date: Oct 2007
Posts: 10
Rep Power: 0
Salrandin is on a distinguished road
Send a message via AIM to Salrandin
Default I dont understand this parser code

C Code:
  1. void my_split(const char* tosplit, const char* delim, char** parsed, int* len)
  2. {
  3.     char *local = strdup(tosplit);
  4.     char *tok = strtok(local, delim);
  5.     int i = 0;
  6.     while(tok) {
  7.         *parsed++ = strdup(tok);
  8.         i++;
  9.         tok = strtok(NULL, delim);
  10.     }
  11.     *len = i;
  12.     free(local);
  13. }

Can anyone explain this to me? i know it parses with a delimeter
but im not quite sure how it works
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 10-23-2007, 12:54 PM
limo limo is offline
Newbie
 
Join Date: Oct 2007
Location: Australia
Posts: 14
Rep Power: 0
limo is on a distinguished road
Default

Here you go, comments added

HTML Code:
  char *local = strdup(tosplit); // makes a copy of tosplit, and 
  // returns a pointer to the allocated copy

    char *tok = strtok(local, delim); // split local into tokens using
    // delim, store the first token in tok

    int i = 0; // counter for token

    while(tok) { // while token is not null

        *parsed++ = strdup(tok); // makes a copy of token, 
        // returns a pointer to the allocated copy which is stored in 
        // parsed, parsed is then increment to point to the next 
        // pointer

        i++; // increment the number of tokens parsed

        tok = strtok(NULL, delim); // get next token

    }

    *len = i; // stores the number of tokens in len

    free(local); // frees the memory taken up local

// when the function returns, *parsed will contain tokens created from tosplit
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 10-23-2007, 09:08 PM
Salrandin Salrandin is offline
Newbie
 
Join Date: Oct 2007
Posts: 10
Rep Power: 0
Salrandin is on a distinguished road
Send a message via AIM to Salrandin
Default

thx, it takes in a string as a delimiter right?
__________________
On C/C++ I use MS Visual Studios 2005 Express (yuck)
On Java I use BlueJ (Rocks!)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 10-24-2007, 12:56 AM
limo limo is offline
Newbie
 
Join Date: Oct 2007
Location: Australia
Posts: 14
Rep Power: 0
limo is on a distinguished road
Default

NP. Yes the function takes in both the string to tokenise, and a string which contains the delimiters.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Basic Calculator AfTriX VB Tutorials 3 02-29-2008 09:53 AM
assembly code help emaney General Programming 0 06-16-2007 05:27 AM
Please Help With A C Program!! siren C and C++ 7 04-17-2007 09:45 AM
Where to Put PHP Code clookid PHP Tutorials 1 01-11-2007 09:44 PM


All times are GMT -5. The time now is 03:29 AM.

Contest Stats

WingedPanther ........ 2753.6
Xav ........ 2704
Brandon W ........ 1702.32
John ........ 1207.73
marwex89 ........ 1175.24
morefood2001 ........ 966.05
dcs ........ 655.75
Steve.L ........ 475.59
orjan ........ 418.58
Aereshaa ........ 383.54

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 100%


Complete - Celebrate!

Ads