Mercurial > hg > octave-nkf
annotate liboctave/str-vec.cc @ 8920:eb63fbe60fab
update copyright notices
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Sat, 07 Mar 2009 10:41:27 -0500 |
parents | e2b4c19c455c |
children | bdcfb756d721 2641c6febd46 |
rev | line source |
---|---|
1810 | 1 /* |
2 | |
8920 | 3 Copyright (C) 1996, 1997, 2000, 2002, 2003, 2005, 2006, 2007, 2009 |
7017 | 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& |
8678
e2b4c19c455c
redo changeset 4238f2600a17 with fixes to sorting
Jaroslav Hajek <highegg@gmail.com>
parents:
8671
diff
changeset
|
87 string_vector::sort (bool make_uniq) |
e2b4c19c455c
redo changeset 4238f2600a17 with fixes to sorting
Jaroslav Hajek <highegg@gmail.com>
parents:
8671
diff
changeset
|
88 { |
e2b4c19c455c
redo changeset 4238f2600a17 with fixes to sorting
Jaroslav Hajek <highegg@gmail.com>
parents:
8671
diff
changeset
|
89 // Don't use Array<std::string>::sort () to allow sorting in place. |
e2b4c19c455c
redo changeset 4238f2600a17 with fixes to sorting
Jaroslav Hajek <highegg@gmail.com>
parents:
8671
diff
changeset
|
90 octave_sort<std::string> lsort; |
e2b4c19c455c
redo changeset 4238f2600a17 with fixes to sorting
Jaroslav Hajek <highegg@gmail.com>
parents:
8671
diff
changeset
|
91 lsort.sort (Array<std::string>::fortran_vec (), length ()); |
e2b4c19c455c
redo changeset 4238f2600a17 with fixes to sorting
Jaroslav Hajek <highegg@gmail.com>
parents:
8671
diff
changeset
|
92 |
e2b4c19c455c
redo changeset 4238f2600a17 with fixes to sorting
Jaroslav Hajek <highegg@gmail.com>
parents:
8671
diff
changeset
|
93 if (make_uniq) |
e2b4c19c455c
redo changeset 4238f2600a17 with fixes to sorting
Jaroslav Hajek <highegg@gmail.com>
parents:
8671
diff
changeset
|
94 uniq (); |
e2b4c19c455c
redo changeset 4238f2600a17 with fixes to sorting
Jaroslav Hajek <highegg@gmail.com>
parents:
8671
diff
changeset
|
95 |
e2b4c19c455c
redo changeset 4238f2600a17 with fixes to sorting
Jaroslav Hajek <highegg@gmail.com>
parents:
8671
diff
changeset
|
96 return *this; |
e2b4c19c455c
redo changeset 4238f2600a17 with fixes to sorting
Jaroslav Hajek <highegg@gmail.com>
parents:
8671
diff
changeset
|
97 } |
e2b4c19c455c
redo changeset 4238f2600a17 with fixes to sorting
Jaroslav Hajek <highegg@gmail.com>
parents:
8671
diff
changeset
|
98 string_vector& |
2941 | 99 string_vector::uniq (void) |
100 { | |
5275 | 101 octave_idx_type len = length (); |
2941 | 102 |
103 if (len > 0) | |
104 { | |
5275 | 105 octave_idx_type k = 0; |
2941 | 106 |
5275 | 107 for (octave_idx_type i = 1; i < len; i++) |
2941 | 108 if (elem(i) != elem(k)) |
109 if (++k != i) | |
110 elem(k) = elem(i); | |
111 | |
112 if (len != ++k) | |
113 resize (k); | |
114 } | |
115 | |
116 return *this; | |
117 } | |
118 | |
4392 | 119 string_vector& |
120 string_vector::append (const std::string& s) | |
121 { | |
5275 | 122 octave_idx_type len = length (); |
4392 | 123 |
124 resize (len + 1); | |
125 | |
126 elem(len) = s; | |
127 | |
128 return *this; | |
129 } | |
130 | |
131 string_vector& | |
132 string_vector::append (const string_vector& sv) | |
133 { | |
5275 | 134 octave_idx_type len = length (); |
135 octave_idx_type sv_len = sv.length (); | |
136 octave_idx_type new_len = len + sv_len; | |
4392 | 137 |
138 resize (new_len); | |
139 | |
5275 | 140 for (octave_idx_type i = 0; i < sv_len; i++) |
4392 | 141 elem(len + i) = sv[i]; |
142 | |
143 return *this; | |
144 } | |
145 | |
2937 | 146 char ** |
147 string_vector::c_str_vec (void) const | |
148 { | |
5275 | 149 octave_idx_type len = length (); |
2937 | 150 |
151 char **retval = new char * [len + 1]; | |
152 | |
153 retval [len] = 0; | |
154 | |
5275 | 155 for (octave_idx_type i = 0; i < len; i++) |
2937 | 156 retval[i] = strsave (elem(i).c_str ()); |
157 | |
158 return retval; | |
159 } | |
160 | |
161 void | |
162 string_vector::delete_c_str_vec (const char * const *v) | |
163 { | |
5304 | 164 const char * const *p = v; |
165 | |
166 while (*p) | |
167 delete [] *p++; | |
2937 | 168 |
169 delete [] v; | |
170 } | |
171 | |
2940 | 172 // Format a list in neat columns. |
1810 | 173 |
3504 | 174 std::ostream& |
5690 | 175 string_vector::list_in_columns (std::ostream& os, int width) const |
1810 | 176 { |
177 // Compute the maximum name length. | |
178 | |
5275 | 179 octave_idx_type max_name_length = 0; |
180 octave_idx_type total_names = length (); | |
1810 | 181 |
5275 | 182 for (octave_idx_type i = 0; i < total_names; i++) |
1810 | 183 { |
5275 | 184 octave_idx_type name_length = elem (i).length (); |
1810 | 185 if (name_length > max_name_length) |
186 max_name_length = name_length; | |
187 } | |
188 | |
189 // Allow at least two spaces between names. | |
190 | |
191 max_name_length += 2; | |
192 | |
193 // Calculate the maximum number of columns that will fit. | |
194 | |
5690 | 195 octave_idx_type line_length |
196 = (width <= 0) ? command_editor::terminal_cols () : width; | |
197 | |
5275 | 198 octave_idx_type nc = line_length / max_name_length; |
4587 | 199 if (nc == 0) |
200 nc = 1; | |
1810 | 201 |
202 // Calculate the number of rows that will be in each column except | |
203 // possibly for a short column on the right. | |
204 | |
5275 | 205 octave_idx_type nr = total_names / nc + (total_names % nc != 0); |
1810 | 206 |
207 // Recalculate columns based on rows. | |
208 | |
4587 | 209 nc = total_names / nr + (total_names % nr != 0); |
1810 | 210 |
5275 | 211 octave_idx_type count; |
212 for (octave_idx_type row = 0; row < nr; row++) | |
1810 | 213 { |
214 count = row; | |
5275 | 215 octave_idx_type pos = 0; |
1810 | 216 |
217 // Print the next row. | |
218 | |
219 while (1) | |
220 { | |
3504 | 221 std::string nm = elem (count); |
1810 | 222 |
223 os << nm; | |
5275 | 224 octave_idx_type name_length = nm.length (); |
1810 | 225 |
4587 | 226 count += nr; |
1810 | 227 if (count >= total_names) |
228 break; | |
229 | |
5275 | 230 octave_idx_type spaces_to_pad = max_name_length - name_length; |
231 for (octave_idx_type i = 0; i < spaces_to_pad; i++) | |
1810 | 232 os << " "; |
233 pos += max_name_length; | |
234 } | |
235 os << "\n"; | |
236 } | |
237 | |
238 return os; | |
239 } | |
240 | |
241 /* | |
242 ;;; Local Variables: *** | |
243 ;;; mode: C++ *** | |
244 ;;; End: *** | |
245 */ |