# HG changeset patch # User jwe # Date 1142576245 0 # Node ID 161ebd1f34108e4b9bc6d8fc68ca2b386c24a665 # Parent 9a16df2b19163d4c5ab199e26a97963ca2fc294d [project @ 2006-03-17 06:17:24 by jwe] diff --git a/liboctave/ChangeLog b/liboctave/ChangeLog --- a/liboctave/ChangeLog +++ b/liboctave/ChangeLog @@ -1,3 +1,7 @@ +2006-03-17 John W. Eaton + + * str-vec.cc (vector::list_in_columns): New optional arg, width. + 2006-03-16 David Bateman * CSparse.cc: Change use of nzmax to nnz to allow automatic diff --git a/liboctave/str-vec.cc b/liboctave/str-vec.cc --- a/liboctave/str-vec.cc +++ b/liboctave/str-vec.cc @@ -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; diff --git a/liboctave/str-vec.h b/liboctave/str-vec.h --- a/liboctave/str-vec.h +++ b/liboctave/str-vec.h @@ -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 diff --git a/scripts/ChangeLog b/scripts/ChangeLog --- a/scripts/ChangeLog +++ b/scripts/ChangeLog @@ -1,3 +1,9 @@ +2006-03-17 John W. Eaton + + * 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 * miscellaneous/dir.m: Improve compatibility. diff --git a/scripts/miscellaneous/dir.m b/scripts/miscellaneous/dir.m --- a/scripts/miscellaneous/dir.m +++ b/scripts/miscellaneous/dir.m @@ -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 diff --git a/src/ChangeLog b/src/ChangeLog --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,7 @@ +2006-03-17 John W. Eaton + + * strfns.cc (F__list_in_columns__): New function. + 2006-03-16 Bill Denney * DLD-FUNCTIONS/time.cc: Improve @seealso entries in doc strings. diff --git a/src/strfns.cc b/src/strfns.cc --- a/src/strfns.cc +++ b/src/strfns.cc @@ -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++ ***