1810
|
1 /* |
|
2 |
7017
|
3 Copyright (C) 1996, 1997, 2000, 2002, 2003, 2005, 2006, 2007 |
|
4 John W. Eaton |
1810
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
7016
|
10 Free Software Foundation; either version 3 of the License, or (at your |
|
11 option) any later version. |
1810
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
7016
|
19 along with Octave; see the file COPYING. If not, see |
|
20 <http://www.gnu.org/licenses/>. |
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 |
5880
|
43 string_vector::string_vector (const std::list<std::string>& lst) |
5894
|
44 : Array<std::string> () |
5880
|
45 { |
|
46 size_t n = lst.size (); |
|
47 |
|
48 resize (n); |
|
49 |
|
50 octave_idx_type i = 0; |
|
51 |
|
52 for (std::list<std::string>::const_iterator p = lst.begin (); |
|
53 p != lst.end (); |
|
54 p++) |
|
55 elem(i++) = *p; |
|
56 } |
|
57 |
2493
|
58 // Create a string vector from a NULL terminated list of C strings. |
|
59 |
|
60 string_vector::string_vector (const char * const *s) |
3504
|
61 : Array<std::string> () |
2493
|
62 { |
5275
|
63 octave_idx_type n = 0; |
2493
|
64 |
3040
|
65 const char * const *t = s; |
|
66 |
|
67 while (*t++) |
2493
|
68 n++; |
|
69 |
|
70 resize (n); |
|
71 |
5275
|
72 for (octave_idx_type i = 0; i < n; i++) |
2493
|
73 elem (i) = s[i]; |
|
74 } |
|
75 |
|
76 // Create a string vector from up to N C strings. Assumes that N is |
|
77 // nonnegative. |
|
78 |
5275
|
79 string_vector::string_vector (const char * const *s, octave_idx_type n) |
3504
|
80 : Array<std::string> (n) |
2493
|
81 { |
5275
|
82 for (octave_idx_type i = 0; i < n; i++) |
2493
|
83 elem (i) = s[i]; |
|
84 } |
|
85 |
4220
|
86 int |
|
87 string_vector::compare (const void *a_arg, const void *b_arg) |
|
88 { |
5760
|
89 const std::string *a = static_cast<const std::string *> (a_arg); |
|
90 const std::string *b = static_cast<const std::string *> (b_arg); |
4220
|
91 |
|
92 return a->compare (*b); |
|
93 } |
|
94 |
2941
|
95 string_vector& |
|
96 string_vector::uniq (void) |
|
97 { |
5275
|
98 octave_idx_type len = length (); |
2941
|
99 |
|
100 if (len > 0) |
|
101 { |
5275
|
102 octave_idx_type k = 0; |
2941
|
103 |
5275
|
104 for (octave_idx_type i = 1; i < len; i++) |
2941
|
105 if (elem(i) != elem(k)) |
|
106 if (++k != i) |
|
107 elem(k) = elem(i); |
|
108 |
|
109 if (len != ++k) |
|
110 resize (k); |
|
111 } |
|
112 |
|
113 return *this; |
|
114 } |
|
115 |
4392
|
116 string_vector& |
|
117 string_vector::append (const std::string& s) |
|
118 { |
5275
|
119 octave_idx_type len = length (); |
4392
|
120 |
|
121 resize (len + 1); |
|
122 |
|
123 elem(len) = s; |
|
124 |
|
125 return *this; |
|
126 } |
|
127 |
|
128 string_vector& |
|
129 string_vector::append (const string_vector& sv) |
|
130 { |
5275
|
131 octave_idx_type len = length (); |
|
132 octave_idx_type sv_len = sv.length (); |
|
133 octave_idx_type new_len = len + sv_len; |
4392
|
134 |
|
135 resize (new_len); |
|
136 |
5275
|
137 for (octave_idx_type i = 0; i < sv_len; i++) |
4392
|
138 elem(len + i) = sv[i]; |
|
139 |
|
140 return *this; |
|
141 } |
|
142 |
2937
|
143 char ** |
|
144 string_vector::c_str_vec (void) const |
|
145 { |
5275
|
146 octave_idx_type len = length (); |
2937
|
147 |
|
148 char **retval = new char * [len + 1]; |
|
149 |
|
150 retval [len] = 0; |
|
151 |
5275
|
152 for (octave_idx_type i = 0; i < len; i++) |
2937
|
153 retval[i] = strsave (elem(i).c_str ()); |
|
154 |
|
155 return retval; |
|
156 } |
|
157 |
|
158 void |
|
159 string_vector::delete_c_str_vec (const char * const *v) |
|
160 { |
5304
|
161 const char * const *p = v; |
|
162 |
|
163 while (*p) |
|
164 delete [] *p++; |
2937
|
165 |
|
166 delete [] v; |
|
167 } |
|
168 |
2940
|
169 // Format a list in neat columns. |
1810
|
170 |
3504
|
171 std::ostream& |
5690
|
172 string_vector::list_in_columns (std::ostream& os, int width) const |
1810
|
173 { |
|
174 // Compute the maximum name length. |
|
175 |
5275
|
176 octave_idx_type max_name_length = 0; |
|
177 octave_idx_type total_names = length (); |
1810
|
178 |
5275
|
179 for (octave_idx_type i = 0; i < total_names; i++) |
1810
|
180 { |
5275
|
181 octave_idx_type name_length = elem (i).length (); |
1810
|
182 if (name_length > max_name_length) |
|
183 max_name_length = name_length; |
|
184 } |
|
185 |
|
186 // Allow at least two spaces between names. |
|
187 |
|
188 max_name_length += 2; |
|
189 |
|
190 // Calculate the maximum number of columns that will fit. |
|
191 |
5690
|
192 octave_idx_type line_length |
|
193 = (width <= 0) ? command_editor::terminal_cols () : width; |
|
194 |
5275
|
195 octave_idx_type nc = line_length / max_name_length; |
4587
|
196 if (nc == 0) |
|
197 nc = 1; |
1810
|
198 |
|
199 // Calculate the number of rows that will be in each column except |
|
200 // possibly for a short column on the right. |
|
201 |
5275
|
202 octave_idx_type nr = total_names / nc + (total_names % nc != 0); |
1810
|
203 |
|
204 // Recalculate columns based on rows. |
|
205 |
4587
|
206 nc = total_names / nr + (total_names % nr != 0); |
1810
|
207 |
5275
|
208 octave_idx_type count; |
|
209 for (octave_idx_type row = 0; row < nr; row++) |
1810
|
210 { |
|
211 count = row; |
5275
|
212 octave_idx_type pos = 0; |
1810
|
213 |
|
214 // Print the next row. |
|
215 |
|
216 while (1) |
|
217 { |
3504
|
218 std::string nm = elem (count); |
1810
|
219 |
|
220 os << nm; |
5275
|
221 octave_idx_type name_length = nm.length (); |
1810
|
222 |
4587
|
223 count += nr; |
1810
|
224 if (count >= total_names) |
|
225 break; |
|
226 |
5275
|
227 octave_idx_type spaces_to_pad = max_name_length - name_length; |
|
228 for (octave_idx_type i = 0; i < spaces_to_pad; i++) |
1810
|
229 os << " "; |
|
230 pos += max_name_length; |
|
231 } |
|
232 os << "\n"; |
|
233 } |
|
234 |
|
235 return os; |
|
236 } |
|
237 |
|
238 /* |
|
239 ;;; Local Variables: *** |
|
240 ;;; mode: C++ *** |
|
241 ;;; End: *** |
|
242 */ |