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 |
2941
|
70 string_vector& |
|
71 string_vector::uniq (void) |
|
72 { |
|
73 int len = length (); |
|
74 |
|
75 if (len > 0) |
|
76 { |
|
77 int k = 0; |
|
78 |
|
79 for (int i = 1; i < len; i++) |
|
80 if (elem(i) != elem(k)) |
|
81 if (++k != i) |
|
82 elem(k) = elem(i); |
|
83 |
|
84 if (len != ++k) |
|
85 resize (k); |
|
86 } |
|
87 |
|
88 return *this; |
|
89 } |
|
90 |
2937
|
91 char ** |
|
92 string_vector::c_str_vec (void) const |
|
93 { |
|
94 int len = length (); |
|
95 |
|
96 char **retval = new char * [len + 1]; |
|
97 |
|
98 retval [len] = 0; |
|
99 |
|
100 for (int i = 0; i < len; i++) |
|
101 retval[i] = strsave (elem(i).c_str ()); |
|
102 |
|
103 return retval; |
|
104 } |
|
105 |
|
106 void |
|
107 string_vector::delete_c_str_vec (const char * const *v) |
|
108 { |
|
109 while (*v) |
|
110 delete [] *v; |
|
111 |
|
112 delete [] v; |
|
113 } |
|
114 |
2940
|
115 // Format a list in neat columns. |
1810
|
116 |
3504
|
117 std::ostream& |
|
118 string_vector::list_in_columns (std::ostream& os) const |
1810
|
119 { |
|
120 // Compute the maximum name length. |
|
121 |
|
122 int max_name_length = 0; |
|
123 int total_names = length (); |
|
124 |
|
125 for (int i = 0; i < total_names; i++) |
|
126 { |
|
127 int name_length = elem (i).length (); |
|
128 if (name_length > max_name_length) |
|
129 max_name_length = name_length; |
|
130 } |
|
131 |
|
132 // Allow at least two spaces between names. |
|
133 |
|
134 max_name_length += 2; |
|
135 |
|
136 // Calculate the maximum number of columns that will fit. |
|
137 |
2926
|
138 int line_length = command_editor::terminal_cols (); |
1810
|
139 int cols = line_length / max_name_length; |
|
140 if (cols == 0) |
|
141 cols = 1; |
|
142 |
|
143 // Calculate the number of rows that will be in each column except |
|
144 // possibly for a short column on the right. |
|
145 |
|
146 int rows = total_names / cols + (total_names % cols != 0); |
|
147 |
|
148 // Recalculate columns based on rows. |
|
149 |
|
150 cols = total_names / rows + (total_names % rows != 0); |
|
151 |
|
152 int count; |
|
153 for (int row = 0; row < rows; row++) |
|
154 { |
|
155 count = row; |
|
156 int pos = 0; |
|
157 |
|
158 // Print the next row. |
|
159 |
|
160 while (1) |
|
161 { |
3504
|
162 std::string nm = elem (count); |
1810
|
163 |
|
164 os << nm; |
|
165 int name_length = nm.length (); |
|
166 |
|
167 count += rows; |
|
168 if (count >= total_names) |
|
169 break; |
|
170 |
|
171 int spaces_to_pad = max_name_length - name_length; |
|
172 for (int i = 0; i < spaces_to_pad; i++) |
|
173 os << " "; |
|
174 pos += max_name_length; |
|
175 } |
|
176 os << "\n"; |
|
177 } |
|
178 |
|
179 return os; |
|
180 } |
|
181 |
|
182 /* |
|
183 ;;; Local Variables: *** |
|
184 ;;; mode: C++ *** |
|
185 ;;; End: *** |
|
186 */ |