source: trunk/athena/bin/delete/col.c @ 24908

Revision 24908, 4.7 KB checked in by ghudson, 14 years ago (diff)
In delete: * Patches from Jonathan Kamens: - The "-f" flag to delete should suppress nonexistent file errors but not other errors. - When the "-v" flag is specified to expunge, the correct totals should be reported. Previously, the totals were incorrect. - Code cleanup.
Line 
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/*
13 * Note that this function has a lot of options I'm not really using
14 * because I took it out of other code that needed a lot more
15 * versatility.
16 */
17
18#include <string.h>
19#include "errors.h"
20#include "delete_errs.h"
21#include "col.h"
22#include "mit-copying.h"
23#include "util.h"
24
25static int calc_string_width(), calc_widths(), num_width();
26static void trim_strings();
27
28int column_array(char **strings, int num_to_print, int screen_width,
29                 int column_width, int number_of_columns, int margin,
30                 int spread_flag, int number_flag, int var_col_flag,
31                 FILE *outfile)
32{
33     char buf[BUFSIZ];
34     int updown, leftright, height;
35     int string_width;
36     int numwidth;
37
38     numwidth = num_width(num_to_print);
39     if (! var_col_flag) {
40          string_width = calc_string_width(column_width, margin, number_flag,
41                                           num_to_print);
42          if (string_width <= 0) {
43               set_error(COL_COLUMNS_TOO_THIN);
44               error("calc_string_width");
45               return error_code;
46          }
47          trim_strings(strings, num_to_print, string_width);
48     } else if (calc_widths(strings, &screen_width, &column_width,
49                            &number_of_columns, num_to_print, &margin,
50                            spread_flag, number_flag)) {
51          error("calc_widths");
52          return error_code;
53     }
54     height = num_to_print / number_of_columns;
55     if (num_to_print % number_of_columns)
56          height++;
57     
58     if (number_flag) for (updown = 0; updown < height; updown++) {
59          for (leftright = updown; leftright < num_to_print; ) {
60               (void) sprintf(buf, "%*d. %s", numwidth, leftright+1,
61                              strings[leftright]);
62               if ((leftright += height) >= num_to_print)
63                    fprintf(outfile, "%s", buf );
64               else
65                    fprintf(outfile, "%*s", -column_width, buf);
66          }
67          fprintf(outfile, "\n");
68     } else for (updown = 0; updown < height; updown++) {
69          for (leftright = updown; leftright < num_to_print; ) {
70               (void) sprintf(buf, "%s", strings[leftright]);
71               if ((leftright += height) >= num_to_print)
72                    fprintf(outfile, "%s", buf );
73               else
74                    fprintf(outfile, "%*s", -column_width, buf);
75          }
76          fprintf(outfile, "\n");
77     }
78     return 0;
79}
80
81static int calc_string_width(column_width, margin, number_flag, max_number)
82{
83     int string_width;
84     
85     string_width = column_width - margin;
86     if (number_flag)
87          string_width = string_width - num_width(max_number) - strlen(". ");
88     return string_width;
89}
90
91
92static void trim_strings(strings, number, width)
93char **strings;
94{
95     int loop;
96     
97     for (loop = 0; loop < number; loop++)
98          if (strlen(strings[loop]) > width)
99               strings[loop][width] = '\0';
100}
101
102
103static int calc_widths(strings, screen_width, column_width, number_of_columns,
104                       num_to_print, margin, spread_flag, number_flag)
105int *screen_width, *column_width, *number_of_columns, *margin;
106char **strings;
107{
108     int loop;
109     int maxlen, templen;
110     int spread;
111     
112     maxlen = templen = 0;
113     for (loop = 0; loop < num_to_print; loop++)
114          if (maxlen < (templen = strlen(strings[loop])))
115               maxlen = templen;
116
117     *column_width = maxlen;
118     
119     if (number_flag)
120          *column_width = *column_width + num_width(num_to_print) +
121               strlen(". ");
122
123     if (! spread_flag) {
124          *column_width += *margin;
125          if (! *number_of_columns) {
126               *number_of_columns = *screen_width / *column_width;
127               if (! *number_of_columns) {
128                    (*number_of_columns)++;
129                    *column_width -= *margin;
130                    *margin = 0;
131                    *screen_width = *column_width;
132               }
133          }
134          else
135               *screen_width = *number_of_columns * *column_width;
136     } else {
137          if (! *number_of_columns) {
138               *number_of_columns = *screen_width / (*column_width + *margin);
139               if (! *number_of_columns) {
140                    (*number_of_columns)++;
141                    *screen_width = *column_width;
142                    *margin = 0;
143               }
144               spread = (*screen_width - *number_of_columns * *column_width)
145                    / *number_of_columns;
146               *column_width += spread;
147          }
148          else {
149               if (*number_of_columns * (*column_width + *margin) >
150                   *screen_width) {
151                    *column_width += *margin;
152                    *screen_width = *column_width;
153               } else {
154                    spread = (*screen_width - (*number_of_columns *
155                                               *column_width)) /
156                                                    *number_of_columns;
157                    *column_width += spread;
158               }
159          }
160     }
161     return 0;
162}
163
164
165               
166
167static int num_width(number)
168int number;
169{
170     char buf[BUFSIZ];
171
172     (void) sprintf(buf, "%d", number);
173     return strlen(buf);
174}
Note: See TracBrowser for help on using the repository browser.