1810
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
1810
|
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 |
2940
|
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 |
1810
|
31 #ifdef HAVE_CONFIG_H |
|
32 #include <config.h> |
|
33 #endif |
|
34 |
|
35 #include <string> |
|
36 |
|
37 #include <iostream.h> |
|
38 |
2926
|
39 #include "cmd-edit.h" |
2937
|
40 #include "lo-utils.h" |
1810
|
41 #include "str-vec.h" |
|
42 |
2493
|
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 while (*s++) |
|
51 n++; |
|
52 |
|
53 resize (n); |
|
54 |
|
55 for (int i = 0; i < n; i++) |
|
56 elem (i) = s[i]; |
|
57 } |
|
58 |
|
59 // Create a string vector from up to N C strings. Assumes that N is |
|
60 // nonnegative. |
|
61 |
|
62 string_vector::string_vector (const char * const *s, int n) |
|
63 : Array<string> (n) |
|
64 { |
|
65 for (int i = 0; i < n; i++) |
|
66 elem (i) = s[i]; |
|
67 } |
|
68 |
2937
|
69 char ** |
|
70 string_vector::c_str_vec (void) const |
|
71 { |
|
72 int len = length (); |
|
73 |
|
74 char **retval = new char * [len + 1]; |
|
75 |
|
76 retval [len] = 0; |
|
77 |
|
78 for (int i = 0; i < len; i++) |
|
79 retval[i] = strsave (elem(i).c_str ()); |
|
80 |
|
81 return retval; |
|
82 } |
|
83 |
|
84 void |
|
85 string_vector::delete_c_str_vec (const char * const *v) |
|
86 { |
|
87 while (*v) |
|
88 delete [] *v; |
|
89 |
|
90 delete [] v; |
|
91 } |
|
92 |
2940
|
93 // Format a list in neat columns. |
1810
|
94 |
|
95 ostream& |
|
96 string_vector::list_in_columns (ostream& os) const |
|
97 { |
|
98 // Compute the maximum name length. |
|
99 |
|
100 int max_name_length = 0; |
|
101 int total_names = length (); |
|
102 |
|
103 for (int i = 0; i < total_names; i++) |
|
104 { |
|
105 int name_length = elem (i).length (); |
|
106 if (name_length > max_name_length) |
|
107 max_name_length = name_length; |
|
108 } |
|
109 |
|
110 // Allow at least two spaces between names. |
|
111 |
|
112 max_name_length += 2; |
|
113 |
|
114 // Calculate the maximum number of columns that will fit. |
|
115 |
2926
|
116 int line_length = command_editor::terminal_cols (); |
1810
|
117 int cols = line_length / max_name_length; |
|
118 if (cols == 0) |
|
119 cols = 1; |
|
120 |
|
121 // Calculate the number of rows that will be in each column except |
|
122 // possibly for a short column on the right. |
|
123 |
|
124 int rows = total_names / cols + (total_names % cols != 0); |
|
125 |
|
126 // Recalculate columns based on rows. |
|
127 |
|
128 cols = total_names / rows + (total_names % rows != 0); |
|
129 |
|
130 int count; |
|
131 for (int row = 0; row < rows; row++) |
|
132 { |
|
133 count = row; |
|
134 int pos = 0; |
|
135 |
|
136 // Print the next row. |
|
137 |
|
138 while (1) |
|
139 { |
|
140 string nm = elem (count); |
|
141 |
|
142 os << nm; |
|
143 int name_length = nm.length (); |
|
144 |
|
145 count += rows; |
|
146 if (count >= total_names) |
|
147 break; |
|
148 |
|
149 int spaces_to_pad = max_name_length - name_length; |
|
150 for (int i = 0; i < spaces_to_pad; i++) |
|
151 os << " "; |
|
152 pos += max_name_length; |
|
153 } |
|
154 os << "\n"; |
|
155 } |
|
156 |
|
157 return os; |
|
158 } |
|
159 |
|
160 /* |
|
161 ;;; Local Variables: *** |
|
162 ;;; mode: C++ *** |
|
163 ;;; End: *** |
|
164 */ |