68 : handle_map (), handle_free_list (),
69 next_handle (-1.0 - (rand () + 1.0) / (RAND_MAX + 2.0)) { }
73 static void create_instance (
void);
75 static bool instance_ok (
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,
202 Matrix retval (1, handle_map.size ());
205 for (
const_iterator p = handle_map.begin (); p != handle_map.end (); p++)
209 retval(i++) = h.
value ();
228 static double maxrand = RAND_MAX + 2.0;
230 return (rand () + 1.0) / maxrand;
280 error (
"ch_manager::free: invalid object %g", h.
value ());
286 DEFUN (urlwrite, args, nargout,
288 @deftypefn {Loadable Function} {} urlwrite (@var{url}, @var{localfile})\n\
289 @deftypefnx {Loadable Function} {@var{f} =} urlwrite (@var{url}, @var{localfile})\n\
290 @deftypefnx {Loadable Function} {[@var{f}, @var{success}] =} urlwrite (@var{url}, @var{localfile})\n\
291 @deftypefnx {Loadable Function} {[@var{f}, @var{success}, @var{message}] =} urlwrite (@var{url}, @var{localfile})\n\
292 Download a remote file specified by its @var{url} and save it as\n\
293 @var{localfile}. For example:\n\
297 urlwrite (\"ftp://ftp.octave.org/pub/octave/README\",\n\
302 The full path of the downloaded file is returned in @var{f}. The\n\
303 variable @var{success} is 1 if the download was successful,\n\
304 otherwise it is 0 in which case @var{message} contains an error\n\
305 message. If no output argument is specified and an error occurs,\n\
306 then the error is signaled through Octave's error handling mechanism.\n\
308 This function uses libcurl. Curl supports, among others, the HTTP,\n\
309 FTP and FILE protocols. Username and password may be specified in\n\
310 the URL, for example:\n\
314 urlwrite (\"http://username:password@@example.com/file.txt\",\n\
319 GET and POST requests can be specified by @var{method} and @var{param}.\n\
320 The parameter @var{method} is either @samp{get} or @samp{post}\n\
321 and @var{param} is a cell array of parameter and value pairs.\n\
326 urlwrite (\"http://www.google.com/search\", \"search.html\",\n\
327 \"get\", @{\"query\", \"octave\"@});\n\
335 int nargin = args.
length ();
338 if (nargin != 2 && nargin != 4)
344 std::string url = args(0).string_value ();
348 error (
"urlwrite: URL must be a character string");
353 std::string filename = args(1).string_value ();
357 error (
"urlwrite: LOCALFILE must be a character string");
366 method = args(2).string_value ();
370 error (
"urlwrite: METHOD must be \"get\" or \"post\"");
374 if (method !=
"get" && method !=
"post")
376 error (
"urlwrite: METHOD must be \"get\" or \"post\"");
380 param = args(3).cellstr_value ();
384 error (
"urlwrite: parameters (PARAM) for get and post requests must be given as a cell array of character strings");
389 if (param.
numel () % 2 == 1 )
391 error (
"urlwrite: number of elements in PARAM must be even");
402 std::ofstream ofile (filename.c_str (), std::ios::out | std::ios::binary);
404 if (! ofile.is_open ())
406 error (
"urlwrite: unable to open file");
427 retval(2) = std::string ();
435 retval(0) = std::string ();
439 if (nargout < 2 && ! curl.
good ())
445 DEFUN (urlread, args, nargout,
447 @deftypefn {Loadable Function} {@var{s} =} urlread (@var{url})\n\
448 @deftypefnx {Loadable Function} {[@var{s}, @var{success}] =} urlread (@var{url})\n\
449 @deftypefnx {Loadable Function} {[@var{s}, @var{success}, @var{message}] =} urlread (@var{url})\n\
450 @deftypefnx {Loadable Function} {[@dots{}] =} urlread (@var{url}, @var{method}, @var{param})\n\
451 Download a remote file specified by its @var{url} and return its content\n\
452 in string @var{s}. For example:\n\
455 s = urlread (\"ftp://ftp.octave.org/pub/octave/README\");\n\
458 The variable @var{success} is 1 if the download was successful,\n\
459 otherwise it is 0 in which case @var{message} contains an error\n\
460 message. If no output argument is specified and an error occurs,\n\
461 then the error is signaled through Octave's error handling mechanism.\n\
463 This function uses libcurl. Curl supports, among others, the HTTP,\n\
464 FTP and FILE protocols. Username and password may be specified in the\n\
465 URL@. For example:\n\
468 s = urlread (\"http://user:password@@example.com/file.txt\");\n\
471 GET and POST requests can be specified by @var{method} and @var{param}.\n\
472 The parameter @var{method} is either @samp{get} or @samp{post}\n\
473 and @var{param} is a cell array of parameter and value pairs.\n\
478 s = urlread (\"http://www.google.com/search\", \"get\",\n\
479 @{\"query\", \"octave\"@});\n\
482 @seealso{urlwrite}\n\
488 int nargin = args.
length ();
491 if (nargin != 1 && nargin != 3)
497 std::string url = args(0).string_value ();
501 error (
"urlread: URL must be a character string");
510 method = args(1).string_value ();
514 error (
"urlread: METHOD must be \"get\" or \"post\"");
518 if (method !=
"get" && method !=
"post")
520 error (
"urlread: METHOD must be \"get\" or \"post\"");
524 param = args(2).cellstr_value ();
528 error (
"urlread: parameters (PARAM) for get and post requests must be given as a cell array of character strings");
532 if (param.
numel () % 2 == 1 )
534 error (
"urlread: number of elements in PARAM must be even");
539 std::ostringstream buf;
551 retval(1) = curl.
good ();
552 retval(0) = buf.str ();
556 if (nargout < 2 && ! curl.
good ())
562 DEFUN (__ftp__, args, ,
564 @deftypefn {Loadable Function} {@var{handle} =} __ftp__ (@var{host})\n\
565 @deftypefnx {Loadable Function} {@var{handle} =} __ftp__ (@var{host}, @var{username}, @var{password})\n\
566 Undocumented internal function\n\
571 int nargin = args.
length ();
573 std::string user =
"anonymous";
574 std::string passwd =
"";
576 if (nargin < 1 || nargin > 3)
586 user = args(1).string_value ();
589 passwd = args(2).string_value ();
597 retval = ch.
value ();
604 DEFUN (__ftp_pwd__, args, ,
606 @deftypefn {Loadable Function} {} __ftp_pwd__ (@var{handle})\n\
607 Undocumented internal function\n\
612 int nargin = args.
length ();
615 error (
"__ftp_pwd__: incorrect number of arguments");
624 retval = curl.
pwd ();
626 error (
"__ftp_pwd__: invalid ftp handle");
632 DEFUN (__ftp_cwd__, args, ,
634 @deftypefn {Loadable Function} {} __ftp_cwd__ (@var{handle}, @var{path})\n\
635 Undocumented internal function\n\
640 int nargin = args.
length ();
642 if (nargin != 1 && nargin != 2)
643 error (
"__ftp_cwd__: incorrect number of arguments");
653 std::string path =
"";
661 error (
"__ftp_cwd__: expecting path as second argument");
664 error (
"__ftp_cwd__: invalid ftp handle");
670 DEFUN (__ftp_dir__, args, nargout,
672 @deftypefn {Loadable Function} {} __ftp_dir__ (@var{handle})\n\
673 Undocumented internal function\n\
678 int nargin = args.
length ();
681 error (
"__ftp_dir__: incorrect number of arguments");
729 fileisdir (i) = fisdir;
730 filectime (i) = ctime (&ftime);
731 filesize (i) = fsize;
732 filedatenum (i) =
double (ftime);
735 st.
assign (
"date", filectime);
736 st.
assign (
"bytes", filesize);
737 st.
assign (
"isdir", fileisdir);
738 st.
assign (
"datenum", filedatenum);
745 error (
"__ftp_dir__: invalid ftp handle");
751 DEFUN (__ftp_ascii__, args, ,
753 @deftypefn {Loadable Function} {} __ftp_ascii__ (@var{handle})\n\
754 Undocumented internal function\n\
759 int nargin = args.
length ();
762 error (
"__ftp_ascii__: incorrect number of arguments");
773 error (
"__ftp_ascii__: invalid ftp handle");
779 DEFUN (__ftp_binary__, args, ,
781 @deftypefn {Loadable Function} {} __ftp_binary__ (@var{handle})\n\
782 Undocumented internal function\n\
787 int nargin = args.
length ();
790 error (
"__ftp_binary__: incorrect number of arguments");
801 error (
"__ftp_binary__: invalid ftp handle");
807 DEFUN (__ftp_close__, args, ,
809 @deftypefn {Loadable Function} {} __ftp_close__ (@var{handle})\n\
810 Undocumented internal function\n\
815 int nargin = args.
length ();
818 error (
"__ftp_close__: incorrect number of arguments");
829 error (
"__ftp_close__: invalid ftp handle");
835 DEFUN (__ftp_mode__, args, ,
837 @deftypefn {Loadable Function} {} __ftp_mode__ (@var{handle})\n\
838 Undocumented internal function\n\
843 int nargin = args.
length ();
846 error (
"__ftp_mode__: incorrect number of arguments");
855 retval = (curl.
is_ascii () ?
"ascii" :
"binary");
857 error (
"__ftp_binary__: invalid ftp handle");
863 DEFUN (__ftp_delete__, args, ,
865 @deftypefn {Loadable Function} {} __ftp_delete__ (@var{handle}, @var{path})\n\
866 Undocumented internal function\n\
871 int nargin = args.
length ();
874 error (
"__ftp_delete__: incorrect number of arguments");
889 error (
"__ftp_delete__: expecting file name as second argument");
892 error (
"__ftp_delete__: invalid ftp handle");
898 DEFUN (__ftp_rmdir__, args, ,
900 @deftypefn {Loadable Function} {} __ftp_rmdir__ (@var{handle}, @var{path})\n\
901 Undocumented internal function\n\
906 int nargin = args.
length ();
909 error (
"__ftp_rmdir__: incorrect number of arguments");
924 error (
"__ftp_rmdir__: expecting directory name as second argument");
927 error (
"__ftp_rmdir__: invalid ftp handle");
933 DEFUN (__ftp_mkdir__, args, ,
935 @deftypefn {Loadable Function} {} __ftp_mkdir__ (@var{handle}, @var{path})\n\
936 Undocumented internal function\n\
941 int nargin = args.
length ();
944 error (
"__ftp_mkdir__: incorrect number of arguments");
959 error (
"__ftp_mkdir__: expecting directory name as second argument");
962 error (
"__ftp_mkdir__: invalid ftp handle");
968 DEFUN (__ftp_rename__, args, ,
970 @deftypefn {Loadable Function} {} __ftp_rename__ (@var{handle}, @var{path})\n\
971 Undocumented internal function\n\
976 int nargin = args.
length ();
979 error (
"__ftp_rename__: incorrect number of arguments");
990 std::string newname = args(2).string_value ();
993 curl.
rename (oldname, newname);
995 error (
"__ftp_rename__: expecting file names for second and third arguments");
998 error (
"__ftp_rename__: invalid ftp handle");
1004 DEFUN (__ftp_mput__, args, nargout,
1006 @deftypefn {Loadable Function} {} __ftp_mput__ (@var{handle}, @var{files})\n\
1007 Undocumented internal function\n\
1012 int nargin = args.
length ();
1015 error (
"__ftp_mput__: incorrect number of arguments");
1036 std::string file = files (i);
1042 error (
"__ftp__mput: file does not exist");
1059 std::ifstream ifile (file.c_str (), std::ios::in |
1062 if (! ifile.is_open ())
1064 error (
"__ftp_mput__: unable to open file");
1068 curl.
put (file, ifile);
1086 error (
"__ftp_mput__: expecting file name patter as second argument");
1089 error (
"__ftp_mput__: invalid ftp handle");
1095 DEFUN (__ftp_mget__, args, ,
1097 @deftypefn {Loadable Function} {} __ftp_mget__ (@var{handle}, @var{files})\n\
1098 Undocumented internal function\n\
1103 int nargin = args.
length ();
1105 if (nargin != 2 && nargin != 3)
1106 error (
"__ftp_mget__: incorrect number of arguments");
1131 if (pattern.
match (sv(i)))
1145 std::ofstream ofile ((target + sv(i)).c_str (),
1149 if (! ofile.is_open ())
1151 error (
"__ftp_mget__: unable to open file");
1159 curl.
get (sv(i), ofile);
1175 error (
"__ftp_mget__: file not found");
1178 error (
"__ftp_mget__: expecting file name and target as second and third arguments");
1181 error (
"__ftp_mget__: invalid ftp handle");