Mercurial > octave-nkf
comparison liboctave/str-vec.cc @ 5275:23b37da9fd5b
[project @ 2005-04-08 16:07:35 by jwe]
author | jwe |
---|---|
date | Fri, 08 Apr 2005 16:07:37 +0000 |
parents | fad289f3df73 |
children | 7b6edb02f8c9 |
comparison
equal
deleted
inserted
replaced
5274:eae7b40388e9 | 5275:23b37da9fd5b |
---|---|
42 // Create a string vector from a NULL terminated list of C strings. | 42 // Create a string vector from a NULL terminated list of C strings. |
43 | 43 |
44 string_vector::string_vector (const char * const *s) | 44 string_vector::string_vector (const char * const *s) |
45 : Array<std::string> () | 45 : Array<std::string> () |
46 { | 46 { |
47 int n = 0; | 47 octave_idx_type n = 0; |
48 | 48 |
49 const char * const *t = s; | 49 const char * const *t = s; |
50 | 50 |
51 while (*t++) | 51 while (*t++) |
52 n++; | 52 n++; |
53 | 53 |
54 resize (n); | 54 resize (n); |
55 | 55 |
56 for (int i = 0; i < n; i++) | 56 for (octave_idx_type i = 0; i < n; i++) |
57 elem (i) = s[i]; | 57 elem (i) = s[i]; |
58 } | 58 } |
59 | 59 |
60 // Create a string vector from up to N C strings. Assumes that N is | 60 // Create a string vector from up to N C strings. Assumes that N is |
61 // nonnegative. | 61 // nonnegative. |
62 | 62 |
63 string_vector::string_vector (const char * const *s, int n) | 63 string_vector::string_vector (const char * const *s, octave_idx_type n) |
64 : Array<std::string> (n) | 64 : Array<std::string> (n) |
65 { | 65 { |
66 for (int i = 0; i < n; i++) | 66 for (octave_idx_type i = 0; i < n; i++) |
67 elem (i) = s[i]; | 67 elem (i) = s[i]; |
68 } | 68 } |
69 | 69 |
70 int | 70 int |
71 string_vector::compare (const void *a_arg, const void *b_arg) | 71 string_vector::compare (const void *a_arg, const void *b_arg) |
77 } | 77 } |
78 | 78 |
79 string_vector& | 79 string_vector& |
80 string_vector::uniq (void) | 80 string_vector::uniq (void) |
81 { | 81 { |
82 int len = length (); | 82 octave_idx_type len = length (); |
83 | 83 |
84 if (len > 0) | 84 if (len > 0) |
85 { | 85 { |
86 int k = 0; | 86 octave_idx_type k = 0; |
87 | 87 |
88 for (int i = 1; i < len; i++) | 88 for (octave_idx_type i = 1; i < len; i++) |
89 if (elem(i) != elem(k)) | 89 if (elem(i) != elem(k)) |
90 if (++k != i) | 90 if (++k != i) |
91 elem(k) = elem(i); | 91 elem(k) = elem(i); |
92 | 92 |
93 if (len != ++k) | 93 if (len != ++k) |
98 } | 98 } |
99 | 99 |
100 string_vector& | 100 string_vector& |
101 string_vector::append (const std::string& s) | 101 string_vector::append (const std::string& s) |
102 { | 102 { |
103 int len = length (); | 103 octave_idx_type len = length (); |
104 | 104 |
105 resize (len + 1); | 105 resize (len + 1); |
106 | 106 |
107 elem(len) = s; | 107 elem(len) = s; |
108 | 108 |
110 } | 110 } |
111 | 111 |
112 string_vector& | 112 string_vector& |
113 string_vector::append (const string_vector& sv) | 113 string_vector::append (const string_vector& sv) |
114 { | 114 { |
115 int len = length (); | 115 octave_idx_type len = length (); |
116 int sv_len = sv.length (); | 116 octave_idx_type sv_len = sv.length (); |
117 int new_len = len + sv_len; | 117 octave_idx_type new_len = len + sv_len; |
118 | 118 |
119 resize (new_len); | 119 resize (new_len); |
120 | 120 |
121 for (int i = 0; i < sv_len; i++) | 121 for (octave_idx_type i = 0; i < sv_len; i++) |
122 elem(len + i) = sv[i]; | 122 elem(len + i) = sv[i]; |
123 | 123 |
124 return *this; | 124 return *this; |
125 } | 125 } |
126 | 126 |
127 char ** | 127 char ** |
128 string_vector::c_str_vec (void) const | 128 string_vector::c_str_vec (void) const |
129 { | 129 { |
130 int len = length (); | 130 octave_idx_type len = length (); |
131 | 131 |
132 char **retval = new char * [len + 1]; | 132 char **retval = new char * [len + 1]; |
133 | 133 |
134 retval [len] = 0; | 134 retval [len] = 0; |
135 | 135 |
136 for (int i = 0; i < len; i++) | 136 for (octave_idx_type i = 0; i < len; i++) |
137 retval[i] = strsave (elem(i).c_str ()); | 137 retval[i] = strsave (elem(i).c_str ()); |
138 | 138 |
139 return retval; | 139 return retval; |
140 } | 140 } |
141 | 141 |
153 std::ostream& | 153 std::ostream& |
154 string_vector::list_in_columns (std::ostream& os) const | 154 string_vector::list_in_columns (std::ostream& os) const |
155 { | 155 { |
156 // Compute the maximum name length. | 156 // Compute the maximum name length. |
157 | 157 |
158 int max_name_length = 0; | 158 octave_idx_type max_name_length = 0; |
159 int total_names = length (); | 159 octave_idx_type total_names = length (); |
160 | 160 |
161 for (int i = 0; i < total_names; i++) | 161 for (octave_idx_type i = 0; i < total_names; i++) |
162 { | 162 { |
163 int name_length = elem (i).length (); | 163 octave_idx_type name_length = elem (i).length (); |
164 if (name_length > max_name_length) | 164 if (name_length > max_name_length) |
165 max_name_length = name_length; | 165 max_name_length = name_length; |
166 } | 166 } |
167 | 167 |
168 // Allow at least two spaces between names. | 168 // Allow at least two spaces between names. |
169 | 169 |
170 max_name_length += 2; | 170 max_name_length += 2; |
171 | 171 |
172 // Calculate the maximum number of columns that will fit. | 172 // Calculate the maximum number of columns that will fit. |
173 | 173 |
174 int line_length = command_editor::terminal_cols (); | 174 octave_idx_type line_length = command_editor::terminal_cols (); |
175 int nc = line_length / max_name_length; | 175 octave_idx_type nc = line_length / max_name_length; |
176 if (nc == 0) | 176 if (nc == 0) |
177 nc = 1; | 177 nc = 1; |
178 | 178 |
179 // Calculate the number of rows that will be in each column except | 179 // Calculate the number of rows that will be in each column except |
180 // possibly for a short column on the right. | 180 // possibly for a short column on the right. |
181 | 181 |
182 int nr = total_names / nc + (total_names % nc != 0); | 182 octave_idx_type nr = total_names / nc + (total_names % nc != 0); |
183 | 183 |
184 // Recalculate columns based on rows. | 184 // Recalculate columns based on rows. |
185 | 185 |
186 nc = total_names / nr + (total_names % nr != 0); | 186 nc = total_names / nr + (total_names % nr != 0); |
187 | 187 |
188 int count; | 188 octave_idx_type count; |
189 for (int row = 0; row < nr; row++) | 189 for (octave_idx_type row = 0; row < nr; row++) |
190 { | 190 { |
191 count = row; | 191 count = row; |
192 int pos = 0; | 192 octave_idx_type pos = 0; |
193 | 193 |
194 // Print the next row. | 194 // Print the next row. |
195 | 195 |
196 while (1) | 196 while (1) |
197 { | 197 { |
198 std::string nm = elem (count); | 198 std::string nm = elem (count); |
199 | 199 |
200 os << nm; | 200 os << nm; |
201 int name_length = nm.length (); | 201 octave_idx_type name_length = nm.length (); |
202 | 202 |
203 count += nr; | 203 count += nr; |
204 if (count >= total_names) | 204 if (count >= total_names) |
205 break; | 205 break; |
206 | 206 |
207 int spaces_to_pad = max_name_length - name_length; | 207 octave_idx_type spaces_to_pad = max_name_length - name_length; |
208 for (int i = 0; i < spaces_to_pad; i++) | 208 for (octave_idx_type i = 0; i < spaces_to_pad; i++) |
209 os << " "; | 209 os << " "; |
210 pos += max_name_length; | 210 pos += max_name_length; |
211 } | 211 } |
212 os << "\n"; | 212 os << "\n"; |
213 } | 213 } |