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 |
2941
|
86 string_vector& |
|
87 string_vector::uniq (void) |
|
88 { |
5275
|
89 octave_idx_type len = length (); |
2941
|
90 |
|
91 if (len > 0) |
|
92 { |
5275
|
93 octave_idx_type k = 0; |
2941
|
94 |
5275
|
95 for (octave_idx_type i = 1; i < len; i++) |
2941
|
96 if (elem(i) != elem(k)) |
|
97 if (++k != i) |
|
98 elem(k) = elem(i); |
|
99 |
|
100 if (len != ++k) |
|
101 resize (k); |
|
102 } |
|
103 |
|
104 return *this; |
|
105 } |
|
106 |
4392
|
107 string_vector& |
|
108 string_vector::append (const std::string& s) |
|
109 { |
5275
|
110 octave_idx_type len = length (); |
4392
|
111 |
|
112 resize (len + 1); |
|
113 |
|
114 elem(len) = s; |
|
115 |
|
116 return *this; |
|
117 } |
|
118 |
|
119 string_vector& |
|
120 string_vector::append (const string_vector& sv) |
|
121 { |
5275
|
122 octave_idx_type len = length (); |
|
123 octave_idx_type sv_len = sv.length (); |
|
124 octave_idx_type new_len = len + sv_len; |
4392
|
125 |
|
126 resize (new_len); |
|
127 |
5275
|
128 for (octave_idx_type i = 0; i < sv_len; i++) |
4392
|
129 elem(len + i) = sv[i]; |
|
130 |
|
131 return *this; |
|
132 } |
|
133 |
2937
|
134 char ** |
|
135 string_vector::c_str_vec (void) const |
|
136 { |
5275
|
137 octave_idx_type len = length (); |
2937
|
138 |
|
139 char **retval = new char * [len + 1]; |
|
140 |
|
141 retval [len] = 0; |
|
142 |
5275
|
143 for (octave_idx_type i = 0; i < len; i++) |
2937
|
144 retval[i] = strsave (elem(i).c_str ()); |
|
145 |
|
146 return retval; |
|
147 } |
|
148 |
|
149 void |
|
150 string_vector::delete_c_str_vec (const char * const *v) |
|
151 { |
5304
|
152 const char * const *p = v; |
|
153 |
|
154 while (*p) |
|
155 delete [] *p++; |
2937
|
156 |
|
157 delete [] v; |
|
158 } |
|
159 |
2940
|
160 // Format a list in neat columns. |
1810
|
161 |
3504
|
162 std::ostream& |
5690
|
163 string_vector::list_in_columns (std::ostream& os, int width) const |
1810
|
164 { |
|
165 // Compute the maximum name length. |
|
166 |
5275
|
167 octave_idx_type max_name_length = 0; |
|
168 octave_idx_type total_names = length (); |
1810
|
169 |
5275
|
170 for (octave_idx_type i = 0; i < total_names; i++) |
1810
|
171 { |
5275
|
172 octave_idx_type name_length = elem (i).length (); |
1810
|
173 if (name_length > max_name_length) |
|
174 max_name_length = name_length; |
|
175 } |
|
176 |
|
177 // Allow at least two spaces between names. |
|
178 |
|
179 max_name_length += 2; |
|
180 |
|
181 // Calculate the maximum number of columns that will fit. |
|
182 |
5690
|
183 octave_idx_type line_length |
|
184 = (width <= 0) ? command_editor::terminal_cols () : width; |
|
185 |
5275
|
186 octave_idx_type nc = line_length / max_name_length; |
4587
|
187 if (nc == 0) |
|
188 nc = 1; |
1810
|
189 |
|
190 // Calculate the number of rows that will be in each column except |
|
191 // possibly for a short column on the right. |
|
192 |
5275
|
193 octave_idx_type nr = total_names / nc + (total_names % nc != 0); |
1810
|
194 |
|
195 // Recalculate columns based on rows. |
|
196 |
4587
|
197 nc = total_names / nr + (total_names % nr != 0); |
1810
|
198 |
5275
|
199 octave_idx_type count; |
|
200 for (octave_idx_type row = 0; row < nr; row++) |
1810
|
201 { |
|
202 count = row; |
5275
|
203 octave_idx_type pos = 0; |
1810
|
204 |
|
205 // Print the next row. |
|
206 |
|
207 while (1) |
|
208 { |
3504
|
209 std::string nm = elem (count); |
1810
|
210 |
|
211 os << nm; |
5275
|
212 octave_idx_type name_length = nm.length (); |
1810
|
213 |
4587
|
214 count += nr; |
1810
|
215 if (count >= total_names) |
|
216 break; |
|
217 |
5275
|
218 octave_idx_type spaces_to_pad = max_name_length - name_length; |
|
219 for (octave_idx_type i = 0; i < spaces_to_pad; i++) |
1810
|
220 os << " "; |
|
221 pos += max_name_length; |
|
222 } |
|
223 os << "\n"; |
|
224 } |
|
225 |
|
226 return os; |
|
227 } |
|
228 |
|
229 /* |
|
230 ;;; Local Variables: *** |
|
231 ;;; mode: C++ *** |
|
232 ;;; End: *** |
|
233 */ |