メインページ   クラス階層   構成   ファイル一覧   構成メンバ   ファイルメンバ  

str-vec.cc

解説を見る。
00001 /*
00002 
00003 Copyright (C) 1996, 1997 John W. Eaton
00004 
00005 This file is part of Octave.
00006 
00007 Octave is free software; you can redistribute it and/or modify it
00008 under the terms of the GNU General Public License as published by the
00009 Free Software Foundation; either version 2, or (at your option) any
00010 later version.
00011 
00012 Octave is distributed in the hope that it will be useful, but WITHOUT
00013 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00015 for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Octave; see the file COPYING.  If not, write to the Free
00019 Software Foundation, 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00020 
00021 */
00022 
00023 /*
00024 
00025 The function string_vector::list_in_columns was adapted from a similar
00026 function distributed in the GNU file utilities, copyright (C) 85, 88,
00027 90, 91, 95, 1996 Free Software Foundation, Inc.
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 // Create a string vector from a NULL terminated list of C strings.
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 // Create a string vector from up to N C strings.  Assumes that N is
00061 // nonnegative.
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 // Format a list in neat columns.
00152 
00153 std::ostream&
00154 string_vector::list_in_columns (std::ostream& os) const
00155 {
00156   // Compute the maximum name length.
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   // Allow at least two spaces between names.
00169 
00170   max_name_length += 2;
00171 
00172   // Calculate the maximum number of columns that will fit.
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   // Calculate the number of rows that will be in each column except
00180   // possibly  for a short column on the right.
00181 
00182   int nr = total_names / nc + (total_names % nc != 0);
00183 
00184   // Recalculate columns based on rows.
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       // Print the next row.
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 ;;; Local Variables: ***
00220 ;;; mode: C++ ***
00221 ;;; End: ***
00222 */

Wed Dec 29 11:52:19 2004に生成されました。 doxygen1.2.18
SEO [PR] 爆速!無料ブログ 無料ホームページ開設 無料ライブ放送