cr*******@gmail.com wrote:
What's the best way to scan in the standard input line by line in C? I
need to signal the end of input after two \n 's have been inputted.
The only solution I right now to deal with it is to scan in each
character, concatenate it to the previous characters entered, and stop
the input when the last four characters and \n\n. Is there a simpler
way to do this?
I see that you want to parse the http/sip protocol headers. But there
is \r\n at eacj end of the line rather than \n\n. And at the end of the
protocol header there would be \r\n\r\n rather that \n\n\n\n.
Anyway, there is no c function that uses readline kind of functionality
and one need to craft it.
What I did was take the input into a buffer and look for \r\n using
strstr function. Then I copy the contents to another buffer and use
strstr function again after 2 bytes (\r\n). If the strstr function
returns null, it could be that there is more input needed. So move the
remaining buffer to the start of the buffer and append the input.
Restart the check. At each stage check for \r\n\r\n which means the end
of the headers.
It is not efficient but it solved my purpose :)
If you got confused check for the apache source code for http parser.
It does almost the same mechanism.
-kondal