changeset 21490:243b04c97b56

textscan: Use references rather than pointers to stream objects * textscan.cc, textscan.h (textscan::scan, textscan::do_scan): Accept a reference to a std::istream rather than a pointer. (Ftextscan): Pass streams by reference.
author Mike Miller <mtmiller@octave.org>
date Fri, 18 Mar 2016 16:36:40 -0700
parents ea81c2fdd568
children ab0a19882615
files libinterp/corefcn/textscan.cc libinterp/corefcn/textscan.h
diffstat 2 files changed, 18 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/corefcn/textscan.cc	Fri Mar 18 22:52:28 2016 +0000
+++ b/libinterp/corefcn/textscan.cc	Fri Mar 18 16:36:40 2016 -0700
@@ -1237,7 +1237,7 @@
 { }
 
 octave_value
-textscan::scan (std::istream *isp, const octave_value_list& args)
+textscan::scan (std::istream& isp, const octave_value_list& args)
 {
   std::string format;
   int params = 0;
@@ -1283,14 +1283,11 @@
 }
 
 octave_value
-textscan::do_scan (std::istream *isp, textscan_format_list& fmt_list,
+textscan::do_scan (std::istream& isp, textscan_format_list& fmt_list,
                    octave_idx_type ntimes)
 {
   octave_value retval;
 
-  if (! isp)
-    error ("internal error: textscan called with invalid istream");
-
   if (fmt_list.num_conversions () == -1)
     error ("textscan: invalid format specified");
 
@@ -1299,8 +1296,8 @@
 
   // skip the first  header_lines
   std::string dummy;
-  for (int i = 0; i < header_lines && *isp; i++)
-    getline (*isp, dummy, static_cast<char> (eol2));
+  for (int i = 0; i < header_lines && isp; i++)
+    getline (isp, dummy, static_cast<char> (eol2));
 
   // Create our own buffered stream, for fast get/putback/tell/seek.
 
@@ -1319,7 +1316,7 @@
       buf_size = std::max (buf_size, ntimes);
     }
   // Finally, create the stream.
-  delimited_stream is (*isp, whitespace + delims, max_lookahead, buf_size);
+  delimited_stream is (isp, whitespace + delims, max_lookahead, buf_size);
 
   // Grow retval dynamically.  "size" is half the initial size
   // (FIXME -- Should we start smaller if ntimes is large?)
@@ -1406,12 +1403,12 @@
 
   // If file does not end in EOL, do not pad columns with NaN.
   bool uneven_columns = false;
-  if (isp->eof () || (err & 4))
+  if (isp.eof () || (err & 4))
     {
-      isp->clear ();
-      isp->seekg (-1, std::ios_base::end);
-      int last_char = isp->get ();
-      isp->setstate (isp->eofbit);
+      isp.clear ();
+      isp.seekg (-1, std::ios_base::end);
+      int last_char = isp.get ();
+      isp.setstate (isp.eofbit);
       uneven_columns = (last_char != eol1 && last_char != eol2);
     }
 
@@ -3039,7 +3036,7 @@
     {
       std::istringstream is (args(0).string_value ());
 
-      retval(0) = tscanner.scan (&is, tmp_args);
+      retval(0) = tscanner.scan (is, tmp_args);
 
       std::ios::iostate state = is.rdstate ();
       is.clear ();
@@ -3049,8 +3046,11 @@
   else
     {
       octave_stream os = octave_stream_list::lookup (args(0), "textscan");
-
-      retval(0) = tscanner.scan (os.input_stream (), tmp_args);
+      std::istream *isp = os.input_stream ();
+      if (! isp)
+        error ("internal error: textscan called with invalid istream");
+
+      retval(0) = tscanner.scan (*isp, tmp_args);
 
       // FIXME -- warn if stream is not opened in binary mode?
       std::ios::iostate state = os.input_stream ()->rdstate ();
--- a/libinterp/corefcn/textscan.h	Fri Mar 18 22:52:28 2016 +0000
+++ b/libinterp/corefcn/textscan.h	Fri Mar 18 16:36:40 2016 -0700
@@ -61,7 +61,7 @@
 
   ~textscan (void) { }
 
-  octave_value scan (std::istream *isp, const octave_value_list& args);
+  octave_value scan (std::istream& isp, const octave_value_list& args);
 
 private:
 
@@ -124,7 +124,7 @@
 
   octave_idx_type lines;
 
-  octave_value do_scan (std::istream *isp, textscan_format_list& fmt_list,
+  octave_value do_scan (std::istream& isp, textscan_format_list& fmt_list,
                         octave_idx_type ntimes);
 
   void parse_options (const octave_value_list& args,