Mercurial > hg > octave-lyh
annotate liboctave/str-vec.cc @ 11449:93b8c7ca211f
isa.m: Add tests against logical types
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Wed, 05 Jan 2011 21:21:37 -0800 |
parents | a9649f994b07 |
children | fd0a3ac60b0e |
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 | |
9582
bdcfb756d721
improve error messages for ambiguous graphics property names
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
43 // FIXME -- isn't there some STL trick that could be used to make this |
bdcfb756d721
improve error messages for ambiguous graphics property names
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
44 // work for all STL containers of std::string objects? |
bdcfb756d721
improve error messages for ambiguous graphics property names
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
45 |
5880 | 46 string_vector::string_vector (const std::list<std::string>& lst) |
5894 | 47 : Array<std::string> () |
5880 | 48 { |
49 size_t n = lst.size (); | |
50 | |
51 resize (n); | |
52 | |
53 octave_idx_type i = 0; | |
54 | |
55 for (std::list<std::string>::const_iterator p = lst.begin (); | |
56 p != lst.end (); | |
57 p++) | |
58 elem(i++) = *p; | |
59 } | |
60 | |
9582
bdcfb756d721
improve error messages for ambiguous graphics property names
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
61 string_vector::string_vector (const std::set<std::string>& lst) |
bdcfb756d721
improve error messages for ambiguous graphics property names
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
62 : Array<std::string> () |
bdcfb756d721
improve error messages for ambiguous graphics property names
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
63 { |
bdcfb756d721
improve error messages for ambiguous graphics property names
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
64 size_t n = lst.size (); |
bdcfb756d721
improve error messages for ambiguous graphics property names
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
65 |
bdcfb756d721
improve error messages for ambiguous graphics property names
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
66 resize (n); |
bdcfb756d721
improve error messages for ambiguous graphics property names
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
67 |
bdcfb756d721
improve error messages for ambiguous graphics property names
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
68 octave_idx_type i = 0; |
bdcfb756d721
improve error messages for ambiguous graphics property names
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
69 |
bdcfb756d721
improve error messages for ambiguous graphics property names
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
70 for (std::set<std::string>::const_iterator p = lst.begin (); |
bdcfb756d721
improve error messages for ambiguous graphics property names
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
71 p != lst.end (); |
bdcfb756d721
improve error messages for ambiguous graphics property names
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
72 p++) |
bdcfb756d721
improve error messages for ambiguous graphics property names
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
73 elem(i++) = *p; |
bdcfb756d721
improve error messages for ambiguous graphics property names
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
74 } |
bdcfb756d721
improve error messages for ambiguous graphics property names
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
75 |
2493 | 76 // Create a string vector from a NULL terminated list of C strings. |
77 | |
78 string_vector::string_vector (const char * const *s) | |
3504 | 79 : Array<std::string> () |
2493 | 80 { |
5275 | 81 octave_idx_type n = 0; |
2493 | 82 |
10477
a9649f994b07
avoid segfault in string_vector constructor
John W. Eaton <jwe@octave.org>
parents:
10355
diff
changeset
|
83 if (s) |
a9649f994b07
avoid segfault in string_vector constructor
John W. Eaton <jwe@octave.org>
parents:
10355
diff
changeset
|
84 { |
a9649f994b07
avoid segfault in string_vector constructor
John W. Eaton <jwe@octave.org>
parents:
10355
diff
changeset
|
85 const char * const *t = s; |
3040 | 86 |
10477
a9649f994b07
avoid segfault in string_vector constructor
John W. Eaton <jwe@octave.org>
parents:
10355
diff
changeset
|
87 while (*t++) |
a9649f994b07
avoid segfault in string_vector constructor
John W. Eaton <jwe@octave.org>
parents:
10355
diff
changeset
|
88 n++; |
a9649f994b07
avoid segfault in string_vector constructor
John W. Eaton <jwe@octave.org>
parents:
10355
diff
changeset
|
89 } |
2493 | 90 |
91 resize (n); | |
92 | |
5275 | 93 for (octave_idx_type i = 0; i < n; i++) |
2493 | 94 elem (i) = s[i]; |
95 } | |
96 | |
97 // Create a string vector from up to N C strings. Assumes that N is | |
98 // nonnegative. | |
99 | |
5275 | 100 string_vector::string_vector (const char * const *s, octave_idx_type n) |
10355
f9347eac65dc
make string_vector be a column vector as it used to be
Jaroslav Hajek <highegg@gmail.com>
parents:
10350
diff
changeset
|
101 : Array<std::string> (n, 1) |
2493 | 102 { |
5275 | 103 for (octave_idx_type i = 0; i < n; i++) |
2493 | 104 elem (i) = s[i]; |
105 } | |
106 | |
2941 | 107 string_vector& |
8678
e2b4c19c455c
redo changeset 4238f2600a17 with fixes to sorting
Jaroslav Hajek <highegg@gmail.com>
parents:
8671
diff
changeset
|
108 string_vector::sort (bool make_uniq) |
e2b4c19c455c
redo changeset 4238f2600a17 with fixes to sorting
Jaroslav Hajek <highegg@gmail.com>
parents:
8671
diff
changeset
|
109 { |
e2b4c19c455c
redo changeset 4238f2600a17 with fixes to sorting
Jaroslav Hajek <highegg@gmail.com>
parents:
8671
diff
changeset
|
110 // 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
|
111 octave_sort<std::string> lsort; |
e2b4c19c455c
redo changeset 4238f2600a17 with fixes to sorting
Jaroslav Hajek <highegg@gmail.com>
parents:
8671
diff
changeset
|
112 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
|
113 |
e2b4c19c455c
redo changeset 4238f2600a17 with fixes to sorting
Jaroslav Hajek <highegg@gmail.com>
parents:
8671
diff
changeset
|
114 if (make_uniq) |
e2b4c19c455c
redo changeset 4238f2600a17 with fixes to sorting
Jaroslav Hajek <highegg@gmail.com>
parents:
8671
diff
changeset
|
115 uniq (); |
e2b4c19c455c
redo changeset 4238f2600a17 with fixes to sorting
Jaroslav Hajek <highegg@gmail.com>
parents:
8671
diff
changeset
|
116 |
e2b4c19c455c
redo changeset 4238f2600a17 with fixes to sorting
Jaroslav Hajek <highegg@gmail.com>
parents:
8671
diff
changeset
|
117 return *this; |
e2b4c19c455c
redo changeset 4238f2600a17 with fixes to sorting
Jaroslav Hajek <highegg@gmail.com>
parents:
8671
diff
changeset
|
118 } |
e2b4c19c455c
redo changeset 4238f2600a17 with fixes to sorting
Jaroslav Hajek <highegg@gmail.com>
parents:
8671
diff
changeset
|
119 string_vector& |
2941 | 120 string_vector::uniq (void) |
121 { | |
5275 | 122 octave_idx_type len = length (); |
2941 | 123 |
124 if (len > 0) | |
125 { | |
5275 | 126 octave_idx_type k = 0; |
2941 | 127 |
5275 | 128 for (octave_idx_type i = 1; i < len; i++) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
129 if (elem(i) != elem(k)) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
130 if (++k != i) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
131 elem(k) = elem(i); |
2941 | 132 |
133 if (len != ++k) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
134 resize (k); |
2941 | 135 } |
136 | |
137 return *this; | |
138 } | |
139 | |
4392 | 140 string_vector& |
141 string_vector::append (const std::string& s) | |
142 { | |
5275 | 143 octave_idx_type len = length (); |
4392 | 144 |
145 resize (len + 1); | |
146 | |
147 elem(len) = s; | |
148 | |
149 return *this; | |
150 } | |
151 | |
152 string_vector& | |
153 string_vector::append (const string_vector& sv) | |
154 { | |
5275 | 155 octave_idx_type len = length (); |
156 octave_idx_type sv_len = sv.length (); | |
157 octave_idx_type new_len = len + sv_len; | |
4392 | 158 |
159 resize (new_len); | |
160 | |
5275 | 161 for (octave_idx_type i = 0; i < sv_len; i++) |
4392 | 162 elem(len + i) = sv[i]; |
163 | |
164 return *this; | |
165 } | |
166 | |
2937 | 167 char ** |
168 string_vector::c_str_vec (void) const | |
169 { | |
5275 | 170 octave_idx_type len = length (); |
2937 | 171 |
172 char **retval = new char * [len + 1]; | |
173 | |
174 retval [len] = 0; | |
175 | |
5275 | 176 for (octave_idx_type i = 0; i < len; i++) |
2937 | 177 retval[i] = strsave (elem(i).c_str ()); |
178 | |
179 return retval; | |
180 } | |
181 | |
182 void | |
183 string_vector::delete_c_str_vec (const char * const *v) | |
184 { | |
5304 | 185 const char * const *p = v; |
186 | |
187 while (*p) | |
188 delete [] *p++; | |
2937 | 189 |
190 delete [] v; | |
191 } | |
192 | |
2940 | 193 // Format a list in neat columns. |
1810 | 194 |
3504 | 195 std::ostream& |
5690 | 196 string_vector::list_in_columns (std::ostream& os, int width) const |
1810 | 197 { |
198 // Compute the maximum name length. | |
199 | |
5275 | 200 octave_idx_type max_name_length = 0; |
201 octave_idx_type total_names = length (); | |
1810 | 202 |
9831
737624cb7560
list_in_columns: Don't SIGFPE when given empty first argument
David Grundberg <davidg@cs.umu.se>
parents:
9582
diff
changeset
|
203 if (total_names == 0) |
737624cb7560
list_in_columns: Don't SIGFPE when given empty first argument
David Grundberg <davidg@cs.umu.se>
parents:
9582
diff
changeset
|
204 { |
737624cb7560
list_in_columns: Don't SIGFPE when given empty first argument
David Grundberg <davidg@cs.umu.se>
parents:
9582
diff
changeset
|
205 // List empty, remember to end output with a newline. |
737624cb7560
list_in_columns: Don't SIGFPE when given empty first argument
David Grundberg <davidg@cs.umu.se>
parents:
9582
diff
changeset
|
206 |
737624cb7560
list_in_columns: Don't SIGFPE when given empty first argument
David Grundberg <davidg@cs.umu.se>
parents:
9582
diff
changeset
|
207 os << "\n"; |
737624cb7560
list_in_columns: Don't SIGFPE when given empty first argument
David Grundberg <davidg@cs.umu.se>
parents:
9582
diff
changeset
|
208 return os; |
737624cb7560
list_in_columns: Don't SIGFPE when given empty first argument
David Grundberg <davidg@cs.umu.se>
parents:
9582
diff
changeset
|
209 } |
737624cb7560
list_in_columns: Don't SIGFPE when given empty first argument
David Grundberg <davidg@cs.umu.se>
parents:
9582
diff
changeset
|
210 |
5275 | 211 for (octave_idx_type i = 0; i < total_names; i++) |
1810 | 212 { |
5275 | 213 octave_idx_type name_length = elem (i).length (); |
1810 | 214 if (name_length > max_name_length) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
215 max_name_length = name_length; |
1810 | 216 } |
217 | |
218 // Allow at least two spaces between names. | |
219 | |
220 max_name_length += 2; | |
221 | |
222 // Calculate the maximum number of columns that will fit. | |
223 | |
5690 | 224 octave_idx_type line_length |
225 = (width <= 0) ? command_editor::terminal_cols () : width; | |
226 | |
5275 | 227 octave_idx_type nc = line_length / max_name_length; |
4587 | 228 if (nc == 0) |
229 nc = 1; | |
1810 | 230 |
231 // Calculate the number of rows that will be in each column except | |
232 // possibly for a short column on the right. | |
233 | |
5275 | 234 octave_idx_type nr = total_names / nc + (total_names % nc != 0); |
1810 | 235 |
236 // Recalculate columns based on rows. | |
237 | |
4587 | 238 nc = total_names / nr + (total_names % nr != 0); |
1810 | 239 |
5275 | 240 octave_idx_type count; |
241 for (octave_idx_type row = 0; row < nr; row++) | |
1810 | 242 { |
243 count = row; | |
5275 | 244 octave_idx_type pos = 0; |
1810 | 245 |
246 // Print the next row. | |
247 | |
248 while (1) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
249 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
250 std::string nm = elem (count); |
1810 | 251 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
252 os << nm; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
253 octave_idx_type name_length = nm.length (); |
1810 | 254 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
255 count += nr; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
256 if (count >= total_names) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
257 break; |
1810 | 258 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
259 octave_idx_type spaces_to_pad = max_name_length - name_length; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
260 for (octave_idx_type i = 0; i < spaces_to_pad; i++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
261 os << " "; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
262 pos += max_name_length; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
263 } |
1810 | 264 os << "\n"; |
265 } | |
266 | |
267 return os; | |
268 } |