Main Page   Modules   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

URL_Addr.cpp

Go to the documentation of this file.
00001 // $Id: URL_Addr.cpp,v 1.1.1.1 2003/02/21 18:36:32 chad Exp $
00002 
00003 #include "ace/Log_Msg.h"
00004 #include "ace/Auto_Ptr.h"
00005 #include "ACEXML/common/URL_Addr.h"
00006 
00007 
00008 ACE_RCSID(common, ACEXML_URL_Addr, "$Id: URL_Addr.cpp,v 1.1.1.1 2003/02/21 18:36:32 chad Exp $")
00009 
00010 #if !defined (__ACEXML_INLINE__)
00011 #include "ACEXML/common/URL_Addr.inl"
00012 #endif /* __ACEXML_INLINE__ */
00013 
00014 ACEXML_URL_Addr::ACEXML_URL_Addr (void)
00015   : path_name_ (0),
00016     addr_string_ (0),
00017     addr_string_len_ (0)
00018 {
00019 }
00020 
00021 int
00022 ACEXML_URL_Addr::addr_to_string (ACEXML_Char *s,
00023                                  size_t size,
00024                                  int ipaddr_format) const
00025 {
00026   size_t total_len = this->calculate_length (ipaddr_format);
00027   if (size < total_len)
00028     return -1;
00029   else
00030     {
00031       ACE_OS::sprintf (s, ACE_TEXT ("%s:%d/%s"),
00032                        ACE_TEXT_CHAR_TO_TCHAR (ipaddr_format == 0
00033                                                ? this->get_host_name ()
00034                                                : this->get_host_addr ()),
00035                        this->get_port_number (),
00036                        this->get_path_name ());
00037       return 0;
00038     }
00039 }
00040 
00041 const ACEXML_Char *
00042 ACEXML_URL_Addr::addr_to_string (int ipaddr_format)
00043 {
00044   size_t size = this->calculate_length (ipaddr_format);
00045   if (size > this->addr_string_len_)
00046     {
00047       ACE_ALLOCATOR_RETURN (this->addr_string_,
00048                             (ACEXML_Char *) ACE_OS::realloc(this->addr_string_,
00049                                                             size), 0);
00050       this->addr_string_len_ = size;
00051     }
00052   ACE_OS::sprintf (this->addr_string_,
00053                    ACE_TEXT ("%s:%d/%s"),
00054                    ACE_TEXT_CHAR_TO_TCHAR (ipaddr_format == 0
00055                                            ? this->get_host_name ()
00056                                           : this->get_host_addr ()),
00057                    this->get_port_number (),
00058                    this->get_path_name ());
00059   return this->addr_string_;
00060 }
00061 
00062 #if defined (ACE_USES_WCHAR)
00063 int
00064 ACEXML_URL_Addr::string_to_addr (const char* s)
00065 {
00066   return this->string_to_addr (ACE_TEXT_CHAR_TO_TCHAR (s));
00067 }
00068 #endif /* ACE_USES_WCHAR */
00069 
00070 int
00071 ACEXML_URL_Addr::string_to_addr (const ACEXML_Char* s)
00072 {
00073   if (s == 0)
00074     return -1;
00075 
00076   const ACEXML_Char* http = ACE_TEXT ("http://");
00077   size_t http_len = ACE_OS::strlen (http);
00078 
00079   // Check validity of URL
00080   if (ACE_OS::strncmp (http, s, http_len) != 0)
00081     ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("Invalid URL %s\n"), s), -1);
00082 
00083   const ACEXML_Char* url = 0;
00084   // Get the host name
00085   for (url = s + http_len; *url != '\0' && *url != ':' && *url != '/'; ++url)
00086     ;
00087 
00088   size_t host_len = url - s;
00089   host_len -= http_len;
00090 
00091   ACEXML_Char* host_name = 0;
00092   ACE_NEW_RETURN (host_name, ACEXML_Char[host_len + 1], -1);
00093   ACE_OS::strncpy (host_name, s + http_len, host_len);
00094   host_name[host_len] = '\0';
00095   ACE_Auto_Basic_Array_Ptr<ACEXML_Char> cleanup_host_name (host_name);
00096 
00097   // Get the port number (if any)
00098   unsigned short port = ACE_DEFAULT_HTTP_PORT;
00099   if (*url == ':')
00100     {
00101       port = (unsigned short) ACE_OS::strtol (++url, 0, 10); // Skip over ':'
00102       while ( *url != '\0' && *url != '/' )
00103         ++url;
00104     }
00105 
00106   // Set the addr
00107   int result = this->ACE_INET_Addr::set (port, host_name);
00108 
00109   if (result == -1)
00110     return -1;
00111 
00112   // Get the path name
00113   const ACEXML_Char* path_name = 0;
00114   if (*url == '\0')
00115     path_name = ACE_TEXT ("/");
00116   else
00117     path_name = url;
00118 
00119   ACE_ALLOCATOR_RETURN (this->path_name_, ACE_OS::strdup (path_name), -1);
00120   return result;
00121 }
00122 
00123 ACEXML_URL_Addr::ACEXML_URL_Addr (const ACEXML_URL_Addr &addr)
00124   : ACE_INET_Addr (),
00125     path_name_ (0),
00126     addr_string_ (0),
00127     addr_string_len_ (0)
00128 {
00129   if (this->set (addr) == -1)
00130     ACE_ERROR ((LM_ERROR,
00131                 ACE_TEXT ("%p\n"),
00132                 ACE_TEXT ("ACEXML_URL_Addr::ACEXML_URL_Addr")));
00133 }
00134 
00135 int
00136 ACEXML_URL_Addr::set (const ACEXML_URL_Addr &addr)
00137 {
00138   ACE_OS::free (this->path_name_);
00139   ACE_OS::free (this->addr_string_);
00140   if (this->ACE_INET_Addr::set (addr) == -1)
00141     return -1;
00142   else
00143     {
00144       if (addr.path_name_)
00145         ACE_ALLOCATOR_RETURN (this->path_name_,
00146                               ACE_OS::strdup (addr.path_name_),
00147                               -1);
00148       if (addr.addr_string_)
00149         ACE_ALLOCATOR_RETURN (this->addr_string_,
00150                               ACE_OS::strdup (addr.addr_string_),
00151                               -1);
00152       this->addr_string_len_ = addr.addr_string_len_;
00153       return 0;
00154     }
00155 }
00156 
00157 ACEXML_URL_Addr::ACEXML_URL_Addr (const ACEXML_Char *host_name,
00158                             const ACEXML_Char *path_name,
00159                             unsigned short port)
00160   : ACE_INET_Addr (port, host_name),
00161     path_name_ (ACE_OS::strdup (path_name)),
00162     addr_string_ (0),
00163     addr_string_len_ (0)
00164 {
00165 }
00166 
00167 ACEXML_URL_Addr::~ACEXML_URL_Addr (void)
00168 {
00169   ACE_OS::free (this->path_name_);
00170   ACE_OS::free (this->addr_string_);
00171   this->path_name_ = 0;
00172 }
00173 
00174 #if defined (ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION)
00175 template class ACE_Auto_Basic_Array_Ptr<ACEXML_Char>;
00176 #elif defined (ACE_HAS_TEMPLATE_INSTANTIATION_PRAGMA)
00177 #pragma instantiate ACE_Auto_Basic_Array_Ptr<ACEXML_Char>
00178 #endif /* ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION */

Generated on Mon Jun 16 13:23:27 2003 for ACEXML by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002