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