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 |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
1810
|
21 |
|
22 */ |
|
23 |
2940
|
24 /* |
|
25 |
|
26 The function string_vector::list_in_columns was adapted from a similar |
|
27 function distributed in the GNU file utilities, copyright (C) 85, 88, |
|
28 90, 91, 95, 1996 Free Software Foundation, Inc. |
|
29 |
|
30 */ |
|
31 |
1810
|
32 #ifdef HAVE_CONFIG_H |
|
33 #include <config.h> |
|
34 #endif |
|
35 |
3503
|
36 #include <iostream> |
1810
|
37 #include <string> |
|
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) |
3504
|
46 : Array<std::string> () |
2493
|
47 { |
5275
|
48 octave_idx_type n = 0; |
2493
|
49 |
3040
|
50 const char * const *t = s; |
|
51 |
|
52 while (*t++) |
2493
|
53 n++; |
|
54 |
|
55 resize (n); |
|
56 |
5275
|
57 for (octave_idx_type i = 0; i < n; i++) |
2493
|
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 |
5275
|
64 string_vector::string_vector (const char * const *s, octave_idx_type n) |
3504
|
65 : Array<std::string> (n) |
2493
|
66 { |
5275
|
67 for (octave_idx_type i = 0; i < n; i++) |
2493
|
68 elem (i) = s[i]; |
|
69 } |
|
70 |
4220
|
71 int |
|
72 string_vector::compare (const void *a_arg, const void *b_arg) |
|
73 { |
|
74 const std::string *a = (const std::string *) a_arg; |
|
75 const std::string *b = (const std::string *) b_arg; |
|
76 |
|
77 return a->compare (*b); |
|
78 } |
|
79 |
2941
|
80 string_vector& |
|
81 string_vector::uniq (void) |
|
82 { |
5275
|
83 octave_idx_type len = length (); |
2941
|
84 |
|
85 if (len > 0) |
|
86 { |
5275
|
87 octave_idx_type k = 0; |
2941
|
88 |
5275
|
89 for (octave_idx_type i = 1; i < len; i++) |
2941
|
90 if (elem(i) != elem(k)) |
|
91 if (++k != i) |
|
92 elem(k) = elem(i); |
|
93 |
|
94 if (len != ++k) |
|
95 resize (k); |
|
96 } |
|
97 |
|
98 return *this; |
|
99 } |
|
100 |
4392
|
101 string_vector& |
|
102 string_vector::append (const std::string& s) |
|
103 { |
5275
|
104 octave_idx_type len = length (); |
4392
|
105 |
|
106 resize (len + 1); |
|
107 |
|
108 elem(len) = s; |
|
109 |
|
110 return *this; |
|
111 } |
|
112 |
|
113 string_vector& |
|
114 string_vector::append (const string_vector& sv) |
|
115 { |
5275
|
116 octave_idx_type len = length (); |
|
117 octave_idx_type sv_len = sv.length (); |
|
118 octave_idx_type new_len = len + sv_len; |
4392
|
119 |
|
120 resize (new_len); |
|
121 |
5275
|
122 for (octave_idx_type i = 0; i < sv_len; i++) |
4392
|
123 elem(len + i) = sv[i]; |
|
124 |
|
125 return *this; |
|
126 } |
|
127 |
2937
|
128 char ** |
|
129 string_vector::c_str_vec (void) const |
|
130 { |
5275
|
131 octave_idx_type len = length (); |
2937
|
132 |
|
133 char **retval = new char * [len + 1]; |
|
134 |
|
135 retval [len] = 0; |
|
136 |
5275
|
137 for (octave_idx_type i = 0; i < len; i++) |
2937
|
138 retval[i] = strsave (elem(i).c_str ()); |
|
139 |
|
140 return retval; |
|
141 } |
|
142 |
|
143 void |
|
144 string_vector::delete_c_str_vec (const char * const *v) |
|
145 { |
5304
|
146 const char * const *p = v; |
|
147 |
|
148 while (*p) |
|
149 delete [] *p++; |
2937
|
150 |
|
151 delete [] v; |
|
152 } |
|
153 |
2940
|
154 // Format a list in neat columns. |
1810
|
155 |
3504
|
156 std::ostream& |
|
157 string_vector::list_in_columns (std::ostream& os) const |
1810
|
158 { |
|
159 // Compute the maximum name length. |
|
160 |
5275
|
161 octave_idx_type max_name_length = 0; |
|
162 octave_idx_type total_names = length (); |
1810
|
163 |
5275
|
164 for (octave_idx_type i = 0; i < total_names; i++) |
1810
|
165 { |
5275
|
166 octave_idx_type name_length = elem (i).length (); |
1810
|
167 if (name_length > max_name_length) |
|
168 max_name_length = name_length; |
|
169 } |
|
170 |
|
171 // Allow at least two spaces between names. |
|
172 |
|
173 max_name_length += 2; |
|
174 |
|
175 // Calculate the maximum number of columns that will fit. |
|
176 |
5275
|
177 octave_idx_type line_length = command_editor::terminal_cols (); |
|
178 octave_idx_type nc = line_length / max_name_length; |
4587
|
179 if (nc == 0) |
|
180 nc = 1; |
1810
|
181 |
|
182 // Calculate the number of rows that will be in each column except |
|
183 // possibly for a short column on the right. |
|
184 |
5275
|
185 octave_idx_type nr = total_names / nc + (total_names % nc != 0); |
1810
|
186 |
|
187 // Recalculate columns based on rows. |
|
188 |
4587
|
189 nc = total_names / nr + (total_names % nr != 0); |
1810
|
190 |
5275
|
191 octave_idx_type count; |
|
192 for (octave_idx_type row = 0; row < nr; row++) |
1810
|
193 { |
|
194 count = row; |
5275
|
195 octave_idx_type pos = 0; |
1810
|
196 |
|
197 // Print the next row. |
|
198 |
|
199 while (1) |
|
200 { |
3504
|
201 std::string nm = elem (count); |
1810
|
202 |
|
203 os << nm; |
5275
|
204 octave_idx_type name_length = nm.length (); |
1810
|
205 |
4587
|
206 count += nr; |
1810
|
207 if (count >= total_names) |
|
208 break; |
|
209 |
5275
|
210 octave_idx_type spaces_to_pad = max_name_length - name_length; |
|
211 for (octave_idx_type i = 0; i < spaces_to_pad; i++) |
1810
|
212 os << " "; |
|
213 pos += max_name_length; |
|
214 } |
|
215 os << "\n"; |
|
216 } |
|
217 |
|
218 return os; |
|
219 } |
|
220 |
|
221 /* |
|
222 ;;; Local Variables: *** |
|
223 ;;; mode: C++ *** |
|
224 ;;; End: *** |
|
225 */ |