]> git.cameronkatri.com Git - bsdgames-darwin.git/blob - boggle/boggle/prtable.c
Fix merge conflicts
[bsdgames-darwin.git] / boggle / boggle / prtable.c
1 /* $NetBSD: prtable.c,v 1.11 2021/05/02 12:50:43 rillig 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. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * @(#)prtable.c 8.1 (Berkeley) 6/11/93
35 */
36
37 #include <sys/cdefs.h>
38 #ifndef lint
39 __RCSID("$NetBSD: prtable.c,v 1.11 2021/05/02 12:50:43 rillig Exp $");
40 #endif /* not lint */
41
42 #include <ncurses.h>
43 #define __USE(a) (/*LINTED*/(void)(a))
44
45 #include "extern.h"
46
47 #define NCOLS 5
48
49 static int get_maxlen(const char *const [], int, int (*)(const char *const *, int));
50
51 /*
52 * Routine to print a table
53 * Modified from 'ls.c' mods (BJB/83)
54 * Arguments:
55 * base - address of first entry
56 * num - number of entries
57 * d_cols - number of columns to use if > 0, "best" size if == 0
58 * width - max line width if not zero
59 * prentry - address of the routine to call to print the string
60 * length - address of the routine to call to determine the length
61 * of string to be printed
62 *
63 * prtable and length are called with the address of the base and
64 * an index
65 */
66 void
67 prtable(const char *const base[], int num, int d_cols, int width,
68 void (*prentry)(const char *const [], int),
69 int (*length)(const char *const [], int))
70 {
71 int c, j;
72 int a, b, cols, loc, maxlen, nrows, z;
73 int col, row;
74
75 if (num == 0)
76 return;
77 maxlen = get_maxlen(base, num, length) + 1;
78 if (d_cols > 0)
79 cols = d_cols;
80 else
81 cols = width / maxlen;
82 if (cols == 0)
83 cols = NCOLS;
84 nrows = (num - 1) / cols + 1;
85 for (a = 1; a <= nrows; a++) {
86 b = c = z = loc = 0;
87 for (j = 0; j < num; j++) {
88 c++;
89 if (c >= a + b)
90 break;
91 }
92 while (j < num) {
93 (*prentry)(base, j);
94 loc += (*length)(base, j);
95 z++;
96 b += nrows;
97 for (j++; j < num; j++) {
98 c++;
99 if (c >= a + b)
100 break;
101 }
102 if (j < num) {
103 while (loc < z * maxlen) {
104 addch(' ');
105 loc++;
106 }
107 }
108 }
109 getyx(stdscr, row, col);
110 __USE(col);
111 move(row + 1, 0);
112 }
113 refresh();
114 }
115
116 static int
117 get_maxlen(const char *const base[], int num,
118 int (*length)(const char *const *, int))
119 {
120 int i, len, max;
121
122 max = (*length)(base, 0);
123 for (i = 0; i < num; i++) {
124 if ((len = (*length)(base, i)) > max)
125 max = len;
126 }
127 return(max);
128 }