Mercurial > octave-nkf
annotate src/DLD-FUNCTIONS/urlwrite.cc @ 10377:fb62fbbe28c0
urlwrite: don't set CURLOPT_USERPWD if userpwd is empty
author | David Bateman <dbateman@free.fr> |
---|---|
date | Sun, 28 Feb 2010 23:27:09 -0500 |
parents | 2d47356a7a1a |
children | 89f4d7e294cc |
rev | line source |
---|---|
6043 | 1 // urlwrite and urlread, a curl front-end for octave |
2 /* | |
3 | |
8920 | 4 Copyright (C) 2006, 2007, 2008 Alexander Barth |
9880 | 5 Copyright (C) 2009 David Bateman |
6043 | 6 |
7 This file is part of Octave. | |
8 | |
9 Octave is free software; you can redistribute it and/or modify it | |
10 under the terms of the GNU General Public License as published by the | |
7016 | 11 Free Software Foundation; either version 3 of the License, or (at your |
12 option) any later version. | |
6043 | 13 |
14 Octave is distributed in the hope that it will be useful, but WITHOUT | |
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
17 for more details. | |
18 | |
19 You should have received a copy of the GNU General Public License | |
7016 | 20 along with Octave; see the file COPYING. If not, see |
21 <http://www.gnu.org/licenses/>. | |
6043 | 22 |
23 */ | |
24 | |
25 // Author: Alexander Barth <abarth@marine.usf.edu> | |
26 // Adapted-By: jwe | |
27 | |
28 #ifdef HAVE_CONFIG_H | |
29 #include <config.h> | |
30 #endif | |
31 | |
32 #include <string> | |
33 #include <fstream> | |
34 #include <iomanip> | |
9880 | 35 #include <iostream> |
6043 | 36 |
9880 | 37 #include "dir-ops.h" |
8151
3725f819b5b3
urlwrite.cc (Furlwrite): delete files we create if download fails
John W. Eaton <jwe@octave.org>
parents:
7481
diff
changeset
|
38 #include "file-ops.h" |
3725f819b5b3
urlwrite.cc (Furlwrite): delete files we create if download fails
John W. Eaton <jwe@octave.org>
parents:
7481
diff
changeset
|
39 #include "file-stat.h" |
6043 | 40 #include "oct-env.h" |
9880 | 41 #include "glob-match.h" |
6043 | 42 |
43 #include "defun-dld.h" | |
44 #include "error.h" | |
45 #include "oct-obj.h" | |
46 #include "ov-cell.h" | |
47 #include "pager.h" | |
9880 | 48 #include "oct-map.h" |
8151
3725f819b5b3
urlwrite.cc (Furlwrite): delete files we create if download fails
John W. Eaton <jwe@octave.org>
parents:
7481
diff
changeset
|
49 #include "unwind-prot.h" |
6043 | 50 |
9905
84a7c11ff928
Correctly compile even when libcurl is not present according to configure
Rik <rdrider0-list@yahoo.com>
parents:
9885
diff
changeset
|
51 #ifdef HAVE_CURL |
6043 | 52 |
53 #include <curl/curl.h> | |
9885 | 54 #include <curl/curlver.h> |
6043 | 55 #include <curl/types.h> |
56 #include <curl/easy.h> | |
57 | |
9880 | 58 static int |
6043 | 59 write_data (void *buffer, size_t size, size_t nmemb, void *streamp) |
60 { | |
61 std::ostream& stream = *(static_cast<std::ostream*> (streamp)); | |
62 stream.write (static_cast<const char*> (buffer), size*nmemb); | |
63 return (stream.fail () ? 0 : size * nmemb); | |
64 } | |
65 | |
9880 | 66 static int |
67 read_data (void *buffer, size_t size, size_t nmemb, void *streamp) | |
6043 | 68 { |
9880 | 69 std::istream& stream = *(static_cast<std::istream*> (streamp)); |
70 stream.read (static_cast<char*> (buffer), size*nmemb); | |
71 if (stream.eof ()) | |
72 return stream.gcount (); | |
73 else | |
74 return (stream.fail () ? 0 : size * nmemb); | |
6043 | 75 } |
76 | |
9880 | 77 static size_t |
78 throw_away (void *, size_t size, size_t nmemb, void *) | |
6992 | 79 { |
9880 | 80 return static_cast<size_t>(size * nmemb); |
6992 | 81 } |
82 | |
9880 | 83 class |
84 curl_handle | |
6043 | 85 { |
9880 | 86 private: |
87 class | |
88 curl_handle_rep | |
89 { | |
90 public: | |
91 curl_handle_rep (void) : count (1), valid (true), ascii (false) | |
92 { | |
93 curl = curl_easy_init (); | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
94 if (!curl) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
95 error ("can not create curl handle"); |
9880 | 96 } |
97 | |
98 ~curl_handle_rep (void) | |
99 { | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
100 if (curl) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
101 curl_easy_cleanup (curl); |
9880 | 102 } |
103 | |
104 bool is_valid (void) const | |
105 { | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
106 return valid; |
9880 | 107 } |
108 | |
109 bool perform (bool curlerror) const | |
110 { | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
111 bool retval = false; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
112 if (!error_state) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
113 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
114 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
6043 | 115 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
116 CURLcode res = curl_easy_perform (curl); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
117 if (res != CURLE_OK) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
118 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
119 if (curlerror) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
120 error ("%s", curl_easy_strerror (res)); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
121 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
122 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
123 retval = true; |
9880 | 124 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
125 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
126 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
127 return retval; |
9880 | 128 } |
6043 | 129 |
9880 | 130 CURL* handle (void) const |
131 { | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
132 return curl; |
9880 | 133 } |
134 | |
135 bool is_ascii (void) const | |
136 { | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
137 return ascii; |
9880 | 138 } |
139 | |
140 bool is_binary (void) const | |
141 { | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
142 return !ascii; |
9880 | 143 } |
144 | |
145 size_t count; | |
146 std::string host; | |
147 bool valid; | |
148 bool ascii; | |
149 | |
150 private: | |
151 CURL *curl; | |
6043 | 152 |
9880 | 153 // No copying! |
154 | |
155 curl_handle_rep (const curl_handle_rep& ov); | |
156 | |
157 curl_handle_rep& operator = (const curl_handle_rep&); | |
158 }; | |
159 | |
160 public: | |
6043 | 161 |
9880 | 162 // I'd love to rewrite this as a private method of the curl_handle |
163 // class, but you can't pass the va_list from the wrapper setopt to | |
164 // the curl_easy_setopt function. | |
165 #define setopt(option, parameter) \ | |
166 { \ | |
167 CURLcode res = curl_easy_setopt (rep->handle (), option, parameter); \ | |
168 if (res != CURLE_OK) \ | |
169 error ("%s", curl_easy_strerror (res)); \ | |
170 } | |
171 | |
172 curl_handle (void) : rep (new curl_handle_rep ()) | |
173 { | |
174 rep->valid = false; | |
175 } | |
176 | |
177 curl_handle (const std::string& _host, const std::string& user, | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
178 const std::string& passwd) : |
9880 | 179 rep (new curl_handle_rep ()) |
180 { | |
181 rep->host = _host; | |
182 init (user, passwd, std::cin, octave_stdout); | |
183 | |
184 std::string url = "ftp://" + _host; | |
185 setopt (CURLOPT_URL, url.c_str()); | |
6043 | 186 |
9880 | 187 // Setup the link, with no transfer |
188 if (!error_state) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
189 perform (); |
9880 | 190 } |
191 | |
192 curl_handle (const std::string& url, const std::string& method, | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
193 const Cell& param, std::ostream& os, bool& retval) : |
9880 | 194 rep (new curl_handle_rep ()) |
195 { | |
196 retval = false; | |
197 | |
198 init ("", "", std::cin, os); | |
199 | |
200 setopt (CURLOPT_NOBODY, 0); | |
201 | |
202 // Don't need to store the parameters here as we can't change | |
203 // the URL after the handle is created | |
204 std::string query_string = form_query_string (param); | |
6043 | 205 |
9880 | 206 if (method == "get") |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
207 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
208 query_string = url + "?" + query_string; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
209 setopt (CURLOPT_URL, query_string.c_str ()); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
210 } |
9880 | 211 else if (method == "post") |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
212 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
213 setopt (CURLOPT_URL, url.c_str ()); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
214 setopt (CURLOPT_POSTFIELDS, query_string.c_str ()); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
215 } |
9880 | 216 else |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
217 setopt (CURLOPT_URL, url.c_str()); |
9880 | 218 |
219 if (!error_state) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
220 retval = perform (false); |
9880 | 221 } |
222 | |
223 curl_handle (const curl_handle& h) : rep (h.rep) | |
6043 | 224 { |
9880 | 225 rep->count++; |
226 } | |
227 | |
228 ~curl_handle (void) | |
229 { | |
230 if (--rep->count == 0) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
231 delete rep; |
9880 | 232 } |
233 | |
234 curl_handle& operator = (const curl_handle& h) | |
235 { | |
236 if (this != &h) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
237 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
238 if (--rep->count == 0) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
239 delete rep; |
9880 | 240 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
241 rep = h.rep; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
242 rep->count++; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
243 } |
9880 | 244 return *this; |
245 } | |
246 | |
247 bool is_valid (void) const | |
248 { | |
249 return rep->is_valid (); | |
6043 | 250 } |
9880 | 251 |
252 std::string lasterror (void) const | |
253 { | |
10084
81e88250bf42
urlwrite: avoid using errno as a local variable
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
254 CURLcode errnum; |
9880 | 255 |
10084
81e88250bf42
urlwrite: avoid using errno as a local variable
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
256 curl_easy_getinfo (rep->handle(), CURLINFO_OS_ERRNO, &errnum); |
9880 | 257 |
10084
81e88250bf42
urlwrite: avoid using errno as a local variable
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
258 return std::string (curl_easy_strerror (errnum)); |
9880 | 259 } |
260 | |
261 void set_ostream (std::ostream& os) const | |
262 { | |
263 setopt (CURLOPT_WRITEDATA, static_cast<void*> (&os)); | |
264 } | |
265 | |
266 void set_istream (std::istream& is) const | |
6043 | 267 { |
9880 | 268 setopt (CURLOPT_READDATA, static_cast<void*> (&is)); |
269 } | |
270 | |
271 void ascii (void) const | |
272 { | |
273 setopt (CURLOPT_TRANSFERTEXT, 1); | |
274 rep->ascii = true; | |
275 } | |
276 | |
277 void binary (void) const | |
278 { | |
279 setopt (CURLOPT_TRANSFERTEXT, 0); | |
280 rep->ascii = false; | |
281 } | |
282 | |
283 bool is_ascii (void) const | |
284 { | |
285 return rep->is_ascii (); | |
286 } | |
287 | |
288 bool is_binary (void) const | |
289 { | |
290 return rep->is_binary (); | |
6043 | 291 } |
9880 | 292 |
293 void cwd (const std::string& path) const | |
294 { | |
295 struct curl_slist *slist = 0; | |
296 std::string cmd = "cwd " + path; | |
297 slist = curl_slist_append (slist, cmd.c_str()); | |
298 setopt (CURLOPT_POSTQUOTE, slist); | |
299 if (! error_state) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
300 perform (); |
9880 | 301 setopt (CURLOPT_POSTQUOTE, 0); |
302 curl_slist_free_all (slist); | |
303 } | |
6043 | 304 |
9880 | 305 void del (const std::string& file) const |
306 { | |
307 struct curl_slist *slist = 0; | |
308 std::string cmd = "dele " + file; | |
309 slist = curl_slist_append (slist, cmd.c_str()); | |
310 setopt (CURLOPT_POSTQUOTE, slist); | |
311 if (! error_state) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
312 perform (); |
9880 | 313 setopt (CURLOPT_POSTQUOTE, 0); |
314 curl_slist_free_all (slist); | |
315 } | |
316 | |
317 void rmdir (const std::string& path) const | |
318 { | |
319 struct curl_slist *slist = 0; | |
320 std::string cmd = "rmd " + path; | |
321 slist = curl_slist_append (slist, cmd.c_str()); | |
322 setopt (CURLOPT_POSTQUOTE, slist); | |
323 if (! error_state) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
324 perform (); |
9880 | 325 setopt (CURLOPT_POSTQUOTE, 0); |
326 curl_slist_free_all (slist); | |
327 } | |
6043 | 328 |
9880 | 329 bool mkdir (const std::string& path, bool curlerror = true) const |
330 { | |
331 bool retval = false; | |
332 struct curl_slist *slist = 0; | |
333 std::string cmd = "mkd " + path; | |
334 slist = curl_slist_append (slist, cmd.c_str()); | |
335 setopt (CURLOPT_POSTQUOTE, slist); | |
336 if (! error_state) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
337 retval = perform (curlerror); |
9880 | 338 setopt (CURLOPT_POSTQUOTE, 0); |
339 curl_slist_free_all (slist); | |
340 return retval; | |
341 } | |
6043 | 342 |
9880 | 343 void rename (const std::string& oldname, const std::string& newname) const |
344 { | |
345 struct curl_slist *slist = 0; | |
346 std::string cmd = "rnfr " + oldname; | |
347 slist = curl_slist_append (slist, cmd.c_str()); | |
348 cmd = "rnto " + newname; | |
349 slist = curl_slist_append (slist, cmd.c_str()); | |
350 setopt (CURLOPT_POSTQUOTE, slist); | |
351 if (! error_state) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
352 perform (); |
9880 | 353 setopt (CURLOPT_POSTQUOTE, 0); |
354 curl_slist_free_all (slist); | |
355 } | |
356 | |
357 void put (const std::string& file, std::istream& is) const | |
358 { | |
359 std::string url = "ftp://" + rep->host + "/" + file; | |
360 setopt (CURLOPT_URL, url.c_str()); | |
361 setopt (CURLOPT_UPLOAD, 1); | |
362 setopt (CURLOPT_NOBODY, 0); | |
363 set_istream (is); | |
364 if (! error_state) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
365 perform (); |
9880 | 366 set_istream (std::cin); |
367 setopt (CURLOPT_NOBODY, 1); | |
368 setopt (CURLOPT_UPLOAD, 0); | |
369 url = "ftp://" + rep->host; | |
370 setopt (CURLOPT_URL, url.c_str()); | |
371 } | |
7013 | 372 |
9880 | 373 void get (const std::string& file, std::ostream& os) const |
374 { | |
375 std::string url = "ftp://" + rep->host + "/" + file; | |
376 setopt (CURLOPT_URL, url.c_str()); | |
377 setopt (CURLOPT_NOBODY, 0); | |
378 set_ostream (os); | |
379 if (! error_state) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
380 perform (); |
9880 | 381 set_ostream (octave_stdout); |
382 setopt (CURLOPT_NOBODY, 1); | |
383 url = "ftp://" + rep->host; | |
384 setopt (CURLOPT_URL, url.c_str()); | |
385 } | |
386 | |
387 void dir (void) const | |
388 { | |
389 std::string url = "ftp://" + rep->host + "/"; | |
390 setopt (CURLOPT_URL, url.c_str()); | |
391 setopt (CURLOPT_NOBODY, 0); | |
392 if (! error_state) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
393 perform (); |
9880 | 394 setopt (CURLOPT_NOBODY, 1); |
395 url = "ftp://" + rep->host; | |
396 setopt (CURLOPT_URL, url.c_str()); | |
397 } | |
6390 | 398 |
9880 | 399 string_vector list (void) const |
400 { | |
401 std::ostringstream buf; | |
402 std::string url = "ftp://" + rep->host + "/"; | |
403 setopt (CURLOPT_WRITEDATA, static_cast<void*> (&buf)); | |
404 setopt (CURLOPT_URL, url.c_str()); | |
405 setopt (CURLOPT_DIRLISTONLY, 1); | |
406 setopt (CURLOPT_NOBODY, 0); | |
407 if (! error_state) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
408 perform (); |
9880 | 409 setopt (CURLOPT_NOBODY, 1); |
410 url = "ftp://" + rep->host; | |
411 setopt (CURLOPT_WRITEDATA, static_cast<void*> (&octave_stdout)); | |
412 setopt (CURLOPT_DIRLISTONLY, 0); | |
413 setopt (CURLOPT_URL, url.c_str()); | |
414 | |
415 // Count number of directory entries | |
416 std::string str = buf.str (); | |
417 octave_idx_type n = 0; | |
418 size_t pos = 0; | |
419 while (true) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
420 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
421 pos = str.find_first_of('\n', pos); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
422 if (pos == std::string::npos) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
423 break; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
424 pos++; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
425 n++; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
426 } |
9880 | 427 string_vector retval (n); |
428 pos = 0; | |
429 for (octave_idx_type i = 0; i < n; i++) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
430 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
431 size_t newpos = str.find_first_of('\n', pos); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
432 if (newpos == std::string::npos) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
433 break; |
9880 | 434 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
435 retval(i) = str.substr(pos, newpos - pos); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
436 pos = newpos + 1; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
437 } |
9880 | 438 return retval; |
439 } | |
440 | |
441 void get_fileinfo (const std::string& filename, double& filesize, | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
442 time_t& filetime, bool& fileisdir) const |
9880 | 443 { |
444 std::string path = pwd(); | |
6043 | 445 |
9880 | 446 std::string url = "ftp://" + rep->host + "/" + path + "/" + filename; |
447 setopt (CURLOPT_URL, url.c_str()); | |
448 setopt (CURLOPT_FILETIME, 1); | |
449 setopt (CURLOPT_HEADERFUNCTION, throw_away); | |
450 setopt (CURLOPT_WRITEFUNCTION, throw_away); | |
6043 | 451 |
9880 | 452 // FIXME |
453 // The MDTM command fails for a directory on the servers I tested | |
454 // so this is a means of testing for directories. It also means | |
455 // I can't get the date of directories! | |
456 if (! error_state) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
457 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
458 if (! perform (false)) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
459 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
460 fileisdir = true; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
461 filetime = -1; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
462 filesize = 0; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
463 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
464 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
465 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
466 fileisdir = false; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
467 time_t ft; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
468 curl_easy_getinfo(rep->handle (), CURLINFO_FILETIME, &ft); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
469 filetime = ft; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
470 double fs; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
471 curl_easy_getinfo(rep->handle (), |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
472 CURLINFO_CONTENT_LENGTH_DOWNLOAD, &fs); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
473 filesize = fs; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
474 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
475 } |
6992 | 476 |
9880 | 477 setopt (CURLOPT_WRITEFUNCTION, write_data); |
478 setopt (CURLOPT_HEADERFUNCTION, 0); | |
479 setopt (CURLOPT_FILETIME, 0); | |
480 url = "ftp://" + rep->host; | |
481 setopt (CURLOPT_URL, url.c_str()); | |
482 | |
483 // The MDTM command seems to reset the path to the root with the | |
484 // servers I tested with, so cd again into the correct path. Make | |
485 // the path absolute so that this will work even with servers that | |
486 // don't end up in the root after an MDTM command. | |
487 cwd ("/" + path); | |
488 } | |
489 | |
490 std::string pwd (void) const | |
491 { | |
492 struct curl_slist *slist = 0; | |
493 std::string retval; | |
494 std::ostringstream buf; | |
6992 | 495 |
9880 | 496 slist = curl_slist_append (slist, "pwd"); |
497 setopt (CURLOPT_POSTQUOTE, slist); | |
498 setopt (CURLOPT_HEADERFUNCTION, write_data); | |
499 setopt (CURLOPT_WRITEHEADER, static_cast<void *>(&buf)); | |
500 | |
501 if (! error_state) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
502 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
503 perform (); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
504 retval = buf.str(); |
9880 | 505 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
506 // Can I assume that the path is alway in "" on the last line |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
507 size_t pos2 = retval.rfind ('"'); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
508 size_t pos1 = retval.rfind ('"', pos2 - 1); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
509 retval = retval.substr(pos1 + 1, pos2 - pos1 - 1); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
510 } |
9880 | 511 setopt (CURLOPT_HEADERFUNCTION, 0); |
512 setopt (CURLOPT_WRITEHEADER, 0); | |
513 setopt (CURLOPT_POSTQUOTE, 0); | |
514 curl_slist_free_all (slist); | |
515 | |
516 return retval; | |
517 } | |
6992 | 518 |
9880 | 519 bool perform (bool curlerror = true) const |
520 { | |
521 return rep->perform (curlerror); | |
522 } | |
523 | |
524 private: | |
525 curl_handle_rep *rep; | |
526 | |
527 std::string form_query_string (const Cell& param) | |
528 { | |
529 std::ostringstream query; | |
530 | |
531 for (int i = 0; i < param.numel (); i += 2) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
532 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
533 std::string name = param(i).string_value (); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
534 std::string text = param(i+1).string_value (); |
9880 | 535 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
536 // Encode strings. |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
537 char *enc_name = curl_easy_escape (rep->handle(), name.c_str (), |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
538 name.length ()); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
539 char *enc_text = curl_easy_escape (rep->handle(), text.c_str (), |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
540 text.length ()); |
9880 | 541 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
542 query << enc_name << "=" << enc_text; |
9880 | 543 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
544 curl_free (enc_name); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
545 curl_free (enc_text); |
9880 | 546 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
547 if (i < param.numel()-1) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
548 query << "&"; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
549 } |
9880 | 550 |
551 query.flush (); | |
6043 | 552 |
9880 | 553 return query.str (); |
554 } | |
555 | |
556 void init (const std::string& user, const std::string& passwd, | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
557 std::istream& is, std::ostream& os) |
9880 | 558 { |
559 // No data transfer by default | |
560 setopt (CURLOPT_NOBODY, 1); | |
561 | |
562 // Set the username and password | |
9918
57b41617c9fd
avoid LIBCURL version check
John W. Eaton <jwe@octave.org>
parents:
9905
diff
changeset
|
563 std::string userpwd = user; |
57b41617c9fd
avoid LIBCURL version check
John W. Eaton <jwe@octave.org>
parents:
9905
diff
changeset
|
564 if (! passwd.empty ()) |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
565 userpwd += ":" + passwd; |
10377
fb62fbbe28c0
urlwrite: don't set CURLOPT_USERPWD if userpwd is empty
David Bateman <dbateman@free.fr>
parents:
10250
diff
changeset
|
566 if (! userpwd.empty ()) |
fb62fbbe28c0
urlwrite: don't set CURLOPT_USERPWD if userpwd is empty
David Bateman <dbateman@free.fr>
parents:
10250
diff
changeset
|
567 setopt (CURLOPT_USERPWD, userpwd.c_str ()); |
9880 | 568 |
569 // Define our callback to get called when there's data to be written. | |
570 setopt (CURLOPT_WRITEFUNCTION, write_data); | |
6992 | 571 |
9880 | 572 // Set a pointer to our struct to pass to the callback. |
573 setopt (CURLOPT_WRITEDATA, static_cast<void*> (&os)); | |
574 | |
575 // Define our callback to get called when there's data to be read | |
576 setopt (CURLOPT_READFUNCTION, read_data); | |
577 | |
578 // Set a pointer to our struct to pass to the callback. | |
579 setopt (CURLOPT_READDATA, static_cast<void*> (&is)); | |
580 | |
581 // Follow redirects. | |
582 setopt (CURLOPT_FOLLOWLOCATION, true); | |
583 | |
584 // Don't use EPSV since connecting to sites that don't support it | |
585 // will hang for some time (3 minutes?) before moving on to try PASV | |
586 // instead. | |
587 setopt (CURLOPT_FTP_USE_EPSV, false); | |
588 | |
589 setopt (CURLOPT_NOPROGRESS, true); | |
590 setopt (CURLOPT_FAILONERROR, true); | |
6992 | 591 |
9880 | 592 setopt (CURLOPT_POSTQUOTE, 0); |
593 setopt (CURLOPT_QUOTE, 0); | |
594 } | |
595 | |
596 #undef setopt | |
597 }; | |
598 | |
599 class | |
600 curl_handles | |
601 { | |
602 public: | |
603 | |
604 typedef std::map<std::string, curl_handle>::iterator iterator; | |
605 typedef std::map<std::string, curl_handle>::const_iterator const_iterator; | |
6043 | 606 |
9880 | 607 curl_handles (void) : map () |
608 { | |
609 curl_global_init(CURL_GLOBAL_DEFAULT); | |
610 } | |
611 | |
612 ~curl_handles (void) | |
613 { | |
614 // Remove the elements of the map explicitly as they should | |
615 // be deleted before the call to curl_global_cleanup | |
616 for (iterator pa = begin (); pa != end (); pa++) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
617 map.erase (pa); |
6992 | 618 |
9880 | 619 curl_global_cleanup (); |
620 } | |
621 | |
622 iterator begin (void) { return iterator (map.begin ()); } | |
623 const_iterator begin (void) const { return const_iterator (map.begin ()); } | |
624 | |
625 iterator end (void) { return iterator (map.end ()); } | |
626 const_iterator end (void) const { return const_iterator (map.end ()); } | |
6992 | 627 |
9880 | 628 iterator seek (const std::string& k) { return map.find (k); } |
629 const_iterator seek (const std::string& k) const { return map.find (k); } | |
630 | |
631 std::string key (const_iterator p) const { return p->first; } | |
632 | |
633 curl_handle& contents (const std::string& k) | |
634 { | |
635 return map[k]; | |
636 } | |
637 | |
638 curl_handle contents (const std::string& k) const | |
639 { | |
640 const_iterator p = seek (k); | |
641 return p != end () ? p->second : curl_handle (); | |
642 } | |
643 | |
644 curl_handle& contents (iterator p) | |
645 { return p->second; } | |
6992 | 646 |
9880 | 647 curl_handle contents (const_iterator p) const |
648 { return p->second; } | |
649 | |
650 void del (const std::string& k) | |
651 { | |
652 iterator p = map.find (k); | |
653 | |
654 if (p != map.end ()) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
655 map.erase (p); |
9880 | 656 } |
6043 | 657 |
9880 | 658 private: |
659 std::map<std::string, curl_handle> map; | |
660 }; | |
661 | |
662 static curl_handles handles; | |
663 | |
10197
4d433bd2d4dc
attempt to avoid trouble with gnulib #defines in a consistent way
John W. Eaton <jwe@octave.org>
parents:
10154
diff
changeset
|
664 static void |
9880 | 665 cleanup_urlwrite (std::string filename) |
666 { | |
10197
4d433bd2d4dc
attempt to avoid trouble with gnulib #defines in a consistent way
John W. Eaton <jwe@octave.org>
parents:
10154
diff
changeset
|
667 octave_unlink (filename); |
6043 | 668 } |
669 | |
10197
4d433bd2d4dc
attempt to avoid trouble with gnulib #defines in a consistent way
John W. Eaton <jwe@octave.org>
parents:
10154
diff
changeset
|
670 static void |
9880 | 671 reset_path (const curl_handle curl) |
672 { | |
673 curl.cwd (".."); | |
674 } | |
8151
3725f819b5b3
urlwrite.cc (Furlwrite): delete files we create if download fails
John W. Eaton <jwe@octave.org>
parents:
7481
diff
changeset
|
675 |
10197
4d433bd2d4dc
attempt to avoid trouble with gnulib #defines in a consistent way
John W. Eaton <jwe@octave.org>
parents:
10154
diff
changeset
|
676 static void |
4d433bd2d4dc
attempt to avoid trouble with gnulib #defines in a consistent way
John W. Eaton <jwe@octave.org>
parents:
10154
diff
changeset
|
677 delete_file (std::string file) |
8151
3725f819b5b3
urlwrite.cc (Furlwrite): delete files we create if download fails
John W. Eaton <jwe@octave.org>
parents:
7481
diff
changeset
|
678 { |
10197
4d433bd2d4dc
attempt to avoid trouble with gnulib #defines in a consistent way
John W. Eaton <jwe@octave.org>
parents:
10154
diff
changeset
|
679 octave_unlink (file); |
8151
3725f819b5b3
urlwrite.cc (Furlwrite): delete files we create if download fails
John W. Eaton <jwe@octave.org>
parents:
7481
diff
changeset
|
680 } |
9880 | 681 #endif |
8151
3725f819b5b3
urlwrite.cc (Furlwrite): delete files we create if download fails
John W. Eaton <jwe@octave.org>
parents:
7481
diff
changeset
|
682 |
6043 | 683 DEFUN_DLD (urlwrite, args, nargout, |
684 "-*- texinfo -*-\n\ | |
9880 | 685 @deftypefn {Loadable Function} {} urlwrite (@var{url}, @var{localfile})\n\ |
6043 | 686 @deftypefnx {Loadable Function} {@var{f} =} urlwrite (@var{url}, @var{localfile})\n\ |
687 @deftypefnx {Loadable Function} {[@var{f}, @var{success}] =} urlwrite (@var{url}, @var{localfile})\n\ | |
688 @deftypefnx {Loadable Function} {[@var{f}, @var{success}, @var{message}] =} urlwrite (@var{url}, @var{localfile})\n\ | |
9880 | 689 Download a remote file specified by its @var{url} and save it as\n\ |
6043 | 690 @var{localfile}. For example,\n\ |
691 \n\ | |
692 @example\n\ | |
9064
7c02ec148a3c
Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
693 @group\n\ |
7031 | 694 urlwrite (\"ftp://ftp.octave.org/pub/octave/README\", \n\ |
695 \"README.txt\");\n\ | |
9064
7c02ec148a3c
Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
696 @end group\n\ |
6043 | 697 @end example\n\ |
698 \n\ | |
699 The full path of the downloaded file is returned in @var{f}. The\n\ | |
700 variable @var{success} is 1 if the download was successful,\n\ | |
701 otherwise it is 0 in which case @var{message} contains an error\n\ | |
702 message. If no output argument is specified and if an error occurs,\n\ | |
6588 | 703 then the error is signaled through Octave's error handling mechanism.\n\ |
6043 | 704 \n\ |
705 This function uses libcurl. Curl supports, among others, the HTTP,\n\ | |
706 FTP and FILE protocols. Username and password may be specified in\n\ | |
707 the URL, for example:\n\ | |
708 \n\ | |
709 @example\n\ | |
9064
7c02ec148a3c
Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
710 @group\n\ |
6588 | 711 urlwrite (\"http://username:password@@example.com/file.txt\",\n\ |
712 \"file.txt\");\n\ | |
9064
7c02ec148a3c
Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
713 @end group\n\ |
6043 | 714 @end example\n\ |
715 \n\ | |
716 GET and POST requests can be specified by @var{method} and @var{param}.\n\ | |
6589 | 717 The parameter @var{method} is either @samp{get} or @samp{post}\n\ |
6588 | 718 and @var{param} is a cell array of parameter and value pairs.\n\ |
719 For example:\n\ | |
6043 | 720 \n\ |
721 @example\n\ | |
9064
7c02ec148a3c
Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
722 @group\n\ |
6588 | 723 urlwrite (\"http://www.google.com/search\", \"search.html\",\n\ |
724 \"get\", @{\"query\", \"octave\"@});\n\ | |
9064
7c02ec148a3c
Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
725 @end group\n\ |
6043 | 726 @end example\n\ |
727 @seealso{urlread}\n\ | |
728 @end deftypefn") | |
729 { | |
730 octave_value_list retval; | |
731 | |
9905
84a7c11ff928
Correctly compile even when libcurl is not present according to configure
Rik <rdrider0-list@yahoo.com>
parents:
9885
diff
changeset
|
732 #ifdef HAVE_CURL |
6043 | 733 |
734 int nargin = args.length (); | |
735 | |
736 // verify arguments | |
737 if (nargin != 2 && nargin != 4) | |
738 { | |
739 print_usage (); | |
740 return retval; | |
741 } | |
742 | |
743 std::string url = args(0).string_value(); | |
744 | |
745 if (error_state) | |
746 { | |
747 error ("urlwrite: url must be a character string"); | |
748 return retval; | |
749 } | |
750 | |
751 // name to store the file if download is succesful | |
752 std::string filename = args(1).string_value(); | |
753 | |
754 if (error_state) | |
755 { | |
756 error ("urlwrite: localfile must be a character string"); | |
757 return retval; | |
758 } | |
759 | |
760 std::string method; | |
761 Cell param; // empty cell array | |
762 | |
763 if (nargin == 4) | |
764 { | |
765 method = args(2).string_value(); | |
766 | |
767 if (error_state) | |
768 { | |
769 error ("urlwrite: method can only be \"get\" or \"post\""); | |
770 return retval; | |
771 } | |
772 | |
773 if (method != "get" && method != "post") | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
774 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
775 error ("urlwrite: method can only be \"get\" or \"post\""); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
776 return retval; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
777 } |
6043 | 778 |
779 param = args(3).cell_value(); | |
780 | |
781 if (error_state) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
782 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
783 error ("urlwrite: parameters for get and post requests must be given as a cell"); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
784 return retval; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
785 } |
6043 | 786 |
787 | |
788 if (param.numel () % 2 == 1 ) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
789 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
790 error ("urlwrite: number of elements in param must be even"); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
791 return retval; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
792 } |
6043 | 793 } |
794 | |
8151
3725f819b5b3
urlwrite.cc (Furlwrite): delete files we create if download fails
John W. Eaton <jwe@octave.org>
parents:
7481
diff
changeset
|
795 // The file should only be deleted if it doesn't initially exist, we |
3725f819b5b3
urlwrite.cc (Furlwrite): delete files we create if download fails
John W. Eaton <jwe@octave.org>
parents:
7481
diff
changeset
|
796 // create it, and the download fails. We use unwind_protect to do |
3725f819b5b3
urlwrite.cc (Furlwrite): delete files we create if download fails
John W. Eaton <jwe@octave.org>
parents:
7481
diff
changeset
|
797 // it so that the deletion happens no matter how we exit the function. |
3725f819b5b3
urlwrite.cc (Furlwrite): delete files we create if download fails
John W. Eaton <jwe@octave.org>
parents:
7481
diff
changeset
|
798 |
3725f819b5b3
urlwrite.cc (Furlwrite): delete files we create if download fails
John W. Eaton <jwe@octave.org>
parents:
7481
diff
changeset
|
799 file_stat fs (filename); |
3725f819b5b3
urlwrite.cc (Furlwrite): delete files we create if download fails
John W. Eaton <jwe@octave.org>
parents:
7481
diff
changeset
|
800 |
6986 | 801 std::ofstream ofile (filename.c_str(), std::ios::out | std::ios::binary); |
6043 | 802 |
6986 | 803 if (! ofile.is_open ()) |
6043 | 804 { |
805 error ("urlwrite: unable to open file"); | |
806 return retval; | |
807 } | |
808 | |
10066
2cd940306a06
make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents:
9918
diff
changeset
|
809 unwind_protect_safe frame; |
8151
3725f819b5b3
urlwrite.cc (Furlwrite): delete files we create if download fails
John W. Eaton <jwe@octave.org>
parents:
7481
diff
changeset
|
810 |
10066
2cd940306a06
make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents:
9918
diff
changeset
|
811 frame.add_fcn (cleanup_urlwrite, filename); |
9880 | 812 |
813 bool res; | |
814 curl_handle curl = curl_handle (url, method, param, ofile, res); | |
6043 | 815 |
6986 | 816 ofile.close (); |
6043 | 817 |
9880 | 818 if (!error_state) |
10066
2cd940306a06
make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents:
9918
diff
changeset
|
819 frame.discard (); |
9880 | 820 else |
10066
2cd940306a06
make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents:
9918
diff
changeset
|
821 frame.run (); |
8151
3725f819b5b3
urlwrite.cc (Furlwrite): delete files we create if download fails
John W. Eaton <jwe@octave.org>
parents:
7481
diff
changeset
|
822 |
6043 | 823 if (nargout > 0) |
824 { | |
9880 | 825 if (res) |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
826 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
827 retval(2) = std::string (); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
828 retval(1) = true; |
10250 | 829 retval(0) = octave_env::make_absolute (filename); |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
830 } |
8151
3725f819b5b3
urlwrite.cc (Furlwrite): delete files we create if download fails
John W. Eaton <jwe@octave.org>
parents:
7481
diff
changeset
|
831 else |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
832 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
833 retval(2) = curl.lasterror (); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
834 retval(1) = false; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
835 retval(0) = std::string (); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
836 } |
6043 | 837 } |
838 | |
9880 | 839 if (nargout < 2 && res) |
840 error ("urlwrite: curl: %s", curl.lasterror ().c_str ()); | |
6043 | 841 |
842 #else | |
6981 | 843 error ("urlwrite: not available in this version of Octave"); |
6043 | 844 #endif |
845 | |
846 return retval; | |
847 } | |
848 | |
849 DEFUN_DLD (urlread, args, nargout, | |
850 "-*- texinfo -*-\n\ | |
6549 | 851 @deftypefn {Loadable Function} {@var{s} =} urlread (@var{url})\n\ |
6043 | 852 @deftypefnx {Loadable Function} {[@var{s}, @var{success}] =} urlread (@var{url})\n\ |
6549 | 853 @deftypefnx {Loadable Function} {[@var{s}, @var{success}, @var{message}] =} urlread (@var{url})\n\ |
6547 | 854 @deftypefnx {Loadable Function} {[@dots{}] =} urlread (@var{url}, @var{method}, @var{param})\n\ |
9880 | 855 Download a remote file specified by its @var{url} and return its content\n\ |
6043 | 856 in string @var{s}. For example,\n\ |
857 \n\ | |
858 @example\n\ | |
6588 | 859 s = urlread (\"ftp://ftp.octave.org/pub/octave/README\");\n\ |
6043 | 860 @end example\n\ |
861 \n\ | |
862 The variable @var{success} is 1 if the download was successful,\n\ | |
863 otherwise it is 0 in which case @var{message} contains an error\n\ | |
864 message. If no output argument is specified and if an error occurs,\n\ | |
6588 | 865 then the error is signaled through Octave's error handling mechanism.\n\ |
6043 | 866 \n\ |
867 This function uses libcurl. Curl supports, among others, the HTTP,\n\ | |
868 FTP and FILE protocols. Username and password may be specified in the\n\ | |
869 URL. For example,\n\ | |
870 \n\ | |
871 @example\n\ | |
7031 | 872 s = urlread (\"http://user:password@@example.com/file.txt\");\n\ |
6043 | 873 @end example\n\ |
874 \n\ | |
875 GET and POST requests can be specified by @var{method} and @var{param}.\n\ | |
6588 | 876 The parameter @var{method} is either @samp{get} or @samp{post}\n\ |
877 and @var{param} is a cell array of parameter and value pairs.\n\ | |
6650 | 878 For example,\n\ |
6043 | 879 \n\ |
880 @example\n\ | |
9064
7c02ec148a3c
Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
881 @group\n\ |
6588 | 882 s = urlread (\"http://www.google.com/search\", \"get\",\n\ |
883 @{\"query\", \"octave\"@});\n\ | |
9064
7c02ec148a3c
Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
884 @end group\n\ |
6043 | 885 @end example\n\ |
886 @seealso{urlwrite}\n\ | |
887 @end deftypefn") | |
888 { | |
6588 | 889 // Octave's return value |
6043 | 890 octave_value_list retval; |
891 | |
9905
84a7c11ff928
Correctly compile even when libcurl is not present according to configure
Rik <rdrider0-list@yahoo.com>
parents:
9885
diff
changeset
|
892 #ifdef HAVE_CURL |
6043 | 893 |
894 int nargin = args.length (); | |
895 | |
896 // verify arguments | |
897 if (nargin != 1 && nargin != 3) | |
898 { | |
899 print_usage (); | |
900 return retval; | |
901 } | |
902 | |
903 std::string url = args(0).string_value(); | |
904 | |
905 if (error_state) | |
906 { | |
907 error ("urlread: url must be a character string"); | |
908 return retval; | |
909 } | |
910 | |
911 std::string method; | |
912 Cell param; // empty cell array | |
913 | |
914 if (nargin == 3) | |
915 { | |
916 method = args(1).string_value(); | |
917 | |
918 if (error_state) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
919 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
920 error ("urlread: method can only be \"get\" or \"post\""); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
921 return retval; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
922 } |
6043 | 923 |
924 if (method != "get" && method != "post") | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
925 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
926 error ("urlread: method can only be \"get\" or \"post\""); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
927 return retval; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
928 } |
6043 | 929 |
930 param = args(2).cell_value(); | |
931 | |
932 if (error_state) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
933 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
934 error ("urlread: parameters for get and post requests must be given as a cell"); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
935 return retval; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
936 } |
6043 | 937 |
938 if (param.numel () % 2 == 1 ) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
939 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
940 error ("urlread: number of elements in param must be even"); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
941 return retval; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
942 } |
6043 | 943 } |
944 | |
6986 | 945 std::ostringstream buf; |
6043 | 946 |
9880 | 947 bool res; |
948 curl_handle curl = curl_handle (url, method, param, buf, res); | |
6043 | 949 |
950 if (nargout > 0) | |
951 { | |
6986 | 952 retval(0) = buf.str (); |
9880 | 953 retval(1) = res; |
6986 | 954 // Return empty string if no error occured. |
9880 | 955 retval(2) = res ? "" : curl.lasterror (); |
6043 | 956 } |
957 | |
9880 | 958 if (nargout < 2 && !res) |
959 error ("urlread: curl: %s", curl.lasterror().c_str()); | |
6043 | 960 |
961 #else | |
6981 | 962 error ("urlread: not available in this version of Octave"); |
6043 | 963 #endif |
964 | |
965 return retval; | |
966 } | |
9880 | 967 |
968 DEFUN_DLD (__ftp__, args, , | |
969 "-*- texinfo -*-\n\ | |
970 @deftypefn {Loadable Function} {} __ftp__ (@var{handle}, @var{host})\n\ | |
971 @deftypefnx {Loadable Function} {} __ftp__ (@var{handle}, @var{host}, @var{username}, @var{password})\n\ | |
972 Undocumented internal function\n\ | |
973 @end deftypefn") | |
974 { | |
975 #ifdef HAVE_CURL | |
976 int nargin = args.length (); | |
977 std::string handle; | |
978 std::string host; | |
979 std::string user = "anonymous"; | |
980 std::string passwd = ""; | |
981 | |
982 if (nargin < 2 || nargin > 4) | |
983 error ("incorrect number of arguments"); | |
984 else | |
985 { | |
986 handle = args(0).string_value (); | |
987 host = args(1).string_value (); | |
988 | |
989 if (nargin > 1) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
990 user = args(2).string_value (); |
9880 | 991 |
992 if (nargin > 2) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
993 passwd = args(3).string_value (); |
9880 | 994 |
995 if (!error_state) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
996 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
997 handles.contents (handle) = curl_handle (host, user, passwd); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
998 |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
999 if (error_state) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1000 handles.del (handle); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1001 } |
9880 | 1002 } |
1003 #else | |
1004 error ("__ftp__: not available in this version of Octave"); | |
1005 #endif | |
1006 | |
1007 return octave_value (); | |
1008 } | |
1009 | |
1010 DEFUN_DLD (__ftp_pwd__, args, , | |
1011 "-*- texinfo -*-\n\ | |
1012 @deftypefn {Loadable Function} {} __ftp_pwd__ (@var{handle})\n\ | |
1013 Undocumented internal function\n\ | |
1014 @end deftypefn") | |
1015 { | |
1016 octave_value retval; | |
1017 #ifdef HAVE_CURL | |
1018 int nargin = args.length (); | |
1019 | |
1020 if (nargin != 1) | |
1021 error ("incorrect number of arguments"); | |
1022 else | |
1023 { | |
1024 std::string handle = args(0).string_value (); | |
1025 | |
1026 if (!error_state) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1027 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1028 const curl_handle curl = handles.contents (handle); |
9880 | 1029 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1030 if (curl.is_valid ()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1031 retval = curl.pwd (); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1032 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1033 error ("__ftp_pwd__: invalid ftp handle"); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1034 } |
9880 | 1035 } |
1036 #else | |
1037 error ("__ftp_pwd__: not available in this version of Octave"); | |
1038 #endif | |
1039 | |
1040 return retval; | |
1041 } | |
1042 | |
1043 DEFUN_DLD (__ftp_cwd__, args, , | |
1044 "-*- texinfo -*-\n\ | |
1045 @deftypefn {Loadable Function} {} __ftp_cwd__ (@var{handle}, @var{path})\n\ | |
1046 Undocumented internal function\n\ | |
1047 @end deftypefn") | |
1048 { | |
1049 #ifdef HAVE_CURL | |
1050 int nargin = args.length (); | |
1051 | |
1052 if (nargin != 1 && nargin != 2) | |
1053 error ("incorrect number of arguments"); | |
1054 else | |
1055 { | |
1056 std::string handle = args(0).string_value (); | |
1057 std::string path = ""; | |
1058 | |
1059 if (nargin > 1) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1060 path = args(1).string_value (); |
9880 | 1061 |
1062 if (!error_state) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1063 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1064 const curl_handle curl = handles.contents (handle); |
9880 | 1065 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1066 if (curl.is_valid ()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1067 curl.cwd (path); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1068 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1069 error ("__ftp_cwd__: invalid ftp handle"); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1070 } |
9880 | 1071 } |
1072 #else | |
1073 error ("__ftp_cwd__: not available in this version of Octave"); | |
1074 #endif | |
1075 | |
1076 return octave_value (); | |
1077 } | |
1078 | |
1079 DEFUN_DLD (__ftp_dir__, args, nargout, | |
1080 "-*- texinfo -*-\n\ | |
1081 @deftypefn {Loadable Function} {} __ftp_dir__ (@var{handle})\n\ | |
1082 Undocumented internal function\n\ | |
1083 @end deftypefn") | |
1084 { | |
1085 octave_value retval; | |
1086 #ifdef HAVE_CURL | |
1087 int nargin = args.length (); | |
1088 | |
1089 if (nargin != 1) | |
1090 error ("incorrect number of arguments"); | |
1091 else | |
1092 { | |
1093 std::string handle = args(0).string_value (); | |
1094 | |
1095 if (!error_state) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1096 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1097 const curl_handle curl = handles.contents (handle); |
9880 | 1098 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1099 if (curl.is_valid ()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1100 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1101 if (nargout == 0) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1102 curl.dir (); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1103 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1104 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1105 string_vector sv = curl.list (); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1106 octave_idx_type n = sv.length (); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1107 if (n == 0) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1108 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1109 string_vector flds (5); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1110 flds(0) = "name"; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1111 flds(1) = "date"; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1112 flds(2) = "bytes"; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1113 flds(3) = "isdir"; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1114 flds(4) = "datenum"; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1115 retval = Octave_map (flds); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1116 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1117 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1118 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1119 Octave_map st; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1120 Cell filectime (dim_vector (n, 1)); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1121 Cell filesize (dim_vector (n, 1)); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1122 Cell fileisdir (dim_vector (n, 1)); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1123 Cell filedatenum (dim_vector (n, 1)); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1124 |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1125 st.assign ("name", Cell (sv)); |
9880 | 1126 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1127 for (octave_idx_type i = 0; i < n; i++) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1128 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1129 time_t ftime; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1130 bool fisdir; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1131 double fsize; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1132 |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1133 curl.get_fileinfo (sv(i), fsize, ftime, fisdir); |
9880 | 1134 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1135 fileisdir (i) = fisdir; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1136 filectime (i) = ctime (&ftime); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1137 filesize (i) = fsize; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1138 filedatenum (i) = double (ftime); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1139 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1140 st.assign ("date", filectime); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1141 st.assign ("bytes", filesize); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1142 st.assign ("isdir", fileisdir); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1143 st.assign ("datenum", filedatenum); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1144 retval = st; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1145 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1146 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1147 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1148 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1149 error ("__ftp_dir__: invalid ftp handle"); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1150 } |
9880 | 1151 } |
1152 #else | |
1153 error ("__ftp_dir__: not available in this version of Octave"); | |
1154 #endif | |
1155 | |
1156 return retval; | |
1157 } | |
1158 | |
1159 DEFUN_DLD (__ftp_ascii__, args, , | |
1160 "-*- texinfo -*-\n\ | |
1161 @deftypefn {Loadable Function} {} __ftp_ascii__ (@var{handle})\n\ | |
1162 Undocumented internal function\n\ | |
1163 @end deftypefn") | |
1164 { | |
1165 #ifdef HAVE_CURL | |
1166 int nargin = args.length (); | |
1167 | |
1168 if (nargin != 1) | |
1169 error ("incorrect number of arguments"); | |
1170 else | |
1171 { | |
1172 std::string handle = args(0).string_value (); | |
1173 | |
1174 if (!error_state) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1175 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1176 const curl_handle curl = handles.contents (handle); |
9880 | 1177 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1178 if (curl.is_valid ()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1179 curl.ascii (); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1180 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1181 error ("__ftp_ascii__: invalid ftp handle"); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1182 } |
9880 | 1183 } |
1184 #else | |
1185 error ("__ftp_ascii__: not available in this version of Octave"); | |
1186 #endif | |
1187 | |
1188 return octave_value (); | |
1189 } | |
1190 | |
1191 DEFUN_DLD (__ftp_binary__, args, , | |
1192 "-*- texinfo -*-\n\ | |
1193 @deftypefn {Loadable Function} {} __ftp_binary__ (@var{handle})\n\ | |
1194 Undocumented internal function\n\ | |
1195 @end deftypefn") | |
1196 { | |
1197 #ifdef HAVE_CURL | |
1198 int nargin = args.length (); | |
1199 | |
1200 if (nargin != 1) | |
1201 error ("incorrect number of arguments"); | |
1202 else | |
1203 { | |
1204 std::string handle = args(0).string_value (); | |
1205 | |
1206 if (!error_state) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1207 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1208 const curl_handle curl = handles.contents (handle); |
9880 | 1209 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1210 if (curl.is_valid ()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1211 curl.binary (); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1212 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1213 error ("__ftp_binary__: invalid ftp handle"); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1214 } |
9880 | 1215 } |
1216 #else | |
1217 error ("__ftp_binary__: not available in this version of Octave"); | |
1218 #endif | |
1219 | |
1220 return octave_value (); | |
1221 } | |
1222 | |
1223 DEFUN_DLD (__ftp_close__, args, , | |
1224 "-*- texinfo -*-\n\ | |
1225 @deftypefn {Loadable Function} {} __ftp_close__ (@var{handle})\n\ | |
1226 Undocumented internal function\n\ | |
1227 @end deftypefn") | |
1228 { | |
1229 #ifdef HAVE_CURL | |
1230 int nargin = args.length (); | |
1231 | |
1232 if (nargin != 1) | |
1233 error ("incorrect number of arguments"); | |
1234 else | |
1235 { | |
1236 std::string handle = args(0).string_value (); | |
1237 | |
1238 if (!error_state) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1239 handles.del (handle); |
9880 | 1240 } |
1241 #else | |
1242 error ("__ftp_close__: not available in this version of Octave"); | |
1243 #endif | |
1244 | |
1245 return octave_value (); | |
1246 } | |
1247 | |
1248 DEFUN_DLD (__ftp_mode__, args, , | |
1249 "-*- texinfo -*-\n\ | |
1250 @deftypefn {Loadable Function} {} __ftp_mode__ (@var{handle})\n\ | |
1251 Undocumented internal function\n\ | |
1252 @end deftypefn") | |
1253 { | |
1254 octave_value retval; | |
1255 #ifdef HAVE_CURL | |
1256 int nargin = args.length (); | |
1257 | |
1258 if (nargin != 1) | |
1259 error ("incorrect number of arguments"); | |
1260 else | |
1261 { | |
1262 std::string handle = args(0).string_value (); | |
1263 | |
1264 | |
1265 if (!error_state) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1266 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1267 const curl_handle curl = handles.contents (handle); |
9880 | 1268 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1269 if (curl.is_valid ()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1270 retval = (curl.is_ascii() ? "ascii" : "binary"); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1271 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1272 error ("__ftp_binary__: invalid ftp handle"); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1273 } |
9880 | 1274 } |
1275 #else | |
1276 error ("__ftp_mode__: not available in this version of Octave"); | |
1277 #endif | |
1278 | |
1279 return retval; | |
1280 } | |
1281 | |
1282 DEFUN_DLD (__ftp_delete__, args, , | |
1283 "-*- texinfo -*-\n\ | |
1284 @deftypefn {Loadable Function} {} __ftp_delete__ (@var{handle}, @var{path})\n\ | |
1285 Undocumented internal function\n\ | |
1286 @end deftypefn") | |
1287 { | |
1288 #ifdef HAVE_CURL | |
1289 int nargin = args.length (); | |
1290 | |
1291 if (nargin != 2) | |
1292 error ("incorrect number of arguments"); | |
1293 else | |
1294 { | |
1295 std::string handle = args(0).string_value (); | |
1296 std::string file = args(1).string_value (); | |
1297 | |
1298 if (!error_state) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1299 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1300 const curl_handle curl = handles.contents (handle); |
9880 | 1301 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1302 if (curl.is_valid ()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1303 curl.del (file); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1304 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1305 error ("__ftp_delete__: invalid ftp handle"); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1306 } |
9880 | 1307 } |
1308 #else | |
1309 error ("__ftp_delete__: not available in this version of Octave"); | |
1310 #endif | |
1311 | |
1312 return octave_value (); | |
1313 } | |
1314 | |
1315 DEFUN_DLD (__ftp_rmdir__, args, , | |
1316 "-*- texinfo -*-\n\ | |
1317 @deftypefn {Loadable Function} {} __ftp_rmdir__ (@var{handle}, @var{path})\n\ | |
1318 Undocumented internal function\n\ | |
1319 @end deftypefn") | |
1320 { | |
1321 #ifdef HAVE_CURL | |
1322 int nargin = args.length (); | |
1323 | |
1324 if (nargin != 2) | |
1325 error ("incorrect number of arguments"); | |
1326 else | |
1327 { | |
1328 std::string handle = args(0).string_value (); | |
1329 std::string dir = args(1).string_value (); | |
1330 | |
1331 if (!error_state) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1332 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1333 const curl_handle curl = handles.contents (handle); |
9880 | 1334 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1335 if (curl.is_valid ()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1336 curl.rmdir (dir); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1337 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1338 error ("__ftp_rmdir__: invalid ftp handle"); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1339 } |
9880 | 1340 } |
1341 #else | |
1342 error ("__ftp_rmdir__: not available in this version of Octave"); | |
1343 #endif | |
1344 | |
1345 return octave_value (); | |
1346 } | |
1347 | |
1348 DEFUN_DLD (__ftp_mkdir__, args, , | |
1349 "-*- texinfo -*-\n\ | |
1350 @deftypefn {Loadable Function} {} __ftp_mkdir__ (@var{handle}, @var{path})\n\ | |
1351 Undocumented internal function\n\ | |
1352 @end deftypefn") | |
1353 { | |
1354 #ifdef HAVE_CURL | |
1355 int nargin = args.length (); | |
1356 | |
1357 if (nargin != 2) | |
1358 error ("incorrect number of arguments"); | |
1359 else | |
1360 { | |
1361 std::string handle = args(0).string_value (); | |
1362 std::string dir = args(1).string_value (); | |
1363 | |
1364 if (!error_state) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1365 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1366 const curl_handle curl = handles.contents (handle); |
9880 | 1367 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1368 if (curl.is_valid ()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1369 curl.mkdir (dir); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1370 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1371 error ("__ftp_mkdir__: invalid ftp handle"); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1372 } |
9880 | 1373 } |
1374 #else | |
1375 error ("__ftp_mkdir__: not available in this version of Octave"); | |
1376 #endif | |
1377 | |
1378 return octave_value (); | |
1379 } | |
1380 | |
1381 DEFUN_DLD (__ftp_rename__, args, , | |
1382 "-*- texinfo -*-\n\ | |
1383 @deftypefn {Loadable Function} {} __ftp_rename__ (@var{handle}, @var{path})\n\ | |
1384 Undocumented internal function\n\ | |
1385 @end deftypefn") | |
1386 { | |
1387 #ifdef HAVE_CURL | |
1388 int nargin = args.length (); | |
1389 | |
1390 if (nargin != 3) | |
1391 error ("incorrect number of arguments"); | |
1392 else | |
1393 { | |
1394 std::string handle = args(0).string_value (); | |
1395 std::string oldname = args(1).string_value (); | |
1396 std::string newname = args(2).string_value (); | |
1397 | |
1398 if (!error_state) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1399 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1400 const curl_handle curl = handles.contents (handle); |
9880 | 1401 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1402 if (curl.is_valid ()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1403 curl.rename (oldname, newname); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1404 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1405 error ("__ftp_rename__: invalid ftp handle"); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1406 } |
9880 | 1407 } |
1408 #else | |
1409 error ("__ftp_rename__: not available in this version of Octave"); | |
1410 #endif | |
1411 | |
1412 return octave_value (); | |
1413 } | |
1414 | |
9905
84a7c11ff928
Correctly compile even when libcurl is not present according to configure
Rik <rdrider0-list@yahoo.com>
parents:
9885
diff
changeset
|
1415 #ifdef HAVE_CURL |
9880 | 1416 static string_vector |
1417 mput_directory (const curl_handle& curl, const std::string& base, | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1418 const std::string& dir) |
9880 | 1419 { |
1420 string_vector retval; | |
1421 | |
1422 if (! curl.mkdir (dir, false)) | |
1423 warning ("__ftp_mput__: can not create the remote directory ""%s""", | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1424 (base.length() == 0 ? dir : base + |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1425 file_ops::dir_sep_str () + dir).c_str ()); |
9880 | 1426 |
1427 curl.cwd (dir); | |
1428 | |
1429 if (! error_state) | |
1430 { | |
10066
2cd940306a06
make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents:
9918
diff
changeset
|
1431 unwind_protect_safe frame; |
9880 | 1432 |
10066
2cd940306a06
make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents:
9918
diff
changeset
|
1433 frame.add_fcn (reset_path, curl); |
9880 | 1434 |
1435 std::string realdir = base.length() == 0 ? dir : base + | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1436 file_ops::dir_sep_str () + dir; |
9880 | 1437 |
1438 dir_entry dirlist (realdir); | |
1439 | |
1440 if (dirlist) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1441 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1442 string_vector files = dirlist.read (); |
9880 | 1443 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1444 for (octave_idx_type i = 0; i < files.length (); i++) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1445 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1446 std::string file = files (i); |
9880 | 1447 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1448 if (file == "." || file == "..") |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1449 continue; |
9880 | 1450 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1451 std::string realfile = realdir + file_ops::dir_sep_str () + file; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1452 file_stat fs (realfile); |
9880 | 1453 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1454 if (! fs.exists ()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1455 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1456 error ("__ftp__mput: file ""%s"" does not exist", |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1457 realfile.c_str ()); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1458 break; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1459 } |
9880 | 1460 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1461 if (fs.is_dir ()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1462 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1463 retval.append (mput_directory (curl, realdir, file)); |
9880 | 1464 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1465 if (error_state) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1466 break; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1467 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1468 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1469 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1470 // FIXME Does ascii mode need to be flagged here? |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1471 std::ifstream ifile (realfile.c_str(), std::ios::in | |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1472 std::ios::binary); |
9880 | 1473 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1474 if (! ifile.is_open ()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1475 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1476 error ("__ftp_mput__: unable to open file ""%s""", |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1477 realfile.c_str ()); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1478 break; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1479 } |
9880 | 1480 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1481 curl.put (file, ifile); |
9880 | 1482 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1483 ifile.close (); |
9880 | 1484 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1485 if (error_state) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1486 break; |
9880 | 1487 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1488 retval.append (realfile); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1489 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1490 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1491 } |
9880 | 1492 else |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1493 error ("__ftp_mput__: can not read the directory ""%s""", |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1494 realdir.c_str()); |
9880 | 1495 } |
1496 | |
1497 return retval; | |
1498 } | |
9905
84a7c11ff928
Correctly compile even when libcurl is not present according to configure
Rik <rdrider0-list@yahoo.com>
parents:
9885
diff
changeset
|
1499 #endif |
9880 | 1500 |
1501 DEFUN_DLD (__ftp_mput__, args, nargout, | |
1502 "-*- texinfo -*-\n\ | |
1503 @deftypefn {Loadable Function} {} __ftp_mput__ (@var{handle}, @var{files})\n\ | |
1504 Undocumented internal function\n\ | |
1505 @end deftypefn") | |
1506 { | |
1507 string_vector retval; | |
1508 | |
1509 #ifdef HAVE_CURL | |
1510 int nargin = args.length (); | |
1511 | |
1512 if (nargin != 2) | |
1513 error ("incorrect number of arguments"); | |
1514 else | |
1515 { | |
1516 std::string handle = args(0).string_value (); | |
1517 std::string pat = args(1).string_value (); | |
1518 | |
1519 if (!error_state) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1520 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1521 const curl_handle curl = handles.contents (handle); |
9880 | 1522 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1523 if (curl.is_valid ()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1524 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1525 glob_match pattern (file_ops::tilde_expand (pat)); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1526 string_vector files = pattern.glob (); |
9880 | 1527 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1528 for (octave_idx_type i = 0; i < files.length (); i++) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1529 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1530 std::string file = files (i); |
9880 | 1531 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1532 file_stat fs (file); |
9880 | 1533 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1534 if (! fs.exists ()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1535 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1536 error ("__ftp__mput: file does not exist"); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1537 break; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1538 } |
9880 | 1539 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1540 if (fs.is_dir ()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1541 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1542 retval.append (mput_directory (curl, "", file)); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1543 if (error_state) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1544 break; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1545 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1546 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1547 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1548 // FIXME Does ascii mode need to be flagged here? |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1549 std::ifstream ifile (file.c_str(), std::ios::in | |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1550 std::ios::binary); |
9880 | 1551 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1552 if (! ifile.is_open ()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1553 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1554 error ("__ftp_mput__: unable to open file"); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1555 break; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1556 } |
9880 | 1557 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1558 curl.put (file, ifile); |
9880 | 1559 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1560 ifile.close (); |
9880 | 1561 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1562 if (error_state) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1563 break; |
9880 | 1564 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1565 retval.append (file); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1566 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1567 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1568 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1569 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1570 error ("__ftp_mput__: invalid ftp handle"); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1571 } |
9880 | 1572 } |
1573 #else | |
1574 error ("__ftp_mput__: not available in this version of Octave"); | |
1575 #endif | |
1576 | |
1577 return (nargout > 0 ? octave_value (retval) : octave_value ()); | |
1578 } | |
1579 | |
1580 #ifdef HAVE_CURL | |
1581 static void | |
1582 getallfiles (const curl_handle& curl, const std::string& dir, | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1583 const std::string& target) |
9880 | 1584 { |
1585 std::string sep = file_ops::dir_sep_str (); | |
1586 file_stat fs (dir); | |
1587 | |
1588 if (!fs || !fs.is_dir ()) | |
1589 { | |
1590 std::string msg; | |
10197
4d433bd2d4dc
attempt to avoid trouble with gnulib #defines in a consistent way
John W. Eaton <jwe@octave.org>
parents:
10154
diff
changeset
|
1591 int status = octave_mkdir (dir, 0777, msg); |
9880 | 1592 |
1593 if (status < 0) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1594 error ("__ftp_mget__: can't create directory %s%s%s. %s", |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1595 target.c_str(), sep.c_str(), dir.c_str(), msg.c_str()); |
9880 | 1596 } |
1597 | |
1598 if (! error_state) | |
1599 { | |
1600 curl.cwd (dir); | |
1601 | |
1602 if (! error_state) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1603 { |
10066
2cd940306a06
make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents:
9918
diff
changeset
|
1604 unwind_protect_safe frame; |
9880 | 1605 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1606 frame.add_fcn (reset_path, curl); |
9880 | 1607 |
10066
2cd940306a06
make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents:
9918
diff
changeset
|
1608 string_vector sv = curl.list (); |
9880 | 1609 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1610 for (octave_idx_type i = 0; i < sv.length (); i++) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1611 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1612 time_t ftime; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1613 bool fisdir; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1614 double fsize; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1615 |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1616 curl.get_fileinfo (sv(i), fsize, ftime, fisdir); |
9880 | 1617 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1618 if (fisdir) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1619 getallfiles (curl, sv(i), target + dir + sep); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1620 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1621 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1622 std::string realfile = target + dir + sep + sv(i); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1623 std::ofstream ofile (realfile.c_str(), |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1624 std::ios::out | |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1625 std::ios::binary); |
9880 | 1626 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1627 if (! ofile.is_open ()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1628 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1629 error ("__ftp_mget__: unable to open file"); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1630 break; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1631 } |
9880 | 1632 |
10066
2cd940306a06
make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents:
9918
diff
changeset
|
1633 unwind_protect_safe frame2; |
9880 | 1634 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1635 frame2.add_fcn (delete_file, realfile); |
9880 | 1636 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1637 curl.get (sv(i), ofile); |
9880 | 1638 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1639 ofile.close (); |
9880 | 1640 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1641 if (!error_state) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1642 frame2.discard (); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1643 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1644 frame2.run (); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1645 } |
9880 | 1646 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1647 if (error_state) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1648 break; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1649 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1650 } |
9880 | 1651 } |
1652 } | |
1653 #endif | |
1654 | |
1655 DEFUN_DLD (__ftp_mget__, args, , | |
1656 "-*- texinfo -*-\n\ | |
1657 @deftypefn {Loadable Function} {} __ftp_mget__ (@var{handle}, @var{files})\n\ | |
1658 Undocumented internal function\n\ | |
1659 @end deftypefn") | |
1660 { | |
1661 #ifdef HAVE_CURL | |
1662 int nargin = args.length (); | |
1663 | |
1664 if (nargin != 2 && nargin != 3) | |
1665 error ("incorrect number of arguments"); | |
1666 else | |
1667 { | |
1668 std::string handle = args(0).string_value (); | |
1669 std::string file = args(1).string_value (); | |
1670 std::string target; | |
1671 | |
1672 if (nargin == 3) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1673 target = args(2).string_value () + file_ops::dir_sep_str (); |
9880 | 1674 |
1675 if (! error_state) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1676 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1677 const curl_handle curl = handles.contents (handle); |
9880 | 1678 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1679 if (curl.is_valid ()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1680 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1681 string_vector sv = curl.list (); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1682 octave_idx_type n = 0; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1683 glob_match pattern (file); |
9880 | 1684 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1685 for (octave_idx_type i = 0; i < sv.length (); i++) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1686 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1687 if (pattern.match (sv(i))) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1688 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1689 n++; |
9880 | 1690 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1691 time_t ftime; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1692 bool fisdir; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1693 double fsize; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1694 |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1695 curl.get_fileinfo (sv(i), fsize, ftime, fisdir); |
9880 | 1696 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1697 if (fisdir) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1698 getallfiles (curl, sv(i), target); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1699 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1700 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1701 std::ofstream ofile ((target + sv(i)).c_str(), |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1702 std::ios::out | |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1703 std::ios::binary); |
9880 | 1704 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1705 if (! ofile.is_open ()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1706 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1707 error ("__ftp_mget__: unable to open file"); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1708 break; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1709 } |
9880 | 1710 |
10066
2cd940306a06
make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents:
9918
diff
changeset
|
1711 unwind_protect_safe frame; |
9880 | 1712 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1713 frame.add_fcn (delete_file, target + sv(i)); |
9880 | 1714 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1715 curl.get (sv(i), ofile); |
9880 | 1716 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1717 ofile.close (); |
9880 | 1718 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1719 if (!error_state) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1720 frame.discard (); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1721 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1722 frame.run (); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1723 } |
9880 | 1724 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1725 if (error_state) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1726 break; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1727 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1728 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1729 if (n == 0) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1730 error ("__ftp_mget__: file not found"); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1731 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1732 } |
9880 | 1733 } |
1734 #else | |
1735 error ("__ftp_mget__: not available in this version of Octave"); | |
1736 #endif | |
1737 | |
1738 return octave_value (); | |
1739 } |