changeset 20981:c11cea70b638

maint: invert if/else/error instances. * input.cc, ls-mat-ascii.cc, ls-mat5.cc, ls-oct-text.cc: Invert if/else/error instances.
author John W. Eaton <jwe@octave.org>
date Fri, 25 Dec 2015 00:36:19 -0500
parents 81c2b14c209f
children d27f66b4b8e6
files libinterp/corefcn/input.cc libinterp/corefcn/ls-mat-ascii.cc libinterp/corefcn/ls-mat5.cc libinterp/corefcn/ls-oct-text.cc
diffstat 4 files changed, 182 insertions(+), 203 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/corefcn/input.cc	Thu Dec 24 13:55:51 2015 -0500
+++ b/libinterp/corefcn/input.cc	Fri Dec 25 00:36:19 2015 -0500
@@ -727,38 +727,36 @@
 
   std::string input_buf = interactive_input (prompt.c_str (), eof);
 
-  if (! input_buf.empty ())
-    {
-      size_t len = input_buf.length ();
+  if (input_buf.empty ())
+    error ("input: reading user-input failed!");
 
-      octave_diary << input_buf;
+  size_t len = input_buf.length ();
 
-      if (input_buf[len - 1] != '\n')
-        octave_diary << "\n";
+  octave_diary << input_buf;
 
-      if (len < 1)
-        return read_as_string ? octave_value ("") : octave_value (Matrix ());
+  if (input_buf[len - 1] != '\n')
+    octave_diary << "\n";
 
-      if (read_as_string)
-        {
-          // FIXME: fix gnu_readline and octave_gets instead!
-          if (input_buf.length () == 1 && input_buf[0] == '\n')
-            retval(0) = "";
-          else
-            retval(0) = input_buf;
-        }
+  if (len < 1)
+    return read_as_string ? octave_value ("") : octave_value (Matrix ());
+
+  if (read_as_string)
+    {
+      // FIXME: fix gnu_readline and octave_gets instead!
+      if (input_buf.length () == 1 && input_buf[0] == '\n')
+        retval(0) = "";
       else
-        {
-          int parse_status = 0;
-
-          retval = eval_string (input_buf, true, parse_status, nargout);
-
-          if (! Vdebugging && retval.length () == 0)
-            retval(0) = Matrix ();
-        }
+        retval(0) = input_buf;
     }
   else
-    error ("input: reading user-input failed!");
+    {
+      int parse_status = 0;
+
+      retval = eval_string (input_buf, true, parse_status, nargout);
+
+      if (! Vdebugging && retval.length () == 0)
+        retval(0) = Matrix ();
+    }
 
   return retval;
 }
--- a/libinterp/corefcn/ls-mat-ascii.cc	Thu Dec 24 13:55:51 2015 -0500
+++ b/libinterp/corefcn/ls-mat-ascii.cc	Fri Dec 25 00:36:19 2015 -0500
@@ -233,8 +233,6 @@
 read_mat_ascii_data (std::istream& is, const std::string& filename,
                      octave_value& tc)
 {
-  std::string retval;
-
   std::string varname;
 
   size_t pos = filename.rfind ('/');
@@ -273,85 +271,76 @@
 
   octave_quit ();
 
-  if (nr > 0 && nc > 0)
-    {
-      Matrix tmp (nr, nc);
-
-      if (nr < 1 || nc < 1)
-        is.clear (std::ios::badbit);
-      else
-        {
-          double d;
-          for (octave_idx_type i = 0; i < nr; i++)
-            {
-              std::string buf = get_mat_data_input_line (is);
-
-              std::istringstream tmp_stream (buf);
-
-              for (octave_idx_type j = 0; j < nc; j++)
-                {
-                  octave_quit ();
-
-                  d = octave_read_value<double> (tmp_stream);
-
-                  if (tmp_stream || tmp_stream.eof ())
-                    {
-                      tmp.elem (i, j) = d;
-                      total_count++;
-
-                      // Skip whitespace and commas.
-                      char c;
-                      while (1)
-                        {
-                          tmp_stream >> c;
-
-                          if (! tmp_stream)
-                            break;
-
-                          if (! (c == ' ' || c == '\t' || c == ','))
-                            {
-                              tmp_stream.putback (c);
-                              break;
-                            }
-                        }
-
-                      if (tmp_stream.eof ())
-                        break;
-                    }
-                  else
-                    error ("load: failed to read matrix from file '%s'",
-                           filename.c_str ());
-                }
-            }
-        }
-
-      if (is || is.eof ())
-        {
-          // FIXME: not sure this is best, but it works.
-
-          if (is.eof ())
-            is.clear ();
-
-          octave_idx_type expected = nr * nc;
-
-          if (expected == total_count)
-            {
-              tc = tmp;
-              retval = varname;
-            }
-          else
-            error ("load: expected %d elements, found %d",
-                   expected, total_count);
-        }
-      else
-        error ("load: failed to read matrix from file '%s'",
-               filename.c_str ());
-    }
-  else
+  if (nr <= 0 || nc <= 0)
     error ("load: unable to extract matrix size from file '%s'",
            filename.c_str ());
 
-  return retval;
+  Matrix tmp (nr, nc);
+
+  if (nr < 1 || nc < 1)
+    is.clear (std::ios::badbit);
+  else
+    {
+      double d;
+      for (octave_idx_type i = 0; i < nr; i++)
+        {
+          std::string buf = get_mat_data_input_line (is);
+
+          std::istringstream tmp_stream (buf);
+
+          for (octave_idx_type j = 0; j < nc; j++)
+            {
+              octave_quit ();
+
+              d = octave_read_value<double> (tmp_stream);
+
+              if (! tmp_stream && ! tmp_stream.eof ())
+                error ("load: failed to read matrix from file '%s'",
+                       filename.c_str ());
+
+              tmp.elem (i, j) = d;
+              total_count++;
+
+              // Skip whitespace and commas.
+              char c;
+              while (1)
+                {
+                  tmp_stream >> c;
+
+                  if (! tmp_stream)
+                    break;
+
+                  if (! (c == ' ' || c == '\t' || c == ','))
+                    {
+                      tmp_stream.putback (c);
+                      break;
+                    }
+                }
+
+              if (tmp_stream.eof ())
+                break;
+            }
+        }
+    }
+
+  if (! is && ! is.eof ())
+    error ("load: failed to read matrix from file '%s'",
+           filename.c_str ());
+
+  // FIXME: not sure this is best, but it works.
+
+  if (is.eof ())
+    is.clear ();
+
+  octave_idx_type expected = nr * nc;
+
+  if (expected != total_count)
+    error ("load: expected %d elements, found %d",
+           expected, total_count);
+
+  tc = tmp;
+
+  return varname;
 }
 
 bool
--- a/libinterp/corefcn/ls-mat5.cc	Thu Dec 24 13:55:51 2015 -0500
+++ b/libinterp/corefcn/ls-mat5.cc	Fri Dec 25 00:36:19 2015 -0500
@@ -528,72 +528,70 @@
       OCTAVE_LOCAL_BUFFER (unsigned int, tmp, 2);
       if (uncompress (reinterpret_cast<Bytef *> (tmp), &destLen,
                       reinterpret_cast<Bytef *> (inbuf), element_length)
-          !=  Z_MEM_ERROR)
+          ==  Z_MEM_ERROR)
+        error ("load: error probing size of compressed data element");
+
+      // Why should I have to initialize outbuf as I'll just overwrite!!
+      if (swap)
+        swap_bytes<4> (tmp, 2);
+
+      destLen = tmp[1] + 8;
+      std::string outbuf (destLen, ' ');
+
+      // FIXME: find a way to avoid casting away const here!
+
+      int err = uncompress (reinterpret_cast<Bytef *>
+                            (const_cast<char *> (outbuf.c_str ())),
+                            &destLen, reinterpret_cast<Bytef *> (inbuf),
+                            element_length);
+
+      if (err != Z_OK)
         {
-          // Why should I have to initialize outbuf as I'll just overwrite!!
-          if (swap)
-            swap_bytes<4> (tmp, 2);
-
-          destLen = tmp[1] + 8;
-          std::string outbuf (destLen, ' ');
-
-          // FIXME: find a way to avoid casting away const here!
-
-          int err = uncompress (reinterpret_cast<Bytef *>
-                                 (const_cast<char *> (outbuf.c_str ())),
-                                &destLen, reinterpret_cast<Bytef *> (inbuf),
-                                element_length);
-
-          if (err != Z_OK)
+          std::string msg;
+          switch (err)
             {
-              std::string msg;
-              switch (err)
-                {
-                case Z_STREAM_END:
-                  msg = "stream end";
-                  break;
-
-                case Z_NEED_DICT:
-                  msg = "need dict";
-                  break;
-
-                case Z_ERRNO:
-                  msg = "errno case";
-                  break;
-
-                case Z_STREAM_ERROR:
-                  msg = "stream error";
-                  break;
-
-                case Z_DATA_ERROR:
-                  msg = "data error";
-                  break;
-
-                case Z_MEM_ERROR:
-                  msg = "mem error";
-                  break;
-
-                case Z_BUF_ERROR:
-                  msg = "buf error";
-                  break;
-
-                case Z_VERSION_ERROR:
-                  msg = "version error";
-                  break;
-                }
-
-              error ("load: error uncompressing data element (%s from zlib)",
-                     msg.c_str ());
+            case Z_STREAM_END:
+              msg = "stream end";
+              break;
+
+            case Z_NEED_DICT:
+              msg = "need dict";
+              break;
+
+            case Z_ERRNO:
+              msg = "errno case";
+              break;
+
+            case Z_STREAM_ERROR:
+              msg = "stream error";
+              break;
+
+            case Z_DATA_ERROR:
+              msg = "data error";
+              break;
+
+            case Z_MEM_ERROR:
+              msg = "mem error";
+              break;
+
+            case Z_BUF_ERROR:
+              msg = "buf error";
+              break;
+
+            case Z_VERSION_ERROR:
+              msg = "version error";
+              break;
             }
-          else
-            {
-              std::istringstream gz_is (outbuf);
-              retval = read_mat5_binary_element (gz_is, filename,
-                                                 swap, global, tc);
-            }
+
+          error ("load: error uncompressing data element (%s from zlib)",
+                 msg.c_str ());
         }
       else
-        error ("load: error probing size of compressed data element");
+        {
+          std::istringstream gz_is (outbuf);
+          retval = read_mat5_binary_element (gz_is, filename,
+                                             swap, global, tc);
+        }
 
       return retval;
 #else
@@ -1009,19 +1007,17 @@
             octave_value anon_fcn_handle =
               eval_string (fname.substr (4), true, parse_status);
 
-            if (parse_status == 0)
-              {
-                octave_fcn_handle *fh =
-                  anon_fcn_handle.fcn_handle_value ();
-
-                if (fh)
-                  tc = new octave_fcn_handle (fh->fcn_val (), "@<anonymous>");
-                else
-                  error ("load: failed to load anonymous function handle");
-              }
-            else
+            if (parse_status != 0)
               error ("load: failed to load anonymous function handle");
 
+            octave_fcn_handle *fh =
+              anon_fcn_handle.fcn_handle_value ();
+
+            if (! fh)
+              error ("load: failed to load anonymous function handle");
+
+            tc = new octave_fcn_handle (fh->fcn_val (), "@<anonymous>");
+
             frame.run ();
           }
         else
@@ -2318,15 +2314,13 @@
           if (compress (reinterpret_cast<Bytef *> (out_buf), &destLen,
                         reinterpret_cast<const Bytef *> (buf_str.c_str ()),
                                                          srcLen)
-              == Z_OK)
-            {
-              write_mat5_tag (os, miCOMPRESSED,
-                              static_cast<octave_idx_type> (destLen));
-
-              os.write (out_buf, destLen);
-            }
-          else
+              != Z_OK)
             error ("save: error compressing data element");
+
+          write_mat5_tag (os, miCOMPRESSED,
+                          static_cast<octave_idx_type> (destLen));
+
+          os.write (out_buf, destLen);
         }
 
       return ret;
--- a/libinterp/corefcn/ls-oct-text.cc	Thu Dec 24 13:55:51 2015 -0500
+++ b/libinterp/corefcn/ls-oct-text.cc	Fri Dec 25 00:36:19 2015 -0500
@@ -258,31 +258,29 @@
 
   std::string tag = extract_keyword (is, "type");
 
-  if (! tag.empty ())
-    {
-      std::string typ;
-      size_t pos = tag.rfind (' ');
+  if (tag.empty ())
+    error ("load: failed to extract keyword specifying value type");
 
-      if (pos != std::string::npos)
-        {
-          global = SUBSTRING_COMPARE_EQ (tag, 0, 6, "global");
+  std::string typ;
+  size_t pos = tag.rfind (' ');
 
-          typ = global ? tag.substr (7) : tag;
-        }
-      else
-        typ = tag;
+  if (pos != std::string::npos)
+    {
+      global = SUBSTRING_COMPARE_EQ (tag, 0, 6, "global");
 
-      // Special case for backward compatibility.  A small bit of cruft
-      if (SUBSTRING_COMPARE_EQ (typ, 0, 12, "string array"))
-        tc = charMatrix ();
-      else
-        tc = octave_value_typeinfo::lookup_type (typ);
-
-      if (! tc.load_ascii (is))
-        error ("load: trouble reading ascii file '%s'", filename.c_str ());
+      typ = global ? tag.substr (7) : tag;
     }
   else
-    error ("load: failed to extract keyword specifying value type");
+    typ = tag;
+
+  // Special case for backward compatibility.  A small bit of cruft
+  if (SUBSTRING_COMPARE_EQ (typ, 0, 12, "string array"))
+    tc = charMatrix ();
+  else
+    tc = octave_value_typeinfo::lookup_type (typ);
+
+  if (! tc.load_ascii (is))
+    error ("load: trouble reading ascii file '%s'", filename.c_str ());
 
   return name;
 }