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