68 : handle_map (), handle_free_list (),
69 next_handle (-1.0 - (rand () + 1.0) / (RAND_MAX + 2.0)) { }
73 static void create_instance (
void);
84 ::error (
"unable to create ch_manager!");
96 return instance_ok () ? instance->do_get_handle () :
curl_handle ();
102 instance->do_free (h);
107 return instance_ok () ? instance->do_lookup (val) :
curl_handle ();
118 return get_object (
lookup (val));
123 return get_object (
lookup (val));
128 return instance_ok () ? instance->do_get_object (h) :
url_transfer ();
132 const std::string& user,
133 const std::string& passwd,
136 return instance_ok ()
137 ? instance->do_make_curl_handle (host, user, passwd, os)
143 return instance_ok () ? instance->do_handle_list () :
Matrix ();
150 typedef std::map<curl_handle, url_transfer>::iterator
iterator;
171 iterator p = (
xisnan (val) ? handle_map.end () : handle_map.find (val));
173 return (p != handle_map.end ()) ? p->first :
curl_handle ();
178 iterator p = (h.
ok () ? handle_map.find (h) : handle_map.end ());
180 return (p != handle_map.end ()) ? p->second :
url_transfer ();
184 const std::string& user,
185 const std::string& passwd,
200 error (
"support for url transfers was disabled when Octave was built");
207 Matrix retval (1, handle_map.size ());
210 for (const_iterator p = handle_map.begin (); p != handle_map.end (); p++)
214 retval(i++) = h.
value ();
233 static double maxrand = RAND_MAX + 2.0;
235 return (rand () + 1.0) / maxrand;
285 error (
"ch_manager::free: invalid object %g", h.
value ());
291 DEFUN (urlwrite, args, nargout,
293 @deftypefn {Loadable Function} {} urlwrite (@var{url}, @var{localfile})\n\
294 @deftypefnx {Loadable Function} {@var{f} =} urlwrite (@var{url}, @var{localfile})\n\
295 @deftypefnx {Loadable Function} {[@var{f}, @var{success}] =} urlwrite (@var{url}, @var{localfile})\n\
296 @deftypefnx {Loadable Function} {[@var{f}, @var{success}, @var{message}] =} urlwrite (@var{url}, @var{localfile})\n\
297 Download a remote file specified by its @var{url} and save it as\n\
304 urlwrite (\"ftp://ftp.octave.org/pub/README\",\n\
309 The full path of the downloaded file is returned in @var{f}.\n\
311 The variable @var{success} is 1 if the download was successful,\n\
312 otherwise it is 0 in which case @var{message} contains an error message.\n\
314 If no output argument is specified and an error occurs, then the error is\n\
315 signaled through Octave's error handling mechanism.\n\
317 This function uses libcurl. Curl supports, among others, the HTTP, FTP and\n\
318 FILE protocols. Username and password may be specified in the URL, for\n\
323 urlwrite (\"http://username:password@@example.com/file.txt\",\n\
328 GET and POST requests can be specified by @var{method} and @var{param}.\n\
329 The parameter @var{method} is either @samp{get} or @samp{post} and\n\
330 @var{param} is a cell array of parameter and value pairs.\n\
335 urlwrite (\"http://www.google.com/search\", \"search.html\",\n\
336 \"get\", @{\"query\", \"octave\"@});\n\
344 int nargin = args.
length ();
347 if (nargin != 2 && nargin != 4)
353 if (! args(0).is_string ())
355 error (
"urlwrite: URL must be a string");
359 std::string url = args(0).string_value ();
361 if (! args(1).is_string ())
363 error (
"urlwrite: LOCALFILE must be a string");
368 std::string filename = args(1).string_value ();
375 if (! args(2).is_string ())
377 error (
"urlwrite: METHOD must be a string");
381 method = args(2).string_value ();
383 if (method !=
"get" && method !=
"post")
385 error (
"urlwrite: METHOD must be \"get\" or \"post\"");
389 param = args(3).cellstr_value ();
393 error (
"urlwrite: parameters (PARAM) for get and post requests must be given as a cell array of strings");
397 if (param.
numel () % 2 == 1)
399 error (
"urlwrite: number of elements in PARAM must be even");
410 std::ofstream ofile (filename.c_str (), std::ios::out | std::ios::binary);
412 if (! ofile.is_open ())
414 error (
"urlwrite: unable to open file");
437 retval(2) = std::string ();
445 retval(0) = std::string ();
449 if (nargout < 2 && ! curl.
good ())
453 error (
"support for url transfers was disabled when Octave was built");
458 DEFUN (urlread, args, nargout,
460 @deftypefn {Loadable Function} {@var{s} =} urlread (@var{url})\n\
461 @deftypefnx {Loadable Function} {[@var{s}, @var{success}] =} urlread (@var{url})\n\
462 @deftypefnx {Loadable Function} {[@var{s}, @var{success}, @var{message}] =} urlread (@var{url})\n\
463 @deftypefnx {Loadable Function} {[@dots{}] =} urlread (@var{url}, @var{method}, @var{param})\n\
464 Download a remote file specified by its @var{url} and return its content\n\
465 in string @var{s}.\n\
470 s = urlread (\"ftp://ftp.octave.org/pub/README\");\n\
473 The variable @var{success} is 1 if the download was successful,\n\
474 otherwise it is 0 in which case @var{message} contains an error\n\
477 If no output argument is specified and an error occurs, then the error is\n\
478 signaled through Octave's error handling mechanism.\n\
480 This function uses libcurl. Curl supports, among others, the HTTP, FTP and\n\
481 FILE protocols. Username and password may be specified in the URL@. For\n\
485 s = urlread (\"http://user:password@@example.com/file.txt\");\n\
488 GET and POST requests can be specified by @var{method} and @var{param}.\n\
489 The parameter @var{method} is either @samp{get} or @samp{post} and\n\
490 @var{param} is a cell array of parameter and value pairs.\n\
495 s = urlread (\"http://www.google.com/search\", \"get\",\n\
496 @{\"query\", \"octave\"@});\n\
499 @seealso{urlwrite}\n\
505 int nargin = args.
length ();
508 if (nargin != 1 && nargin != 3)
514 if (! args(0).is_string ())
516 error (
"urlread: URL must be a string");
520 std::string url = args(0).string_value ();
527 if (! args(1).is_string ())
529 error (
"urlread: METHOD must be a string");
533 method = args(1).string_value ();
535 if (method !=
"get" && method !=
"post")
537 error (
"urlread: METHOD must be \"get\" or \"post\"");
541 param = args(2).cellstr_value ();
545 error (
"urlread: parameters (PARAM) for get and post requests must be given as a cell array of strings");
549 if (param.
numel () % 2 == 1)
551 error (
"urlread: number of elements in PARAM must be even");
556 std::ostringstream buf;
568 retval(1) = curl.
good ();
569 retval(0) = buf.str ();
572 if (nargout < 2 && ! curl.
good ())
576 error (
"support for url transfers was disabled when Octave was built");
581 DEFUN (__ftp__, args, ,
583 @deftypefn {Loadable Function} {@var{handle} =} __ftp__ (@var{host})\n\
584 @deftypefnx {Loadable Function} {@var{handle} =} __ftp__ (@var{host}, @var{username}, @var{password})\n\
585 Undocumented internal function\n\
590 int nargin = args.
length ();
592 std::string user =
"anonymous";
593 std::string passwd =
"";
595 if (nargin < 1 || nargin > 3)
605 user = args(1).string_value ();
608 passwd = args(2).string_value ();
616 retval = ch.
value ();
623 DEFUN (__ftp_pwd__, args, ,
625 @deftypefn {Loadable Function} {} __ftp_pwd__ (@var{handle})\n\
626 Undocumented internal function\n\
631 int nargin = args.
length ();
634 error (
"__ftp_pwd__: incorrect number of arguments");
643 retval = curl.
pwd ();
645 error (
"__ftp_pwd__: invalid ftp handle");
651 DEFUN (__ftp_cwd__, args, ,
653 @deftypefn {Loadable Function} {} __ftp_cwd__ (@var{handle}, @var{path})\n\
654 Undocumented internal function\n\
659 int nargin = args.
length ();
661 if (nargin != 1 && nargin != 2)
662 error (
"__ftp_cwd__: incorrect number of arguments");
672 std::string path =
"";
680 error (
"__ftp_cwd__: expecting path as second argument");
683 error (
"__ftp_cwd__: invalid ftp handle");
689 DEFUN (__ftp_dir__, args, nargout,
691 @deftypefn {Loadable Function} {} __ftp_dir__ (@var{handle})\n\
692 Undocumented internal function\n\
697 int nargin = args.
length ();
700 error (
"__ftp_dir__: incorrect number of arguments");
748 fileisdir (i) = fisdir;
749 filectime (i) = ctime (&ftime);
750 filesize (i) = fsize;
751 filedatenum (i) =
double (ftime);
754 st.
assign (
"date", filectime);
755 st.
assign (
"bytes", filesize);
756 st.
assign (
"isdir", fileisdir);
757 st.
assign (
"datenum", filedatenum);
764 error (
"__ftp_dir__: invalid ftp handle");
770 DEFUN (__ftp_ascii__, args, ,
772 @deftypefn {Loadable Function} {} __ftp_ascii__ (@var{handle})\n\
773 Undocumented internal function\n\
778 int nargin = args.
length ();
781 error (
"__ftp_ascii__: incorrect number of arguments");
792 error (
"__ftp_ascii__: invalid ftp handle");
798 DEFUN (__ftp_binary__, args, ,
800 @deftypefn {Loadable Function} {} __ftp_binary__ (@var{handle})\n\
801 Undocumented internal function\n\
806 int nargin = args.
length ();
809 error (
"__ftp_binary__: incorrect number of arguments");
820 error (
"__ftp_binary__: invalid ftp handle");
826 DEFUN (__ftp_close__, args, ,
828 @deftypefn {Loadable Function} {} __ftp_close__ (@var{handle})\n\
829 Undocumented internal function\n\
834 int nargin = args.
length ();
837 error (
"__ftp_close__: incorrect number of arguments");
848 error (
"__ftp_close__: invalid ftp handle");
854 DEFUN (__ftp_mode__, args, ,
856 @deftypefn {Loadable Function} {} __ftp_mode__ (@var{handle})\n\
857 Undocumented internal function\n\
862 int nargin = args.
length ();
865 error (
"__ftp_mode__: incorrect number of arguments");
874 retval = (curl.
is_ascii () ?
"ascii" :
"binary");
876 error (
"__ftp_binary__: invalid ftp handle");
882 DEFUN (__ftp_delete__, args, ,
884 @deftypefn {Loadable Function} {} __ftp_delete__ (@var{handle}, @var{path})\n\
885 Undocumented internal function\n\
890 int nargin = args.
length ();
893 error (
"__ftp_delete__: incorrect number of arguments");
908 error (
"__ftp_delete__: expecting file name as second argument");
911 error (
"__ftp_delete__: invalid ftp handle");
917 DEFUN (__ftp_rmdir__, args, ,
919 @deftypefn {Loadable Function} {} __ftp_rmdir__ (@var{handle}, @var{path})\n\
920 Undocumented internal function\n\
925 int nargin = args.
length ();
928 error (
"__ftp_rmdir__: incorrect number of arguments");
943 error (
"__ftp_rmdir__: expecting directory name as second argument");
946 error (
"__ftp_rmdir__: invalid ftp handle");
952 DEFUN (__ftp_mkdir__, args, ,
954 @deftypefn {Loadable Function} {} __ftp_mkdir__ (@var{handle}, @var{path})\n\
955 Undocumented internal function\n\
960 int nargin = args.
length ();
963 error (
"__ftp_mkdir__: incorrect number of arguments");
978 error (
"__ftp_mkdir__: expecting directory name as second argument");
981 error (
"__ftp_mkdir__: invalid ftp handle");
987 DEFUN (__ftp_rename__, args, ,
989 @deftypefn {Loadable Function} {} __ftp_rename__ (@var{handle}, @var{path})\n\
990 Undocumented internal function\n\
995 int nargin = args.
length ();
998 error (
"__ftp_rename__: incorrect number of arguments");
1009 std::string newname = args(2).string_value ();
1012 curl.
rename (oldname, newname);
1014 error (
"__ftp_rename__: expecting file names for second and third arguments");
1017 error (
"__ftp_rename__: invalid ftp handle");
1023 DEFUN (__ftp_mput__, args, nargout,
1025 @deftypefn {Loadable Function} {} __ftp_mput__ (@var{handle}, @var{files})\n\
1026 Undocumented internal function\n\
1031 int nargin = args.
length ();
1034 error (
"__ftp_mput__: incorrect number of arguments");
1055 std::string file = files (i);
1061 error (
"__ftp__mput: file does not exist");
1078 std::ifstream ifile (file.c_str (), std::ios::in |
1081 if (! ifile.is_open ())
1083 error (
"__ftp_mput__: unable to open file");
1087 curl.
put (file, ifile);
1105 error (
"__ftp_mput__: expecting file name patter as second argument");
1108 error (
"__ftp_mput__: invalid ftp handle");
1114 DEFUN (__ftp_mget__, args, ,
1116 @deftypefn {Loadable Function} {} __ftp_mget__ (@var{handle}, @var{files})\n\
1117 Undocumented internal function\n\
1122 int nargin = args.
length ();
1124 if (nargin != 2 && nargin != 3)
1125 error (
"__ftp_mget__: incorrect number of arguments");
1150 if (pattern.
match (sv(i)))
1164 std::ofstream ofile ((target + sv(i)).c_str (),
1168 if (! ofile.is_open ())
1170 error (
"__ftp_mget__: unable to open file");
1178 curl.
get (sv(i), ofile);
1194 error (
"__ftp_mget__: file not found");
1197 error (
"__ftp_mget__: expecting file name and target as second and third arguments");
1200 error (
"__ftp_mget__: invalid ftp handle");
int octave_unlink(const std::string &name)
static url_transfer get_object(const curl_handle &h)
std::map< curl_handle, url_transfer > handle_map
void assign(const std::string &k, const Cell &val)
OCTINTERP_API void print_usage(void)
octave_idx_type numel(void) const
Number of elements in the array.
curl_handle do_lookup(double val)
static ch_manager * instance
octave_idx_type length(void) const
static Matrix handle_list(void)
#define DEFUN(name, args_name, nargout_name, doc)
void error(const char *fmt,...)
octave_idx_type lookup(const T *x, octave_idx_type n, T y)
static url_transfer get_object(double val)
static double make_handle_fraction(void)
curl_handle do_get_handle(void)
octave_handle curl_handle
std::set< curl_handle >::const_iterator const_free_list_iterator
static std::string tilde_expand(const std::string &)
void put(const std::string &file, std::istream &is)
void del(const std::string &file)
void get(const std::string &file, std::ostream &os)
static curl_handle make_curl_handle(const std::string &host, const std::string &user, const std::string &passwd, std::ostream &os)
void add_fcn(void(*fcn)(void))
static bool instance_ok(void)
void rename(const std::string &oldname, const std::string &newname)
bool is_real_scalar(void) const
std::string string_value(bool force=false) const
static void create_instance(void)
bool is_valid(void) const
static std::string make_absolute(const std::string &s, const std::string &dot_path=get_current_directory())
std::set< curl_handle > handle_free_list
std::complex< T > ceil(const std::complex< T > &x)
static curl_handle lookup(const octave_value &val)
static void cleanup_instance(void)
string_vector & append(const std::string &s)
void do_free(const curl_handle &h)
bool match(const std::string &str) const
octave_idx_type length(void) const
static curl_handle get_handle(void)
std::set< curl_handle >::iterator free_list_iterator
std::string lasterror(void) const
void cwd(const std::string &path)
std::map< curl_handle, url_transfer >::const_iterator const_iterator
void get_fileinfo(const std::string &filename, double &filesize, time_t &filetime, bool &fileisdir)
octave_idx_type length(void) const
Number of elements in the array.
void http_action(const Array< std::string > ¶m, const std::string &action)
static curl_handle lookup(double val)
string_vector mput_directory(const std::string &base, const std::string &directory)
void mkdir(const std::string &path)
static void delete_file(const std::string &file)
url_transfer do_get_object(const curl_handle &h)
void mget_directory(const std::string &directory, const std::string &target)
static void free(const curl_handle &h)
std::map< curl_handle, url_transfer >::iterator iterator
void rmdir(const std::string &path)
static std::string dir_sep_str(void)
static url_transfer get_object(const octave_value &val)
double double_value(bool frc_str_conv=false) const
curl_handle do_make_curl_handle(const std::string &host, const std::string &user, const std::string &passwd, std::ostream &os)
Matrix do_handle_list(void)
bool is_ascii(void) const
string_vector glob(void) const