1 /* vi: set tabstop=4 : */ 2 3 #include <curses.h> 4 5 #define NCOLS 5 6 7 static int get_maxlen(); 8 9 /* 10 * Routine to print a table 11 * Modified from 'ls.c' mods (BJB/83) 12 * Arguments: 13 * base - address of first entry 14 * num - number of entries 15 * d_cols - number of columns to use if > 0, "best" size if == 0 16 * width - max line width if not zero 17 * prentry - address of the routine to call to print the string 18 * length - address of the routine to call to determine the length 19 * of string to be printed 20 * 21 * prtable and length are called with the the address of the base and 22 * an index 23 */ 24 prtable(base, num, d_cols, width, prentry, length) 25 char *base; 26 int num, d_cols; 27 int (*prentry)(), (*length)(); 28 { 29 register int c, j; 30 register int a, b, cols, loc, maxlen, nrows, z; 31 int col, row; 32 33 if (num == 0) 34 return; 35 maxlen = get_maxlen(base, num, length) + 1; 36 if (d_cols > 0) 37 cols = d_cols; 38 else 39 cols = width / maxlen; 40 if (cols == 0) 41 cols = NCOLS; 42 nrows = (num - 1) / cols + 1; 43 for (a = 1; a <= nrows; a++) { 44 b = c = z = loc = 0; 45 for (j = 0; j < num; j++) { 46 c++; 47 if (c >= a + b) 48 break; 49 } 50 while (j < num) { 51 (*prentry)(base, j); 52 loc += (*length)(base, j); 53 z++; 54 b += nrows; 55 for (j++; j < num; j++) { 56 c++; 57 if (c >= a + b) 58 break; 59 } 60 if (j < num) { 61 while (loc < z * maxlen) { 62 addch(' '); 63 loc++; 64 } 65 } 66 } 67 getyx(stdscr, row, col); 68 move(row + 1, 0); 69 } 70 refresh(); 71 } 72 73 static int 74 get_maxlen(base, num, length) 75 char *base; 76 int num; 77 int (*length)(); 78 { 79 register int i, len, max; 80 81 max = (*length)(base, 0); 82 for (i = 0; i < num; i++) { 83 if ((len = (*length)(base, i)) > max) 84 max = len; 85 } 86 return(max); 87 } 88 89