Merge pull request #2216 from jwillemsen/jwi-cxxversionchecks
[ACE_TAO.git] / ACE / examples / Web_Crawler / URL_Addr.cpp
blobbf9e9d439eb1cae0861a6695a31b07c105eee385
1 #include "URL_Addr.h"
2 #include "ace/Log_Msg.h"
3 #include "ace/OS_NS_string.h"
4 #include "ace/OS_NS_stdio.h"
5 #include "ace/OS_NS_stdlib.h"
6 #include "ace/OS_Memory.h"
8 ACE_URL_Addr::ACE_URL_Addr ()
9 : path_name_ (0),
10 addr_string_ (0),
11 addr_string_len_ (0)
15 int
16 ACE_URL_Addr::addr_to_string (ACE_TCHAR *s,
17 size_t size,
18 int ipaddr_format) const
20 const size_t total_len =
21 ACE_OS::strlen (ipaddr_format == 0 ?
22 this->get_host_name () :
23 this->get_host_addr ())
24 + ACE_OS::strlen ("65536") // Assume the max port number.
25 + ACE_OS::strlen (this->get_path_name ())
26 + sizeof (':')
27 + sizeof ('/')
28 + sizeof ('\0'); // For trailing '\0'.
30 if (size < total_len)
31 return -1;
32 else
34 ACE_OS::sprintf (s, ACE_TEXT ("%s:%d/%s"),
35 ACE_TEXT_CHAR_TO_TCHAR (ipaddr_format == 0
36 ? this->get_host_name ()
37 : this->get_host_addr ()),
38 this->get_port_number (),
39 this->get_path_name ());
40 return 0;
44 const ACE_TCHAR *
45 ACE_URL_Addr::addr_to_string (int ipaddr_format) const
47 ACE_URL_Addr *this_ptr = const_cast<ACE_URL_Addr *> (this);
49 size_t size =
50 ACE_OS::strlen (ipaddr_format == 0 ?
51 this->get_host_name () :
52 this->get_host_addr ())
53 + ACE_OS::strlen ("65536") // Assume the max port number.
54 + ACE_OS::strlen (this->get_path_name ())
55 + sizeof (':')
56 + sizeof ('/')
57 + sizeof ('\0'); // For trailing '\0'.
59 if (size > this->addr_string_len_)
61 ACE_ALLOCATOR_RETURN (this_ptr->addr_string_,
62 (ACE_TCHAR *) ACE_OS::realloc ((void *) this->addr_string_,
63 size),
64 0);
65 this_ptr->addr_string_len_ = size;
67 ACE_OS::sprintf (this->addr_string_,
68 ACE_TEXT ("%s:%d/%s"),
69 ACE_TEXT_CHAR_TO_TCHAR (ipaddr_format == 0
70 ? this->get_host_name ()
71 : this->get_host_addr ()),
72 this->get_port_number (),
73 this->get_path_name ());
74 return this->addr_string_;
77 int
78 ACE_URL_Addr::string_to_addr (const ACE_TCHAR *s,
79 int /* address_family */)
81 int result;
82 ACE_TCHAR *t;
84 // Need to make a duplicate since we'll be overwriting the string.
85 ACE_ALLOCATOR_RETURN (t,
86 ACE_OS::strdup (s),
87 -1);
90 // First split off the path_name.
92 ACE_TCHAR *path_name = ACE_OS::strchr (t, ACE_TEXT ('/'));
93 const ACE_TCHAR *name = ACE_TEXT ("index.html");
94 if (path_name != 0)
96 if (ACE_OS::strlen (path_name + 1) > 0)
97 name = path_name + 1;
99 *path_name = '\0';
102 ACE_ALLOCATOR_RETURN (this->path_name_,
103 // Skip over '/'
104 ACE_OS::strdup (name),
105 -1);
107 // Now handle the host address and port number.
108 ACE_TCHAR *port_number = ACE_OS::strchr (t, ':');
110 if (port_number == 0)
112 // Assume it's an ip-address or ip-number.
113 result = this->ACE_INET_Addr::set (ACE_DEFAULT_HTTP_PORT,
116 else
118 *port_number = '\0';
119 u_short port = (u_short) ACE_OS::atoi (port_number + 1); // Skip over ':'
120 result = this->ACE_INET_Addr::set (port, t);
123 ACE_OS::free (ACE_MALLOC_T (t));
124 return result;
127 ACE_URL_Addr::ACE_URL_Addr (const ACE_URL_Addr &addr)
128 : ACE_INET_Addr (),
129 path_name_ (0),
130 addr_string_ (0),
131 addr_string_len_ (0)
133 if (this->set (addr) == -1)
134 ACE_ERROR ((LM_ERROR,
135 ACE_TEXT ("%p\n"),
136 ACE_TEXT ("ACE_URL_Addr::ACE_URL_Addr")));
140 ACE_URL_Addr::set (const ACE_URL_Addr &addr)
142 ACE_OS::free (reinterpret_cast<void *> (const_cast<ACE_TCHAR *>
143 (this->path_name_)));
144 ACE_OS::free (reinterpret_cast<void *> (const_cast<ACE_TCHAR *>
145 (this->addr_string_)));
146 if (this->ACE_INET_Addr::set (addr) == -1)
147 return -1;
148 else
150 if (addr.path_name_)
151 ACE_ALLOCATOR_RETURN (this->path_name_,
152 ACE_OS::strdup (addr.path_name_),
153 -1);
154 if (addr.addr_string_)
155 ACE_ALLOCATOR_RETURN (this->addr_string_,
156 ACE_OS::strdup (addr.addr_string_),
157 -1);
158 this->addr_string_len_ =
159 addr.addr_string_len_;
160 return 0;
164 void
165 ACE_URL_Addr::operator= (const ACE_URL_Addr &addr)
167 if (this->set (addr) == -1)
168 ACE_ERROR ((LM_ERROR,
169 ACE_TEXT ("%p\n"),
170 ACE_TEXT ("ACE_URL_Addr::ACE_URL_Addr")));
173 u_long
174 ACE_URL_Addr::hash () const
176 u_long result = this->ACE_INET_Addr::hash ()
177 + ACE::hash_pjw (this->get_path_name ());
179 return result;
182 bool
183 ACE_URL_Addr::operator== (const ACE_URL_Addr &addr) const
185 return ACE_OS::strcmp (addr.get_path_name (),
186 this->get_path_name ()) == 0
187 && addr.get_port_number () == this->get_port_number ()
188 && addr.get_ip_address () == this->get_ip_address ();
191 bool
192 ACE_URL_Addr::operator!= (const ACE_URL_Addr &addr) const
194 return !(*this == addr);
197 ACE_URL_Addr::ACE_URL_Addr (const ACE_TCHAR *host_name,
198 const ACE_TCHAR *path_name,
199 u_short port)
200 : ACE_INET_Addr (port, host_name),
201 path_name_ (ACE_OS::strdup (path_name)),
202 addr_string_ (0),
203 addr_string_len_ (0)
207 const ACE_TCHAR *
208 ACE_URL_Addr::get_path_name () const
210 return this->path_name_;
213 ACE_URL_Addr::~ACE_URL_Addr ()
215 ACE_OS::free (reinterpret_cast<void *> (const_cast<ACE_TCHAR *>
216 (this->path_name_)));
217 ACE_OS::free (reinterpret_cast<void *> (const_cast<ACE_TCHAR *>
218 (this->addr_string_)));
219 this->path_name_ = 0;
223 ACE_URL_Addr::destroy ()
225 // Commit suicide.
226 delete this;
227 return 0;