GNU Octave  4.4.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
url-handle-manager.h
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2017-2018 John W. Eaton
4 
5 This file is part of Octave.
6 
7 Octave is free software: you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with Octave; see the file COPYING. If not, see
19 <https://www.gnu.org/licenses/>.
20 
21 */
22 
23 // Extracted from urlwrite.cc.
24 
25 #if ! defined (octave_url_handle_manager)
26 #define octave_url_handle_manager 1
27 
28 #include "octave-config.h"
29 
30 #include <cmath>
31 
32 #include <map>
33 #include <set>
34 
35 #include "lo-mappers.h"
36 
37 #include "oct-handle.h"
38 #include "url-transfer.h"
39 
40 namespace octave
41 {
43 
44  class OCTINTERP_API url_handle_manager
45  {
46  public:
47 
49  : handle_map (), handle_free_list (),
50  next_handle (-1.0 - (rand () + 1.0) / (RAND_MAX + 2.0)) { }
51 
52  url_handle_manager (const url_handle_manager&) = delete;
53 
54  url_handle_manager& operator = (const url_handle_manager&) = delete;
55 
56  ~url_handle_manager (void) = default;
57 
58  url_handle get_handle (void);
59 
60  void free (const url_handle& h);
61 
63  {
64  iterator p = (math::isnan (val) ? handle_map.end ()
65  : handle_map.find (val));
66 
67  return (p != handle_map.end ()) ? p->first : url_handle ();
68  }
69 
71  {
72  return val.is_real_scalar () ? lookup (val.double_value ())
73  : url_handle ();
74  }
75 
77  {
78  return get_object (lookup (val));
79  }
80 
82  {
83  return get_object (lookup (val));
84  }
85 
87  {
88  iterator p = (h.ok () ? handle_map.find (h) : handle_map.end ());
89 
90  return (p != handle_map.end ()) ? p->second : url_transfer ();
91  }
92 
94  const std::string& user,
95  const std::string& passwd,
96  std::ostream& os)
97  {
98  url_handle h = get_handle ();
99 
100  url_transfer obj (host, user, passwd, os);
101 
102  if (! obj.is_valid ())
103  error ("support for URL transfers was disabled when Octave was built");
104 
105  handle_map[h] = obj;
106 
107  return h;
108  }
109 
111  {
112  Matrix retval (1, handle_map.size ());
113 
114  octave_idx_type i = 0;
115  for (const auto& h_obj : handle_map)
116  {
117  url_handle h = h_obj.first;
118 
119  retval(i++) = h.value ();
120  }
121 
122  return retval;
123  }
124 
125 
126  private:
127 
128  typedef std::map<url_handle, url_transfer>::iterator iterator;
129  typedef std::map<url_handle, url_transfer>::const_iterator const_iterator;
130 
131  typedef std::set<url_handle>::iterator free_list_iterator;
132  typedef std::set<url_handle>::const_iterator const_free_list_iterator;
133 
134  // A map of handles to curl objects.
135  std::map<url_handle, url_transfer> handle_map;
136 
137  // The available curl handles.
138  std::set<url_handle> handle_free_list;
139 
140  // The next handle available if handle_free_list is empty.
141  double next_handle;
142  };
143 }
144 
145 #endif
url_handle make_url_handle(const std::string &host, const std::string &user, const std::string &passwd, std::ostream &os)
bool is_valid(void) const
Definition: url-transfer.h:185
std::set< url_handle >::iterator free_list_iterator
url_handle lookup(double val)
octave_handle url_handle
std::set< url_handle > handle_free_list
std::set< url_handle >::const_iterator const_free_list_iterator
identity matrix If supplied two scalar respectively For allows like xample val
Definition: data.cc:4986
std::map< url_handle, url_transfer > handle_map
bool isnan(bool)
Definition: lo-mappers.h:187
void error(const char *fmt,...)
Definition: error.cc:578
octave_idx_type lookup(const T *x, octave_idx_type n, T y)
std::map< url_handle, url_transfer >::iterator iterator
url_handle lookup(const octave_value &val)
double h
Definition: graphics.cc:11808
octave_value retval
Definition: data.cc:6246
Definition: dMatrix.h:36
url_transfer get_object(const url_handle &h)
p
Definition: lu.cc:138
url_transfer get_object(double val)
for i
Definition: data.cc:5264
url_transfer get_object(const octave_value &val)
std::map< url_handle, url_transfer >::const_iterator const_iterator
If this string is the system will ring the terminal sometimes it is useful to be able to print the original representation of the string
Definition: utils.cc:888
octave::stream os
Definition: file-io.cc:627