home *** CD-ROM | disk | FTP | other *** search
/ The UNIX CD Bookshelf / OREILLY_TUCB_UNIX_CD.iso / upt / examples / SOURCES / DELETE / PART01.Z / PART01 / col.c next >
Encoding:
C/C++ Source or Header  |  1998-07-24  |  5.0 KB  |  188 lines

  1. /*
  2.  * $Source: /afs/athena.mit.edu/user/j/jik/delete/src/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/delete/src/RCS/col.c,v 1.7 90/06/06 19:05:56 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. #ifdef SYSV
  25. #include <string.h>
  26. #define index strchr
  27. #define rindex strrchr
  28. #else
  29. #include <strings.h>
  30. #endif /* SYSV */
  31. #include "errors.h"
  32. #include "delete_errs.h"
  33. #include "col.h"
  34. #include "mit-copyright.h"
  35.  
  36.  
  37. static int calc_string_width(), calc_widths(), num_width();
  38. static void trim_strings();
  39.  
  40. int column_array(strings, num_to_print, screen_width, column_width,
  41.          number_of_columns, margin, spread_flag, 
  42.          number_flag, var_col_flag, outfile)
  43. char **strings;
  44. FILE *outfile;
  45. {
  46.      char buf[BUFSIZ];
  47.      int updown, leftright, height;
  48.      int string_width;
  49.      int numwidth;
  50.  
  51.      numwidth = num_width(num_to_print);
  52.      if (! var_col_flag) {
  53.       string_width = calc_string_width(column_width, margin, number_flag,
  54.                        num_to_print);
  55.       if (string_width <= 0) {
  56.            set_error(COL_COLUMNS_TOO_THIN);
  57.            error("calc_string_width");
  58.            return error_code;
  59.       }
  60.       trim_strings(strings, num_to_print, string_width);
  61.      } else if (calc_widths(strings, &screen_width, &column_width,
  62.                 &number_of_columns, num_to_print, &margin,
  63.                 spread_flag, number_flag)) {
  64.       error("calc_widths");
  65.       return error_code;
  66.      }
  67.      height = num_to_print / number_of_columns;
  68.      if (num_to_print % number_of_columns)
  69.       height++;
  70.      
  71.      if (number_flag) for (updown = 0; updown < height; updown++) {
  72.       for (leftright = updown; leftright < num_to_print; ) {
  73.            (void) sprintf(buf, "%*d. %s", numwidth, leftright+1,
  74.                   strings[leftright]);
  75.            if ((leftright += height) >= num_to_print)
  76.             fprintf(outfile, "%s", buf );
  77.            else
  78.             fprintf(outfile, "%*s", -column_width, buf);
  79.       }
  80.       fprintf(outfile, "\n");
  81.      } else for (updown = 0; updown < height; updown++) {
  82.       for (leftright = updown; leftright < num_to_print; ) {
  83.            (void) sprintf(buf, "%s", strings[leftright]);
  84.            if ((leftright += height) >= num_to_print)
  85.             fprintf(outfile, "%s", buf );
  86.            else
  87.             fprintf(outfile, "%*s", -column_width, buf);
  88.       }
  89.       fprintf(outfile, "\n");
  90.      }
  91.      return 0;
  92. }
  93.  
  94. static int calc_string_width(column_width, margin, number_flag, max_number)
  95. {
  96.      int string_width;
  97.      
  98.      string_width = column_width - margin;
  99.      if (number_flag)
  100.       string_width = string_width - num_width(max_number) - strlen(". ");
  101.      return string_width;
  102. }
  103.  
  104.  
  105. static void trim_strings(strings, number, width)
  106. char **strings;
  107. {
  108.      int loop;
  109.      
  110.      for (loop = 0; loop < number; loop++)
  111.       if (strlen(strings[loop]) > width)
  112.            strings[loop][width] = '\0';
  113. }
  114.  
  115.  
  116. static int calc_widths(strings, screen_width, column_width, number_of_columns,
  117.                num_to_print, margin, spread_flag, number_flag)
  118. int *screen_width, *column_width, *number_of_columns, *margin;
  119. char **strings;
  120. {
  121.      int loop;
  122.      int maxlen, templen;
  123.      int spread;
  124.      
  125.      maxlen = templen = 0;
  126.      for (loop = 0; loop < num_to_print; loop++)
  127.       if (maxlen < (templen = strlen(strings[loop])))
  128.            maxlen = templen;
  129.  
  130.      *column_width = maxlen;
  131.      
  132.      if (number_flag)
  133.       *column_width = *column_width + num_width(num_to_print) +
  134.            strlen(". ");
  135.  
  136.      if (! spread_flag) {
  137.       *column_width += *margin;
  138.       if (! *number_of_columns) {
  139.            *number_of_columns = *screen_width / *column_width;
  140.            if (! *number_of_columns) {
  141.             (*number_of_columns)++;
  142.             *column_width -= *margin;
  143.             *margin = 0;
  144.             *screen_width = *column_width;
  145.            }
  146.       }
  147.       else
  148.            *screen_width = *number_of_columns * *column_width;
  149.      } else {
  150.       if (! *number_of_columns) {
  151.            *number_of_columns = *screen_width / (*column_width + *margin);
  152.            if (! *number_of_columns) {
  153.             (*number_of_columns)++;
  154.             *screen_width = *column_width;
  155.             *margin = 0;
  156.            }
  157.            spread = (*screen_width - *number_of_columns * *column_width)
  158.             / *number_of_columns;
  159.            *column_width += spread;
  160.       }
  161.       else {
  162.            if (*number_of_columns * (*column_width + *margin) >
  163.            *screen_width) {
  164.             *column_width += *margin;
  165.             *screen_width = *column_width;
  166.            } else {
  167.             spread = (*screen_width - (*number_of_columns *
  168.                            *column_width)) /
  169.                             *number_of_columns;
  170.             *column_width += spread;
  171.            }
  172.       }
  173.      }
  174.      return 0;
  175. }
  176.  
  177.  
  178.            
  179.  
  180. static int num_width(number)
  181. int number;
  182. {
  183.      char buf[BUFSIZ];
  184.  
  185.      (void) sprintf(buf, "%d", number);
  186.      return strlen(buf);
  187. }
  188.