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

Glynn Clements glynn at gclements.plus.com
Sun Jan 28 19:01:38 EST 2007


Paul Kelly wrote:

> > Ok, It's working now. Attached.
> 
> What about a library function that does the same thing as system("ls") or 
> system("ls -C"). I can think of several places where it would be useful. 
> It could accept the directory as an argument, also the stream to output 
> too (e.g. stdout or stderr)

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.

> and perhaps a flag to do a column-wise listing 
> (a la "ls -C"). I think this would be the hardest part to do; not sure how 
> ls does it - unless there is something easy we could do with format 
> conversion strings?

Read all of the names, find the longest name to determine the column
widths, divide the width of the terminal[1] by the column width to
determine the number of columns, write out all of the strings padded
to a fixed width.

For the last part, you can either use "%*s" or explicitly add with
"columns + 1 - strlen(name)" spaces.

[1] This is the hardest part, as there are several different ways to
get the terminal width, and they may return different results. E.g.

1. tigetnum("cols")
2. tgetnum("co")
3. getenv("COLUMNS")
4. ioctl(TIOCGWINSZ)
5. The "columns" macro from <term.h> (in conjuction with [n]curses).
6. A hardcoded default of 80 columns

#1 requires terminfo, #2 requires termcap, #3 requires that COLUMNS is
set, #4 may not work on all platforms (although I don't know of any
where it doesn't work), #5 requires [n]curses.

If #4 works, it should be the most reliable, as it handles the case of
an xterm (etc) which has been resized by the user. Depending upon the
curses implementation #5 may also use #4 if it works, or it may use #3
(unless use_env(FALSE) has been called).

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




More information about the grass-dev mailing list