Connect with Expertise | Find Experts, Get Answers, Share Insights

End line of text based network protocol

Mike Mimic
 
Posts: n/a
#1: Jul 22 '05
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

Ioannis Vranos
 
Posts: n/a
#2: Jul 22 '05

re: End line of text based network protocol


"Mike Mimic" <ppagee@yahoo.com> wrote in message
news:c7ehnj$c0n$1@planja.arnes.si...[color=blue]
> 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.[/color]


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

Mike Mimic
 
Posts: n/a
#3: Jul 22 '05

re: End line of text based network protocol


Hi!

Ioannis Vranos wrote:
Well a check for '\r' detectes both '\r' and '\n' so you could do:[color=blue]
>
> 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.[/color]

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


Mike
John Harrison
 
Posts: n/a
#4: Jul 22 '05

re: End line of text based network protocol



"Mike Mimic" <ppagee@yahoo.com> wrote in message
news:c7ehnj$c0n$1@planja.arnes.si...[color=blue]
> 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.
>[/color]

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


Mike Mimic
 
Posts: n/a
#5: Jul 22 '05

re: End line of text based network protocol


Hi!

John Harrison wrote:[color=blue]
> 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?[/color]

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.
[color=blue]
> 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.[/color]

Anyone any other suggestions?


Mike
John Harrison
 
Posts: n/a
#6: Jul 22 '05

re: End line of text based network protocol



"Mike Mimic" <ppagee@yahoo.com> wrote in message
news:c7g3s0$l5b$1@planja.arnes.si...[color=blue]
> Hi!
>
> John Harrison wrote:[color=green]
> > I don't see this as anything other than a parsing issue. Presumably you[/color][/color]
are[color=blue][color=green]
> > parsing the content of lines in your program as well, so why not use
> > whatever techniques you are already using?[/color]
>
> 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.
>[color=green]
> > Unless the parsing task was really simple, this is an area where I would[/color][/color]
get[color=blue][color=green]
> > some help. One well established parsing tool is the flex/bison[/color][/color]
combination,[color=blue][color=green]
> > but there are loads more. Just google for parser generator.[/color]
>
> Anyone any other suggestions?
>[/color]

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


Closed Thread