[GRASS-dev] r.colors: rules file list unsorted

Glynn Clements glynn at gclements.plus.com
Mon Jan 29 04:27:34 EST 2007


Paul Kelly wrote:

> >> A library function should probably return the entries as an array of
> >> strings (char **) rather than write them to a stream. The caller can
> >> deal with writing them, if that's desired.
> >
> > That's how it's done in the patched r.colors. If it must be turned
> > into a library function, where would it go? What would its name be?
> > I'm willing to do it (and also change any system("ls")'s remaining).
> 
> I've just finished it - see attached - planning on committing it as 
> lib/gis/ls.c tomorrow after some more polishing if no objections.
> 
> The way I've done it there are 3 functions:
> 
> G__ls() reads the directory and stores the entries in an array.

> G_ls() prints the listing to a specified stream, one entry per line.

Do we really need a library function for a for/fprintf loop?

> > The parser uses column break with a hardcoded value. We certainly
> > could use a global function for determining column width.
> 
> I used the ioctl method. Works for me on Linux but not on Windows - have 
> disabled the functionality there and it just uses hard-coded 80 characters 
> for now. But it does work nicely on Linux as far as I can see.

> #ifndef __MINGW32__
> #  include <sys/ioctl.h>
> #endif

This should use HAVE_SYS_IOCTL_H from <grass/config.h>.

> #ifndef __MINGW32__       
>     if (isatty(fileno(stream)))
>     {
>         /* Determine screen_width if output is a terminal */
> 	struct winsize size;
> 
> 	ioctl(fileno(stream), TIOCGWINSZ, (char *) &size);
> 	screen_width = size.ws_col;
>     }      
> #endif       

The code should be conditionalised upon "#ifdef TIOCGWINSZ". That
should ensure that we don't try to use it if it isn't defined. Even if
the platform has it, it might not be defined in <sys/ioctl.h>. FWIW,
SUSv2 only specifies ioctl() with regard to STREAMS (which is an
optional feature).

Also, the return value from ioctl() should be checked. Having done
that, you can dispense with the isatty() check; call ioctl()
unconditionally and only use the result if it succeeds.

However, I'm still unsure whether we might have problems due to
requiring some unspecified header in order to get "struct winsize". 
Bear in mind that this is libgis, and if libgis fails to compile so
does everything else. Personally, I'd be inclined to just hard-code a
setting of 80 columns.

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




More information about the grass-dev mailing list