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
|
/*
Copyright (C) 1996, 1997 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 2, 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, write to the Free
Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
/*
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"
// Create a string vector from a NULL terminated list of C strings.
string_vector::string_vector (const char * const *s)
: Array<std::string> ()
{
int n = 0;
const char * const *t = s;
while (*t++)
n++;
resize (n);
for (int 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, int n)
: Array<std::string> (n)
{
for (int 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 = (const std::string *) a_arg;
const std::string *b = (const std::string *) b_arg;
return a->compare (*b);
}
string_vector&
string_vector::uniq (void)
{
int len = length ();
if (len > 0)
{
int k = 0;
for (int 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)
{
int len = length ();
resize (len + 1);
elem(len) = s;
return *this;
}
string_vector&
string_vector::append (const string_vector& sv)
{
int len = length ();
int sv_len = sv.length ();
int new_len = len + sv_len;
resize (new_len);
for (int i = 0; i < sv_len; i++)
elem(len + i) = sv[i];
return *this;
}
char **
string_vector::c_str_vec (void) const
{
int len = length ();
char **retval = new char * [len + 1];
retval [len] = 0;
for (int 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) const
{
// Compute the maximum name length.
int max_name_length = 0;
int total_names = length ();
for (int i = 0; i < total_names; i++)
{
int 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.
int line_length = command_editor::terminal_cols ();
int 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.
int nr = total_names / nc + (total_names % nc != 0);
// Recalculate columns based on rows.
nc = total_names / nr + (total_names % nr != 0);
int count;
for (int row = 0; row < nr; row++)
{
count = row;
int pos = 0;
// Print the next row.
while (1)
{
std::string nm = elem (count);
os << nm;
int name_length = nm.length ();
count += nr;
if (count >= total_names)
break;
int spaces_to_pad = max_name_length - name_length;
for (int i = 0; i < spaces_to_pad; i++)
os << " ";
pos += max_name_length;
}
os << "\n";
}
return os;
}
/*
;;; Local Variables: ***
;;; mode: C++ ***
;;; End: ***
*/
|