home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21fs.zip / octave / octave-2.1.23 / liboctave / str-vec.cc < prev    next >
C/C++ Source or Header  |  2000-01-15  |  4KB  |  190 lines

  1. /*
  2.  
  3. Copyright (C) 1996, 1997 John W. Eaton
  4.  
  5. This file is part of Octave.
  6.  
  7. Octave is free software; you can redistribute it and/or modify it
  8. under the terms of the GNU General Public License as published by the
  9. Free Software Foundation; either version 2, or (at your option) any
  10. later version.
  11.  
  12. Octave is distributed in the hope that it will be useful, but WITHOUT
  13. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15. for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with Octave; see the file COPYING.  If not, write to the Free
  19. Software Foundation, 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  20.  
  21. */
  22.  
  23. /*
  24.  
  25. The function string_vector::list_in_columns was adapted from a similar
  26. function distributed in the GNU file utilities, copyright (C) 85, 88,
  27. 90, 91, 95, 1996 Free Software Foundation, Inc.
  28.  
  29. */
  30.  
  31. #ifdef HAVE_CONFIG_H
  32. #include <config.h>
  33. #endif
  34.  
  35. #include <string>
  36.  
  37. #include <iostream.h>
  38.  
  39. #include "cmd-edit.h"
  40. #include "lo-utils.h"
  41. #include "str-vec.h"
  42.  
  43. // Create a string vector from a NULL terminated list of C strings.
  44.  
  45. string_vector::string_vector (const char * const *s)
  46.   : Array<string> ()
  47. {
  48.   int n = 0;
  49.  
  50.   const char * const *t = s;
  51.  
  52.   while (*t++)
  53.     n++;
  54.  
  55.   resize (n);
  56.  
  57.   for (int i = 0; i < n; i++)
  58.     elem (i) = s[i];
  59. }
  60.  
  61. // Create a string vector from up to N C strings.  Assumes that N is
  62. // nonnegative.
  63.  
  64. string_vector::string_vector (const char * const *s, int n)
  65.   : Array<string> (n)
  66. {
  67.   for (int i = 0; i < n; i++)
  68.     elem (i) = s[i];
  69. }
  70.  
  71. string_vector&
  72. string_vector::uniq (void)
  73. {
  74.   int len = length ();
  75.  
  76.   if (len > 0)
  77.     {
  78.       int k = 0;
  79.  
  80.       for (int i = 1; i < len; i++)
  81.     if (elem(i) != elem(k))
  82.       if (++k != i)
  83.         elem(k) = elem(i);
  84.  
  85.       if (len != ++k)
  86.     resize (k);
  87.     }
  88.  
  89.   return *this;
  90. }
  91.  
  92. char **
  93. string_vector::c_str_vec (void) const
  94. {
  95.   int len = length ();
  96.  
  97.   char **retval = new char * [len + 1];
  98.  
  99.   retval [len] = 0;
  100.  
  101.   for (int i = 0; i < len; i++)
  102.     retval[i] = strsave (elem(i).c_str ());
  103.  
  104.   return retval;
  105. }
  106.  
  107. void
  108. string_vector::delete_c_str_vec (const char * const *v)
  109. {
  110.   int i = 0;
  111.  
  112.   while (v[i])
  113.     delete [] v[i++];
  114.  
  115.   delete [] v;
  116. }
  117.  
  118. // Format a list in neat columns.
  119.  
  120. ostream&
  121. string_vector::list_in_columns (ostream& os) const
  122. {
  123.   // Compute the maximum name length.
  124.  
  125.   int max_name_length = 0;
  126.   int total_names = length ();
  127.  
  128.   for (int i = 0; i < total_names; i++)
  129.     {
  130.       int name_length = elem (i).length ();
  131.       if (name_length > max_name_length)
  132.     max_name_length = name_length;
  133.     }
  134.  
  135.   // Allow at least two spaces between names.
  136.  
  137.   max_name_length += 2;
  138.  
  139.   // Calculate the maximum number of columns that will fit.
  140.  
  141.   int line_length = command_editor::terminal_cols ();
  142.   int cols = line_length / max_name_length;
  143.   if (cols == 0)
  144.     cols = 1;
  145.  
  146.   // Calculate the number of rows that will be in each column except
  147.   // possibly  for a short column on the right.
  148.  
  149.   int rows = total_names / cols + (total_names % cols != 0);
  150.  
  151.   // Recalculate columns based on rows.
  152.  
  153.   cols = total_names / rows + (total_names % rows != 0);
  154.  
  155.   int count;
  156.   for (int row = 0; row < rows; row++)
  157.     {
  158.       count = row;
  159.       int pos = 0;
  160.  
  161.       // Print the next row.
  162.  
  163.       while (1)
  164.     {
  165.       string nm = elem (count);
  166.  
  167.       os << nm;
  168.       int name_length = nm.length ();
  169.  
  170.       count += rows;
  171.       if (count >= total_names)
  172.         break;
  173.  
  174.       int spaces_to_pad = max_name_length - name_length;
  175.       for (int i = 0; i < spaces_to_pad; i++)
  176.         os << " ";
  177.       pos += max_name_length;
  178.     }
  179.       os << "\n";
  180.     }
  181.  
  182.   return os;
  183. }
  184.  
  185. /*
  186. ;;; Local Variables: ***
  187. ;;; mode: C++ ***
  188. ;;; End: ***
  189. */
  190.