xref: /original-bsd/games/boggle/boggle/prtable.c (revision 3705696b)
1 /*-
2  * Copyright (c) 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Barry Brachman.
7  *
8  * %sccs.include.redist.c%
9  *
10  *	@(#)prtable.c	8.1 (Berkeley) 06/11/93
11  */
12 
13 #include <curses.h>
14 
15 #include "extern.h"
16 
17 #define NCOLS	5
18 
19 static int	get_maxlen __P((char *[], int, int (*)(char **, int)));
20 
21 /*
22  * Routine to print a table
23  * Modified from 'ls.c' mods (BJB/83)
24  * Arguments:
25  *	base	- address of first entry
26  *	num     - number of entries
27  *	d_cols  - number of columns to use if > 0, "best" size if == 0
28  *	width	- max line width if not zero
29  *	prentry - address of the routine to call to print the string
30  *	length  - address of the routine to call to determine the length
31  *		  of string to be printed
32  *
33  * prtable and length are called with the the address of the base and
34  * an index
35  */
36 void
37 prtable(base, num, d_cols, width, prentry, length)
38 	char *base[];
39 	int num, d_cols, width;
40 	void (*prentry) __P((char *[], int));
41 	int (*length) __P((char *[], int));
42 {
43         register int c, j;
44         register int a, b, cols, loc, maxlen, nrows, z;
45 	int col, row;
46 
47         if (num == 0)
48                 return;
49 	maxlen = get_maxlen(base, num, length) + 1;
50 	if (d_cols > 0)
51 		cols = d_cols;
52 	else
53 		cols = width / maxlen;
54 	if (cols == 0)
55 		cols = NCOLS;
56         nrows = (num - 1) / cols + 1;
57         for (a = 1; a <= nrows; a++) {
58                 b = c = z = loc = 0;
59                 for (j = 0; j < num; j++) {
60                         c++;
61                         if (c >= a + b)
62                                 break;
63                 }
64                 while (j < num) {
65                         (*prentry)(base, j);
66 			loc += (*length)(base, j);
67                         z++;
68                         b += nrows;
69                         for (j++; j < num; j++) {
70                                 c++;
71                                 if (c >= a + b)
72                                         break;
73                         }
74                         if (j < num) {
75                                 while (loc < z * maxlen) {
76 					addch(' ');
77                                         loc++;
78                                 }
79 			}
80                 }
81 		getyx(stdscr, row, col);
82 		move(row + 1, 0);
83         }
84 	refresh();
85 }
86 
87 static int
88 get_maxlen(base, num, length)
89 	char *base[];
90 	int num;
91 	int (*length) __P((char **, int));
92 {
93 	register int i, len, max;
94 
95 	max = (*length)(base, 0);
96 	for (i = 0; i < num; i++) {
97 		if ((len = (*length)(base, i)) > max)
98 			max = len;
99 	}
100 	return(max);
101 }
102