End line of text based network protocol

M

Mike Mimic

Hi!

I am programming small program which would use its own
text based network protocol. I come to the problem with
ending lines. I was thinking about ending them with \r\n
combination. But the problem is that I would like that
program would be tolerant to other endings (only \r or \n).

So the question is if there is some efficient way to
implement this? Is there some library which would do this?
It should be platform independent.


Mike
 
I

Ioannis Vranos

Mike Mimic said:
Hi!

I am programming small program which would use its own
text based network protocol. I come to the problem with
ending lines. I was thinking about ending them with \r\n
combination. But the problem is that I would like that
program would be tolerant to other endings (only \r or \n).

So the question is if there is some efficient way to
implement this? Is there some library which would do this?
It should be platform independent.


Well a check for '\r' detectes both '\r' and '\n' so you could do:

if(c=='\r' || c=='\n')
// ...

The first check detects both '\r' and '\r' '\n' sequence and if none of them
exist checks for '\n' alone.






Regards,

Ioannis Vranos
 
M

Mike Mimic

Hi!

Ioannis Vranos wrote:
Well a check for '\r' detectes both '\r' and '\n' so you could do:
if(c=='\r' || c=='\n')
// ...

The first check detects both '\r' and '\r' '\n' sequence and if none of them
exist checks for '\n' alone.

But the problem with this is if end line is \r\n I will get after that
one emtpy line. (Because of \n.)


Mike
 
J

John Harrison

Mike Mimic said:
Hi!

I am programming small program which would use its own
text based network protocol. I come to the problem with
ending lines. I was thinking about ending them with \r\n
combination. But the problem is that I would like that
program would be tolerant to other endings (only \r or \n).

So the question is if there is some efficient way to
implement this? Is there some library which would do this?
It should be platform independent.

I don't see this as anything other than a parsing issue. Presumably you are
parsing the content of lines in your program as well, so why not use
whatever techniques you are already using?

Unless the parsing task was really simple, this is an area where I would get
some help. One well established parsing tool is the flex/bison combination,
but there are loads more. Just google for parser generator.

John
 
M

Mike Mimic

Hi!

John said:
I don't see this as anything other than a parsing issue. Presumably you are
parsing the content of lines in your program as well, so why not use
whatever techniques you are already using?

I would like to have line by line so that I can than know where is the
end of line and so on. I think that it will be easier for me to have
a line to parse than to have multiple lines.
Unless the parsing task was really simple, this is an area where I would get
some help. One well established parsing tool is the flex/bison combination,
but there are loads more. Just google for parser generator.

Anyone any other suggestions?


Mike
 
J

John Harrison

Mike Mimic said:
Hi!



I would like to have line by line so that I can than know where is the
end of line and so on. I think that it will be easier for me to have
a line to parse than to have multiple lines.


Anyone any other suggestions?

Well then you are going to have to write your own line reading function

string read_line(istream& in)
{
string line;
char ch;
while (in.get(ch))
{
if (ch == '\n')
break;
if (ch == '\r')
{
if (in.get(ch) && ch != '\n')
in.putback(ch);
break;
}
line += ch;
}
return line;
}

Untested code.

john
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top