[1714] | 1 | /* |
---|
[12350] | 2 | * $Id: col.c,v 1.10 1999-01-22 23:08:50 ghudson Exp $ |
---|
[1714] | 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. |
---|
[4505] | 9 | * For copying and distribution information, see the file "mit-copying.h." |
---|
[1714] | 10 | */ |
---|
| 11 | |
---|
| 12 | #if (!defined(lint) && !defined(SABER)) |
---|
[12350] | 13 | static char rcsid_col_c[] = "$Id: col.c,v 1.10 1999-01-22 23:08:50 ghudson Exp $"; |
---|
[1714] | 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> |
---|
[3049] | 23 | #include <string.h> |
---|
[2186] | 24 | #include "errors.h" |
---|
| 25 | #include "delete_errs.h" |
---|
[1714] | 26 | #include "col.h" |
---|
[4505] | 27 | #include "mit-copying.h" |
---|
[1714] | 28 | |
---|
| 29 | |
---|
[2239] | 30 | static int calc_string_width(), calc_widths(), num_width(); |
---|
[1714] | 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); |
---|
[2186] | 48 | if (string_width <= 0) { |
---|
| 49 | set_error(COL_COLUMNS_TOO_THIN); |
---|
| 50 | error("calc_string_width"); |
---|
| 51 | return error_code; |
---|
[1714] | 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, |
---|
[2186] | 56 | spread_flag, number_flag)) { |
---|
| 57 | error("calc_widths"); |
---|
| 58 | return error_code; |
---|
| 59 | } |
---|
[1714] | 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++) { |
---|
[1847] | 65 | for (leftright = updown; leftright < num_to_print; ) { |
---|
[1714] | 66 | (void) sprintf(buf, "%*d. %s", numwidth, leftright+1, |
---|
| 67 | strings[leftright]); |
---|
[1847] | 68 | if ((leftright += height) >= num_to_print) |
---|
| 69 | fprintf(outfile, "%s", buf ); |
---|
| 70 | else |
---|
| 71 | fprintf(outfile, "%*s", -column_width, buf); |
---|
[1714] | 72 | } |
---|
| 73 | fprintf(outfile, "\n"); |
---|
| 74 | } else for (updown = 0; updown < height; updown++) { |
---|
[1847] | 75 | for (leftright = updown; leftright < num_to_print; ) { |
---|
[1714] | 76 | (void) sprintf(buf, "%s", strings[leftright]); |
---|
[1847] | 77 | if ((leftright += height) >= num_to_print) |
---|
| 78 | fprintf(outfile, "%s", buf ); |
---|
| 79 | else |
---|
| 80 | fprintf(outfile, "%*s", -column_width, buf); |
---|
[1714] | 81 | } |
---|
| 82 | fprintf(outfile, "\n"); |
---|
| 83 | } |
---|
[2186] | 84 | return 0; |
---|
[1714] | 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(". "); |
---|
[2186] | 94 | return string_width; |
---|
[1714] | 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; |
---|
[2186] | 122 | |
---|
[1714] | 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 | } |
---|
[2186] | 167 | return 0; |
---|
[1714] | 168 | } |
---|
| 169 | |
---|
| 170 | |
---|
| 171 | |
---|
| 172 | |
---|
| 173 | static int num_width(number) |
---|
[2186] | 174 | int number; |
---|
[1714] | 175 | { |
---|
| 176 | char buf[BUFSIZ]; |
---|
| 177 | |
---|
[2186] | 178 | (void) sprintf(buf, "%d", number); |
---|
| 179 | return strlen(buf); |
---|
[1714] | 180 | } |
---|