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