Use =default for skeleton copy constructor
[ACE_TAO.git] / ACE / ACEXML / common / URL_Addr.cpp
blobf136478a733ef23b5e141763a5f4079ce2f73747
1 #include "ACEXML/common/URL_Addr.h"
3 #if !defined (__ACEXML_INLINE__)
4 #include "ACEXML/common/URL_Addr.inl"
5 #endif /* __ACEXML_INLINE__ */
7 #include "ace/Log_Msg.h"
8 #include "ace/OS_Memory.h"
9 #include "ace/OS_NS_stdio.h"
10 #include "ace/OS_NS_stdlib.h"
11 #include "ace/OS_NS_string.h"
12 #include <memory>
14 ACEXML_URL_Addr::ACEXML_URL_Addr ()
15 : path_name_ (0),
16 addr_string_ (0),
17 addr_string_len_ (0)
21 int
22 ACEXML_URL_Addr::addr_to_string (ACEXML_Char *s,
23 size_t size,
24 int ipaddr_format) const
26 size_t total_len = this->calculate_length (ipaddr_format);
27 if (size < total_len)
28 return -1;
29 else
31 ACE_OS::sprintf (s, ACE_TEXT ("%s:%d/%s"),
32 ACE_TEXT_CHAR_TO_TCHAR (ipaddr_format == 0
33 ? this->get_host_name ()
34 : this->get_host_addr ()),
35 this->get_port_number (),
36 this->get_path_name ());
37 return 0;
41 const ACEXML_Char *
42 ACEXML_URL_Addr::addr_to_string (int ipaddr_format)
44 size_t size = this->calculate_length (ipaddr_format);
45 if (size > this->addr_string_len_)
47 ACE_ALLOCATOR_RETURN (this->addr_string_,
48 (ACEXML_Char *) ACE_OS::realloc(this->addr_string_,
49 size), 0);
50 this->addr_string_len_ = size;
52 ACE_OS::sprintf (this->addr_string_,
53 ACE_TEXT ("%s:%d/%s"),
54 ACE_TEXT_CHAR_TO_TCHAR (ipaddr_format == 0
55 ? this->get_host_name ()
56 : this->get_host_addr ()),
57 this->get_port_number (),
58 this->get_path_name ());
59 return this->addr_string_;
62 #if defined (ACE_USES_WCHAR)
63 int
64 ACEXML_URL_Addr::string_to_addr (const char* s,
65 int address_family)
67 return this->string_to_addr (ACE_TEXT_CHAR_TO_TCHAR (s), address_family);
69 #endif /* ACE_USES_WCHAR */
71 int
72 ACEXML_URL_Addr::string_to_addr (const ACEXML_Char* s,
73 int /*address_family */)
75 if (s == 0)
76 return -1;
78 const ACEXML_Char* http = ACE_TEXT ("http://");
79 size_t http_len = ACE_OS::strlen (http);
81 // Check validity of URL
82 if (ACE_OS::strncmp (http, s, http_len) != 0)
83 ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("Invalid URL %s\n"), s), -1);
85 const ACEXML_Char* url = 0;
86 // Get the host name
87 for (url = s + http_len; *url != '\0' && *url != ':' && *url != '/'; ++url)
90 size_t host_len = url - s;
91 host_len -= http_len;
93 ACEXML_Char* host_name = 0;
94 ACE_NEW_RETURN (host_name, ACEXML_Char[host_len + 1], -1);
95 ACE_OS::strncpy (host_name, s + http_len, host_len);
96 host_name[host_len] = '\0';
97 std::unique_ptr<ACEXML_Char[]> cleanup_host_name (host_name);
99 // Get the port number (if any)
100 unsigned short port = ACE_DEFAULT_HTTP_PORT;
101 if (*url == ':')
103 port = (unsigned short) ACE_OS::strtol (++url, 0, 10); // Skip over ':'
104 while ( *url != '\0' && *url != '/' )
105 ++url;
108 // Set the addr
109 int result = this->ACE_INET_Addr::set (port, host_name);
111 if (result == -1)
112 return -1;
114 // Get the path name
115 const ACEXML_Char* path_name = 0;
116 if (*url == '\0')
117 path_name = ACE_TEXT ("/");
118 else
119 path_name = url;
121 ACE_ALLOCATOR_RETURN (this->path_name_, ACE_OS::strdup (path_name), -1);
122 return result;
125 ACEXML_URL_Addr::ACEXML_URL_Addr (const ACEXML_URL_Addr &addr)
126 : ACE_INET_Addr (),
127 path_name_ (0),
128 addr_string_ (0),
129 addr_string_len_ (0)
131 if (this->set (addr) == -1)
132 ACE_ERROR ((LM_ERROR,
133 ACE_TEXT ("%p\n"),
134 ACE_TEXT ("ACEXML_URL_Addr::ACEXML_URL_Addr")));
138 ACEXML_URL_Addr::set (const ACEXML_URL_Addr &addr)
140 ACE_OS::free (this->path_name_);
141 ACE_OS::free (this->addr_string_);
142 if (this->ACE_INET_Addr::set (addr) == -1)
143 return -1;
144 else
146 if (addr.path_name_)
147 ACE_ALLOCATOR_RETURN (this->path_name_,
148 ACE_OS::strdup (addr.path_name_),
149 -1);
150 if (addr.addr_string_)
151 ACE_ALLOCATOR_RETURN (this->addr_string_,
152 ACE_OS::strdup (addr.addr_string_),
153 -1);
154 this->addr_string_len_ = addr.addr_string_len_;
155 return 0;
159 ACEXML_URL_Addr::ACEXML_URL_Addr (const ACEXML_Char *host_name,
160 const ACEXML_Char *path_name,
161 unsigned short port)
162 : ACE_INET_Addr (port, host_name),
163 path_name_ (ACE_OS::strdup (path_name)),
164 addr_string_ (0),
165 addr_string_len_ (0)
169 ACEXML_URL_Addr::~ACEXML_URL_Addr ()
171 ACE_OS::free (this->path_name_);
172 ACE_OS::free (this->addr_string_);
173 this->path_name_ = 0;