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