changeset 21506:128703a29388

avoid some duplicate code in file-io functions * file-io.cc (Ffscanf, Fsscanf): Share code for extracting stream and format from function arguments.
author John W. Eaton <jwe@octave.org>
date Sat, 19 Mar 2016 14:12:10 -0400
parents 7f7d7cb73e0d
children 2cdbae31c022
files libinterp/corefcn/file-io.cc
diffstat 1 files changed, 19 insertions(+), 30 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/corefcn/file-io.cc	Sat Mar 19 18:05:44 2016 +0000
+++ b/libinterp/corefcn/file-io.cc	Sat Mar 19 14:12:10 2016 -0400
@@ -938,8 +938,12 @@
   if (nargin == 0)
     print_usage ();
 
+  // We don't use octave_ostrstream::create here because need direct
+  // access to the OSTR object so that we can extract a string object
+  // from it to return.
   octave_ostrstream *ostr = new octave_ostrstream ();
 
+  // The octave_stream destructor will delete OSTR for us.
   octave_stream os (ostr);
 
   if (! os.is_valid ())
@@ -1033,22 +1037,17 @@
 
   octave_value_list retval;
 
+  octave_stream os = octave_stream_list::lookup (args(0), who);
+
+  if (! args(1).is_string ())
+    error ("%s: format TEMPLATE must be a string", who.c_str ());
+
   if (nargin == 3 && args(2).is_string ())
     {
-      octave_stream os = octave_stream_list::lookup (args(0), who);
-
-      if (! args(1).is_string ())
-        error ("%s: format TEMPLATE must be a string", who.c_str ());
-
       retval = ovl (os.oscanf (args(1), who));
     }
   else
     {
-      octave_stream os = octave_stream_list::lookup (args(0), who);
-
-      if (! args(1).is_string ())
-        error ("%s: format must be a string", who.c_str ());
-
       octave_idx_type count = 0;
 
       Array<double> size = (nargin == 3)
@@ -1101,32 +1100,22 @@
 
   octave_value_list retval;
 
+  std::string data = get_sscanf_data (args(0));
+
+  octave_stream os = octave_istrstream::create (data);
+
+  if (! os.is_valid ())
+    error ("%s: unable to create temporary input buffer", who.c_str ());
+
+  if (! args(1).is_string ())
+    error ("%s: format TEMPLATE must be a string", who.c_str ());
+
   if (nargin == 3 && args(2).is_string ())
     {
-      std::string data = get_sscanf_data (args(0));
-
-      octave_stream os = octave_istrstream::create (data);
-
-      if (! os.is_valid ())
-        error ("%s: unable to create temporary input buffer", who.c_str ());
-
-      if (! args(1).is_string ())
-        error ("%s: format TEMPLATE must be a string", who.c_str ());
-
       retval = ovl (os.oscanf (args(1), who));
     }
   else
     {
-      std::string data = get_sscanf_data (args(0));
-
-      octave_stream os = octave_istrstream::create (data);
-
-      if (! os.is_valid ())
-        error ("%s: unable to create temporary input buffer", who.c_str ());
-
-      if (! args(1).is_string ())
-        error ("%s: format TEMPLATE must be a string", who.c_str ());
-
       octave_idx_type count = 0;
 
       Array<double> size = (nargin == 3) ? args(2).vector_value ()