changeset 4243:7e4d5b5520e5

[project @ 2002-12-27 05:30:59 by jwe]
author jwe
date Fri, 27 Dec 2002 05:30:59 +0000
parents 4d3994172bd5
children 189df16144fc
files src/ChangeLog src/dynamic-ld.cc src/dynamic-ld.h src/ov-cell.cc src/ov-cell.h src/parse.y src/utils.cc src/utils.h
diffstat 8 files changed, 169 insertions(+), 48 deletions(-) [+]
line wrap: on
line diff
--- a/src/ChangeLog	Thu Dec 26 22:06:30 2002 +0000
+++ b/src/ChangeLog	Fri Dec 27 05:30:59 2002 +0000
@@ -1,3 +1,21 @@
+2002-12-26  John W. Eaton  <jwe@bevo.che.wisc.edu>
+
+	* utils.cc (search_path_for_file): Second arg now string_vector.
+	Use find_first_of, not find.
+	(search_path_for_all_files): Second arg now string_vector.
+	Use find_all_first_of, not find_all.
+	(Ffile_in_path): Accept cell array of strings as first argument.
+	(Ffile_in_loadpath): Likewise.
+
+	* dynamic-ld.cc (octave_dynamic_loader::load): New arg, file_name.
+	(octave_dynamic_loader::do_load): Likewise.  If file_name is not
+	empty, use it instead of searching in path.
+
+	* parse.y (load_fcn_from_file): Use find_first_of to perform search.
+
+	* ov-cell.cc (octave_cell::all_strings): New function.
+	* ov-cell.h: Provide decl.
+
 2002-12-25  John W. Eaton  <jwe@bevo.che.wisc.edu>
 
 	* parse.y (function_end): If parsing a nested function, set
--- a/src/dynamic-ld.cc	Thu Dec 26 22:06:30 2002 +0000
+++ b/src/dynamic-ld.cc	Fri Dec 27 05:30:59 2002 +0000
@@ -209,7 +209,8 @@
 }
 
 bool
-octave_dynamic_loader::do_load (const std::string& fcn_name)
+octave_dynamic_loader::do_load (const std::string& fcn_name,
+				const std::string& file_name)
 {
   bool retval = false;
 
@@ -241,7 +242,8 @@
 
       if (! function)
 	{
-	  std::string oct_file_name = oct_file_in_path (fcn_name);
+	  std::string oct_file_name
+	    = file_name.empty () ? oct_file_in_path (fcn_name) : file_name;
 
 	  if (! oct_file_name.empty ())
 	    {
@@ -299,9 +301,10 @@
 }
 
 bool
-octave_dynamic_loader::load (const std::string& fcn_name)
+octave_dynamic_loader::load (const std::string& fcn_name,
+			     const std::string& file_name)
 {
-  return (instance_ok ()) ? instance->do_load (fcn_name) : false;
+  return (instance_ok ()) ? instance->do_load (fcn_name, file_name) : false;
 }
 
 bool
--- a/src/dynamic-ld.h	Thu Dec 26 22:06:30 2002 +0000
+++ b/src/dynamic-ld.h	Fri Dec 27 05:30:59 2002 +0000
@@ -38,7 +38,8 @@
 
   virtual ~octave_dynamic_loader (void) { }
 
-  static bool load (const std::string& fcn_name);
+  static bool load (const std::string& fcn_name,
+		    const std::string& file_name = std::string ());
 
   static bool remove (const std::string& fcn_name, octave_shlib& shl);
 
@@ -54,7 +55,8 @@
 
   static bool instance_ok (void);
 
-  bool do_load (const std::string& fcn_name);
+  bool do_load (const std::string& fcn_name,
+		const std::string& file_name = std::string ());
 
   bool do_remove (const std::string& fcn_name, octave_shlib& shl);
 
--- a/src/ov-cell.cc	Thu Dec 26 22:06:30 2002 +0000
+++ b/src/ov-cell.cc	Fri Dec 27 05:30:59 2002 +0000
@@ -248,6 +248,30 @@
   return retval;
 }
 
+string_vector
+octave_cell::all_strings (void) const
+{
+  int nr = rows ();
+  int nc = columns ();
+
+  string_vector retval (nr * nc);
+
+  int k = 0;
+
+  for (int j = 0; j < nc; j++)
+    {
+      for (int i = 0; i < nr; i++)
+	{
+	  retval[k++] = matrix(i,j).string_value ();
+
+	  if (error_state)
+	    return string_vector ();
+	}
+    }
+
+  return retval;
+}
+
 void
 octave_cell::print (std::ostream& os, bool) const
 {
--- a/src/ov-cell.h	Thu Dec 26 22:06:30 2002 +0000
+++ b/src/ov-cell.h	Fri Dec 27 05:30:59 2002 +0000
@@ -88,6 +88,8 @@
 
   octave_value_list list_value (void) const;
 
+  string_vector all_strings (void) const;
+
   void print (std::ostream& os, bool pr_as_read_syntax = false) const;
 
   void print_raw (std::ostream& os, bool pr_as_read_syntax = false) const;
--- a/src/parse.y	Thu Dec 26 22:06:30 2002 +0000
+++ b/src/parse.y	Fri Dec 27 05:30:59 2002 +0000
@@ -45,11 +45,14 @@
 #include "file-ops.h"
 #include "file-stat.h"
 #include "lo-sstream.h"
+#include "oct-env.h"
 #include "oct-time.h"
 #include "quit.h"
 
 #include "comment-list.h"
+#include "defaults.h"
 #include "defun.h"
+#include "dirfns.h"
 #include "dynamic-ld.h"
 #include "error.h"
 #include "input.h"
@@ -3373,14 +3376,24 @@
 
   std::string nm = sym_rec->name ();
 
-  if (octave_dynamic_loader::load (nm))
+  static string_vector names (2);
+
+  names[0] = nm + ".oct";
+  names[1] = nm + ".m";
+
+  std::string file
+   = octave_env::make_absolute (Vload_path_dir_path.find_first_of (names),
+                                octave_env::getcwd ());
+
+  int len = file.length ();
+
+  if (file.substr (len-4, len-1) == ".oct")
     {
-      force_link_to_function (nm);
+      if (octave_dynamic_loader::load (nm, file))
+        force_link_to_function (nm);
     }
   else
     {
-      std::string ff = fcn_file_in_path (nm);
-
       // These are needed by yyparse.
 
       unwind_protect::begin_frame ("load_fcn_from_file");
@@ -3389,10 +3402,10 @@
       unwind_protect_str (curr_fcn_file_full_name);
 
       curr_fcn_file_name = nm;
-      curr_fcn_file_full_name = ff;
-
-      if (ff.length () > 0)
-	script_file_executed = parse_fcn_file (ff, exec_script);
+      curr_fcn_file_full_name = file;
+
+      if (file.length () > 0)
+	script_file_executed = parse_fcn_file (file, exec_script);
 
       if (! (error_state || script_file_executed))
 	force_link_to_function (nm);
--- a/src/utils.cc	Thu Dec 26 22:06:30 2002 +0000
+++ b/src/utils.cc	Fri Dec 27 05:30:59 2002 +0000
@@ -231,21 +231,22 @@
 // See if the given file is in the path.
 
 std::string
-search_path_for_file (const std::string& path, const std::string& name)
+search_path_for_file (const std::string& path, const string_vector& names)
 {
   dir_path p (path);
 
-  return octave_env::make_absolute (p.find (name), octave_env::getcwd ());
+  return octave_env::make_absolute (p.find_first_of (names),
+				    octave_env::getcwd ());
 }
 
 // Find all locations of the given file in the path.
 
 string_vector
-search_path_for_all_files (const std::string& path, const std::string& name)
+search_path_for_all_files (const std::string& path, const string_vector& names)
 {
   dir_path p (path);
 
-  string_vector sv = p.find_all (name);
+  string_vector sv = p.find_all_first_of (names);
 
   int len = sv.length ();
 
@@ -275,6 +276,10 @@
 the list of directories specified by @code{LOADPATH}.\n\
 If no file is found, return an empty matrix.\n\
 \n\
+If the first argument is a cell array of of strings, search each\n\
+directory of the loadpath for element of the cell array and return\n\
+the first that matches.\n\
+\n\
 If the second optional argument @code{\"all\"} is supplied, return\n\
 a cell array containing the list of all files that have the same\n\
 name in the path.  If no files are found, return an empty cell array.\n\
@@ -283,25 +288,38 @@
 {
   octave_value retval;
 
-  int argc = args.length () + 1;
-
-  string_vector argv = args.make_argv ("file_in_loadpath");
+  int nargin = args.length ();
 
-  if (error_state)
-    return retval;
-
-  if (argc == 2)
+  if (nargin == 1 || nargin == 2)
     {
-      std::string fname
-	= octave_env::make_absolute (Vload_path_dir_path.find (argv[1]),
-				     octave_env::getcwd ());
-      if (fname.empty ())
-	retval = Matrix ();
+      string_vector names = args(0).all_strings ();
+
+      if (! error_state && names.length () > 0)
+	{
+	  if (nargin == 1)
+	    {
+	      std::string fname = octave_env::make_absolute
+		(Vload_path_dir_path.find_first_of (names),
+		 octave_env::getcwd ());
+
+	      if (fname.empty ())
+		retval = Matrix ();
+	      else
+		retval = fname;
+	    }
+	  else if (nargin == 2)
+	    {
+	      std::string opt = args(1).string_value ();
+
+	      if (! error_state && opt == "all")
+		retval = Cell (make_absolute (Vload_path_dir_path.find_all_first_of (names)));
+	      else
+		print_usage ("file_in_loadpath: invalid option");
+	    }
+	}
       else
-	retval = fname;
+	error ("file_in_loadpath: expecting string as first argument");
     }
-  else if (argc == 3 && argv[2] == "all")
-    retval = Cell (make_absolute (Vload_path_dir_path.find_all (argv[1])));
   else
     print_usage ("file_in_loadpath");
 
@@ -323,6 +341,10 @@
      @result{} \"@value{OCTAVEHOME}/share/octave/2.0/m/general/nargchk.m\"\n\
 @end example\n\
 \n\
+If the second argument is a cell array of of strings, search each\n\
+directory of the path for element of the cell array and return\n\
+the first that matches.\n\
+\n\
 If the third optional argument @code{\"all\"} is supplied, return\n\
 a cell array containing the list of all files that have the same\n\
 name in the path.  If no files are found, return an empty cell array.\n\
@@ -330,24 +352,43 @@
 {
   octave_value retval;
 
-  int argc = args.length () + 1;
-
-  string_vector argv = args.make_argv ("file_in_path");
+  int nargin = args.length ();
 
-  if (error_state)
-    return retval;
-
-  if (argc == 3)
+  if (nargin == 2 || nargin == 3)
     {
-      std::string fname = search_path_for_file (argv[1], argv[2]);
+      std::string path = args(0).string_value ();
+
+      if (! error_state)
+	{
+	  string_vector names = args(0).all_strings ();
+
+	  if (! error_state && names.length () > 0)
+	    {
+	      if (nargin == 2)
+		{
+		  std::string fname = search_path_for_file (path, names);
 
-      if (fname.empty ())
-	retval = Matrix ();
+		  if (fname.empty ())
+		    retval = Matrix ();
+		  else
+		    retval = fname;
+		}
+	      else if (nargin == 3)
+		{
+		  std::string opt = args(1).string_value ();
+
+		  if (! error_state && opt == "all")
+		    retval = Cell (make_absolute (search_path_for_all_files (path, names)));
+		  else
+		    print_usage ("file_in_path: invalid option");
+		}
+	    }
+	  else
+	    error ("file_in_path: expecting string as second argument");
+	}
       else
-	retval = fname;
+	error ("file_in_path: expecting string as first argument");
     }
-  else if (argc == 4 && argv[3] == "all")
-    retval = Cell (make_absolute (search_path_for_all_files (argv[1], argv[2])));
   else
     print_usage ("file_in_path");
 
@@ -620,6 +661,24 @@
   return retval;
 }
 
+DEFUN (find_first_of_in_loadpath, args, , "")
+{
+  octave_value retval;
+
+  if (args.length () == 1)
+    {
+      string_vector names = args(0).all_strings ();
+
+      if (! error_state)
+	retval = Vload_path_dir_path.find_all_first_of (names);
+    }
+  else
+    print_usage ("find_first_of_in_loadpath");
+
+  return retval;
+}
+
+
 // #if 0
 
 // Octave could use some way to access the value of ERRNO, but this is
--- a/src/utils.h	Thu Dec 26 22:06:30 2002 +0000
+++ b/src/utils.h	Fri Dec 27 05:30:59 2002 +0000
@@ -48,10 +48,10 @@
 extern int empty_arg (const char *name, int nr, int nc);
 
 extern std::string
-search_path_for_file (const std::string&, const std::string&);
+search_path_for_file (const std::string&, const string_vector&);
 
 extern string_vector
-search_path_for_all_files (const std::string&, const std::string&);
+search_path_for_all_files (const std::string&, const string_vector&);
 
 extern std::string file_in_path (const std::string&, const std::string&);
 extern std::string fcn_file_in_path (const std::string&);