diff libinterp/corefcn/urlwrite.cc @ 20939:b17fda023ca6

maint: Use new C++ archetype in more files. Place input validation first in files. Move declaration of retval down in function to be closer to point of usage. Eliminate else clause after if () error. Use "return ovl()" where it makes sense. * find.cc, gammainc.cc, gcd.cc, getgrent.cc, getpwent.cc, givens.cc, graphics.cc, help.cc, hess.cc, hex2num.cc, input.cc, kron.cc, load-path.cc, load-save.cc, lookup.cc, mappers.cc, matrix_type.cc, mgorth.cc, nproc.cc, ordschur.cc, pager.cc, pinv.cc, pr-output.cc, profiler.cc, psi.cc, quad.cc, rcond.cc, regexp.cc, schur.cc, sighandlers.cc, sparse.cc, str2double.cc, strfind.cc, strfns.cc, sub2ind.cc, svd.cc, sylvester.cc, symtab.cc, syscalls.cc, sysdep.cc, time.cc, toplev.cc, tril.cc, tsearch.cc, typecast.cc, urlwrite.cc, utils.cc, variables.cc, __delaunayn__.cc, __eigs__.cc, __glpk__.cc, __magick_read__.cc, __osmesa_print__.cc, __voronoi__.cc, amd.cc, audiodevinfo.cc, audioread.cc, chol.cc, colamd.cc, dmperm.cc, fftw.cc, qr.cc, symbfact.cc, symrcm.cc, ov-bool-mat.cc, ov-cell.cc, ov-class.cc, ov-classdef.cc, ov-fcn-handle.cc, ov-fcn-inline.cc, ov-flt-re-mat.cc, ov-java.cc, ov-null-mat.cc, ov-oncleanup.cc, ov-re-mat.cc, ov-struct.cc, ov-typeinfo.cc, ov-usr-fcn.cc, ov.cc, octave.cc: Use new C++ archetype in more files.
author Rik <rik@octave.org>
date Fri, 18 Dec 2015 15:37:22 -0800
parents 03e4ddd49396
children 48b2ad5ee801
line wrap: on
line diff
--- a/libinterp/corefcn/urlwrite.cc	Fri Dec 18 16:22:53 2015 -0500
+++ b/libinterp/corefcn/urlwrite.cc	Fri Dec 18 15:37:22 2015 -0800
@@ -330,8 +330,6 @@
 @seealso{urlread}\n\
 @end deftypefn")
 {
-  octave_value_list retval;
-
   int nargin = args.length ();
 
   // verify arguments
@@ -340,7 +338,7 @@
 
   std::string url = args(0).xstring_value ("urlwrite: URL must be a string");
 
-  // name to store the file if download is succesful
+  // name to store the file if download is successful
   std::string filename = args(1).xstring_value ("urlwrite: LOCALFILE must be a string");
 
   std::string method;
@@ -376,29 +374,28 @@
 
   url_transfer curl = url_transfer (url, ofile);
 
-  if (curl.is_valid ())
-    {
-      curl.http_action (param, method);
+  octave_value_list retval;
 
-      ofile.close ();
+  if (! curl.is_valid ())
+    error ("support for URL transfers was disabled when Octave was built");
 
-      if (curl.good ())
-        frame.discard ();
+  curl.http_action (param, method);
+
+  ofile.close ();
 
-      if (nargout > 0)
-        {
-          if (curl.good ())
-            retval = ovl (octave_env::make_absolute (filename),
-                          true, std::string ());
-          else
-            retval = ovl (std::string (), false, curl.lasterror ());
-        }
+  if (curl.good ())
+    frame.discard ();
 
-      if (nargout < 2 && ! curl.good ())
-        error ("urlwrite: %s", curl.lasterror ().c_str ());
+  if (nargout > 0)
+    {
+      if (curl.good ())
+        retval = ovl (octave_env::make_absolute (filename), true, "");
+      else
+        retval = ovl ("", false, curl.lasterror ());
     }
-  else
-    error ("support for URL transfers was disabled when Octave was built");
+
+  if (nargout < 2 && ! curl.good ())
+    error ("urlwrite: %s", curl.lasterror ().c_str ());
 
   return retval;
 }
@@ -447,9 +444,6 @@
 @seealso{urlwrite}\n\
 @end deftypefn")
 {
-  // Octave's return value
-  octave_value_list retval;
-
   int nargin = args.length ();
 
   // verify arguments
@@ -478,22 +472,22 @@
 
   url_transfer curl = url_transfer (url, buf);
 
-  if (curl.is_valid ())
-    {
-      curl.http_action (param, method);
+  if (! curl.is_valid ())
+    error ("support for URL transfers was disabled when Octave was built");
+
+  curl.http_action (param, method);
+
+  octave_value_list retval;
 
-      if (nargout > 0)
-        {
-          // Return empty string if no error occurred.
-          retval = ovl (buf.str (), curl.good (),
-                        curl.good () ? "" : curl.lasterror ());
-        }
+  if (nargout > 0)
+    {
+      // Return empty string if no error occurred.
+      retval = ovl (buf.str (), curl.good (),
+                    curl.good () ? "" : curl.lasterror ());
+    }
 
-      if (nargout < 2 && ! curl.good ())
-        error ("urlread: %s", curl.lasterror().c_str());
-    }
-  else
-    error ("support for URL transfers was disabled when Octave was built");
+  if (nargout < 2 && ! curl.good ())
+    error ("urlread: %s", curl.lasterror ().c_str ());
 
   return retval;
 }
@@ -532,21 +526,15 @@
 Undocumented internal function\n\
 @end deftypefn")
 {
-  octave_value retval;
-
   if (args.length () != 1)
     error ("__ftp_pwd__: incorrect number of arguments");
-  else
-    {
-      url_transfer curl = ch_manager::get_object (args(0));
+
+  url_transfer curl = ch_manager::get_object (args(0));
 
-      if (curl.is_valid ())
-        retval = curl.pwd ();
-      else
-        error ("__ftp_pwd__: invalid ftp handle");
-    }
+  if (! curl.is_valid ())
+    error ("__ftp_pwd__: invalid ftp handle");
 
-  return retval;
+  return ovl (curl.pwd ());
 }
 
 DEFUN (__ftp_cwd__, args, ,
@@ -555,30 +543,23 @@
 Undocumented internal function\n\
 @end deftypefn")
 {
-  octave_value retval;
-
   int nargin = args.length ();
 
   if (nargin != 1 && nargin != 2)
     error ("__ftp_cwd__: incorrect number of arguments");
-  else
-    {
-      url_transfer curl = ch_manager::get_object (args(0));
 
-      if (curl.is_valid ())
-        {
-          std::string path = "";
+  std::string path = "";
+  if (nargin > 1)
+    path = args(1).xstring_value ("__ftp_cwd__: PATH must be a string");
+
+  url_transfer curl = ch_manager::get_object (args(0));
 
-          if (nargin > 1)
-            path = args(1).xstring_value ("__ftp_cwd__: PATH must be a string");
+  if (! curl.is_valid ())
+    error ("__ftp_cwd__: invalid ftp handle");
 
-          curl.cwd (path);
-        }
-      else
-        error ("__ftp_cwd__: invalid ftp handle");
-    }
+  curl.cwd (path);
 
-  return retval;
+  return octave_value_list ();
 }
 
 DEFUN (__ftp_dir__, args, nargout,
@@ -587,71 +568,67 @@
 Undocumented internal function\n\
 @end deftypefn")
 {
+  if (args.length () != 1)
+    error ("__ftp_dir__: incorrect number of arguments");
+
+  url_transfer curl = ch_manager::get_object (args(0));
+
+  if (! curl.is_valid ())
+    error ("__ftp_dir__: invalid ftp handle");
+
   octave_value retval;
 
-  if (args.length () != 1)
-    error ("__ftp_dir__: incorrect number of arguments");
+  if (nargout == 0)
+    curl.dir ();
   else
     {
-      url_transfer curl = ch_manager::get_object (args(0));
-
-      if (curl.is_valid ())
-        {
-          if (nargout == 0)
-            curl.dir ();
-          else
-            {
-              string_vector sv = curl.list ();
-              octave_idx_type n = sv.numel ();
+      string_vector sv = curl.list ();
+      octave_idx_type n = sv.numel ();
 
-              if (n == 0)
-                {
-                  string_vector flds (5);
-
-                  flds(0) = "name";
-                  flds(1) = "date";
-                  flds(2) = "bytes";
-                  flds(3) = "isdir";
-                  flds(4) = "datenum";
-
-                  retval = octave_map (flds);
-                }
-              else
-                {
-                  octave_map st;
+      if (n == 0)
+        {
+          string_vector flds (5);
 
-                  Cell filectime (dim_vector (n, 1));
-                  Cell filesize (dim_vector (n, 1));
-                  Cell fileisdir (dim_vector (n, 1));
-                  Cell filedatenum (dim_vector (n, 1));
-
-                  st.assign ("name", Cell (sv));
-
-                  for (octave_idx_type i = 0; i < n; i++)
-                    {
-                      time_t ftime;
-                      bool fisdir;
-                      double fsize;
-
-                      curl.get_fileinfo (sv(i), fsize, ftime, fisdir);
+          flds(0) = "name";
+          flds(1) = "date";
+          flds(2) = "bytes";
+          flds(3) = "isdir";
+          flds(4) = "datenum";
 
-                      fileisdir (i) = fisdir;
-                      filectime (i) = ctime (&ftime);
-                      filesize (i) = fsize;
-                      filedatenum (i) = double (ftime);
-                    }
-
-                  st.assign ("date", filectime);
-                  st.assign ("bytes", filesize);
-                  st.assign ("isdir", fileisdir);
-                  st.assign ("datenum", filedatenum);
-
-                  retval = st;
-                }
-            }
+          retval = octave_map (flds);
         }
       else
-        error ("__ftp_dir__: invalid ftp handle");
+        {
+          octave_map st;
+
+          Cell filectime (dim_vector (n, 1));
+          Cell filesize (dim_vector (n, 1));
+          Cell fileisdir (dim_vector (n, 1));
+          Cell filedatenum (dim_vector (n, 1));
+
+          st.assign ("name", Cell (sv));
+
+          for (octave_idx_type i = 0; i < n; i++)
+            {
+              time_t ftime;
+              bool fisdir;
+              double fsize;
+
+              curl.get_fileinfo (sv(i), fsize, ftime, fisdir);
+
+              fileisdir (i) = fisdir;
+              filectime (i) = ctime (&ftime);
+              filesize (i) = fsize;
+              filedatenum (i) = double (ftime);
+            }
+
+          st.assign ("date", filectime);
+          st.assign ("bytes", filesize);
+          st.assign ("isdir", fileisdir);
+          st.assign ("datenum", filedatenum);
+
+          retval = st;
+        }
     }
 
   return retval;
@@ -663,21 +640,17 @@
 Undocumented internal function\n\
 @end deftypefn")
 {
-  octave_value retval;
-
   if (args.length () != 1)
     error ("__ftp_ascii__: incorrect number of arguments");
-  else
-    {
-      url_transfer curl = ch_manager::get_object (args(0));
+
+  url_transfer curl = ch_manager::get_object (args(0));
 
-      if (curl.is_valid ())
-        curl.ascii ();
-      else
-        error ("__ftp_ascii__: invalid ftp handle");
-    }
+  if (! curl.is_valid ())
+    error ("__ftp_ascii__: invalid ftp handle");
 
-  return retval;
+  curl.ascii ();
+
+  return octave_value_list ();
 }
 
 DEFUN (__ftp_binary__, args, ,
@@ -686,21 +659,17 @@
 Undocumented internal function\n\
 @end deftypefn")
 {
-  octave_value retval;
-
   if (args.length () != 1)
     error ("__ftp_binary__: incorrect number of arguments");
-  else
-    {
-      url_transfer curl = ch_manager::get_object (args(0));
+
+  url_transfer curl = ch_manager::get_object (args(0));
 
-      if (curl.is_valid ())
-        curl.binary ();
-      else
-        error ("__ftp_binary__: invalid ftp handle");
-    }
+  if (! curl.is_valid ())
+    error ("__ftp_binary__: invalid ftp handle");
 
-  return retval;
+  curl.binary ();
+
+  return octave_value_list ();
 }
 
 DEFUN (__ftp_close__, args, ,
@@ -709,21 +678,17 @@
 Undocumented internal function\n\
 @end deftypefn")
 {
-  octave_value retval;
-
   if (args.length () != 1)
     error ("__ftp_close__: incorrect number of arguments");
-  else
-    {
-      curl_handle h = ch_manager::lookup (args(0));
+
+  curl_handle h = ch_manager::lookup (args(0));
 
-      if (h.ok ())
-        ch_manager::free (h);
-      else
-        error ("__ftp_close__: invalid ftp handle");
-    }
+  if (! h.ok ())
+    error ("__ftp_close__: invalid ftp handle");
 
-  return retval;
+  ch_manager::free (h);
+
+  return octave_value_list ();
 }
 
 DEFUN (__ftp_mode__, args, ,
@@ -732,21 +697,17 @@
 Undocumented internal function\n\
 @end deftypefn")
 {
-  octave_value retval;
-
   if (args.length () != 1)
     error ("__ftp_mode__: incorrect number of arguments");
-  else
-    {
-      url_transfer curl = ch_manager::get_object (args(0));
+
+  octave_value retval;
+
+  url_transfer curl = ch_manager::get_object (args(0));
 
-      if (curl.is_valid ())
-        retval = (curl.is_ascii () ? "ascii" : "binary");
-      else
-        error ("__ftp_binary__: invalid ftp handle");
-    }
+  if (! curl.is_valid ())
+    error ("__ftp_binary__: invalid ftp handle");
 
-  return retval;
+  return ovl (curl.is_ascii () ? "ascii" : "binary");
 }
 
 DEFUN (__ftp_delete__, args, ,
@@ -755,25 +716,19 @@
 Undocumented internal function\n\
 @end deftypefn")
 {
-  octave_value retval;
-
   if (args.length () != 2)
     error ("__ftp_delete__: incorrect number of arguments");
-  else
-    {
-      url_transfer curl = ch_manager::get_object (args(0));
 
-      if (curl.is_valid ())
-        {
-          std::string file = args(1).xstring_value ("__ftp_delete__: FILE must be a string");
+  std::string file = args(1).xstring_value ("__ftp_delete__: FILE must be a string");
+
+  url_transfer curl = ch_manager::get_object (args(0));
 
-          curl.del (file);
-        }
-      else
-        error ("__ftp_delete__: invalid ftp handle");
-    }
+  if (! curl.is_valid ())
+    error ("__ftp_delete__: invalid ftp handle");
 
-  return retval;
+  curl.del (file);
+
+  return octave_value_list ();
 }
 
 DEFUN (__ftp_rmdir__, args, ,
@@ -786,21 +741,17 @@
 
   if (args.length () != 2)
     error ("__ftp_rmdir__: incorrect number of arguments");
-  else
-    {
-      url_transfer curl = ch_manager::get_object (args(0));
 
-      if (curl.is_valid ())
-        {
-          std::string dir = args(1).xstring_value ("__ftp_rmdir__: DIR must be a string");
+  std::string dir = args(1).xstring_value ("__ftp_rmdir__: DIR must be a string");
+
+  url_transfer curl = ch_manager::get_object (args(0));
 
-          curl.rmdir (dir);
-        }
-      else
-        error ("__ftp_rmdir__: invalid ftp handle");
-    }
+  if (! curl.is_valid ())
+    error ("__ftp_rmdir__: invalid ftp handle");
 
-  return retval;
+  curl.rmdir (dir);
+
+  return octave_value_list ();
 }
 
 DEFUN (__ftp_mkdir__, args, ,
@@ -809,25 +760,19 @@
 Undocumented internal function\n\
 @end deftypefn")
 {
-  octave_value retval;
-
   if (args.length () != 2)
     error ("__ftp_mkdir__: incorrect number of arguments");
-  else
-    {
-      url_transfer curl = ch_manager::get_object (args(0));
 
-      if (curl.is_valid ())
-        {
-          std::string dir = args(1).xstring_value ("__ftp_mkdir__: DIR must be a string");
+  std::string dir = args(1).xstring_value ("__ftp_mkdir__: DIR must be a string");
+
+  url_transfer curl = ch_manager::get_object (args(0));
 
-          curl.mkdir (dir);
-        }
-      else
-        error ("__ftp_mkdir__: invalid ftp handle");
-    }
+  if (! curl.is_valid ())
+    error ("__ftp_mkdir__: invalid ftp handle");
 
-  return retval;
+  curl.mkdir (dir);
+
+  return octave_value_list ();
 }
 
 DEFUN (__ftp_rename__, args, ,
@@ -836,26 +781,20 @@
 Undocumented internal function\n\
 @end deftypefn")
 {
-  octave_value retval;
-
   if (args.length () != 3)
     error ("__ftp_rename__: incorrect number of arguments");
-  else
-    {
-      url_transfer curl = ch_manager::get_object (args(0));
+
+  std::string oldname = args(1).xstring_value ("__ftp_rename__: OLDNAME must be a string");
+  std::string newname = args(2).xstring_value ("__ftp_rename__: NEWNAME must be a string");
+
+  url_transfer curl = ch_manager::get_object (args(0));
 
-      if (curl.is_valid ())
-        {
-          std::string oldname = args(1).xstring_value ("__ftp_rename__: OLDNAME must be a string");
-          std::string newname = args(2).xstring_value ("__ftp_rename__: NEWNAME must be a string");
+  if (curl.is_valid ())
+    error ("__ftp_rename__: invalid ftp handle");
 
-          curl.rename (oldname, newname);
-        }
-      else
-        error ("__ftp_rename__: invalid ftp handle");
-    }
+  curl.rename (oldname, newname);
 
-  return retval;
+  return octave_value_list ();
 }
 
 DEFUN (__ftp_mput__, args, nargout,
@@ -864,67 +803,60 @@
 Undocumented internal function\n\
 @end deftypefn")
 {
-  octave_value retval;
-
   if (args.length () != 2)
     error ("__ftp_mput__: incorrect number of arguments");
-  else
-    {
-      url_transfer curl = ch_manager::get_object (args(0));
 
-      if (curl.is_valid ())
-        {
-          std::string pat = args(1).xstring_value ("__ftp_mput__: PATTERN must be a string");
+  std::string pat = args(1).xstring_value ("__ftp_mput__: PATTERN must be a string");
 
-          string_vector file_list;
-
-          glob_match pattern (file_ops::tilde_expand (pat));
-          string_vector files = pattern.glob ();
+  url_transfer curl = ch_manager::get_object (args(0));
 
-          for (octave_idx_type i = 0; i < files.numel (); i++)
-            {
-              std::string file = files (i);
-
-              file_stat fs (file);
+  if (! curl.is_valid ())
+    error ("__ftp_mput__: invalid ftp handle");
 
-              if (! fs.exists ())
-                error ("__ftp__mput: file does not exist");
+  string_vector file_list;
 
-              if (fs.is_dir ())
-                {
-                  file_list.append (curl.mput_directory ("", file));
+  glob_match pattern (file_ops::tilde_expand (pat));
+  string_vector files = pattern.glob ();
 
-                  if (! curl.good ())
-                    error ("__ftp_mput__: %s", curl.lasterror().c_str());
-                }
-              else
-                {
-                  // FIXME: Does ascii mode need to be flagged here?
-                  std::ifstream ifile (file.c_str (), std::ios::in |
-                                       std::ios::binary);
+  for (octave_idx_type i = 0; i < files.numel (); i++)
+    {
+      std::string file = files(i);
 
-                  if (! ifile.is_open ())
-                    error ("__ftp_mput__: unable to open file");
+      file_stat fs (file);
 
-                  curl.put (file, ifile);
-
-                  ifile.close ();
-
-                  if (! curl.good ())
-                    error ("__ftp_mput__: %s", curl.lasterror().c_str());
+      if (! fs.exists ())
+        error ("__ftp__mput: file does not exist");
 
-                  file_list.append (file);
-                }
-            }
+      if (fs.is_dir ())
+        {
+          file_list.append (curl.mput_directory ("", file));
 
-          if (nargout > 0)
-            retval = file_list;
+          if (! curl.good ())
+            error ("__ftp_mput__: %s", curl.lasterror ().c_str ());
         }
       else
-        error ("__ftp_mput__: invalid ftp handle");
+        {
+          // FIXME: Does ascii mode need to be flagged here?
+          std::ifstream ifile (file.c_str (), std::ios::in | std::ios::binary);
+
+          if (! ifile.is_open ())
+            error ("__ftp_mput__: unable to open file");
+
+          curl.put (file, ifile);
+
+          ifile.close ();
+
+          if (! curl.good ())
+            error ("__ftp_mput__: %s", curl.lasterror ().c_str ());
+
+          file_list.append (file);
+        }
     }
 
-  return retval;
+  if (nargout > 0)
+    return ovl (file_list);
+  else
+    return octave_value_list ();
 }
 
 DEFUN (__ftp_mget__, args, ,
@@ -934,75 +866,69 @@
 Undocumented internal function\n\
 @end deftypefn")
 {
-  octave_value retval;
-
   int nargin = args.length ();
 
   if (nargin != 2 && nargin != 3)
     error ("__ftp_mget__: incorrect number of arguments");
-  else
-    {
-      url_transfer curl = ch_manager::get_object (args(0));
+
+  std::string file = args(1).xstring_value ("__ftp_mget__: PATTERN must be a string");
+
+  std::string target;
 
-      if (curl.is_valid ())
-        {
-          std::string file = args(1).xstring_value ("__ftp_mget__: PATTERN must be a string");
-          std::string target;
+  if (nargin == 3 && ! args(2).is_empty ())
+    target = args(2).xstring_value ("__ftp_mget__: TARGET must be a string") + file_ops::dir_sep_str ();
 
-          if (nargin == 3 && ! args(2).is_empty ())
-            target = args(2).xstring_value ("__ftp_mget__: TARGET must be a string") + file_ops::dir_sep_str ();
+  url_transfer curl = ch_manager::get_object (args(0));
 
-          string_vector sv = curl.list ();
-          octave_idx_type n = 0;
-          glob_match pattern (file);
-
+  if (! curl.is_valid ())
+    error ("__ftp_mget__: invalid ftp handle");
 
-          for (octave_idx_type i = 0; i < sv.numel (); i++)
-            {
-              if (pattern.match (sv(i)))
-                {
-                  n++;
+  string_vector sv = curl.list ();
+  octave_idx_type n = 0;
+  glob_match pattern (file);
 
-                  time_t ftime;
-                  bool fisdir;
-                  double fsize;
+  for (octave_idx_type i = 0; i < sv.numel (); i++)
+    {
+      if (pattern.match (sv(i)))
+        {
+          n++;
 
-                  curl.get_fileinfo (sv(i), fsize, ftime, fisdir);
+          time_t ftime;
+          bool fisdir;
+          double fsize;
+
+          curl.get_fileinfo (sv(i), fsize, ftime, fisdir);
 
-                  if (fisdir)
-                    curl.mget_directory (sv(i), target);
-                  else
-                    {
-                      std::ofstream ofile ((target + sv(i)).c_str (),
-                                           std::ios::out |
-                                           std::ios::binary);
-
-                      if (! ofile.is_open ())
-                        error ("__ftp_mget__: unable to open file");
+          if (fisdir)
+            curl.mget_directory (sv(i), target);
+          else
+            {
+              std::ofstream ofile ((target + sv(i)).c_str (),
+                                   std::ios::out |
+                                   std::ios::binary);
 
-                      unwind_protect_safe frame;
-
-                      frame.add_fcn (delete_file, target + sv(i));
+              if (! ofile.is_open ())
+                error ("__ftp_mget__: unable to open file");
 
-                      curl.get (sv(i), ofile);
+              unwind_protect_safe frame;
 
-                      ofile.close ();
+              frame.add_fcn (delete_file, target + sv(i));
 
-                      if (curl.good ())
-                        frame.discard ();
-                    }
+              curl.get (sv(i), ofile);
 
-                  if (! curl.good ())
-                    error ("__ftp_mget__: %s", curl.lasterror().c_str());
-                }
+              ofile.close ();
+
+              if (curl.good ())
+                frame.discard ();
             }
 
-          if (n == 0)
-            error ("__ftp_mget__: file not found");
+          if (! curl.good ())
+            error ("__ftp_mget__: %s", curl.lasterror().c_str());
         }
-      else
-        error ("__ftp_mget__: invalid ftp handle");
     }
 
-  return retval;
+  if (n == 0)
+    error ("__ftp_mget__: file not found");
+
+  return octave_value_list ();
 }