home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume22 / undel2 / part02 / col.c next >
C/C++ Source or Header  |  1990-06-07  |  5KB  |  182 lines

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