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