[GRASS-dev] Right way to use G_getl2

Glynn Clements glynn at gclements.plus.com
Fri Sep 13 19:45:23 PDT 2013


Vaclav Petras wrote:

> So, finally: What is the right usage of G_getl2? Should the caller use
> buffer size equal to maximum number of expected characters on line plus end
> of line character (plus 2 on MS Windows) plus terminating character? Or
> should he just pass the n parameter increased by one (two on MS Windows)
> since in fact nothing will be stored to buffer? Or should he just allocate
> really huge buffer and than read chars from buffer if he wants to store
> them?

The short answer is that the buffer needs to be more than large
enough. As it turns out, it needs at least two more characters: one
for the NUL and one to ensure that the line terminator is read even
though it isn't stored.

FWIW, I suggest changing the existing behaviour so that it always
reads an entire line, even if it can't store it all. IOW, read until
EOL or EOF, stop storing characters once the buffer is full.

Unlike with fgets(), which stores the line terminator in the buffer,
there is no way for the caller of G_getl2() to determine that an
incomplete line was read. If the buffer is too small, you lose either
way; discarding the trailing characters is likely to be more robust
than returning them as if they were a subsequent line (the current
behaviour).

-- 
Glynn Clements <glynn at gclements.plus.com>


More information about the grass-dev mailing list