octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Change of current context ? (whos -file ...)


From: David Bateman
Subject: Re: Change of current context ? (whos -file ...)
Date: Mon, 22 Sep 2008 21:54:40 +0200
User-agent: Thunderbird 2.0.0.16 (X11/20080725)

John W. Eaton wrote:
OK, can you send me your preliminary changes as a diff that I could
apply to look at the scoping problem?  What is the symptom of the
problem?  How does it fail?


I believe the changset attached is correct, though you might want to check that I got the scoping correct. In any case


a = randn(2,2)
save file.mat a
clear a
b = 1
global c
c = randn(3,2)
whos -file file.mat
a
b
c

returns what I'd expect it to return.

Regards
David
# HG changeset patch
# User David Bateman <address@hidden>
# Date 1222113229 -7200
# Node ID 6ee98d2955b0926225632939a39e7ee4ec906802
# Parent  4855ea64d1d5afefc1e3feb7510e428c377c52af
Add -file option to who/whos

diff --git a/src/ChangeLog b/src/ChangeLog
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,3 +1,9 @@ 2008-09-19  David Bateman  <address@hidden
+2008-09-22  David Bateman  <address@hidden>
+
+       * variables.cc (static octave_value do_who (int, const string_vector&,
+       bool, bool, std::string): Add final message argument and simple
+       treatment of the "-file" option.
+
 2008-09-19  David Bateman  <address@hidden>
 
        * debug.cc (static octave_user_code * get_user_code 
diff --git a/src/variables.cc b/src/variables.cc
--- a/src/variables.cc
+++ b/src/variables.cc
@@ -1631,7 +1631,7 @@ private:
 
 static octave_value
 do_who (int argc, const string_vector& argv, bool return_list,
-       bool verbose = false)
+       bool verbose = false, std::string msg = std::string ())
 {
   octave_value retval;
 
@@ -1644,7 +1644,48 @@ do_who (int argc, const string_vector& a
   for (i = 1; i < argc; i++)
     {
       if (argv[i] == "-file")
-       error ("%s: `-file' option not implemented", my_name.c_str ());
+       {
+         // FIXME. This is an inefficient manner to implement this as the 
+         // variables are loaded in to a temporary context and then treated.
+         // It would be better to refecat symbol_info_list to not store the
+         // symbol records and then use it in load-save.cc (do_load) to
+         // implement this option there so that the variables are never 
+         // stored at all.
+         if (i == argc - 1)
+           error ("whos: -file argument must be followed by a file name");
+         else
+           {
+             std::string nm = argv [i + 1];
+
+             symbol_table::scope_id tmp_scope = symbol_table::alloc_scope ();
+
+             unwind_protect::begin_frame ("do_who_file");
+
+             symbol_table::push_scope (tmp_scope);
+             symbol_table::push_context ();
+             octave_call_stack::push (0);
+
+             unwind_protect::add (octave_call_stack::unwind_pop, 0);
+
+             unwind_protect::add (symbol_table::clear_variables);
+
+             feval ("load", octave_value (nm), 0);
+
+             if (! error_state)
+               {
+                 std::string newmsg = std::string ("Variables in the file ") + 
+                   nm + ":\n\n";
+
+                 retval =  do_who (i, argv, return_list, verbose, newmsg);
+               }
+
+             unwind_protect::run_frame ("do_who_file");
+
+             symbol_table::erase_scope (tmp_scope);
+           }
+
+         return retval;
+       }
       else if (argv[i] == "-regexp")
        have_regexp = true;
       else if (argv[i] == "global")
@@ -1763,10 +1804,13 @@ do_who (int argc, const string_vector& a
     }
   else if (! (symbol_stats.empty () && symbol_names.empty ()))
     {
-      if (global_only)
-       octave_stdout << "Global variables:\n\n";
-      else
-       octave_stdout << "Variables in the current scope:\n\n";
+      if (msg.length () == 0)
+       if (global_only)
+         octave_stdout << "Global variables:\n\n";
+       else
+         octave_stdout << "Variables in the current scope:\n\n";
+      else
+       octave_stdout << msg;
 
       if (verbose)
        symbol_stats.display (octave_stdout);
@@ -1798,6 +1842,9 @@ The patterns are considered as regular e
 The patterns are considered as regular expressions and will be used\n\
 for matching the variables to display. The same pattern syntax as for\n\
 the @code{regexp} function is used.\n\
address@hidden -file\n\
+The following argument is treated as a filename, and the variables that\n\
+are found within this file are listed.\n\
 @end table\n\
 \n\
 Valid patterns are the same as described for the @code{clear} command\n\

reply via email to

[Prev in Thread] Current Thread [Next in Thread]