changeset 2493:8ed4362aa0d6

[project @ 1996-11-11 02:35:04 by jwe]
author jwe
date Mon, 11 Nov 1996 02:39:26 +0000
parents 06595bc7f2d0
children 902f330e9584
files liboctave/Array-b.cc liboctave/ChangeLog liboctave/Makefile.in liboctave/chMatrix.cc liboctave/chMatrix.h liboctave/oct-glob.cc liboctave/oct-glob.h liboctave/str-vec.cc liboctave/str-vec.h src/ov-base.cc src/ov-base.h src/ov-str-mat.cc src/ov-str-mat.h src/ov.h src/sysdep.cc src/sysdep.h
diffstat 16 files changed, 257 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/liboctave/Array-b.cc	Mon Nov 11 02:39:26 1996 +0000
@@ -0,0 +1,40 @@
+/*
+
+Copyright (C) 1996 John W. Eaton
+
+This file is part of Octave.
+
+Octave is free software; you can redistribute it and/or modify it
+under the terms of the GNU General Public License as published by the
+Free Software Foundation; either version 2, or (at your option) any
+later version.
+
+Octave is distributed in the hope that it will be useful, but WITHOUT
+ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+for more details.
+
+You should have received a copy of the GNU General Public License
+along with Octave; see the file COPYING.  If not, write to the Free
+Software Foundation, 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+
+*/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+// Instantiate Arrays of bool values.
+
+#include "Array.h"
+#include "Array.cc"
+
+template class Array<bool>;
+
+template void assign (Array<bool>&, const Array<bool>&);
+
+/*
+;;; Local Variables: ***
+;;; mode: C++ ***
+;;; End: ***
+*/
--- a/liboctave/ChangeLog	Sat Nov 09 00:13:54 1996 +0000
+++ b/liboctave/ChangeLog	Mon Nov 11 02:39:26 1996 +0000
@@ -1,3 +1,18 @@
+Sun Nov 10 17:09:24 1996  John W. Eaton  <jwe@bevo.che.wisc.edu>
+
+	* str-vec.h, str-vec.cc: Add constructors to make string vectors
+	from vectors of C strings.
+
+	* oct-glob.h, oct-glob.cc (glob_match): Allow pat to be a string
+	vector.
+	(glob_match::match): Allow match string to be a string vector.
+	(glob_match::glob): New function.
+
+	* chMatrix.cc (charMatrix::row_as_string): New arg, strip_ws.
+
+	* Array-b.cc: New file.
+	* Makefile.in (TI_SRC): Add it to the list.
+
 Fri Nov  8 18:09:12 1996  John W. Eaton  <jwe@bevo.che.wisc.edu>
 
 	* file-ops.cc: Change #include "" to #include <> for safe-lstat.h
--- a/liboctave/Makefile.in	Sat Nov 09 00:13:54 1996 +0000
+++ b/liboctave/Makefile.in	Mon Nov 11 02:39:26 1996 +0000
@@ -47,8 +47,8 @@
 TEMPLATE_SRC := Array.cc Array2.cc Array3.cc DiagArray2.cc \
 	MArray.cc MArray2.cc MDiagArray2.cc base-lu.cc
 
-TI_SRC := Array-C.cc Array-ch.cc Array-i.cc Array-d.cc Array-s.cc \
-	Array-str.cc \
+TI_SRC := Array-C.cc Array-b.cc Array-ch.cc Array-i.cc Array-d.cc \
+	Array-s.cc Array-str.cc \
 	MArray-C.cc MArray-ch.cc MArray-i.cc MArray-d.cc MArray-s.cc
 
 MATRIX_SRC := Array-flags.cc CColVector.cc CDiagMatrix.cc CMatrix.cc \
--- a/liboctave/chMatrix.cc	Sat Nov 09 00:13:54 1996 +0000
+++ b/liboctave/chMatrix.cc	Mon Nov 11 02:39:26 1996 +0000
@@ -115,7 +115,7 @@
 }
 
 string
-charMatrix::row_as_string (int r) const
+charMatrix::row_as_string (int r, bool strip_ws = false) const 
 {
   if (r < 0 || r >= rows ())
     {
@@ -130,9 +130,21 @@
   for (int i = 0; i < nc; i++)
     retval[i] = elem (r, i);
 
-  while (--nc >= 0)
-    if (retval[nc])
-      break;
+  if (strip_ws)
+    {
+      while (--nc >= 0)
+	{
+	  char c = retval[nc];
+	  if (c && c != ' ')
+	    break;
+	}
+    }
+  else
+    {
+      while (--nc >= 0)
+	if (retval[nc])
+	  break;
+    }
 
   retval.resize (nc+1);
 
--- a/liboctave/chMatrix.h	Sat Nov 09 00:13:54 1996 +0000
+++ b/liboctave/chMatrix.h	Mon Nov 11 02:39:26 1996 +0000
@@ -67,7 +67,7 @@
   charMatrix& insert (const char *s, int r, int c);
   charMatrix& insert (const charMatrix& a, int r, int c);
 
-  string row_as_string (int r) const;
+  string row_as_string (int r, bool strip_trailing_whitespace = false) const;
 
 #if 0
   Matrix& insert (const RowVector& a, int r, int c);
--- a/liboctave/oct-glob.cc	Sat Nov 09 00:13:54 1996 +0000
+++ b/liboctave/oct-glob.cc	Mon Nov 11 02:39:26 1996 +0000
@@ -25,15 +25,107 @@
 #endif
 
 #include <fnmatch.h>
+#include <glob.h>
+
+#include <iostream.h>
 
 #include <string>
 
+#include "file-ops.h"
 #include "oct-glob.h"
+#include "str-vec.h"
 
 bool
-glob_match::match (const string& s)
+glob_match::match (const string& s, match_type mt)
+{
+  int npat = pat.length ();
+
+  const char *str = s.c_str ();
+
+  if (mt == all)
+    {
+      for (int i = 0; i < npat; i++)
+	if (fnmatch (pat(i).c_str (), str, flags) == FNM_NOMATCH)
+	  return false;
+
+      return true;
+    }
+  else
+    {
+      for (int i = 0; i < npat; i++)
+	if (fnmatch (pat(i).c_str (), str, flags) != FNM_NOMATCH)
+	  return true;
+
+      return false;
+    }
+}
+
+Array<bool>
+glob_match::match (const string_vector& s, match_type mt)
+{
+  int n = s.length ();
+
+  Array<bool> retval (n);
+
+  for (int i = 0; i < n; i++)
+    retval(i) = match (s[i], mt);
+
+  return retval;
+}
+
+static bool
+single_match_exists (const string& file)
 {
-  return fnmatch (pat.c_str (), s.c_str (), flags) != FNM_NOMATCH;
+  file_stat s (file);
+
+  return s.exists ();
+}
+
+string_vector
+glob_match::glob (void)
+{
+  string_vector retval;
+
+  int npat = pat.length ();
+
+  int k = 0;
+
+  for (int i = 0; i < npat; i++)
+    {
+      string xpat = pat(i);
+
+      if (! xpat.empty ())
+	{
+	  glob_t glob_info;
+
+	  int err = ::glob (xpat.c_str (), GLOB_NOSORT, 0, &glob_info);
+
+	  if (! err)
+	    {
+	      int n = glob_info.gl_pathc;
+
+	      char **matches = glob_info.gl_pathv;
+
+	      // XXX FIXME XXX -- we shouldn't have to check to see if
+	      // a single match exists, but it seems that glob() won't
+	      // check for us unless the pattern contains globbing
+	      // characters.  Hmm.
+
+	      if (n > 1
+		  || (n == 1 && single_match_exists (string (matches[0]))))
+		{
+		  retval.resize (k+n);
+
+		  for (int j = 0; j < n; j++)
+		    retval[k++] = matches[j];
+		}
+
+	      globfree (&glob_info);
+	    }
+	}
+    }
+
+  return retval.qsort ();
 }
 
 /*
@@ -41,3 +133,4 @@
 ;;; mode: C++ ***
 ;;; End: ***
 */
+
--- a/liboctave/oct-glob.h	Sat Nov 09 00:13:54 1996 +0000
+++ b/liboctave/oct-glob.h	Mon Nov 11 02:39:26 1996 +0000
@@ -25,6 +25,9 @@
 
 #include <string>
 
+#include "Array.h"
+#include "str-vec.h"
+
 class
 glob_match
 {
@@ -37,10 +40,20 @@
       period = 4     // Leading `.' is matched only explicitly.
    };
 
+  enum match_type
+    {
+      any = 2,  // Match any pattern.
+      all = 2   // Must match all patterns.
+    };
+
   glob_match (const string& p = string (),
 	      unsigned int f = pathname|noescape|period)
     : pat (p), flags (f) { }
 
+  glob_match (const string_vector& p = string_vector (),
+	      unsigned int f = pathname|noescape|period)
+    : pat (p), flags (f) { }
+
   glob_match (const glob_match& gm) : pat (gm.pat), flags (gm.flags) { }
 
   glob_match& operator = (const glob_match& gm)
@@ -57,12 +70,18 @@
 
   void set_pattern (const string& p) { pat = p; }
 
-  bool match (const string&);
+  void set_pattern (const string_vector& p) { pat = p; }
+
+  bool match (const string&, match_type mt = any);
+
+  Array<bool> match (const string_vector&, match_type mt = any);
+
+  string_vector glob (void);
 
 private:
 
-  // Globbing pattern.
-  string pat;
+  // Globbing pattern(s).
+  string_vector pat;
 
   // Option flags.
   unsigned int flags;
--- a/liboctave/str-vec.cc	Sat Nov 09 00:13:54 1996 +0000
+++ b/liboctave/str-vec.cc	Mon Nov 11 02:39:26 1996 +0000
@@ -31,6 +31,32 @@
 #include "oct-term.h"
 #include "str-vec.h"
 
+// Create a string vector from a NULL terminated list of C strings.
+
+string_vector::string_vector (const char * const *s)
+  : Array<string> ()
+{
+  int n = 0;
+
+  while (*s++)
+    n++;
+
+  resize (n);
+
+  for (int i = 0; i < n; i++)
+    elem (i) = s[i];
+}
+
+// Create a string vector from up to N C strings.  Assumes that N is
+// nonnegative.
+
+string_vector::string_vector (const char * const *s, int n)
+  : Array<string> (n)
+{
+  for (int i = 0; i < n; i++)
+    elem (i) = s[i];
+}
+
 // Format a list in neat columns.  Mostly stolen from GNU ls.
 
 ostream&
--- a/liboctave/str-vec.h	Sat Nov 09 00:13:54 1996 +0000
+++ b/liboctave/str-vec.h	Mon Nov 11 02:39:26 1996 +0000
@@ -53,6 +53,10 @@
 
   string_vector (const string_vector& s) : Array<string> (s) { }
 
+  string_vector (const char * const *s);
+
+  string_vector (const char * const *s, int n);
+
   string_vector& operator = (const string_vector& s)
     {
       if (this != &s)
--- a/src/ov-base.cc	Sat Nov 09 00:13:54 1996 +0000
+++ b/src/ov-base.cc	Mon Nov 11 02:39:26 1996 +0000
@@ -145,10 +145,10 @@
   return retval;
 }
 
-charMatrix
+string_vector
 octave_base_value::all_strings (void) const
 {
-  charMatrix retval;
+  string_vector retval;
   gripe_wrong_type_arg ("octave_base_value::all_strings()", type_name ());
   return retval;
 }
--- a/src/ov-base.h	Sat Nov 09 00:13:54 1996 +0000
+++ b/src/ov-base.h	Mon Nov 11 02:39:26 1996 +0000
@@ -142,7 +142,7 @@
 
   charMatrix char_matrix_value (bool frc_str_conv = false) const;
 
-  charMatrix all_strings (void) const;
+  string_vector all_strings (void) const;
 
   string string_value (void) const;
 
--- a/src/ov-str-mat.cc	Sat Nov 09 00:13:54 1996 +0000
+++ b/src/ov-str-mat.cc	Mon Nov 11 02:39:26 1996 +0000
@@ -189,10 +189,17 @@
   return retval;
 }
 
-charMatrix
+string_vector
 octave_char_matrix_str::all_strings (void) const
 {
-  return matrix;
+  int n = matrix.rows ();
+
+  string_vector retval (n);
+
+  for (int i = 0; i < n; i++)
+    retval[i] = matrix.row_as_string (i, true);
+
+  return retval;
 }
 
 string
--- a/src/ov-str-mat.h	Sat Nov 09 00:13:54 1996 +0000
+++ b/src/ov-str-mat.h	Mon Nov 11 02:39:26 1996 +0000
@@ -98,7 +98,7 @@
 
   Matrix matrix_value (bool = false) const;
 
-  charMatrix all_strings (void) const;
+  string_vector all_strings (void) const;
 
   string string_value (void) const;
 
--- a/src/ov.h	Sat Nov 09 00:13:54 1996 +0000
+++ b/src/ov.h	Mon Nov 11 02:39:26 1996 +0000
@@ -315,7 +315,7 @@
   virtual charMatrix char_matrix_value (bool frc_str_conv = false) const
     { return rep->char_matrix_value (frc_str_conv); }
 
-  virtual charMatrix all_strings (void) const
+  virtual string_vector all_strings (void) const
     { return rep->all_strings (); }
 
   virtual string string_value (void) const
--- a/src/sysdep.cc	Sat Nov 09 00:13:54 1996 +0000
+++ b/src/sysdep.cc	Mon Nov 11 02:39:26 1996 +0000
@@ -554,6 +554,26 @@
   return retval;
 }
 
+// A vector version of the above.
+
+string_vector
+oct_tilde_expand (const string_vector& names)
+{
+  string_vector retval;
+
+  if (! error_state)
+    {
+      int n = names.length ();
+
+      retval.resize (n);
+
+      for (int i = 0; i < n; i++)
+	retval[i] = oct_tilde_expand (names[i]);
+    }
+
+  return retval;
+}
+
 DEFUN (tilde_expand, args, ,
   "tilde_expand (STRING): perform tilde expansion on STRING")
 {
@@ -562,7 +582,7 @@
   int nargin = args.length ();
 
   if (nargin == 1)
-    retval = oct_tilde_expand (args(0).string_value ());
+    retval = oct_tilde_expand (args(0).all_strings ());
   else
     print_usage ("tilde_expand");
 
--- a/src/sysdep.h	Sat Nov 09 00:13:54 1996 +0000
+++ b/src/sysdep.h	Mon Nov 11 02:39:26 1996 +0000
@@ -40,6 +40,7 @@
 #endif
 
 extern string oct_tilde_expand (const string&);
+extern string_vector oct_tilde_expand (const string_vector&);
 
 #endif