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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
10-26-2004, 04:28 AM
|
#1
|
Member
Registered: Jul 2004
Location: Denmark, Aarhus
Distribution: Ubuntu, Suse
Posts: 98
Rep:
|
another sscanf Q
Hey... I'm trying to make some utility func, to interpret headers. I've haven't really been able to find any libraries that have the ability to process http headers, so I'm making my own (ne1 know any?)
Here my problem in a nutshell
Code:
char buf[2000];
sscanf("include1\r\ninclude2\r\ninclude3\r\n\r\ndonotinclude", "%[^:]\r\n\r\n%*", buf);
printf("\n%s", buf);
My delimter is "\r\n\r\n" and i don't need anything else from this point forward.
This is probably a "must know" question, but I've not been able to figure out sscanf with a string as a delimiter. Any information will be greatly appriciated.
Regards J.
|
|
|
10-26-2004, 05:01 AM
|
#2
|
Moderator
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,795
|
There are tons of http libraries available, see for example:
http://curl.haxx.se/libcurl/competitors.html
http://www.webdav.org/neon/
Beyond that, I'm not sure what precisely your question is ... can you elaborate ?
|
|
|
10-26-2004, 05:23 AM
|
#3
|
Member
Registered: Jul 2004
Location: Denmark, Aarhus
Distribution: Ubuntu, Suse
Posts: 98
Original Poster
Rep:
|
If you look at my string, I need to get everything before the "\r\n\r\n" into the buf variable (including the \r\n's).
|
|
|
10-26-2004, 05:51 AM
|
#4
|
Senior Member
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246
Rep:
|
strcpy(buf, "include1\r\ninclude2\r\ninclude3\r\n\r\ndonotinclude");
|
|
|
10-26-2004, 06:04 AM
|
#5
|
Member
Registered: Jul 2004
Location: Denmark, Aarhus
Distribution: Ubuntu, Suse
Posts: 98
Original Poster
Rep:
|
Ehm... guess you misunderstod it:
I have a string (well... a whole http request actually, but lets keep it simple):
Code:
"include1\r\ninclude2\r\ninclude3\r\n\r\ndonotinclude"
What I need to extract out of this string is:
Code:
"include1\r\ninclude2\r\ninclude3"
and discard
Code:
"\r\n\r\ndonotinclude"
I've been messing around with sscanf, but no luck. With the following code, I just get the entire string saved in buf
Code:
sscanf("include1\r\ninclude2\r\ninclude3\r\n\r\ndonotinclude", "%[^:]\r\n\r\n%*", buf);
output:
Code:
include1
include2
include3
donotinclude
I'm not completly sure how to use the brackets [] and I have read the man page 
|
|
|
10-26-2004, 06:46 AM
|
#6
|
Moderator
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,795
|
Try this instead:
Code:
char *source="include1\r\ninclude2\r\ninclude3\r\n\r\ndonotinclude";
char *s=strstr(source,"\r\n\r\n");
printf("%*.*s\n",0,s-source,source);
|
|
|
10-26-2004, 08:55 AM
|
#7
|
Member
Registered: Jul 2004
Location: Denmark, Aarhus
Distribution: Ubuntu, Suse
Posts: 98
Original Poster
Rep:
|
Thanks jlliagre.. this will work, but I don't understand your syntax (it works though). I would really like to use sscanf, if possible. Ne1 know what is wrong with my syntax?
|
|
|
10-26-2004, 09:43 AM
|
#8
|
Moderator
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,795
|
I'm afraid you want to push scanf beyond its limits ...
Is your goal doing it with scanf, and I'm out of the game, or to extract the HTTP header, and I can explain how to do it with my approach ?
|
|
|
10-27-2004, 01:31 AM
|
#9
|
Member
Registered: Jul 2004
Location: Denmark, Aarhus
Distribution: Ubuntu, Suse
Posts: 98
Original Poster
Rep:
|
Please do. I really don't need to display the string, but return it from a function. I tried using sprintf to save it into a varible.
Code:
char* ptr;
sprintf(ptr, "%*.*s", 0,s-source, source);
It does work, but can you try to explain it further? Or if there's another way to do this. I also thought og readoing the buffer one char at a time, and look for the combination "\r\n\r\n" and return the index. With that number you could just read the nessary header information with the index number. Which would take less time/resources. The function will only be called 1 time pr. request, so I don't know if this will have any impact.
Anyway please do elaborate your ideas
|
|
|
10-27-2004, 02:19 AM
|
#10
|
Moderator
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,795
|
Here's a commented code, I guess it says it all:
Code:
char *getHTTPHeader(char *s)
{
char *trailer=strstr(s,"\r\n\r\n");
// trailer points now to the first occurrence of an empty CRLF terminated line.
char *header;
int len=trailer-s; // len is the HTTP header length
header=(char *)malloc(len+1); // allocate memory for the reply
strncpy(header,s,len); // copy the header part to the reply string
header[len]='\0'; // terminate the string
return(header); // that's it
}
main() // test the function
{
printf("%s\n",
getHTTPHeader("include1\r\ninclude2\r\ninclude3\r\n\r\ndonotinclude"));
}
Last edited by jlliagre; 10-27-2004 at 02:20 AM.
|
|
|
10-27-2004, 02:33 AM
|
#11
|
Member
Registered: Jul 2004
Location: Denmark, Aarhus
Distribution: Ubuntu, Suse
Posts: 98
Original Poster
Rep:
|
I appriciate it jlliagre. Thx
|
|
|
All times are GMT -5. The time now is 06:38 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|