changeset 13194:3e1871badab9

allow sscanf to accept character arrays with more than one row * file-io.cc (get_sscanf_data): New function. Flatten character arrays before extracting character data. (Fsscanf): Use it. * test_io.m: New sscanf test.
author John W. Eaton <jwe@octave.org>
date Mon, 19 Sep 2011 16:45:31 -0400
parents a00ff5cedb9b
children 08650b6fbf67
files scripts/plot/surface.m src/file-io.cc test/test_io.m
diffstat 3 files changed, 29 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/plot/surface.m	Thu Sep 22 17:08:49 2011 -0400
+++ b/scripts/plot/surface.m	Mon Sep 19 16:45:31 2011 -0400
@@ -80,9 +80,12 @@
     z = varargin{3};
     c = varargin{4};
 
-    if (! size_equal (z, c))
+    [z_nr, z_nc] = size (z);
+    [c_nr, c_nc, c_np] = size (c);
+    if (! (z_nr == c_nr && z_nc == c_nc && (c_np == 1 || c_np == 3)))
       error ("surface: Z and C must have the same size");
     endif
+
     if (isvector (x) && isvector (y) && ismatrix (z))
       if (rows (z) == length (y) && columns (z) == length (x))
         x = x(:)';
--- a/src/file-io.cc	Thu Sep 22 17:08:49 2011 -0400
+++ b/src/file-io.cc	Mon Sep 19 16:45:31 2011 -0400
@@ -1174,6 +1174,23 @@
   return retval;
 }
 
+static std::string
+get_sscanf_data (const octave_value& val)
+{
+  std::string retval;
+
+  if (val.is_string ())
+    {
+      octave_value tmp = val.reshape (dim_vector (1, val.numel ()));
+
+      retval = tmp.string_value ();
+    }
+  else
+    ::error ("sscanf: argument STRING must be a string");
+
+  return retval;
+}
+
 DEFUN (sscanf, args, ,
   "-*- texinfo -*-\n\
 @deftypefn  {Built-in Function} {[@var{val}, @var{count}, @var{pos}] =} sscanf (@var{string}, @var{template}, @var{size})\n\
@@ -1194,10 +1211,10 @@
 
   if (nargin == 3 && args(2).is_string ())
     {
-      if (args(0).is_string ())
+      std::string data = get_sscanf_data (args(0));
+
+      if (! error_state)
         {
-          std::string data = args(0).string_value ();
-
           octave_stream os = octave_istrstream::create (data);
 
           if (os.is_valid ())
@@ -1223,10 +1240,10 @@
           retval(1) = 0.0;
           retval(0) = Matrix ();
 
-          if (args(0).is_string ())
+          std::string data = get_sscanf_data (args(0));
+
+          if (! error_state)
             {
-              std::string data = args(0).string_value ();
-
               octave_stream os = octave_istrstream::create (data);
 
               if (os.is_valid ())
@@ -1263,8 +1280,6 @@
                 ::error ("%s: unable to create temporary input buffer",
                          who.c_str  ());
             }
-          else
-            ::error ("%s: argument STRING must be a string", who.c_str ());
         }
       else
         print_usage ();
--- a/test/test_io.m	Thu Sep 22 17:08:49 2011 -0400
+++ b/test/test_io.m	Mon Sep 19 16:45:31 2011 -0400
@@ -245,6 +245,8 @@
 %!assert (sscanf ('123456', '%10c'), '123456')
 %!assert (sscanf ('123456', '%10s'), '123456')
 
+%!assert (sscanf (['ab'; 'cd'], '%s'), 'acbd');
+
 %!test
 %! [val, count, msg, pos] = sscanf ("3I2", "%f");
 %! assert (val, 3);