GNU Octave  3.8.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
url-transfer.h
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2013 John W. Eaton
4 Copyright (C) 2006-2013 Alexander Barth
5 Copyright (C) 2009 David Bateman
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
11 Free Software Foundation; either version 3 of the License, or (at your
12 option) any later version.
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
20 along with Octave; see the file COPYING. If not, see
21 <http://www.gnu.org/licenses/>.
22 
23 */
24 
25 // Author: Alexander Barth <abarth@marine.usf.edu>
26 // Author: jwe
27 
28 #if !defined (octave_url_transfer_h)
29 #define octave_url_transfer_h 1
30 
31 #include <iosfwd>
32 #include <string>
33 
34 class
35 OCTAVE_API
37 {
38 private:
39 
40  static void delete_file (const std::string& file);
41 
42  static void reset_path (base_url_transfer *curl_xfer)
43  {
44  curl_xfer->cwd ("..");
45  }
46 
47 public:
48 
49  friend class url_transfer;
50 
52  : count (1), host_or_url (), valid (false), ftp (false),
53  ascii_mode (false), ok (true), errmsg (),
54  curr_istream (&std::cin), curr_ostream (&std::cout)
55  { }
56 
57  base_url_transfer (const std::string& host,
58  const std::string& /* user_arg */,
59  const std::string& /* passwd */,
60  std::ostream& os)
61  : count (1), host_or_url (host), valid (false), ftp (true),
62  ascii_mode (false), ok (true), errmsg (), curr_istream (&std::cin),
63  curr_ostream (&os) { }
64 
65  base_url_transfer (const std::string& url, std::ostream& os)
66  : count (1), host_or_url (url), valid (false), ftp (false),
67  ascii_mode (false), ok (true), errmsg (),
68  curr_istream (&std::cin), curr_ostream (&os) { }
69 
70  virtual ~base_url_transfer (void) { }
71 
72  bool is_valid (void) const { return valid; }
73 
74  bool good (void) const { return valid && ok; }
75 
76  virtual void perform (void) { }
77 
78  virtual std::string lasterror (void) const { return errmsg; }
79 
80  virtual std::ostream& set_ostream (std::ostream& /* os */)
81  {
82  return *curr_ostream;
83  }
84 
85  virtual std::istream& set_istream (std::istream& /* is */)
86  {
87  return *curr_istream;
88  }
89 
90  virtual void ascii (void) { }
91 
92  virtual void binary (void) { }
93 
94  bool is_ascii (void) const { return ascii_mode; }
95 
96  bool is_binary (void) const { return !ascii_mode; }
97 
98  virtual void cwd (const std::string& /* path */) { }
99 
100  virtual void del (const std::string& /* file */) { }
101 
102  virtual void rmdir (const std::string& /* path */) { }
103 
104  virtual void mkdir (const std::string& /* path */) { }
105 
106  virtual void rename (const std::string& /* oldname */,
107  const std::string& /* newname */) { }
108 
109  virtual void put (const std::string& /* file */,
110  std::istream& /* is */) { }
111 
112  virtual void get (const std::string& /* file */,
113  std::ostream& /* os */) { }
114 
115  void mget_directory (const std::string& directory,
116  const std::string& target);
117 
118  string_vector mput_directory (const std::string& base,
119  const std::string& directory);
120 
121  virtual void dir (void) { }
122 
123  virtual string_vector list (void) { return string_vector (); }
124 
125  virtual void get_fileinfo (const std::string& /* filename */,
126  double& /* filesize */,
127  time_t& /* filetime */,
128  bool& /* fileisdir */) { }
129 
130  virtual std::string pwd (void) { return std::string (); }
131 
132  virtual void http_get (const Array<std::string>& /* param */) { }
133 
134  virtual void http_post (const Array<std::string>& /* param */) { }
135 
136  virtual void http_action (const Array<std::string>& /* param */,
137  const std::string& /* action */) { }
138 
139 protected:
140 
141  // Reference count.
143 
144  // Host for ftp transfers or full URL for http requests.
145  std::string host_or_url;
146  bool valid;
147  bool ftp;
149  bool ok;
150  std::string errmsg;
151  std::istream *curr_istream;
152  std::ostream *curr_ostream;
153 
154 private:
155 
156  // No copying!
157 
159 
161 };
162 
163 class
164 OCTAVE_API
166 {
167 public:
168 
169  url_transfer (void);
170 
171  url_transfer (const std::string& host, const std::string& user,
172  const std::string& passwd, std::ostream& os);
173 
174  url_transfer (const std::string& url, std::ostream& os);
175 
176  url_transfer (const url_transfer& h) : rep (h.rep)
177  {
178  rep->count++;
179  }
180 
182  {
183  if (--rep->count == 0)
184  delete rep;
185  }
186 
188  {
189  if (this != &h)
190  {
191  if (--rep->count == 0)
192  delete rep;
193 
194  rep = h.rep;
195  rep->count++;
196  }
197 
198  return *this;
199  }
200 
201  bool is_valid (void) const { return rep->is_valid (); }
202 
203  bool good (void) const { return rep->good (); }
204 
205  std::string lasterror (void) const { return rep->lasterror (); }
206 
207  std::ostream& set_ostream (std::ostream& os)
208  {
209  return rep->set_ostream (os);
210  }
211 
212  std::istream& set_istream (std::istream& is)
213  {
214  return rep->set_istream (is);
215  }
216 
217  void ascii (void) { rep->ascii (); }
218 
219  void binary (void) { rep->binary (); }
220 
221  bool is_ascii (void) const { return rep->is_ascii (); }
222 
223  bool is_binary (void) const { return rep->is_binary (); }
224 
225  void cwd (const std::string& path) { rep->cwd (path); }
226 
227  void del (const std::string& file) { rep->del (file); }
228 
229  void rmdir (const std::string& path) { rep->rmdir (path); }
230 
231  void mkdir (const std::string& path) { rep->mkdir (path); }
232 
233  void rename (const std::string& oldname, const std::string& newname)
234  {
235  rep->rename (oldname, newname);
236  }
237 
238  void put (const std::string& file, std::istream& is)
239  {
240  rep->put (file, is);
241  }
242 
243  void get (const std::string& file, std::ostream& os)
244  {
245  rep->get (file, os);
246  }
247 
248  void mget_directory (const std::string& directory,
249  const std::string& target)
250  {
251  rep->mget_directory (directory, target);
252  }
253 
254  string_vector mput_directory (const std::string& base,
255  const std::string& directory)
256  {
257  return rep->mput_directory (base, directory);
258  }
259 
260  void dir (void) { rep->dir (); }
261 
262  string_vector list (void) { return rep->list (); }
263 
264  void get_fileinfo (const std::string& filename, double& filesize,
265  time_t& filetime, bool& fileisdir)
266  {
267  rep->get_fileinfo (filename, filesize, filetime, fileisdir);
268  }
269 
270  std::string pwd (void) { return rep->pwd (); }
271 
272  void http_get (const Array<std::string>& param) { rep->http_get (param); }
273 
274  void http_post (const Array<std::string>& param) { rep->http_post (param); }
275 
276  void http_action (const Array<std::string>& param,
277  const std::string& action)
278  {
279  rep->http_action (param, action);
280  }
281 
282 private:
283 
285 };
286 
287 #endif