home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume18 / undel / part02 / col.c next >
Encoding:
C/C++ Source or Header  |  1989-05-06  |  5.2 KB  |  186 lines

  1. /*
  2.  * $Source: /mit/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: col.c,v 1.2 89/03/27 12:05:12 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 "col.h"
  26. #include "mit-copyright.h"
  27.  
  28.  
  29. static int calc_string_width();
  30. static void trim_strings();
  31. extern char *malloc();
  32. extern char *whoami;
  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.  
  46.      numwidth = num_width(num_to_print);
  47.      if (! var_col_flag) {
  48.       string_width = calc_string_width(column_width, margin, number_flag,
  49.                        num_to_print);
  50.       if (string_width < 0) {
  51.            fprintf(stderr,
  52.                "%s: do_wait: your columns aren't wide enough!\n",
  53.                whoami);
  54.            return(1);
  55.       }
  56.       trim_strings(strings, num_to_print, string_width);
  57.      } else if (calc_widths(strings, &screen_width, &column_width,
  58.                 &number_of_columns, num_to_print, &margin,
  59.                 spread_flag, number_flag))
  60.       return(1);
  61.  
  62.      height = num_to_print / number_of_columns;
  63.      if (num_to_print % number_of_columns)
  64.       height++;
  65.      
  66.      if (number_flag) for (updown = 0; updown < height; updown++) {
  67.       for (leftright = updown; leftright < num_to_print;
  68.            leftright += height) {
  69.            (void) sprintf(buf, "%*d. %s", numwidth, leftright+1,
  70.                   strings[leftright]);
  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.            leftright += height) {
  77.            (void) sprintf(buf, "%s", strings[leftright]);
  78.            fprintf(outfile, "%*s", -column_width, buf);
  79.       }
  80.       fprintf(outfile, "\n");
  81.      }
  82.      
  83.      return(0);
  84. }
  85.  
  86. static int calc_string_width(column_width, margin, number_flag, max_number)
  87. {
  88.      int string_width;
  89.      
  90.      string_width = column_width - margin;
  91.      if (number_flag)
  92.       string_width = string_width - num_width(max_number) - strlen(". ");
  93.      return(string_width);
  94. }
  95.  
  96.  
  97. static void trim_strings(strings, number, width)
  98. char **strings;
  99. {
  100.      int loop;
  101.      
  102.      for (loop = 0; loop < number; loop++)
  103.       if (strlen(strings[loop]) > width)
  104.            strings[loop][width] = '\0';
  105. }
  106.  
  107.  
  108. static int calc_widths(strings, screen_width, column_width, number_of_columns,
  109.                num_to_print, margin, spread_flag, number_flag)
  110. int *screen_width, *column_width, *number_of_columns, *margin;
  111. char **strings;
  112. {
  113.      int loop;
  114.      int maxlen, templen;
  115.      int spread;
  116.      
  117. #ifdef DEBUG
  118.      printf("calc_widths starting with screen_width %d column_width %d number_of_columns %d margin %d num_to_print %d spread_flag %d number_flag %d\n", *screen_width, *column_width, *number_of_columns, *margin, num_to_print, spread_flag, number_flag);
  119. #endif
  120.      maxlen = templen = 0;
  121.      for (loop = 0; loop < num_to_print; loop++)
  122.       if (maxlen < (templen = strlen(strings[loop])))
  123.            maxlen = templen;
  124. #ifdef DEBUG
  125.      printf("calc_widths maxlen %d\n", maxlen);
  126. #endif
  127.      *column_width = maxlen;
  128.      
  129.      if (number_flag)
  130.       *column_width = *column_width + num_width(num_to_print) +
  131.            strlen(". ");
  132.  
  133.      if (! spread_flag) {
  134.       *column_width += *margin;
  135.       if (! *number_of_columns) {
  136.            *number_of_columns = *screen_width / *column_width;
  137.            if (! *number_of_columns) {
  138.             (*number_of_columns)++;
  139.             *column_width -= *margin;
  140.             *margin = 0;
  141.             *screen_width = *column_width;
  142.            }
  143.       }
  144.       else
  145.            *screen_width = *number_of_columns * *column_width;
  146.      } else {
  147.       if (! *number_of_columns) {
  148.            *number_of_columns = *screen_width / (*column_width + *margin);
  149.            if (! *number_of_columns) {
  150.             (*number_of_columns)++;
  151.             *screen_width = *column_width;
  152.             *margin = 0;
  153.            }
  154.            spread = (*screen_width - *number_of_columns * *column_width)
  155.             / *number_of_columns;
  156.            *column_width += spread;
  157.       }
  158.       else {
  159.            if (*number_of_columns * (*column_width + *margin) >
  160.            *screen_width) {
  161.             *column_width += *margin;
  162.             *screen_width = *column_width;
  163.            } else {
  164.             spread = (*screen_width - (*number_of_columns *
  165.                            *column_width)) /
  166.                             *number_of_columns;
  167.             *column_width += spread;
  168.            }
  169.       }
  170.      }
  171. #ifdef DEBUG
  172.      printf("calc_widths returning screen_width %d column_width %d number_of_columns %d margin %d\n", *screen_width, *column_width, *number_of_columns, *margin);
  173. #endif
  174.      return(0);
  175. }
  176.  
  177.  
  178.            
  179.  
  180. static int num_width(number)
  181. {
  182.      char buf[BUFSIZ];
  183.  
  184.      return(strlen(sprintf(buf, "%d", number)));
  185. }
  186.