LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-18-2014, 06:01 AM   #1
bislinux
LQ Newbie
 
Registered: Dec 2014
Posts: 7

Rep: Reputation: Disabled
What is parsing in c programming?


what does parse function do??why is it needed in c programming?please explain in simple terms with a simple example...

i have a sample code, but i dont understand what it does..any explaination would be appreciated.thankss

Code:
void parseReply(deviceRepliesType *pReply)
{
    int i;
    char *key = strtok(pReply->binaryReply, ",");
    printf("\nkey:%s\n", key);
    if (strcmp(key,"Hello")!=0) return;
    
    strtok(NULL, ","); //Skip 0
    
    //mac address
    char *macAddrRaw = strtok(NULL, ",");
    //printf("\nmacAddrRaw:%s\n", macAddrRaw);
    pReply->n.macAddr[0]=sHexDec2ToNum(&macAddrRaw[0]);
    pReply->n.macAddr[1]=sHexDec2ToNum(&macAddrRaw[3]);
    pReply->n.macAddr[2]=sHexDec2ToNum(&macAddrRaw[6]);
    pReply->n.macAddr[3]=sHexDec2ToNum(&macAddrRaw[9]);
    pReply->n.macAddr[4]=sHexDec2ToNum(&macAddrRaw[12]);
    pReply->n.macAddr[5]=sHexDec2ToNum(&macAddrRaw[15]);
}
what is going on here???
 
Old 12-18-2014, 06:39 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,849

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
have you checked the man page of strtok?
 
Old 12-18-2014, 07:33 AM   #3
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Transforming a stream of text into some other form of information is called "parsing".
It is very common in computer programs for a wide range of purposes.
For example, when the user types a reply to any prompt in your program.
 
Old 12-18-2014, 12:59 PM   #4
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
johnsfine said it well, I was going to jump on their coattails and elaborate a bit; however I'll address your question about "why".

The answer is you are not required to use parsing to write program in C. And you are not required to write the equivalent of a parser when you write a program in C. It's more like a sort algorithm, if you need to do that, you do it, not because there's some purpose why every C program necessitates the need for a sort algorithm.

You can write or use a parser, as someone has done in the example you've cited and it is a very common programming action to perform when you're converting information from one form to another.

Be aware that verbiage use by a programmer does not guarantee that they are doing exactly what they used for names. In this case I do agree that they are parsing a string; however it seems odd that the name of the string is "binaryReply" which would imply to me that the data potentially could be non-printable ASCII. Further where they say //Skip 0, you have to wonder if that's 0x30 which is ASCII printable "0" or if that's 0x00 which is the NULL string terminator. That would cause some problems, so a guess is that they're skipping a field which they know to be there whatever the content of it is. The strtok() function will just return the next string found after a comma.

The programmer is not verifying the return from strtok(), it could be NULL, and therefore a lot of their following actions could end up becoming segment violations. The string is very customized. How many strings would be exactly: "Hello,<something>,00:00:00:00:00:00\0" as an example? Because that's approximately what it appears they're parsing specifically for. And if there's a format screwup anywhere in there, the program goes kaboom!
 
Old 12-19-2014, 06:29 AM   #5
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
"Hickory dickory dock the mouse ran up the clock"

That's the string I've got, and I want to "eat it, one blank-delimited word at a time," producing, in a loop, nine words in all.

In compiler-writing jargon, those words are called tokens, and that's where the name strtok() comes from: "str[ing] tok[en]." (Not "string to 'k'.")
 
Old 12-27-2014, 09:53 AM   #6
bislinux
LQ Newbie
 
Registered: Dec 2014
Posts: 7

Original Poster
Rep: Reputation: Disabled
Thank u for your kind reply..dat was really helpfull.Cheerss
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Parsing mountpoint from /etc/mtab (parsing "string") in C deadeyes Programming 3 09-06-2011 05:33 PM
Programming in BASH - Parsing a String Mistro116@yahoo.com Programming 9 06-18-2008 06:43 PM
Yacc(Bison) Parameter Parsing -- Modifying a Scientific Programming Language! IncendiaryProgrammer Programming 3 06-23-2006 04:29 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:22 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration