octave-maintainers
[Top][All Lists]
Advanced

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

Re: rat and rats ported from octave-forge


From: John W. Eaton
Subject: Re: rat and rats ported from octave-forge
Date: Thu, 26 Jul 2007 14:04:39 -0400

On 26-Jul-2007, David Bateman wrote:

| This version of the patch should do it, though there might be a better way..

I checked in this variation instead.  It avoids resizing the
string_vector object multiple times (might be bad for large arrays),
requires the input to be numeric and uses unwind_protect to handle
resetting the global values.

jwe


Index: src/pr-output.cc
===================================================================
RCS file: /cvs/octave/src/pr-output.cc,v
retrieving revision 1.153
diff -u -u -r1.153 pr-output.cc
--- src/pr-output.cc    23 Jul 2007 22:05:30 -0000      1.153
+++ src/pr-output.cc    26 Jul 2007 18:01:08 -0000
@@ -55,6 +55,7 @@
 #include "pager.h"
 #include "pr-output.h"
 #include "sysdep.h"
+#include "unwind-prot.h"
 #include "utils.h"
 #include "variables.h"
 
@@ -2751,28 +2752,66 @@
 @end deftypefn")
 {
   octave_value retval;
+
   int nargin = args.length ();
 
+  unwind_protect::begin_frame ("Frats");
+
+  unwind_protect_int (rat_string_len);
+
   rat_string_len = 9;
+
   if (nargin == 2)
     rat_string_len = args(1).nint_value ();
 
-  if (!error_state)
+  if (! error_state)
     {
       if (nargin < 3 && nargout < 2)
        {
-         bool save_rat_format = rat_format;
-         rat_format = true;
-         std::ostringstream buf;
-         args(0).print (buf);
-         retval = buf.str ();
-         rat_format = save_rat_format;
+         octave_value arg = args(0);
+
+         if (arg.is_numeric_type ())
+           {
+             unwind_protect_bool (rat_format);
+
+             rat_format = true;
+
+             std::ostringstream buf;
+             args(0).print (buf);
+             std::string s = buf.str ();
+
+             std::list<std::string> lst;
+
+             size_t n = 0;
+             size_t s_len = s.length ();
+
+             while (n < s_len)
+               {
+                 size_t m = s.find ('\n',  n);
+
+                 if (m == NPOS)
+                   {
+                     lst.push_back (s.substr (n));
+                     break;
+                   }
+                 else
+                   {
+                     lst.push_back (s.substr (n, m - n));
+                     n = m + 1;
+                   }
+               }
+
+             retval = string_vector (lst);
+           }
+         else
+           error ("rats: expecting numeric input");
        }
       else
        print_usage ();
     }
 
-  rat_string_len = -1;
+  unwind_protect::run_frame ("Frats");
+
   return retval;
 }
 


reply via email to

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