Mercurial > hg > octave-lyh
changeset 1781:e090f89bf2f5
[project @ 1996-01-24 08:04:14 by jwe]
author | jwe |
---|---|
date | Wed, 24 Jan 1996 08:09:03 +0000 |
parents | 6101360af703 |
children | b17d014b6926 |
files | liboctave/Array.h liboctave/str-vec.h src/dirfns.cc src/help.cc src/utils.cc src/utils.h |
diffstat | 6 files changed, 53 insertions(+), 101 deletions(-) [+] |
line wrap: on
line diff
--- a/liboctave/Array.h +++ b/liboctave/Array.h @@ -188,7 +188,7 @@ T *fortran_vec (void); - void qsort (int (*compare) (const void *, const void *)) + Array<T>& qsort (int (*compare) (const void *, const void *)) { if (rep->count > 1) { @@ -197,6 +197,8 @@ } rep->qsort (compare); + + return *this; } #ifdef HEAVYWEIGHT_INDEXING
--- a/liboctave/str-vec.h +++ b/liboctave/str-vec.h @@ -74,7 +74,11 @@ string operator[] (int i) const { return Array<string>::elem (i); } - void qsort (void) { Array<string>::qsort (str_vec_compare); } + string_vector& qsort (void) + { + Array<string>::qsort (str_vec_compare); + return *this; + } }; #endif
--- a/src/dirfns.cc +++ b/src/dirfns.cc @@ -51,6 +51,7 @@ #include "str-vec.h" #include "defun.h" +#include "dir-ops.h" #include "dirfns.h" #include "error.h" #include "file-ops.h" @@ -62,7 +63,6 @@ #include "procstream.h" #include "pt-plot.h" #include "sysdep.h" -#include "sysdir.h" #include "toplev.h" #include "unwind-prot.h" #include "utils.h" @@ -446,80 +446,32 @@ is printed.") { Octave_object retval; - charMatrix dirlist; - int status = 0; if (args.length () == 1) { string dirname = args(0).string_value (); if (error_state) - { - status = -1; - gripe_wrong_type_arg ("readdir", args(0)); - } + gripe_wrong_type_arg ("readdir", args(0)); else { - string tmp = oct_tilde_expand (dirname); - - DIR *dir = opendir (tmp.c_str ()); + dir_entry dir (oct_tilde_expand (dirname)); if (dir) { - int count = 0; - int max_len = 0; - - struct dirent *dir_entry; - - while ((dir_entry = readdir (dir))) - { - count++; - int len = strlen (dir_entry->d_name); - if (len > max_len) - max_len = len; - } - - rewinddir (dir); - - dirlist.resize (count, max_len, 0); - - while ((dir_entry = readdir (dir))) - { - if (--count < 0) - break; - - dirlist.insert (dir_entry->d_name, count, 0); - } - -#if defined (CLOSEDIR_VOID) - closedir (dir); -#else - if (closedir (dir) < 0) - { - status = -1; - error ("%s", strerror (errno)); - } -#endif - - if (count != 0) - { - status = -1; - error ("readdir: failed reading directory"); - } + string_vector dirlist = dir.read (); + retval(0) = dirlist.qsort (); } else { - status = -1; - error ("%s", strerror (errno)); + string msg = dir.error (); + error ("%s", msg.c_str ()); } } } else print_usage ("readdir"); - if (status == 0) - retval(0) = tree_constant (dirlist, 1); - return retval; }
--- a/src/help.cc +++ b/src/help.cc @@ -494,8 +494,7 @@ if (elt_dir) { - int count; - string_vector names = get_fcn_file_names (count, elt_dir, 0); + string_vector names = get_fcn_file_names (elt_dir); output_buf << "\n*** function files in " << make_absolute (elt_dir, the_current_working_directory)
--- a/src/utils.cc +++ b/src/utils.cc @@ -60,6 +60,7 @@ #include "str-vec.h" #include "defun.h" +#include "dir-ops.h" #include "dirfns.h" #include "error.h" #include "gripes.h" @@ -71,7 +72,6 @@ #include "pager.h" #include "pathsearch.h" #include "sysdep.h" -#include "sysdir.h" #include "toplev.h" #include "unwind-prot.h" #include "user-prefs.h" @@ -315,58 +315,54 @@ } string_vector -get_fcn_file_names (int& num, const char *dir, int no_suffix) +get_fcn_file_names (const string& name, int no_suffix) { - static int num_max = 256; - string_vector retval (num_max); - int i = 0; + string_vector retval; + + dir_entry dir (name); + + if (dir) + { + string_vector tmp = dir.read (); - DIR *dirp = opendir (dir); - if (dirp) - { - struct dirent *entry; - while ((entry = readdir (dirp)) != 0) + int max_len = tmp.length (); + + retval.resize (max_len); + + int k = 0; + int i; + for (i = 0; i < max_len; i++) { - int len = NLENGTH (entry); + string entry = tmp[i]; + + int len = entry.length (); + #if defined (WITH_DYNAMIC_LINKING) if ((len > 2 - && entry->d_name[len-2] == '.' - && entry->d_name[len-1] == 'm') + && entry[len-2] == '.' && entry[len-1] == 'm') || (len > 4 - && entry->d_name[len-4] == '.' - && entry->d_name[len-3] == 'o' - && entry->d_name[len-2] == 'c' - && entry->d_name[len-1] == 't')) + && entry[len-4] == '.' && entry[len-3] == 'o' + && entry[len-2] == 'c' && entry[len-1] == 't')) #else if (len > 2 - && entry->d_name[len-2] == '.' - && entry->d_name[len-1] == 'm') + && entry[len-2] == '.' && entry[len-1] == 'm') #endif { - retval[i] = entry->d_name; if (no_suffix) { - if (retval[i][len-1] == 'm') - retval[i][len-2] = '\0'; + if (entry[len-1] == 'm') + entry.resize (len-2); else - retval[i][len-4] = '\0'; + entry.resize (len-4); } - i++; - - if (i == num_max - 1) - { - num_max += 256; - retval.resize (num_max); - } + retval[k++] = entry; } } - closedir (dirp); + + retval.resize (i); } - num = i; - retval.resize (num); - return retval; } @@ -390,13 +386,13 @@ if (elt_dir) { - int tmp_num; - string_vector names - = get_fcn_file_names (tmp_num, elt_dir, no_suffix); + string_vector names = get_fcn_file_names (elt_dir, no_suffix); + + int tmp_num = names.length (); - if (i + tmp_num >= num_max - 1) + if (i + tmp_num > num_max) { - num_max += 1024; + num_max += tmp_num; retval.resize (num_max); }
--- a/src/utils.h +++ b/src/utils.h @@ -61,10 +61,9 @@ const string& s, int min_toks_to_match, int max_toks); -extern string_vector get_fcn_file_names (int& ffl_len, const char *dir, - int no_suffix); +extern string_vector get_fcn_file_names (const string&, int = 0); -extern string_vector get_fcn_file_names (int& ffl_len, int no_suffix); +extern string_vector get_fcn_file_names (int& ffl_len, int = 0); extern int NINT (double x); extern double D_NINT (double x);