LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 10-26-2004, 04:28 AM   #1
jnusa
Member
 
Registered: Jul 2004
Location: Denmark, Aarhus
Distribution: Ubuntu, Suse
Posts: 98

Rep: Reputation: 15
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.
 
Old 10-26-2004, 05:01 AM   #2
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
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 ?
 
Old 10-26-2004, 05:23 AM   #3
jnusa
Member
 
Registered: Jul 2004
Location: Denmark, Aarhus
Distribution: Ubuntu, Suse
Posts: 98

Original Poster
Rep: Reputation: 15
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).
 
Old 10-26-2004, 05:51 AM   #4
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
strcpy(buf, "include1\r\ninclude2\r\ninclude3\r\n\r\ndonotinclude");
 
Old 10-26-2004, 06:04 AM   #5
jnusa
Member
 
Registered: Jul 2004
Location: Denmark, Aarhus
Distribution: Ubuntu, Suse
Posts: 98

Original Poster
Rep: Reputation: 15
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
 
Old 10-26-2004, 06:46 AM   #6
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
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);
 
Old 10-26-2004, 08:55 AM   #7
jnusa
Member
 
Registered: Jul 2004
Location: Denmark, Aarhus
Distribution: Ubuntu, Suse
Posts: 98

Original Poster
Rep: Reputation: 15
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?
 
Old 10-26-2004, 09:43 AM   #8
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
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 ?
 
Old 10-27-2004, 01:31 AM   #9
jnusa
Member
 
Registered: Jul 2004
Location: Denmark, Aarhus
Distribution: Ubuntu, Suse
Posts: 98

Original Poster
Rep: Reputation: 15
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
 
Old 10-27-2004, 02:19 AM   #10
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
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.
 
Old 10-27-2004, 02:33 AM   #11
jnusa
Member
 
Registered: Jul 2004
Location: Denmark, Aarhus
Distribution: Ubuntu, Suse
Posts: 98

Original Poster
Rep: Reputation: 15
I appriciate it jlliagre. Thx
 
  


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
Using sscanf to grab an unknown number of variables R00ts Programming 4 06-06-2005 05:03 PM
read /proc/net/dev with sscanf? Thinking Linux - General 0 03-30-2005 09:14 AM
Garbage value dumped by sscanf sheenak Programming 2 05-27-2004 11:55 PM
sscanf help jpc82 Programming 8 11-04-2003 02:01 PM
glibc patch for sscanf.c jarin scott Linux - Software 2 08-03-2003 02:28 PM

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

All times are GMT -5. The time now is 01: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