1 | /* |
---|
2 | * $Id: col.c,v 1.10 1999-01-22 23:08:50 ghudson Exp $ |
---|
3 | * |
---|
4 | * This program is part of a package including delete, undelete, |
---|
5 | * lsdel, expunge and purge. The software suite is meant as a |
---|
6 | * replacement for rm which allows for file recovery. |
---|
7 | * |
---|
8 | * Copyright (c) 1989 by the Massachusetts Institute of Technology. |
---|
9 | * For copying and distribution information, see the file "mit-copying.h." |
---|
10 | */ |
---|
11 | |
---|
12 | #if (!defined(lint) && !defined(SABER)) |
---|
13 | static char rcsid_col_c[] = "$Id: col.c,v 1.10 1999-01-22 23:08:50 ghudson Exp $"; |
---|
14 | #endif |
---|
15 | |
---|
16 | /* |
---|
17 | * Note that this function has a lot of options I'm not really using |
---|
18 | * because I took it out of other code that needed a lot more |
---|
19 | * versatility. |
---|
20 | */ |
---|
21 | |
---|
22 | #include <stdio.h> |
---|
23 | #include <string.h> |
---|
24 | #include "errors.h" |
---|
25 | #include "delete_errs.h" |
---|
26 | #include "col.h" |
---|
27 | #include "mit-copying.h" |
---|
28 | |
---|
29 | |
---|
30 | static int calc_string_width(), calc_widths(), num_width(); |
---|
31 | static void trim_strings(); |
---|
32 | |
---|
33 | int column_array(strings, num_to_print, screen_width, column_width, |
---|
34 | number_of_columns, margin, spread_flag, |
---|
35 | number_flag, var_col_flag, outfile) |
---|
36 | char **strings; |
---|
37 | FILE *outfile; |
---|
38 | { |
---|
39 | char buf[BUFSIZ]; |
---|
40 | int updown, leftright, height; |
---|
41 | int string_width; |
---|
42 | int numwidth; |
---|
43 | |
---|
44 | numwidth = num_width(num_to_print); |
---|
45 | if (! var_col_flag) { |
---|
46 | string_width = calc_string_width(column_width, margin, number_flag, |
---|
47 | num_to_print); |
---|
48 | if (string_width <= 0) { |
---|
49 | set_error(COL_COLUMNS_TOO_THIN); |
---|
50 | error("calc_string_width"); |
---|
51 | return error_code; |
---|
52 | } |
---|
53 | trim_strings(strings, num_to_print, string_width); |
---|
54 | } else if (calc_widths(strings, &screen_width, &column_width, |
---|
55 | &number_of_columns, num_to_print, &margin, |
---|
56 | spread_flag, number_flag)) { |
---|
57 | error("calc_widths"); |
---|
58 | return error_code; |
---|
59 | } |
---|
60 | height = num_to_print / number_of_columns; |
---|
61 | if (num_to_print % number_of_columns) |
---|
62 | height++; |
---|
63 | |
---|
64 | if (number_flag) for (updown = 0; updown < height; updown++) { |
---|
65 | for (leftright = updown; leftright < num_to_print; ) { |
---|
66 | (void) sprintf(buf, "%*d. %s", numwidth, leftright+1, |
---|
67 | strings[leftright]); |
---|
68 | if ((leftright += height) >= num_to_print) |
---|
69 | fprintf(outfile, "%s", buf ); |
---|
70 | else |
---|
71 | fprintf(outfile, "%*s", -column_width, buf); |
---|
72 | } |
---|
73 | fprintf(outfile, "\n"); |
---|
74 | } else for (updown = 0; updown < height; updown++) { |
---|
75 | for (leftright = updown; leftright < num_to_print; ) { |
---|
76 | (void) sprintf(buf, "%s", strings[leftright]); |
---|
77 | if ((leftright += height) >= num_to_print) |
---|
78 | fprintf(outfile, "%s", buf ); |
---|
79 | else |
---|
80 | fprintf(outfile, "%*s", -column_width, buf); |
---|
81 | } |
---|
82 | fprintf(outfile, "\n"); |
---|
83 | } |
---|
84 | return 0; |
---|
85 | } |
---|
86 | |
---|
87 | static int calc_string_width(column_width, margin, number_flag, max_number) |
---|
88 | { |
---|
89 | int string_width; |
---|
90 | |
---|
91 | string_width = column_width - margin; |
---|
92 | if (number_flag) |
---|
93 | string_width = string_width - num_width(max_number) - strlen(". "); |
---|
94 | return string_width; |
---|
95 | } |
---|
96 | |
---|
97 | |
---|
98 | static void trim_strings(strings, number, width) |
---|
99 | char **strings; |
---|
100 | { |
---|
101 | int loop; |
---|
102 | |
---|
103 | for (loop = 0; loop < number; loop++) |
---|
104 | if (strlen(strings[loop]) > width) |
---|
105 | strings[loop][width] = '\0'; |
---|
106 | } |
---|
107 | |
---|
108 | |
---|
109 | static int calc_widths(strings, screen_width, column_width, number_of_columns, |
---|
110 | num_to_print, margin, spread_flag, number_flag) |
---|
111 | int *screen_width, *column_width, *number_of_columns, *margin; |
---|
112 | char **strings; |
---|
113 | { |
---|
114 | int loop; |
---|
115 | int maxlen, templen; |
---|
116 | int spread; |
---|
117 | |
---|
118 | maxlen = templen = 0; |
---|
119 | for (loop = 0; loop < num_to_print; loop++) |
---|
120 | if (maxlen < (templen = strlen(strings[loop]))) |
---|
121 | maxlen = templen; |
---|
122 | |
---|
123 | *column_width = maxlen; |
---|
124 | |
---|
125 | if (number_flag) |
---|
126 | *column_width = *column_width + num_width(num_to_print) + |
---|
127 | strlen(". "); |
---|
128 | |
---|
129 | if (! spread_flag) { |
---|
130 | *column_width += *margin; |
---|
131 | if (! *number_of_columns) { |
---|
132 | *number_of_columns = *screen_width / *column_width; |
---|
133 | if (! *number_of_columns) { |
---|
134 | (*number_of_columns)++; |
---|
135 | *column_width -= *margin; |
---|
136 | *margin = 0; |
---|
137 | *screen_width = *column_width; |
---|
138 | } |
---|
139 | } |
---|
140 | else |
---|
141 | *screen_width = *number_of_columns * *column_width; |
---|
142 | } else { |
---|
143 | if (! *number_of_columns) { |
---|
144 | *number_of_columns = *screen_width / (*column_width + *margin); |
---|
145 | if (! *number_of_columns) { |
---|
146 | (*number_of_columns)++; |
---|
147 | *screen_width = *column_width; |
---|
148 | *margin = 0; |
---|
149 | } |
---|
150 | spread = (*screen_width - *number_of_columns * *column_width) |
---|
151 | / *number_of_columns; |
---|
152 | *column_width += spread; |
---|
153 | } |
---|
154 | else { |
---|
155 | if (*number_of_columns * (*column_width + *margin) > |
---|
156 | *screen_width) { |
---|
157 | *column_width += *margin; |
---|
158 | *screen_width = *column_width; |
---|
159 | } else { |
---|
160 | spread = (*screen_width - (*number_of_columns * |
---|
161 | *column_width)) / |
---|
162 | *number_of_columns; |
---|
163 | *column_width += spread; |
---|
164 | } |
---|
165 | } |
---|
166 | } |
---|
167 | return 0; |
---|
168 | } |
---|
169 | |
---|
170 | |
---|
171 | |
---|
172 | |
---|
173 | static int num_width(number) |
---|
174 | int number; |
---|
175 | { |
---|
176 | char buf[BUFSIZ]; |
---|
177 | |
---|
178 | (void) sprintf(buf, "%d", number); |
---|
179 | return strlen(buf); |
---|
180 | } |
---|