GNU Octave  4.2.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
str-vec.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1996-2017 John W. Eaton
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 3 of the License, or (at your
10 option) any 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, see
19 <http://www.gnu.org/licenses/>.
20 
21 */
22 
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 
31 #if defined (HAVE_CONFIG_H)
32 # include "config.h"
33 #endif
34 
35 #include <iostream>
36 #include <string>
37 
38 #include "cmd-edit.h"
39 #include "lo-utils.h"
40 #include "str-vec.h"
41 
42 // Create a string vector from a NULL terminated list of C strings.
43 
44 string_vector::string_vector (const char * const *s)
45  : Array<std::string> ()
46 {
47  octave_idx_type n = 0;
48 
49  if (s)
50  {
51  const char * const *t = s;
52 
53  while (*t++)
54  n++;
55  }
56 
57  resize (n);
58 
59  for (octave_idx_type i = 0; i < n; i++)
60  elem (i) = s[i];
61 }
62 
63 // Create a string vector from up to N C strings. Assumes that N is
64 // nonnegative.
65 
67  : Array<std::string> (dim_vector (n, 1))
68 {
69  for (octave_idx_type i = 0; i < n; i++)
70  elem (i) = s[i];
71 }
72 
74 string_vector::sort (bool make_uniq)
75 {
76  // Don't use Array<std::string>::sort () to allow sorting in place.
79 
80  if (make_uniq)
81  uniq ();
82 
83  return *this;
84 }
87 {
88  octave_idx_type len = numel ();
89 
90  if (len > 0)
91  {
92  octave_idx_type k = 0;
93 
94  for (octave_idx_type i = 1; i < len; i++)
95  if (elem (i) != elem (k))
96  if (++k != i)
97  elem (k) = elem (i);
98 
99  if (len != ++k)
100  resize (k);
101  }
102 
103  return *this;
104 }
105 
108 {
109  octave_idx_type len = numel ();
110 
111  resize (len + 1);
112 
113  elem (len) = s;
114 
115  return *this;
116 }
117 
120 {
121  octave_idx_type len = numel ();
122  octave_idx_type sv_len = sv.numel ();
123  octave_idx_type new_len = len + sv_len;
124 
125  resize (new_len);
126 
127  for (octave_idx_type i = 0; i < sv_len; i++)
128  elem (len + i) = sv[i];
129 
130  return *this;
131 }
132 
135 {
137 
138  octave_idx_type len = numel ();
139 
140  if (len > 0)
141  {
143 
144  for (i = 0; i < len - 1; i++)
145  retval += elem (i) + sep;
146 
147  retval += elem (i);
148  }
149 
150  return retval;
151 }
152 
153 char **
155 {
156  octave_idx_type len = numel ();
157 
158  char **retval = new char * [len + 1];
159 
160  retval[len] = 0;
161 
162  for (octave_idx_type i = 0; i < len; i++)
163  retval[i] = strsave (elem (i).c_str ());
164 
165  return retval;
166 }
167 
168 std::list<std::string>
170 {
171  octave_idx_type len = numel ();
172 
173  std::list<std::string> retval;
174 
175  for (octave_idx_type i = 0; i < len; i++)
176  retval.push_back (elem (i));
177 
178  return retval;
179 }
180 
181 void
182 string_vector::delete_c_str_vec (const char * const *v)
183 {
184  const char * const *p = v;
185 
186  while (*p)
187  delete [] *p++;
188 
189  delete [] v;
190 }
191 
192 // Format a list in neat columns.
193 
194 std::ostream&
195 string_vector::list_in_columns (std::ostream& os, int width,
196  const std::string& prefix) const
197 {
198  // Compute the maximum name length.
199 
200  octave_idx_type max_name_length = 0;
201  octave_idx_type total_names = numel ();
202 
203  if (total_names == 0)
204  {
205  // List empty, remember to end output with a newline.
206 
207  os << "\n";
208  return os;
209  }
210 
211  for (octave_idx_type i = 0; i < total_names; i++)
212  {
213  octave_idx_type name_length = elem (i).length ();
214  if (name_length > max_name_length)
215  max_name_length = name_length;
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 
224  octave_idx_type line_length
225  = ((width <= 0 ? octave::command_editor::terminal_cols () : width)
226  - prefix.length ());
227 
228  octave_idx_type nc = line_length / max_name_length;
229  if (nc == 0)
230  nc = 1;
231 
232  // Calculate the number of rows that will be in each column except
233  // possibly for a short column on the right.
234 
235  octave_idx_type nr = total_names / nc + (total_names % nc != 0);
236 
237  octave_idx_type count;
238  for (octave_idx_type row = 0; row < nr; row++)
239  {
240  count = row;
241  octave_idx_type pos = 0;
242 
243  // Print the next row.
244 
245  os << prefix;
246 
247  while (1)
248  {
249  std::string nm = elem (count);
250 
251  os << nm;
252  octave_idx_type name_length = nm.length ();
253 
254  count += nr;
255  if (count >= total_names)
256  break;
257 
258  octave_idx_type spaces_to_pad = max_name_length - name_length;
259  for (octave_idx_type i = 0; i < spaces_to_pad; i++)
260  os << " ";
261  pos += max_name_length;
262  }
263  os << "\n";
264  }
265 
266  return os;
267 }
string_vector(void)
Definition: str-vec.h:40
octave_idx_type numel(void) const
Number of elements in the array.
Definition: Array.h:363
static char * strsave(const char *s)
Definition: main.cc:183
for large enough k
Definition: lu.cc:606
string_vector & uniq(void)
Definition: str-vec.cc:86
char ** c_str_vec(void) const
Definition: str-vec.cc:154
STL namespace.
static void delete_c_str_vec(const char *const *)
Definition: str-vec.cc:182
std::string & elem(octave_idx_type n)
Definition: Array.h:482
OCTAVE_EXPORT octave_value_list return the number of command line arguments passed to Octave If called with the optional argument the function t
Definition: ov-usr-fcn.cc:935
s
Definition: file-io.cc:2682
void resize(octave_idx_type n, const std::string &rfv="")
Definition: str-vec.h:97
OCTAVE_EXPORT octave_value_list isdir nd deftypefn *std::string nm
Definition: utils.cc:941
std::string join(const std::string &sep="") const
Definition: str-vec.cc:134
string_vector & append(const std::string &s)
Definition: str-vec.cc:107
octave_value retval
Definition: data.cc:6294
string_vector & sort(bool make_uniq=false)
Definition: str-vec.cc:74
std::ostream & list_in_columns(std::ostream &, int width=0, const std::string &prefix="") const
Definition: str-vec.cc:195
N Dimensional Array with copy-on-write semantics.
Definition: Array.h:126
void sort(T *data, octave_idx_type nel)
Definition: oct-sort.cc:1506
=val(i)}if ode{val(i)}occurs in table i
Definition: lookup.cc:239
p
Definition: lu.cc:138
std::list< std::string > std_list(void) const
Definition: str-vec.cc:169
static int terminal_cols(void)
Definition: cmd-edit.cc:1230
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:87
If this string is the system will ring the terminal sometimes it is useful to be able to print the original representation of the string
Definition: utils.cc:854
where the brackets indicate optional arguments and and character or cell array For character arrays the conversion is repeated for every row
Definition: str2double.cc:342