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