Mercurial > hg > octave-jordi
annotate src/DLD-FUNCTIONS/urlwrite.cc @ 11553:01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Use same variable names in error() strings and in documentation.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Sun, 16 Jan 2011 22:13:23 -0800 |
parents | 50a7935f2512 |
children | 12df7854fa7c |
rev | line source |
---|---|
6043 | 1 // urlwrite and urlread, a curl front-end for octave |
2 /* | |
3 | |
11523 | 4 Copyright (C) 2006-2011 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 | |
11548
50a7935f2512
Don't invalidate iterators when calling std::map::erase, found by cppcheck
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11523
diff
changeset
|
616 map.erase (begin(), end()); |
6992 | 617 |
9880 | 618 curl_global_cleanup (); |
619 } | |
620 | |
621 iterator begin (void) { return iterator (map.begin ()); } | |
622 const_iterator begin (void) const { return const_iterator (map.begin ()); } | |
623 | |
624 iterator end (void) { return iterator (map.end ()); } | |
625 const_iterator end (void) const { return const_iterator (map.end ()); } | |
6992 | 626 |
9880 | 627 iterator seek (const std::string& k) { return map.find (k); } |
628 const_iterator seek (const std::string& k) const { return map.find (k); } | |
629 | |
630 std::string key (const_iterator p) const { return p->first; } | |
631 | |
632 curl_handle& contents (const std::string& k) | |
633 { | |
634 return map[k]; | |
635 } | |
636 | |
637 curl_handle contents (const std::string& k) const | |
638 { | |
639 const_iterator p = seek (k); | |
640 return p != end () ? p->second : curl_handle (); | |
641 } | |
642 | |
643 curl_handle& contents (iterator p) | |
644 { return p->second; } | |
6992 | 645 |
9880 | 646 curl_handle contents (const_iterator p) const |
647 { return p->second; } | |
648 | |
649 void del (const std::string& k) | |
650 { | |
651 iterator p = map.find (k); | |
652 | |
653 if (p != map.end ()) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
654 map.erase (p); |
9880 | 655 } |
6043 | 656 |
9880 | 657 private: |
658 std::map<std::string, curl_handle> map; | |
659 }; | |
660 | |
661 static curl_handles handles; | |
662 | |
10197
4d433bd2d4dc
attempt to avoid trouble with gnulib #defines in a consistent way
John W. Eaton <jwe@octave.org>
parents:
10154
diff
changeset
|
663 static void |
9880 | 664 cleanup_urlwrite (std::string filename) |
665 { | |
10197
4d433bd2d4dc
attempt to avoid trouble with gnulib #defines in a consistent way
John W. Eaton <jwe@octave.org>
parents:
10154
diff
changeset
|
666 octave_unlink (filename); |
6043 | 667 } |
668 | |
10197
4d433bd2d4dc
attempt to avoid trouble with gnulib #defines in a consistent way
John W. Eaton <jwe@octave.org>
parents:
10154
diff
changeset
|
669 static void |
9880 | 670 reset_path (const curl_handle curl) |
671 { | |
672 curl.cwd (".."); | |
673 } | |
8151
3725f819b5b3
urlwrite.cc (Furlwrite): delete files we create if download fails
John W. Eaton <jwe@octave.org>
parents:
7481
diff
changeset
|
674 |
10197
4d433bd2d4dc
attempt to avoid trouble with gnulib #defines in a consistent way
John W. Eaton <jwe@octave.org>
parents:
10154
diff
changeset
|
675 static void |
4d433bd2d4dc
attempt to avoid trouble with gnulib #defines in a consistent way
John W. Eaton <jwe@octave.org>
parents:
10154
diff
changeset
|
676 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
|
677 { |
10197
4d433bd2d4dc
attempt to avoid trouble with gnulib #defines in a consistent way
John W. Eaton <jwe@octave.org>
parents:
10154
diff
changeset
|
678 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
|
679 } |
9880 | 680 #endif |
8151
3725f819b5b3
urlwrite.cc (Furlwrite): delete files we create if download fails
John W. Eaton <jwe@octave.org>
parents:
7481
diff
changeset
|
681 |
6043 | 682 DEFUN_DLD (urlwrite, args, nargout, |
683 "-*- texinfo -*-\n\ | |
10840 | 684 @deftypefn {Loadable Function} {} urlwrite (@var{url}, @var{localfile})\n\ |
6043 | 685 @deftypefnx {Loadable Function} {@var{f} =} urlwrite (@var{url}, @var{localfile})\n\ |
686 @deftypefnx {Loadable Function} {[@var{f}, @var{success}] =} urlwrite (@var{url}, @var{localfile})\n\ | |
687 @deftypefnx {Loadable Function} {[@var{f}, @var{success}, @var{message}] =} urlwrite (@var{url}, @var{localfile})\n\ | |
9880 | 688 Download a remote file specified by its @var{url} and save it as\n\ |
10840 | 689 @var{localfile}. For example:\n\ |
6043 | 690 \n\ |
691 @example\n\ | |
9064
7c02ec148a3c
Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
692 @group\n\ |
7031 | 693 urlwrite (\"ftp://ftp.octave.org/pub/octave/README\", \n\ |
694 \"README.txt\");\n\ | |
9064
7c02ec148a3c
Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
695 @end group\n\ |
6043 | 696 @end example\n\ |
697 \n\ | |
698 The full path of the downloaded file is returned in @var{f}. The\n\ | |
699 variable @var{success} is 1 if the download was successful,\n\ | |
700 otherwise it is 0 in which case @var{message} contains an error\n\ | |
11553
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11548
diff
changeset
|
701 message. If no output argument is specified and an error occurs,\n\ |
6588 | 702 then the error is signaled through Octave's error handling mechanism.\n\ |
6043 | 703 \n\ |
704 This function uses libcurl. Curl supports, among others, the HTTP,\n\ | |
705 FTP and FILE protocols. Username and password may be specified in\n\ | |
706 the URL, for example:\n\ | |
707 \n\ | |
708 @example\n\ | |
9064
7c02ec148a3c
Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
709 @group\n\ |
6588 | 710 urlwrite (\"http://username:password@@example.com/file.txt\",\n\ |
711 \"file.txt\");\n\ | |
9064
7c02ec148a3c
Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
712 @end group\n\ |
6043 | 713 @end example\n\ |
714 \n\ | |
715 GET and POST requests can be specified by @var{method} and @var{param}.\n\ | |
6589 | 716 The parameter @var{method} is either @samp{get} or @samp{post}\n\ |
6588 | 717 and @var{param} is a cell array of parameter and value pairs.\n\ |
718 For example:\n\ | |
6043 | 719 \n\ |
720 @example\n\ | |
9064
7c02ec148a3c
Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
721 @group\n\ |
6588 | 722 urlwrite (\"http://www.google.com/search\", \"search.html\",\n\ |
723 \"get\", @{\"query\", \"octave\"@});\n\ | |
9064
7c02ec148a3c
Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
724 @end group\n\ |
6043 | 725 @end example\n\ |
726 @seealso{urlread}\n\ | |
727 @end deftypefn") | |
728 { | |
729 octave_value_list retval; | |
730 | |
9905
84a7c11ff928
Correctly compile even when libcurl is not present according to configure
Rik <rdrider0-list@yahoo.com>
parents:
9885
diff
changeset
|
731 #ifdef HAVE_CURL |
6043 | 732 |
733 int nargin = args.length (); | |
734 | |
735 // verify arguments | |
736 if (nargin != 2 && nargin != 4) | |
737 { | |
738 print_usage (); | |
739 return retval; | |
740 } | |
741 | |
742 std::string url = args(0).string_value(); | |
743 | |
744 if (error_state) | |
745 { | |
11553
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11548
diff
changeset
|
746 error ("urlwrite: URL must be a character string"); |
6043 | 747 return retval; |
748 } | |
749 | |
750 // name to store the file if download is succesful | |
751 std::string filename = args(1).string_value(); | |
752 | |
753 if (error_state) | |
754 { | |
11553
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11548
diff
changeset
|
755 error ("urlwrite: LOCALFILE must be a character string"); |
6043 | 756 return retval; |
757 } | |
758 | |
759 std::string method; | |
760 Cell param; // empty cell array | |
761 | |
762 if (nargin == 4) | |
763 { | |
764 method = args(2).string_value(); | |
765 | |
766 if (error_state) | |
767 { | |
11553
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11548
diff
changeset
|
768 error ("urlwrite: METHOD must be \"get\" or \"post\""); |
6043 | 769 return retval; |
770 } | |
771 | |
772 if (method != "get" && method != "post") | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
773 { |
11553
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11548
diff
changeset
|
774 error ("urlwrite: METHOD must be \"get\" or \"post\""); |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
775 return retval; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
776 } |
6043 | 777 |
778 param = args(3).cell_value(); | |
779 | |
780 if (error_state) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
781 { |
11553
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11548
diff
changeset
|
782 error ("urlwrite: parameters (PARAM) for get and post requests must be given as a cell"); |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
783 return retval; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
784 } |
6043 | 785 |
786 | |
787 if (param.numel () % 2 == 1 ) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
788 { |
11553
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11548
diff
changeset
|
789 error ("urlwrite: number of elements in PARAM must be even"); |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
790 return retval; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
791 } |
6043 | 792 } |
793 | |
8151
3725f819b5b3
urlwrite.cc (Furlwrite): delete files we create if download fails
John W. Eaton <jwe@octave.org>
parents:
7481
diff
changeset
|
794 // 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
|
795 // 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
|
796 // 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
|
797 |
3725f819b5b3
urlwrite.cc (Furlwrite): delete files we create if download fails
John W. Eaton <jwe@octave.org>
parents:
7481
diff
changeset
|
798 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
|
799 |
6986 | 800 std::ofstream ofile (filename.c_str(), std::ios::out | std::ios::binary); |
6043 | 801 |
6986 | 802 if (! ofile.is_open ()) |
6043 | 803 { |
804 error ("urlwrite: unable to open file"); | |
805 return retval; | |
806 } | |
807 | |
10066
2cd940306a06
make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents:
9918
diff
changeset
|
808 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
|
809 |
10066
2cd940306a06
make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents:
9918
diff
changeset
|
810 frame.add_fcn (cleanup_urlwrite, filename); |
9880 | 811 |
812 bool res; | |
813 curl_handle curl = curl_handle (url, method, param, ofile, res); | |
6043 | 814 |
6986 | 815 ofile.close (); |
6043 | 816 |
9880 | 817 if (!error_state) |
10066
2cd940306a06
make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents:
9918
diff
changeset
|
818 frame.discard (); |
9880 | 819 else |
10066
2cd940306a06
make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents:
9918
diff
changeset
|
820 frame.run (); |
8151
3725f819b5b3
urlwrite.cc (Furlwrite): delete files we create if download fails
John W. Eaton <jwe@octave.org>
parents:
7481
diff
changeset
|
821 |
6043 | 822 if (nargout > 0) |
823 { | |
9880 | 824 if (res) |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
825 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
826 retval(2) = std::string (); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
827 retval(1) = true; |
10250 | 828 retval(0) = octave_env::make_absolute (filename); |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
829 } |
8151
3725f819b5b3
urlwrite.cc (Furlwrite): delete files we create if download fails
John W. Eaton <jwe@octave.org>
parents:
7481
diff
changeset
|
830 else |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
831 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
832 retval(2) = curl.lasterror (); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
833 retval(1) = false; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
834 retval(0) = std::string (); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
835 } |
6043 | 836 } |
837 | |
9880 | 838 if (nargout < 2 && res) |
839 error ("urlwrite: curl: %s", curl.lasterror ().c_str ()); | |
6043 | 840 |
841 #else | |
6981 | 842 error ("urlwrite: not available in this version of Octave"); |
6043 | 843 #endif |
844 | |
845 return retval; | |
846 } | |
847 | |
848 DEFUN_DLD (urlread, args, nargout, | |
849 "-*- texinfo -*-\n\ | |
10840 | 850 @deftypefn {Loadable Function} {@var{s} =} urlread (@var{url})\n\ |
6043 | 851 @deftypefnx {Loadable Function} {[@var{s}, @var{success}] =} urlread (@var{url})\n\ |
6549 | 852 @deftypefnx {Loadable Function} {[@var{s}, @var{success}, @var{message}] =} urlread (@var{url})\n\ |
6547 | 853 @deftypefnx {Loadable Function} {[@dots{}] =} urlread (@var{url}, @var{method}, @var{param})\n\ |
9880 | 854 Download a remote file specified by its @var{url} and return its content\n\ |
10840 | 855 in string @var{s}. For example:\n\ |
6043 | 856 \n\ |
857 @example\n\ | |
6588 | 858 s = urlread (\"ftp://ftp.octave.org/pub/octave/README\");\n\ |
6043 | 859 @end example\n\ |
860 \n\ | |
861 The variable @var{success} is 1 if the download was successful,\n\ | |
862 otherwise it is 0 in which case @var{message} contains an error\n\ | |
11553
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11548
diff
changeset
|
863 message. If no output argument is specified and an error occurs,\n\ |
6588 | 864 then the error is signaled through Octave's error handling mechanism.\n\ |
6043 | 865 \n\ |
866 This function uses libcurl. Curl supports, among others, the HTTP,\n\ | |
867 FTP and FILE protocols. Username and password may be specified in the\n\ | |
10840 | 868 URL@. For example:\n\ |
6043 | 869 \n\ |
870 @example\n\ | |
7031 | 871 s = urlread (\"http://user:password@@example.com/file.txt\");\n\ |
6043 | 872 @end example\n\ |
873 \n\ | |
874 GET and POST requests can be specified by @var{method} and @var{param}.\n\ | |
6588 | 875 The parameter @var{method} is either @samp{get} or @samp{post}\n\ |
876 and @var{param} is a cell array of parameter and value pairs.\n\ | |
10840 | 877 For example:\n\ |
6043 | 878 \n\ |
879 @example\n\ | |
9064
7c02ec148a3c
Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
880 @group\n\ |
6588 | 881 s = urlread (\"http://www.google.com/search\", \"get\",\n\ |
882 @{\"query\", \"octave\"@});\n\ | |
9064
7c02ec148a3c
Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
883 @end group\n\ |
6043 | 884 @end example\n\ |
885 @seealso{urlwrite}\n\ | |
886 @end deftypefn") | |
887 { | |
6588 | 888 // Octave's return value |
6043 | 889 octave_value_list retval; |
890 | |
9905
84a7c11ff928
Correctly compile even when libcurl is not present according to configure
Rik <rdrider0-list@yahoo.com>
parents:
9885
diff
changeset
|
891 #ifdef HAVE_CURL |
6043 | 892 |
893 int nargin = args.length (); | |
894 | |
895 // verify arguments | |
896 if (nargin != 1 && nargin != 3) | |
897 { | |
898 print_usage (); | |
899 return retval; | |
900 } | |
901 | |
902 std::string url = args(0).string_value(); | |
903 | |
904 if (error_state) | |
905 { | |
11553
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11548
diff
changeset
|
906 error ("urlread: URL must be a character string"); |
6043 | 907 return retval; |
908 } | |
909 | |
910 std::string method; | |
911 Cell param; // empty cell array | |
912 | |
913 if (nargin == 3) | |
914 { | |
915 method = args(1).string_value(); | |
916 | |
917 if (error_state) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
918 { |
11553
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11548
diff
changeset
|
919 error ("urlread: METHOD must be \"get\" or \"post\""); |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
920 return retval; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
921 } |
6043 | 922 |
923 if (method != "get" && method != "post") | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
924 { |
11553
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11548
diff
changeset
|
925 error ("urlread: METHOD must be \"get\" or \"post\""); |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
926 return retval; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
927 } |
6043 | 928 |
929 param = args(2).cell_value(); | |
930 | |
931 if (error_state) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
932 { |
11553
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11548
diff
changeset
|
933 error ("urlread: parameters (PARAM) for get and post requests must be given as a cell"); |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
934 return retval; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
935 } |
6043 | 936 |
937 if (param.numel () % 2 == 1 ) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
938 { |
11553
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11548
diff
changeset
|
939 error ("urlread: number of elements in PARAM must be even"); |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
940 return retval; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
941 } |
6043 | 942 } |
943 | |
6986 | 944 std::ostringstream buf; |
6043 | 945 |
9880 | 946 bool res; |
947 curl_handle curl = curl_handle (url, method, param, buf, res); | |
6043 | 948 |
949 if (nargout > 0) | |
950 { | |
6986 | 951 retval(0) = buf.str (); |
9880 | 952 retval(1) = res; |
6986 | 953 // Return empty string if no error occured. |
9880 | 954 retval(2) = res ? "" : curl.lasterror (); |
6043 | 955 } |
956 | |
9880 | 957 if (nargout < 2 && !res) |
958 error ("urlread: curl: %s", curl.lasterror().c_str()); | |
6043 | 959 |
960 #else | |
6981 | 961 error ("urlread: not available in this version of Octave"); |
6043 | 962 #endif |
963 | |
964 return retval; | |
965 } | |
9880 | 966 |
967 DEFUN_DLD (__ftp__, args, , | |
968 "-*- texinfo -*-\n\ | |
10840 | 969 @deftypefn {Loadable Function} {} __ftp__ (@var{handle}, @var{host})\n\ |
9880 | 970 @deftypefnx {Loadable Function} {} __ftp__ (@var{handle}, @var{host}, @var{username}, @var{password})\n\ |
971 Undocumented internal function\n\ | |
972 @end deftypefn") | |
973 { | |
974 #ifdef HAVE_CURL | |
975 int nargin = args.length (); | |
976 std::string handle; | |
977 std::string host; | |
978 std::string user = "anonymous"; | |
979 std::string passwd = ""; | |
980 | |
981 if (nargin < 2 || nargin > 4) | |
982 error ("incorrect number of arguments"); | |
983 else | |
984 { | |
985 handle = args(0).string_value (); | |
986 host = args(1).string_value (); | |
987 | |
988 if (nargin > 1) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
989 user = args(2).string_value (); |
9880 | 990 |
991 if (nargin > 2) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
992 passwd = args(3).string_value (); |
9880 | 993 |
994 if (!error_state) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
995 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
996 handles.contents (handle) = curl_handle (host, user, passwd); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
997 |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
998 if (error_state) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
999 handles.del (handle); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1000 } |
9880 | 1001 } |
1002 #else | |
1003 error ("__ftp__: not available in this version of Octave"); | |
1004 #endif | |
1005 | |
1006 return octave_value (); | |
1007 } | |
1008 | |
1009 DEFUN_DLD (__ftp_pwd__, args, , | |
1010 "-*- texinfo -*-\n\ | |
1011 @deftypefn {Loadable Function} {} __ftp_pwd__ (@var{handle})\n\ | |
1012 Undocumented internal function\n\ | |
1013 @end deftypefn") | |
1014 { | |
1015 octave_value retval; | |
1016 #ifdef HAVE_CURL | |
1017 int nargin = args.length (); | |
1018 | |
1019 if (nargin != 1) | |
11553
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11548
diff
changeset
|
1020 error ("__ftp_pwd__: incorrect number of arguments"); |
9880 | 1021 else |
1022 { | |
1023 std::string handle = args(0).string_value (); | |
1024 | |
1025 if (!error_state) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1026 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1027 const curl_handle curl = handles.contents (handle); |
9880 | 1028 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1029 if (curl.is_valid ()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1030 retval = curl.pwd (); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1031 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1032 error ("__ftp_pwd__: invalid ftp handle"); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1033 } |
9880 | 1034 } |
1035 #else | |
1036 error ("__ftp_pwd__: not available in this version of Octave"); | |
1037 #endif | |
1038 | |
1039 return retval; | |
1040 } | |
1041 | |
1042 DEFUN_DLD (__ftp_cwd__, args, , | |
1043 "-*- texinfo -*-\n\ | |
1044 @deftypefn {Loadable Function} {} __ftp_cwd__ (@var{handle}, @var{path})\n\ | |
1045 Undocumented internal function\n\ | |
1046 @end deftypefn") | |
1047 { | |
1048 #ifdef HAVE_CURL | |
1049 int nargin = args.length (); | |
1050 | |
1051 if (nargin != 1 && nargin != 2) | |
11553
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11548
diff
changeset
|
1052 error ("__ftp_cwd__: incorrect number of arguments"); |
9880 | 1053 else |
1054 { | |
1055 std::string handle = args(0).string_value (); | |
1056 std::string path = ""; | |
1057 | |
1058 if (nargin > 1) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1059 path = args(1).string_value (); |
9880 | 1060 |
1061 if (!error_state) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1062 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1063 const curl_handle curl = handles.contents (handle); |
9880 | 1064 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1065 if (curl.is_valid ()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1066 curl.cwd (path); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1067 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1068 error ("__ftp_cwd__: invalid ftp handle"); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1069 } |
9880 | 1070 } |
1071 #else | |
1072 error ("__ftp_cwd__: not available in this version of Octave"); | |
1073 #endif | |
1074 | |
1075 return octave_value (); | |
1076 } | |
1077 | |
1078 DEFUN_DLD (__ftp_dir__, args, nargout, | |
1079 "-*- texinfo -*-\n\ | |
1080 @deftypefn {Loadable Function} {} __ftp_dir__ (@var{handle})\n\ | |
1081 Undocumented internal function\n\ | |
1082 @end deftypefn") | |
1083 { | |
1084 octave_value retval; | |
1085 #ifdef HAVE_CURL | |
1086 int nargin = args.length (); | |
1087 | |
1088 if (nargin != 1) | |
11553
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11548
diff
changeset
|
1089 error ("__ftp_dir__: incorrect number of arguments"); |
9880 | 1090 else |
1091 { | |
1092 std::string handle = args(0).string_value (); | |
1093 | |
1094 if (!error_state) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1095 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1096 const curl_handle curl = handles.contents (handle); |
9880 | 1097 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1098 if (curl.is_valid ()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1099 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1100 if (nargout == 0) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1101 curl.dir (); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1102 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1103 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1104 string_vector sv = curl.list (); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1105 octave_idx_type n = sv.length (); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1106 if (n == 0) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1107 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1108 string_vector flds (5); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1109 flds(0) = "name"; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1110 flds(1) = "date"; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1111 flds(2) = "bytes"; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1112 flds(3) = "isdir"; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1113 flds(4) = "datenum"; |
11050
b1ee705aef45
urlwrite.cc: use octave_scalar_map instead of Octave_map
John W. Eaton <jwe@octave.org>
parents:
10840
diff
changeset
|
1114 retval = octave_map (flds); |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1115 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1116 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1117 { |
11050
b1ee705aef45
urlwrite.cc: use octave_scalar_map instead of Octave_map
John W. Eaton <jwe@octave.org>
parents:
10840
diff
changeset
|
1118 octave_map st; |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1119 Cell filectime (dim_vector (n, 1)); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1120 Cell filesize (dim_vector (n, 1)); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1121 Cell fileisdir (dim_vector (n, 1)); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1122 Cell filedatenum (dim_vector (n, 1)); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1123 |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1124 st.assign ("name", Cell (sv)); |
9880 | 1125 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1126 for (octave_idx_type i = 0; i < n; i++) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1127 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1128 time_t ftime; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1129 bool fisdir; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1130 double fsize; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1131 |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1132 curl.get_fileinfo (sv(i), fsize, ftime, fisdir); |
9880 | 1133 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1134 fileisdir (i) = fisdir; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1135 filectime (i) = ctime (&ftime); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1136 filesize (i) = fsize; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1137 filedatenum (i) = double (ftime); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1138 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1139 st.assign ("date", filectime); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1140 st.assign ("bytes", filesize); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1141 st.assign ("isdir", fileisdir); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1142 st.assign ("datenum", filedatenum); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1143 retval = st; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1144 } |
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 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1148 error ("__ftp_dir__: invalid ftp handle"); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1149 } |
9880 | 1150 } |
1151 #else | |
1152 error ("__ftp_dir__: not available in this version of Octave"); | |
1153 #endif | |
1154 | |
1155 return retval; | |
1156 } | |
1157 | |
1158 DEFUN_DLD (__ftp_ascii__, args, , | |
1159 "-*- texinfo -*-\n\ | |
1160 @deftypefn {Loadable Function} {} __ftp_ascii__ (@var{handle})\n\ | |
1161 Undocumented internal function\n\ | |
1162 @end deftypefn") | |
1163 { | |
1164 #ifdef HAVE_CURL | |
1165 int nargin = args.length (); | |
1166 | |
1167 if (nargin != 1) | |
11553
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11548
diff
changeset
|
1168 error ("__ftp_ascii__: incorrect number of arguments"); |
9880 | 1169 else |
1170 { | |
1171 std::string handle = args(0).string_value (); | |
1172 | |
1173 if (!error_state) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1174 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1175 const curl_handle curl = handles.contents (handle); |
9880 | 1176 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1177 if (curl.is_valid ()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1178 curl.ascii (); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1179 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1180 error ("__ftp_ascii__: invalid ftp handle"); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1181 } |
9880 | 1182 } |
1183 #else | |
1184 error ("__ftp_ascii__: not available in this version of Octave"); | |
1185 #endif | |
1186 | |
1187 return octave_value (); | |
1188 } | |
1189 | |
1190 DEFUN_DLD (__ftp_binary__, args, , | |
1191 "-*- texinfo -*-\n\ | |
1192 @deftypefn {Loadable Function} {} __ftp_binary__ (@var{handle})\n\ | |
1193 Undocumented internal function\n\ | |
1194 @end deftypefn") | |
1195 { | |
1196 #ifdef HAVE_CURL | |
1197 int nargin = args.length (); | |
1198 | |
1199 if (nargin != 1) | |
11553
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11548
diff
changeset
|
1200 error ("__ftp_binary__: incorrect number of arguments"); |
9880 | 1201 else |
1202 { | |
1203 std::string handle = args(0).string_value (); | |
1204 | |
1205 if (!error_state) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1206 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1207 const curl_handle curl = handles.contents (handle); |
9880 | 1208 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1209 if (curl.is_valid ()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1210 curl.binary (); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1211 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1212 error ("__ftp_binary__: invalid ftp handle"); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1213 } |
9880 | 1214 } |
1215 #else | |
1216 error ("__ftp_binary__: not available in this version of Octave"); | |
1217 #endif | |
1218 | |
1219 return octave_value (); | |
1220 } | |
1221 | |
1222 DEFUN_DLD (__ftp_close__, args, , | |
1223 "-*- texinfo -*-\n\ | |
1224 @deftypefn {Loadable Function} {} __ftp_close__ (@var{handle})\n\ | |
1225 Undocumented internal function\n\ | |
1226 @end deftypefn") | |
1227 { | |
1228 #ifdef HAVE_CURL | |
1229 int nargin = args.length (); | |
1230 | |
1231 if (nargin != 1) | |
11553
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11548
diff
changeset
|
1232 error ("__ftp_close__: incorrect number of arguments"); |
9880 | 1233 else |
1234 { | |
1235 std::string handle = args(0).string_value (); | |
1236 | |
1237 if (!error_state) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1238 handles.del (handle); |
9880 | 1239 } |
1240 #else | |
1241 error ("__ftp_close__: not available in this version of Octave"); | |
1242 #endif | |
1243 | |
1244 return octave_value (); | |
1245 } | |
1246 | |
1247 DEFUN_DLD (__ftp_mode__, args, , | |
1248 "-*- texinfo -*-\n\ | |
1249 @deftypefn {Loadable Function} {} __ftp_mode__ (@var{handle})\n\ | |
1250 Undocumented internal function\n\ | |
1251 @end deftypefn") | |
1252 { | |
1253 octave_value retval; | |
1254 #ifdef HAVE_CURL | |
1255 int nargin = args.length (); | |
1256 | |
1257 if (nargin != 1) | |
11553
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11548
diff
changeset
|
1258 error ("__ftp_mode__: incorrect number of arguments"); |
9880 | 1259 else |
1260 { | |
1261 std::string handle = args(0).string_value (); | |
1262 | |
1263 | |
1264 if (!error_state) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1265 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1266 const curl_handle curl = handles.contents (handle); |
9880 | 1267 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1268 if (curl.is_valid ()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1269 retval = (curl.is_ascii() ? "ascii" : "binary"); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1270 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1271 error ("__ftp_binary__: invalid ftp handle"); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1272 } |
9880 | 1273 } |
1274 #else | |
1275 error ("__ftp_mode__: not available in this version of Octave"); | |
1276 #endif | |
1277 | |
1278 return retval; | |
1279 } | |
1280 | |
1281 DEFUN_DLD (__ftp_delete__, args, , | |
1282 "-*- texinfo -*-\n\ | |
1283 @deftypefn {Loadable Function} {} __ftp_delete__ (@var{handle}, @var{path})\n\ | |
1284 Undocumented internal function\n\ | |
1285 @end deftypefn") | |
1286 { | |
1287 #ifdef HAVE_CURL | |
1288 int nargin = args.length (); | |
1289 | |
1290 if (nargin != 2) | |
11553
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11548
diff
changeset
|
1291 error ("__ftp_delete__: incorrect number of arguments"); |
9880 | 1292 else |
1293 { | |
1294 std::string handle = args(0).string_value (); | |
1295 std::string file = args(1).string_value (); | |
1296 | |
1297 if (!error_state) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1298 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1299 const curl_handle curl = handles.contents (handle); |
9880 | 1300 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1301 if (curl.is_valid ()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1302 curl.del (file); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1303 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1304 error ("__ftp_delete__: invalid ftp handle"); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1305 } |
9880 | 1306 } |
1307 #else | |
1308 error ("__ftp_delete__: not available in this version of Octave"); | |
1309 #endif | |
1310 | |
1311 return octave_value (); | |
1312 } | |
1313 | |
1314 DEFUN_DLD (__ftp_rmdir__, args, , | |
1315 "-*- texinfo -*-\n\ | |
1316 @deftypefn {Loadable Function} {} __ftp_rmdir__ (@var{handle}, @var{path})\n\ | |
1317 Undocumented internal function\n\ | |
1318 @end deftypefn") | |
1319 { | |
1320 #ifdef HAVE_CURL | |
1321 int nargin = args.length (); | |
1322 | |
1323 if (nargin != 2) | |
11553
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11548
diff
changeset
|
1324 error ("__ftp_rmdir__: incorrect number of arguments"); |
9880 | 1325 else |
1326 { | |
1327 std::string handle = args(0).string_value (); | |
1328 std::string dir = args(1).string_value (); | |
1329 | |
1330 if (!error_state) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1331 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1332 const curl_handle curl = handles.contents (handle); |
9880 | 1333 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1334 if (curl.is_valid ()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1335 curl.rmdir (dir); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1336 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1337 error ("__ftp_rmdir__: invalid ftp handle"); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1338 } |
9880 | 1339 } |
1340 #else | |
1341 error ("__ftp_rmdir__: not available in this version of Octave"); | |
1342 #endif | |
1343 | |
1344 return octave_value (); | |
1345 } | |
1346 | |
1347 DEFUN_DLD (__ftp_mkdir__, args, , | |
1348 "-*- texinfo -*-\n\ | |
1349 @deftypefn {Loadable Function} {} __ftp_mkdir__ (@var{handle}, @var{path})\n\ | |
1350 Undocumented internal function\n\ | |
1351 @end deftypefn") | |
1352 { | |
1353 #ifdef HAVE_CURL | |
1354 int nargin = args.length (); | |
1355 | |
1356 if (nargin != 2) | |
11553
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11548
diff
changeset
|
1357 error ("__ftp_mkdir__: incorrect number of arguments"); |
9880 | 1358 else |
1359 { | |
1360 std::string handle = args(0).string_value (); | |
1361 std::string dir = args(1).string_value (); | |
1362 | |
1363 if (!error_state) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1364 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1365 const curl_handle curl = handles.contents (handle); |
9880 | 1366 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1367 if (curl.is_valid ()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1368 curl.mkdir (dir); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1369 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1370 error ("__ftp_mkdir__: invalid ftp handle"); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1371 } |
9880 | 1372 } |
1373 #else | |
1374 error ("__ftp_mkdir__: not available in this version of Octave"); | |
1375 #endif | |
1376 | |
1377 return octave_value (); | |
1378 } | |
1379 | |
1380 DEFUN_DLD (__ftp_rename__, args, , | |
1381 "-*- texinfo -*-\n\ | |
1382 @deftypefn {Loadable Function} {} __ftp_rename__ (@var{handle}, @var{path})\n\ | |
1383 Undocumented internal function\n\ | |
1384 @end deftypefn") | |
1385 { | |
1386 #ifdef HAVE_CURL | |
1387 int nargin = args.length (); | |
1388 | |
1389 if (nargin != 3) | |
11553
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11548
diff
changeset
|
1390 error ("__ftp_rename__: incorrect number of arguments"); |
9880 | 1391 else |
1392 { | |
1393 std::string handle = args(0).string_value (); | |
1394 std::string oldname = args(1).string_value (); | |
1395 std::string newname = args(2).string_value (); | |
1396 | |
1397 if (!error_state) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1398 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1399 const curl_handle curl = handles.contents (handle); |
9880 | 1400 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1401 if (curl.is_valid ()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1402 curl.rename (oldname, newname); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1403 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1404 error ("__ftp_rename__: invalid ftp handle"); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1405 } |
9880 | 1406 } |
1407 #else | |
1408 error ("__ftp_rename__: not available in this version of Octave"); | |
1409 #endif | |
1410 | |
1411 return octave_value (); | |
1412 } | |
1413 | |
9905
84a7c11ff928
Correctly compile even when libcurl is not present according to configure
Rik <rdrider0-list@yahoo.com>
parents:
9885
diff
changeset
|
1414 #ifdef HAVE_CURL |
9880 | 1415 static string_vector |
1416 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
|
1417 const std::string& dir) |
9880 | 1418 { |
1419 string_vector retval; | |
1420 | |
1421 if (! curl.mkdir (dir, false)) | |
1422 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
|
1423 (base.length() == 0 ? dir : base + |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1424 file_ops::dir_sep_str () + dir).c_str ()); |
9880 | 1425 |
1426 curl.cwd (dir); | |
1427 | |
1428 if (! error_state) | |
1429 { | |
10066
2cd940306a06
make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents:
9918
diff
changeset
|
1430 unwind_protect_safe frame; |
9880 | 1431 |
10066
2cd940306a06
make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents:
9918
diff
changeset
|
1432 frame.add_fcn (reset_path, curl); |
9880 | 1433 |
1434 std::string realdir = base.length() == 0 ? dir : base + | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1435 file_ops::dir_sep_str () + dir; |
9880 | 1436 |
1437 dir_entry dirlist (realdir); | |
1438 | |
1439 if (dirlist) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1440 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1441 string_vector files = dirlist.read (); |
9880 | 1442 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1443 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
|
1444 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1445 std::string file = files (i); |
9880 | 1446 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1447 if (file == "." || file == "..") |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1448 continue; |
9880 | 1449 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1450 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
|
1451 file_stat fs (realfile); |
9880 | 1452 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1453 if (! fs.exists ()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1454 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1455 error ("__ftp__mput: file ""%s"" does not exist", |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1456 realfile.c_str ()); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1457 break; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1458 } |
9880 | 1459 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1460 if (fs.is_dir ()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1461 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1462 retval.append (mput_directory (curl, realdir, file)); |
9880 | 1463 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1464 if (error_state) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1465 break; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1466 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1467 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1468 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1469 // FIXME Does ascii mode need to be flagged here? |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1470 std::ifstream ifile (realfile.c_str(), std::ios::in | |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1471 std::ios::binary); |
9880 | 1472 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1473 if (! ifile.is_open ()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1474 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1475 error ("__ftp_mput__: unable to open file ""%s""", |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1476 realfile.c_str ()); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1477 break; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1478 } |
9880 | 1479 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1480 curl.put (file, ifile); |
9880 | 1481 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1482 ifile.close (); |
9880 | 1483 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1484 if (error_state) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1485 break; |
9880 | 1486 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1487 retval.append (realfile); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1488 } |
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 } |
9880 | 1491 else |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1492 error ("__ftp_mput__: can not read the directory ""%s""", |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1493 realdir.c_str()); |
9880 | 1494 } |
1495 | |
1496 return retval; | |
1497 } | |
9905
84a7c11ff928
Correctly compile even when libcurl is not present according to configure
Rik <rdrider0-list@yahoo.com>
parents:
9885
diff
changeset
|
1498 #endif |
9880 | 1499 |
1500 DEFUN_DLD (__ftp_mput__, args, nargout, | |
1501 "-*- texinfo -*-\n\ | |
1502 @deftypefn {Loadable Function} {} __ftp_mput__ (@var{handle}, @var{files})\n\ | |
1503 Undocumented internal function\n\ | |
1504 @end deftypefn") | |
1505 { | |
1506 string_vector retval; | |
1507 | |
1508 #ifdef HAVE_CURL | |
1509 int nargin = args.length (); | |
1510 | |
1511 if (nargin != 2) | |
11553
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11548
diff
changeset
|
1512 error ("__ftp_mput__: incorrect number of arguments"); |
9880 | 1513 else |
1514 { | |
1515 std::string handle = args(0).string_value (); | |
1516 std::string pat = args(1).string_value (); | |
1517 | |
1518 if (!error_state) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1519 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1520 const curl_handle curl = handles.contents (handle); |
9880 | 1521 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1522 if (curl.is_valid ()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1523 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1524 glob_match pattern (file_ops::tilde_expand (pat)); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1525 string_vector files = pattern.glob (); |
9880 | 1526 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1527 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
|
1528 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1529 std::string file = files (i); |
9880 | 1530 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1531 file_stat fs (file); |
9880 | 1532 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1533 if (! fs.exists ()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1534 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1535 error ("__ftp__mput: file does not exist"); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1536 break; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1537 } |
9880 | 1538 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1539 if (fs.is_dir ()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1540 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1541 retval.append (mput_directory (curl, "", file)); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1542 if (error_state) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1543 break; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1544 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1545 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1546 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1547 // FIXME Does ascii mode need to be flagged here? |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1548 std::ifstream ifile (file.c_str(), std::ios::in | |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1549 std::ios::binary); |
9880 | 1550 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1551 if (! ifile.is_open ()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1552 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1553 error ("__ftp_mput__: unable to open file"); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1554 break; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1555 } |
9880 | 1556 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1557 curl.put (file, ifile); |
9880 | 1558 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1559 ifile.close (); |
9880 | 1560 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1561 if (error_state) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1562 break; |
9880 | 1563 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1564 retval.append (file); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1565 } |
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 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1569 error ("__ftp_mput__: invalid ftp handle"); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1570 } |
9880 | 1571 } |
1572 #else | |
1573 error ("__ftp_mput__: not available in this version of Octave"); | |
1574 #endif | |
1575 | |
1576 return (nargout > 0 ? octave_value (retval) : octave_value ()); | |
1577 } | |
1578 | |
1579 #ifdef HAVE_CURL | |
1580 static void | |
1581 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
|
1582 const std::string& target) |
9880 | 1583 { |
1584 std::string sep = file_ops::dir_sep_str (); | |
1585 file_stat fs (dir); | |
1586 | |
1587 if (!fs || !fs.is_dir ()) | |
1588 { | |
1589 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
|
1590 int status = octave_mkdir (dir, 0777, msg); |
9880 | 1591 |
1592 if (status < 0) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1593 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
|
1594 target.c_str(), sep.c_str(), dir.c_str(), msg.c_str()); |
9880 | 1595 } |
1596 | |
1597 if (! error_state) | |
1598 { | |
1599 curl.cwd (dir); | |
1600 | |
1601 if (! error_state) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1602 { |
10066
2cd940306a06
make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents:
9918
diff
changeset
|
1603 unwind_protect_safe frame; |
9880 | 1604 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1605 frame.add_fcn (reset_path, curl); |
9880 | 1606 |
10066
2cd940306a06
make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents:
9918
diff
changeset
|
1607 string_vector sv = curl.list (); |
9880 | 1608 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1609 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
|
1610 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1611 time_t ftime; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1612 bool fisdir; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1613 double fsize; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1614 |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1615 curl.get_fileinfo (sv(i), fsize, ftime, fisdir); |
9880 | 1616 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1617 if (fisdir) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1618 getallfiles (curl, sv(i), target + dir + sep); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1619 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1620 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1621 std::string realfile = target + dir + sep + sv(i); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1622 std::ofstream ofile (realfile.c_str(), |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1623 std::ios::out | |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1624 std::ios::binary); |
9880 | 1625 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1626 if (! ofile.is_open ()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1627 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1628 error ("__ftp_mget__: unable to open file"); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1629 break; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1630 } |
9880 | 1631 |
10066
2cd940306a06
make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents:
9918
diff
changeset
|
1632 unwind_protect_safe frame2; |
9880 | 1633 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1634 frame2.add_fcn (delete_file, realfile); |
9880 | 1635 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1636 curl.get (sv(i), ofile); |
9880 | 1637 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1638 ofile.close (); |
9880 | 1639 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1640 if (!error_state) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1641 frame2.discard (); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1642 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1643 frame2.run (); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1644 } |
9880 | 1645 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1646 if (error_state) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1647 break; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1648 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1649 } |
9880 | 1650 } |
1651 } | |
1652 #endif | |
1653 | |
1654 DEFUN_DLD (__ftp_mget__, args, , | |
1655 "-*- texinfo -*-\n\ | |
1656 @deftypefn {Loadable Function} {} __ftp_mget__ (@var{handle}, @var{files})\n\ | |
1657 Undocumented internal function\n\ | |
1658 @end deftypefn") | |
1659 { | |
1660 #ifdef HAVE_CURL | |
1661 int nargin = args.length (); | |
1662 | |
1663 if (nargin != 2 && nargin != 3) | |
11553
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11548
diff
changeset
|
1664 error ("__ftp_mget__: incorrect number of arguments"); |
9880 | 1665 else |
1666 { | |
1667 std::string handle = args(0).string_value (); | |
1668 std::string file = args(1).string_value (); | |
1669 std::string target; | |
1670 | |
1671 if (nargin == 3) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1672 target = args(2).string_value () + file_ops::dir_sep_str (); |
9880 | 1673 |
1674 if (! error_state) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1675 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1676 const curl_handle curl = handles.contents (handle); |
9880 | 1677 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1678 if (curl.is_valid ()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1679 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1680 string_vector sv = curl.list (); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1681 octave_idx_type n = 0; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1682 glob_match pattern (file); |
9880 | 1683 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1684 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
|
1685 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1686 if (pattern.match (sv(i))) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1687 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1688 n++; |
9880 | 1689 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1690 time_t ftime; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1691 bool fisdir; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1692 double fsize; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1693 |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1694 curl.get_fileinfo (sv(i), fsize, ftime, fisdir); |
9880 | 1695 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1696 if (fisdir) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1697 getallfiles (curl, sv(i), target); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1698 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1699 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1700 std::ofstream ofile ((target + sv(i)).c_str(), |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1701 std::ios::out | |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1702 std::ios::binary); |
9880 | 1703 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1704 if (! ofile.is_open ()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1705 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1706 error ("__ftp_mget__: unable to open file"); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1707 break; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1708 } |
9880 | 1709 |
10066
2cd940306a06
make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents:
9918
diff
changeset
|
1710 unwind_protect_safe frame; |
9880 | 1711 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1712 frame.add_fcn (delete_file, target + sv(i)); |
9880 | 1713 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1714 curl.get (sv(i), ofile); |
9880 | 1715 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1716 ofile.close (); |
9880 | 1717 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1718 if (!error_state) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1719 frame.discard (); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1720 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1721 frame.run (); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1722 } |
9880 | 1723 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1724 if (error_state) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1725 break; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1726 } |
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 if (n == 0) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1729 error ("__ftp_mget__: file not found"); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1730 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10084
diff
changeset
|
1731 } |
9880 | 1732 } |
1733 #else | |
1734 error ("__ftp_mget__: not available in this version of Octave"); | |
1735 #endif | |
1736 | |
1737 return octave_value (); | |
1738 } |