1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
|
/*
Copyright (C) 1996, 1997, 2000, 2002, 2003, 2005, 2006, 2007
John W. Eaton
This file is part of Octave.
Octave is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 3 of the License, or (at your
option) any later version.
Octave is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with Octave; see the file COPYING. If not, see
<http://www.gnu.org/licenses/>.
*/
/*
The function string_vector::list_in_columns was adapted from a similar
function distributed in the GNU file utilities, copyright (C) 85, 88,
90, 91, 95, 1996 Free Software Foundation, Inc.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <iostream>
#include <string>
#include "cmd-edit.h"
#include "lo-utils.h"
#include "str-vec.h"
string_vector::string_vector (const std::list<std::string>& lst)
: Array<std::string> ()
{
size_t n = lst.size ();
resize (n);
octave_idx_type i = 0;
for (std::list<std::string>::const_iterator p = lst.begin ();
p != lst.end ();
p++)
elem(i++) = *p;
}
// Create a string vector from a NULL terminated list of C strings.
string_vector::string_vector (const char * const *s)
: Array<std::string> ()
{
octave_idx_type n = 0;
const char * const *t = s;
while (*t++)
n++;
resize (n);
for (octave_idx_type i = 0; i < n; i++)
elem (i) = s[i];
}
// Create a string vector from up to N C strings. Assumes that N is
// nonnegative.
string_vector::string_vector (const char * const *s, octave_idx_type n)
: Array<std::string> (n)
{
for (octave_idx_type i = 0; i < n; i++)
elem (i) = s[i];
}
int
string_vector::compare (const void *a_arg, const void *b_arg)
{
const std::string *a = static_cast<const std::string *> (a_arg);
const std::string *b = static_cast<const std::string *> (b_arg);
return a->compare (*b);
}
string_vector&
string_vector::uniq (void)
{
octave_idx_type len = length ();
if (len > 0)
{
octave_idx_type k = 0;
for (octave_idx_type i = 1; i < len; i++)
if (elem(i) != elem(k))
if (++k != i)
elem(k) = elem(i);
if (len != ++k)
resize (k);
}
return *this;
}
string_vector&
string_vector::append (const std::string& s)
{
octave_idx_type len = length ();
resize (len + 1);
elem(len) = s;
return *this;
}
string_vector&
string_vector::append (const string_vector& sv)
{
octave_idx_type len = length ();
octave_idx_type sv_len = sv.length ();
octave_idx_type new_len = len + sv_len;
resize (new_len);
for (octave_idx_type i = 0; i < sv_len; i++)
elem(len + i) = sv[i];
return *this;
}
char **
string_vector::c_str_vec (void) const
{
octave_idx_type len = length ();
char **retval = new char * [len + 1];
retval [len] = 0;
for (octave_idx_type i = 0; i < len; i++)
retval[i] = strsave (elem(i).c_str ());
return retval;
}
void
string_vector::delete_c_str_vec (const char * const *v)
{
const char * const *p = v;
while (*p)
delete [] *p++;
delete [] v;
}
// Format a list in neat columns.
std::ostream&
string_vector::list_in_columns (std::ostream& os, int width) const
{
// Compute the maximum name length.
octave_idx_type max_name_length = 0;
octave_idx_type total_names = length ();
for (octave_idx_type i = 0; i < total_names; i++)
{
octave_idx_type name_length = elem (i).length ();
if (name_length > max_name_length)
max_name_length = name_length;
}
// Allow at least two spaces between names.
max_name_length += 2;
// Calculate the maximum number of columns that will fit.
octave_idx_type line_length
= (width <= 0) ? command_editor::terminal_cols () : width;
octave_idx_type nc = line_length / max_name_length;
if (nc == 0)
nc = 1;
// Calculate the number of rows that will be in each column except
// possibly for a short column on the right.
octave_idx_type nr = total_names / nc + (total_names % nc != 0);
// Recalculate columns based on rows.
nc = total_names / nr + (total_names % nr != 0);
octave_idx_type count;
for (octave_idx_type row = 0; row < nr; row++)
{
count = row;
octave_idx_type pos = 0;
// Print the next row.
while (1)
{
std::string nm = elem (count);
os << nm;
octave_idx_type name_length = nm.length ();
count += nr;
if (count >= total_names)
break;
octave_idx_type spaces_to_pad = max_name_length - name_length;
for (octave_idx_type i = 0; i < spaces_to_pad; i++)
os << " ";
pos += max_name_length;
}
os << "\n";
}
return os;
}
/*
;;; Local Variables: ***
;;; mode: C++ ***
;;; End: ***
*/
|