changeset 5690:161ebd1f3410

[project @ 2006-03-17 06:17:24 by jwe]
author jwe
date Fri, 17 Mar 2006 06:17:25 +0000
parents 9a16df2b1916
children 9be68956e450
files liboctave/ChangeLog liboctave/str-vec.cc liboctave/str-vec.h scripts/ChangeLog scripts/miscellaneous/dir.m src/ChangeLog src/strfns.cc
diffstat 7 files changed, 101 insertions(+), 33 deletions(-) [+]
line wrap: on
line diff
--- a/liboctave/ChangeLog	Fri Mar 17 05:00:57 2006 +0000
+++ b/liboctave/ChangeLog	Fri Mar 17 06:17:25 2006 +0000
@@ -1,3 +1,7 @@
+2006-03-17  John W. Eaton  <jwe@octave.org>
+
+	* str-vec.cc (vector::list_in_columns): New optional arg, width.
+
 2006-03-16  David Bateman  <dbateman@free.fr>
 
 	* CSparse.cc: Change use of nzmax to nnz to allow automatic
--- a/liboctave/str-vec.cc	Fri Mar 17 05:00:57 2006 +0000
+++ b/liboctave/str-vec.cc	Fri Mar 17 06:17:25 2006 +0000
@@ -154,7 +154,7 @@
 // Format a list in neat columns.
 
 std::ostream&
-string_vector::list_in_columns (std::ostream& os) const
+string_vector::list_in_columns (std::ostream& os, int width) const
 {
   // Compute the maximum name length.
 
@@ -174,7 +174,9 @@
 
   // Calculate the maximum number of columns that will fit.
 
-  octave_idx_type line_length = command_editor::terminal_cols ();
+  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;
--- a/liboctave/str-vec.h	Fri Mar 17 05:00:57 2006 +0000
+++ b/liboctave/str-vec.h	Fri Mar 17 06:17:25 2006 +0000
@@ -102,7 +102,7 @@
 
   static void delete_c_str_vec (const char * const*);
 
-  std::ostream& list_in_columns (std::ostream&) const;
+  std::ostream& list_in_columns (std::ostream&, int width = 0) const;
 };
 
 #endif
--- a/scripts/ChangeLog	Fri Mar 17 05:00:57 2006 +0000
+++ b/scripts/ChangeLog	Fri Mar 17 06:17:25 2006 +0000
@@ -1,3 +1,9 @@
+2006-03-17  John W. Eaton  <jwe@octave.org>
+
+	* miscellaneous/dir.m: Use readdir instead of glob.
+	Special case for "." to avoid globbing.  Use list_in_columns.
+	Eliminate unused variables len and finfo.
+
 2006-03-16  Bill Denney  <bill@givebillmoney.com>
 
 	* miscellaneous/dir.m: Improve compatibility.
--- a/scripts/miscellaneous/dir.m	Fri Mar 17 05:00:57 2006 +0000
+++ b/scripts/miscellaneous/dir.m	Fri Mar 17 06:17:25 2006 +0000
@@ -51,43 +51,50 @@
 function retval = dir (file)
 
   if (nargin == 0)
-    file = '.';
+    file = ".";
   elseif (nargin > 1)
     usage ("dir (file)");
   endif
 
-  ## prep the retval
-  info = struct(zeros(0,1));
+  ## Prep the retval.
+  info = struct (zeros (0, 1));
 
   if (ischar (file))
-    if (strcmp(file, '*'))
-      file = '.';
+    if (strcmp (file, "*"))
+      file = ".";
     endif
-    flst = glob (file);
-    nf = length (flst);
+    if (strcmp (file, "."))
+      flst = {"."};
+      nf = 1;
+    else
+      flst = glob (file);
+      nf = length (flst);
+    endif
 
-    ## determine the file list for the case where a directory is
-    ## specified and that directory should be recursed into.
-    if ((nf == 1) && strcmp(file, flist{1}))
-      [st, err, msg] = lstat(flst{1});
+    ## Determine the file list for the case where a single directory is
+    ## specified.
+    if (nf == 1)
+      fn = flst{1};
+      [st, err, msg] = lstat (fn);
       if (err < 0)
-	warning("dir: nonexistent file \"%s\"", flst{i});
+	warning ("dir: `lstat (%s)' failed: %s", fn, msg);
 	nf = 0;
       elseif (st.modestr(1) == "d")
-	flst = glob ([flst{1} filesep '*']);
-	nf = length(flst);
+	flst = readdir (flst{1});
+	nf = length (flst);
+	for i = 1:nf
+	  flst{i} = fullfile (fn, flst{i});
+	endfor
       endif
     endif
 
-    if (length(flst) > 0)
-      len = zeros (nf, 1);
-      finfo = cell (nf, 1);
+    if (length (flst) > 0)
       ## Collect results.
       for i = nf:-1:1
 	fn = flst{i};
 	[st, err, msg] = lstat (fn);
 	if (err < 0)
-	  warning ("dir: nonexistent file \"%s\"", fn);
+	  warning ("dir: `lstat (%s)' failed: %s", fn, msg);
 	else
 	  [dummy, fn, ext] = fileparts (fn);
 	  fn = strcat (fn, ext);
@@ -98,26 +105,21 @@
 	  info(i).statinfo = st;
 	endif
       endfor
+    endif
 
-    endif
   else
     error ("dir: expecting directory or filename to be a char array");
   endif
 
-  ## return the output arguements
+  ## Return the output arguments.
   if (nargout > 0)
-    ## Return the requested structure
+    ## Return the requested structure.
     retval = info;
+  elseif (length (info) > 0)
+    ## Print the structure to the screen.
+    printf ("%s", list_in_columns ({info.name}));
   else
-    if (length(info) > 0)
-      ## Print the structure to the screen
-      ## XXX FIXME XXX -- need a way to neatly list these in columns.
-      for i = 1:length(info)
-	printf ("  %s\n", info(i).name);
-      endfor
-    else
-      warning("dir: nonexistent file \"%s\"", file);
-    endif
+    warning ("dir: nonexistent file `%s'", file);
   endif
 
 endfunction
--- a/src/ChangeLog	Fri Mar 17 05:00:57 2006 +0000
+++ b/src/ChangeLog	Fri Mar 17 06:17:25 2006 +0000
@@ -1,3 +1,7 @@
+2006-03-17  John W. Eaton  <jwe@octave.org>
+
+	* strfns.cc (F__list_in_columns__): New function.
+
 2006-03-16  Bill Denney  <bill@givebillmoney.com>
 
 	* DLD-FUNCTIONS/time.cc: Improve @seealso entries in doc strings.
--- a/src/strfns.cc	Fri Mar 17 05:00:57 2006 +0000
+++ b/src/strfns.cc	Fri Mar 17 06:17:25 2006 +0000
@@ -373,6 +373,56 @@
   return retval;
 }
 
+DEFUN (list_in_columns, args, ,
+  "-*- texinfo -*-\n\
+@deftypefn {Built-in Function} {} list_in_columns (@var{arg}, @var{width})\n\
+Return a string containing the elements of @var{arg} listed in\n\
+columns with an overall maximum width of @var{width}.  The argument\n\
+@var{arg} must be a cell array of character strings or a character array.\n\
+If @var{width} is not specified, the width of the terminal screen is used.\n\
+@seealso{terminal_size}\n\
+@end deftypefn")
+{
+  octave_value retval;
+
+  int nargin = args.length ();
+
+  if (nargin == 1 || nargin == 2)
+    {
+      string_vector s = args(0).all_strings ();
+
+      if (! error_state)
+	{
+	  OSSTREAM buf;
+
+	  if (nargin == 1)
+	    // Let list_in_columns query terminal width.
+	    s.list_in_columns (buf);
+	  else
+	    {
+	      int width = args(1).int_value ();
+
+	      if (! error_state)
+		s.list_in_columns (buf, width);
+	      else
+		error ("list_in_columns: expecting width to be an integer");
+	    }
+
+	  buf << OSSTREAM_ENDS;
+
+	  retval = OSSTREAM_STR (buf);
+
+	  OSSTREAM_FREEZE (buf);
+	}
+      else
+	error ("list_in_columns: expecting cellstr or char array");
+    }
+  else
+    print_usage ("list_in_columns");
+
+  return retval;
+}
+
 /*
 ;;; Local Variables: ***
 ;;; mode: C++ ***