changeset 7744:14b841c47a5f

handle load/save for handles to built-in functions
author John W. Eaton <jwe@octave.org>
date Wed, 30 Apr 2008 14:32:41 -0400
parents fbe27e477578
children 0ff0fc033f28
files src/ChangeLog src/ls-oct-ascii.cc src/ov-fcn-handle.cc
diffstat 3 files changed, 53 insertions(+), 78 deletions(-) [+]
line wrap: on
line diff
--- a/src/ChangeLog	Wed Apr 30 14:25:31 2008 -0400
+++ b/src/ChangeLog	Wed Apr 30 14:32:41 2008 -0400
@@ -1,10 +1,18 @@
 2008-04-30  John W. Eaton  <jwe@octave.org>
 
+	* ls-oct-ascii.cc (extract_keyword): Return early if first char is
+	not a comment character.
+	(read_ascii_data): Accept .nargin. and .nargout. as valid identifiers.
+
+	* ov-fcn-handle.cc: Combine tests.  Test saving and loading
+	handles for built-in, .oct, and .m functions.
+	(octave_fcn_handle::save_ascii, octave_fcn_handle::save_binary,
+	octave_fcn_handle::save_hdf5, octave_fcn_handle::print):
+	Avoid dereferencing invalid pointer.
+
 	* data.cc: Fix tests.  Use "%!assert", not "%! assert" for
 	individual assert tests.
 
-	* ov-fcn-handle.cc: Create handle pointing to flops instead of log2.
-
 2008-04-30  Jaroslav Hajek <highegg@gmail.com>
 
 	* ov-base.cc, ov-base.h, ov-bool-mat.h, ov-bool-sparse.h,
--- a/src/ls-oct-ascii.cc	Wed Apr 30 14:25:31 2008 -0400
+++ b/src/ls-oct-ascii.cc	Wed Apr 30 14:32:41 2008 -0400
@@ -81,6 +81,10 @@
 {
   std::string retval;
 
+  int ch = is.peek ();
+  if (next_only && ch != '%' && ch != '#')
+    return retval;
+
   char c;
   while (is.get (c))
     {
@@ -255,11 +259,8 @@
       return std::string ();
     }
 
-  if (name == CELL_ELT_TAG)
-    {
-      // This is OK -- name won't be used.
-    }
-  else if (! valid_identifier (name))
+  if (! (name == ".nargin." || name == ".nargout."
+	 || name == CELL_ELT_TAG || valid_identifier (name)))
     {
       error ("load: bogus identifier `%s' found in file `%s'",
 	     name.c_str (), filename.c_str ());
--- a/src/ov-fcn-handle.cc	Wed Apr 30 14:25:31 2008 -0400
+++ b/src/ov-fcn-handle.cc	Wed Apr 30 14:32:41 2008 -0400
@@ -250,8 +250,12 @@
     }
   else
     {
+      octave_function *f = function_value ();
+      std::string fnm = f ? f->fcn_file_name () : std::string ();
+
       os << "# octaveroot: " << OCTAVE_EXEC_PREFIX << "\n";
-      os << "# path: " << user_function_value ()-> fcn_file_name () << "\n";
+      if (! fnm.empty ())
+	os << "# path: " << fnm << "\n";
       os << nm << "\n";
     }
 
@@ -375,32 +379,6 @@
   return success;
 }
 
-/* 
-
-%!test
-%! a = 2;
-%! f = @(x) a + x;
-%! g = @(x) 2 * x;
-%! h = @flops;
-%! f2 = f;
-%! g2 = g;
-%! h2 = h;
-%! nm = tmpnam();
-%! unwind_protect
-%!   save ("-text", nm, "f2", "g2", "h2");
-%!   clear f2 g2 h2
-%!   load (nm);
-%!   assert (f(2),f2(2));
-%!   assert (g(2),g2(2));
-%!   assert (g(3),g2(3));
-%!   unlink (nm);
-%!   save ("-text", nm, "f2", "g2", "h2");
-%! unwind_protect_cleanup
-%!   unlink (nm);
-%! end_unwind_protect
-
-*/
-
 bool
 octave_fcn_handle::save_binary (std::ostream& os, bool& save_as_floats)
 {
@@ -450,8 +428,10 @@
     {
       std::ostringstream nmbuf;
 
-      nmbuf << nm << "\n" << OCTAVE_EXEC_PREFIX << "\n" 
-	    << user_function_value ()-> fcn_file_name () ;
+      octave_function *f = function_value ();
+      std::string fnm = f ? f->fcn_file_name () : std::string ();
+
+      nmbuf << nm << "\n" << OCTAVE_EXEC_PREFIX << "\n" << fnm;
 
       std::string buf_str = nmbuf.str ();
       int32_t tmp = buf_str.length ();
@@ -572,32 +552,6 @@
  return success;
 }
 
-/* 
-
-%!test
-%! a = 2;
-%! f = @(x) a + x;
-%! g = @(x) 2 * x;
-%! h = @flops;
-%! f2 = f;
-%! g2 = g;
-%! h2 = h;
-%! nm = tmpnam();
-%! unwind_protect
-%!   save ("-binary", nm, "f2", "g2", "h2");
-%!   clear f2 g2 h2
-%!   load (nm);
-%!   assert (f(2),f2(2));
-%!   assert (g(2),g2(2));
-%!   assert (g(3),g2(3));
-%!   unlink (nm);
-%!   save ("-binary", nm, "f2", "g2", "h2");
-%! unwind_protect_cleanup
-%!   unlink (nm);
-%! end_unwind_protect
-
-*/
-
 #if defined (HAVE_HDF5)
 bool
 octave_fcn_handle::save_hdf5 (hid_t loc_id, const char *name,
@@ -723,7 +677,9 @@
   else
     {
       std::string octaveroot = OCTAVE_EXEC_PREFIX;
-      std::string fpath = user_function_value ()-> fcn_file_name ();
+
+      octave_function *f = function_value ();
+      std::string fpath = f ? f->fcn_file_name () : std::string ();
 
       H5Sclose (space_hid);
       hdims[0] = 1;
@@ -1112,34 +1068,44 @@
   return success;
 }
 
+#endif
+
 /* 
 
 %!test
+%! a = 2;
+%! f = @(x) a + x;
+%! g = @(x) 2 * x;
+%! hm = @flops;
+%! hdld = @time;
+%! hbi = @log2;
+%! f2 = f;
+%! g2 = g;
+%! hm2 = hm;
+%! hdld2 = hdld;
+%! hbi2 = hbi;
+%! modes = {"-text", "-binary"};
 %! if (!isempty(findstr(octave_config_info ("DEFS"),"HAVE_HDF5")))
-%!   a = 2;
-%!   f = @(x) a + x;
-%!   g = @(x) 2 * x;
-%!   h = @flops;
-%!   f2 = f;
-%!   g2 = g;
-%!   h2 = h;
+%!   modes(end+1) = "-hdf5";
+%! endif
+%! for i = modes
+%!   mode = modes{1};
 %!   nm = tmpnam();
 %!   unwind_protect
-%!     save ("-hdf5", nm, "f2", "g2", "h2");
-%!     clear f2 g2 h2
+%!     save (mode, nm, "f2", "g2", "hm2", "hdld2", "hbi2");
+%!     clear f2 g2 hm2 hdld2 hbi2
 %!     load (nm);
 %!     assert (f(2),f2(2));
 %!     assert (g(2),g2(2));
 %!     assert (g(3),g2(3));
 %!     unlink (nm);
-%!     save ("-hdf5", nm, "f2", "g2", "h2");
+%!     save (mode, nm, "f2", "g2", "hm2", "hdld2", "hbi2");
 %!   unwind_protect_cleanup
 %!     unlink (nm);
 %!   end_unwind_protect
-%! endif
+%! endfor
 
 */
-#endif
 
 void
 octave_fcn_handle::print (std::ostream& os, bool pr_as_read_syntax) const
@@ -1242,7 +1208,7 @@
 
       if (! error_state)
 	{
-	  octave_function *fcn = fh ? fh->function_value (true) : 0;
+	  octave_function *fcn = fh ? fh->function_value () : 0;
 
 	  if (fcn)
 	    {
@@ -1301,9 +1267,9 @@
 			  m.assign ("workspace", ws);
 			}
 		    }
-		  else if (fcn->is_user_function ())
+		  else if (fcn->is_user_function () || fcn->is_user_script ())
 		    {
-		      octave_user_function *fu = fh->user_function_value ();
+		      octave_function *fu = fh->function_value ();
 		      m.assign ("file", fu->fcn_file_name ());
 		    }
 		  else