Getting a string from a file or socket

View: New views
3 Messages — Rating Filter:   Alert me  

Getting a string from a file or socket

by Joachim Ziegler-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I am missing a function like

   char *fgets(char *s, int size, FILE *stream);

in NSPR, which reads characters from a file or socket and stops when it
encounters an EOF or newline, storing the characters read in a 0-byte
terminated buffer. This is a very common task in server programming. I
do not want do (re-)implement it.

Any ideas? Shouldn't such a basic operation included in the library? As
far as I can see, NSPR only offers the very basic PR_Read().

Joachim
_______________________________________________
dev-tech-nspr mailing list
dev-tech-nspr@...
https://lists.mozilla.org/listinfo/dev-tech-nspr

Re: Getting a string from a file or socket

by Julien R Pierre - Sun Microsystems :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Joachim,

Joachim Ziegler wrote:

> I am missing a function like
>
>    char *fgets(char *s, int size, FILE *stream);
>
> in NSPR, which reads characters from a file or socket and stops when it
> encounters an EOF or newline, storing the characters read in a 0-byte
> terminated buffer. This is a very common task in server programming. I
> do not want do (re-)implement it.
>
> Any ideas? Shouldn't such a basic operation included in the library? As
> far as I can see, NSPR only offers the very basic PR_Read().
>
> Joachim

I don't believe it does. It should be fairly simple for you to implement
that function on top of PR_Read.

One of the reasons for this not being included in NSPR may be that the
EOL characters are platform-specific.

Having done a lot of server programming with NSPR, I can also say that I
would not use a generic function to perform this task - each protocol
RFC is different and allows a certain number of newlines, spaces, etc.
You are better off using the binary PR_Read or PR_Recv functions, and
doing exactly what you want on top of it.
_______________________________________________
dev-tech-nspr mailing list
dev-tech-nspr@...
https://lists.mozilla.org/listinfo/dev-tech-nspr

Re: Getting a string from a file or socket

by Joachim Ziegler-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello Julien,

> One of the reasons for this not being included in NSPR may be that the
> EOL characters are platform-specific.

Now that I try to run my client/server code under Unix *and* Windows, I
see what you mean. :-)

Yes, I really have to write my own version of fgets() that is able to
handle both cases of LF *and* CR+LF being used as the newline indicator.

I guess this has been done a thousand times before, so I would like to
see it in a "repository of useful code snippets" or so.

It is really annoying to have to do this myself. There are so many tiny
little special cases. For example, what if I encounter a CR? Can I rely
on the client also sending a LF as the next character? (I guess this
should be part of the protocol). What if the CR happens to be the last
character of the the buffer? What if the connection is closed after the
CR? And so on.

But I am surely able to do this.

Joachim
_______________________________________________
dev-tech-nspr mailing list
dev-tech-nspr@...
https://lists.mozilla.org/listinfo/dev-tech-nspr
LightInTheBox - Buy quality products at wholesale price!