xref: /netbsd/games/boggle/boggle/prtable.c (revision bf9ec67e)
1 /*	$NetBSD: prtable.c,v 1.6 2000/03/13 22:49:31 soren Exp $	*/
2 
3 /*-
4  * Copyright (c) 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Barry Brachman.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the University of
21  *	California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  *	@(#)prtable.c	8.1 (Berkeley) 6/11/93
39  */
40 
41 #include <sys/cdefs.h>
42 #ifndef lint
43 __RCSID("$NetBSD: prtable.c,v 1.6 2000/03/13 22:49:31 soren Exp $");
44 #endif /* not lint */
45 
46 #include <curses.h>
47 
48 #include "extern.h"
49 
50 #define NCOLS	5
51 
52 static int	get_maxlen __P((const char *const [], int, int (*)(const char *const *, int)));
53 
54 /*
55  * Routine to print a table
56  * Modified from 'ls.c' mods (BJB/83)
57  * Arguments:
58  *	base	- address of first entry
59  *	num     - number of entries
60  *	d_cols  - number of columns to use if > 0, "best" size if == 0
61  *	width	- max line width if not zero
62  *	prentry - address of the routine to call to print the string
63  *	length  - address of the routine to call to determine the length
64  *		  of string to be printed
65  *
66  * prtable and length are called with the address of the base and
67  * an index
68  */
69 void
70 prtable(base, num, d_cols, width, prentry, length)
71 	const char *const base[];
72 	int num, d_cols, width;
73 	void (*prentry) __P((const char *const [], int));
74 	int (*length) __P((const char *const [], int));
75 {
76         int c, j;
77         int a, b, cols, loc, maxlen, nrows, z;
78 	int col, row;
79 
80         if (num == 0)
81                 return;
82 	maxlen = get_maxlen(base, num, length) + 1;
83 	if (d_cols > 0)
84 		cols = d_cols;
85 	else
86 		cols = width / maxlen;
87 	if (cols == 0)
88 		cols = NCOLS;
89         nrows = (num - 1) / cols + 1;
90         for (a = 1; a <= nrows; a++) {
91                 b = c = z = loc = 0;
92                 for (j = 0; j < num; j++) {
93                         c++;
94                         if (c >= a + b)
95                                 break;
96                 }
97                 while (j < num) {
98                         (*prentry)(base, j);
99 			loc += (*length)(base, j);
100                         z++;
101                         b += nrows;
102                         for (j++; j < num; j++) {
103                                 c++;
104                                 if (c >= a + b)
105                                         break;
106                         }
107                         if (j < num) {
108                                 while (loc < z * maxlen) {
109 					addch(' ');
110                                         loc++;
111                                 }
112 			}
113                 }
114 		getyx(stdscr, row, col);
115 		move(row + 1, 0);
116         }
117 	refresh();
118 }
119 
120 static int
121 get_maxlen(base, num, length)
122 	const char *const base[];
123 	int num;
124 	int (*length) __P((const char *const *, int));
125 {
126 	int i, len, max;
127 
128 	max = (*length)(base, 0);
129 	for (i = 0; i < num; i++) {
130 		if ((len = (*length)(base, i)) > max)
131 			max = len;
132 	}
133 	return(max);
134 }
135