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 |
3503
|
35 #include <iostream> |
1810
|
36 #include <string> |
|
37 |
2926
|
38 #include "cmd-edit.h" |
2937
|
39 #include "lo-utils.h" |
1810
|
40 #include "str-vec.h" |
|
41 |
2493
|
42 // Create a string vector from a NULL terminated list of C strings. |
|
43 |
|
44 string_vector::string_vector (const char * const *s) |
3504
|
45 : Array<std::string> () |
2493
|
46 { |
|
47 int n = 0; |
|
48 |
3040
|
49 const char * const *t = s; |
|
50 |
|
51 while (*t++) |
2493
|
52 n++; |
|
53 |
|
54 resize (n); |
|
55 |
|
56 for (int i = 0; i < n; i++) |
|
57 elem (i) = s[i]; |
|
58 } |
|
59 |
|
60 // Create a string vector from up to N C strings. Assumes that N is |
|
61 // nonnegative. |
|
62 |
|
63 string_vector::string_vector (const char * const *s, int n) |
3504
|
64 : Array<std::string> (n) |
2493
|
65 { |
|
66 for (int i = 0; i < n; i++) |
|
67 elem (i) = s[i]; |
|
68 } |
|
69 |
4220
|
70 int |
|
71 string_vector::compare (const void *a_arg, const void *b_arg) |
|
72 { |
|
73 const std::string *a = (const std::string *) a_arg; |
|
74 const std::string *b = (const std::string *) b_arg; |
|
75 |
|
76 return a->compare (*b); |
|
77 } |
|
78 |
2941
|
79 string_vector& |
|
80 string_vector::uniq (void) |
|
81 { |
|
82 int len = length (); |
|
83 |
|
84 if (len > 0) |
|
85 { |
|
86 int k = 0; |
|
87 |
|
88 for (int i = 1; i < len; i++) |
|
89 if (elem(i) != elem(k)) |
|
90 if (++k != i) |
|
91 elem(k) = elem(i); |
|
92 |
|
93 if (len != ++k) |
|
94 resize (k); |
|
95 } |
|
96 |
|
97 return *this; |
|
98 } |
|
99 |
2937
|
100 char ** |
|
101 string_vector::c_str_vec (void) const |
|
102 { |
|
103 int len = length (); |
|
104 |
|
105 char **retval = new char * [len + 1]; |
|
106 |
|
107 retval [len] = 0; |
|
108 |
|
109 for (int i = 0; i < len; i++) |
|
110 retval[i] = strsave (elem(i).c_str ()); |
|
111 |
|
112 return retval; |
|
113 } |
|
114 |
|
115 void |
|
116 string_vector::delete_c_str_vec (const char * const *v) |
|
117 { |
|
118 while (*v) |
|
119 delete [] *v; |
|
120 |
|
121 delete [] v; |
|
122 } |
|
123 |
2940
|
124 // Format a list in neat columns. |
1810
|
125 |
3504
|
126 std::ostream& |
|
127 string_vector::list_in_columns (std::ostream& os) const |
1810
|
128 { |
|
129 // Compute the maximum name length. |
|
130 |
|
131 int max_name_length = 0; |
|
132 int total_names = length (); |
|
133 |
|
134 for (int i = 0; i < total_names; i++) |
|
135 { |
|
136 int name_length = elem (i).length (); |
|
137 if (name_length > max_name_length) |
|
138 max_name_length = name_length; |
|
139 } |
|
140 |
|
141 // Allow at least two spaces between names. |
|
142 |
|
143 max_name_length += 2; |
|
144 |
|
145 // Calculate the maximum number of columns that will fit. |
|
146 |
2926
|
147 int line_length = command_editor::terminal_cols (); |
1810
|
148 int cols = line_length / max_name_length; |
|
149 if (cols == 0) |
|
150 cols = 1; |
|
151 |
|
152 // Calculate the number of rows that will be in each column except |
|
153 // possibly for a short column on the right. |
|
154 |
|
155 int rows = total_names / cols + (total_names % cols != 0); |
|
156 |
|
157 // Recalculate columns based on rows. |
|
158 |
|
159 cols = total_names / rows + (total_names % rows != 0); |
|
160 |
|
161 int count; |
|
162 for (int row = 0; row < rows; row++) |
|
163 { |
|
164 count = row; |
|
165 int pos = 0; |
|
166 |
|
167 // Print the next row. |
|
168 |
|
169 while (1) |
|
170 { |
3504
|
171 std::string nm = elem (count); |
1810
|
172 |
|
173 os << nm; |
|
174 int name_length = nm.length (); |
|
175 |
|
176 count += rows; |
|
177 if (count >= total_names) |
|
178 break; |
|
179 |
|
180 int spaces_to_pad = max_name_length - name_length; |
|
181 for (int i = 0; i < spaces_to_pad; i++) |
|
182 os << " "; |
|
183 pos += max_name_length; |
|
184 } |
|
185 os << "\n"; |
|
186 } |
|
187 |
|
188 return os; |
|
189 } |
|
190 |
|
191 /* |
|
192 ;;; Local Variables: *** |
|
193 ;;; mode: C++ *** |
|
194 ;;; End: *** |
|
195 */ |