00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #ifdef HAVE_CONFIG_H
00032 #include <config.h>
00033 #endif
00034
00035 #include <iostream>
00036 #include <string>
00037
00038 #include "cmd-edit.h"
00039 #include "lo-utils.h"
00040 #include "str-vec.h"
00041
00042
00043
00044 string_vector::string_vector (const char * const *s)
00045 : Array<std::string> ()
00046 {
00047 int n = 0;
00048
00049 const char * const *t = s;
00050
00051 while (*t++)
00052 n++;
00053
00054 resize (n);
00055
00056 for (int i = 0; i < n; i++)
00057 elem (i) = s[i];
00058 }
00059
00060
00061
00062
00063 string_vector::string_vector (const char * const *s, int n)
00064 : Array<std::string> (n)
00065 {
00066 for (int i = 0; i < n; i++)
00067 elem (i) = s[i];
00068 }
00069
00070 int
00071 string_vector::compare (const void *a_arg, const void *b_arg)
00072 {
00073 const std::string *a = (const std::string *) a_arg;
00074 const std::string *b = (const std::string *) b_arg;
00075
00076 return a->compare (*b);
00077 }
00078
00079 string_vector&
00080 string_vector::uniq (void)
00081 {
00082 int len = length ();
00083
00084 if (len > 0)
00085 {
00086 int k = 0;
00087
00088 for (int i = 1; i < len; i++)
00089 if (elem(i) != elem(k))
00090 if (++k != i)
00091 elem(k) = elem(i);
00092
00093 if (len != ++k)
00094 resize (k);
00095 }
00096
00097 return *this;
00098 }
00099
00100 string_vector&
00101 string_vector::append (const std::string& s)
00102 {
00103 int len = length ();
00104
00105 resize (len + 1);
00106
00107 elem(len) = s;
00108
00109 return *this;
00110 }
00111
00112 string_vector&
00113 string_vector::append (const string_vector& sv)
00114 {
00115 int len = length ();
00116 int sv_len = sv.length ();
00117 int new_len = len + sv_len;
00118
00119 resize (new_len);
00120
00121 for (int i = 0; i < sv_len; i++)
00122 elem(len + i) = sv[i];
00123
00124 return *this;
00125 }
00126
00127 char **
00128 string_vector::c_str_vec (void) const
00129 {
00130 int len = length ();
00131
00132 char **retval = new char * [len + 1];
00133
00134 retval [len] = 0;
00135
00136 for (int i = 0; i < len; i++)
00137 retval[i] = strsave (elem(i).c_str ());
00138
00139 return retval;
00140 }
00141
00142 void
00143 string_vector::delete_c_str_vec (const char * const *v)
00144 {
00145 while (*v)
00146 delete [] *v;
00147
00148 delete [] v;
00149 }
00150
00151
00152
00153 std::ostream&
00154 string_vector::list_in_columns (std::ostream& os) const
00155 {
00156
00157
00158 int max_name_length = 0;
00159 int total_names = length ();
00160
00161 for (int i = 0; i < total_names; i++)
00162 {
00163 int name_length = elem (i).length ();
00164 if (name_length > max_name_length)
00165 max_name_length = name_length;
00166 }
00167
00168
00169
00170 max_name_length += 2;
00171
00172
00173
00174 int line_length = command_editor::terminal_cols ();
00175 int nc = line_length / max_name_length;
00176 if (nc == 0)
00177 nc = 1;
00178
00179
00180
00181
00182 int nr = total_names / nc + (total_names % nc != 0);
00183
00184
00185
00186 nc = total_names / nr + (total_names % nr != 0);
00187
00188 int count;
00189 for (int row = 0; row < nr; row++)
00190 {
00191 count = row;
00192 int pos = 0;
00193
00194
00195
00196 while (1)
00197 {
00198 std::string nm = elem (count);
00199
00200 os << nm;
00201 int name_length = nm.length ();
00202
00203 count += nr;
00204 if (count >= total_names)
00205 break;
00206
00207 int spaces_to_pad = max_name_length - name_length;
00208 for (int i = 0; i < spaces_to_pad; i++)
00209 os << " ";
00210 pos += max_name_length;
00211 }
00212 os << "\n";
00213 }
00214
00215 return os;
00216 }
00217
00218
00219
00220
00221
00222