Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
New function: xbt_host_copy (even if it's trivial)
[simgrid.git] / src / xbt / xbt_host.c
index c668926..d1b3930 100644 (file)
 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(host,xbt,"Host management");
 
 /** \brief constructor */
 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(host,xbt,"Host management");
 
 /** \brief constructor */
-xbt_host_t xbt_host_new(char *name, int port)  {
+xbt_host_t xbt_host_new(const char *name, int port)  {
    xbt_host_t res=xbt_new(s_xbt_host_t, 1);
    xbt_host_t res=xbt_new(s_xbt_host_t, 1);
-   res->name = name;
+   res->name = xbt_strdup(name);
    res->port = port;
    return res;
 }
 
    res->port = port;
    return res;
 }
 
+xbt_host_t xbt_host_copy(xbt_host_t h) {
+   return xbt_host_new(h->name,h->port);
+}
+
+/** \brief constructor. Argument should be of form '<hostname>:<port>'. */
+xbt_host_t xbt_host_from_string(const char *hostport)  {
+   xbt_host_t res=xbt_new(s_xbt_host_t, 1);
+   char *name=xbt_strdup(hostport);
+   char *port_str=strchr(name,':');
+   xbt_assert1(port_str,"argument of xbt_host_from_string should be of form <hostname>:<port>, it's '%s'", hostport);
+   *port_str='\0';
+   port_str++;
+   
+   res->name = xbt_strdup(name); /* it will be shorter now that we cut the port */
+   res->port = atoi(port_str);
+   free(name);
+   return res;
+}
+
 /** \brief destructor */
 void xbt_host_free(xbt_host_t host) {
    if (host) {
 /** \brief destructor */
 void xbt_host_free(xbt_host_t host) {
    if (host) {