/* This file is auto-generated from "url.xml" by the typemaker
   tool of Gwenhywfar.
   Do not edit this file -- all changes will be lost! */
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include "url_p.h"
#include <gwenhywfar/misc.h>
#include <gwenhywfar/db.h>
#include <gwenhywfar/debug.h>
#include <assert.h>
#include <stdlib.h>
#include <strings.h>

#include <gwenhywfar/types.h>
#include <gwenhywfar/urlfns.h>


GWEN_LIST_FUNCTIONS(GWEN_URL, GWEN_Url)
GWEN_LIST2_FUNCTIONS(GWEN_URL, GWEN_Url)




GWEN_URL *GWEN_Url_new(void) {
  GWEN_URL *st;

  GWEN_NEW_OBJECT(GWEN_URL, st)
  st->_usage=1;
  GWEN_LIST_INIT(GWEN_URL, st)
  st->vars=GWEN_DB_Group_new("vars");
  return st;
}


void GWEN_Url_free(GWEN_URL *st) {
  if (st) {
    assert(st->_usage);
    if (--(st->_usage)==0) {
      if (st->protocol)
        free(st->protocol);
      if (st->server)
        free(st->server);
      if (st->path)
        free(st->path);
      if (st->userName)
        free(st->userName);
      if (st->password)
        free(st->password);
      if (st->vars)
        GWEN_DB_Group_free(st->vars);
      if (st->url)
        free(st->url);
      GWEN_LIST_FINI(GWEN_URL, st)
      GWEN_FREE_OBJECT(st);
    }
  }

}


GWEN_URL *GWEN_Url_dup(const GWEN_URL *d) {
  GWEN_URL *st;

  assert(d);
  st=GWEN_Url_new();
  if (d->protocol)
    st->protocol=strdup(d->protocol);
  if (d->server)
    st->server=strdup(d->server);
  st->port=d->port;
  if (d->path)
    st->path=strdup(d->path);
  if (d->userName)
    st->userName=strdup(d->userName);
  if (d->password)
    st->password=strdup(d->password);
  if (d->vars) {
    GWEN_DB_Group_free(st->vars);
    st->vars=GWEN_DB_Group_dup(d->vars);
  }
  if (d->url)
    st->url=strdup(d->url);
  return st;
}


int GWEN_Url_toDb(const GWEN_URL *st, GWEN_DB_NODE *db) {
  assert(st);
  assert(db);
  if (st->protocol)
    if (GWEN_DB_SetCharValue(db, GWEN_DB_FLAGS_OVERWRITE_VARS, "protocol", st->protocol))
      return -1;
  if (st->server)
    if (GWEN_DB_SetCharValue(db, GWEN_DB_FLAGS_OVERWRITE_VARS, "server", st->server))
      return -1;
  if (GWEN_DB_SetIntValue(db, GWEN_DB_FLAGS_OVERWRITE_VARS, "port", st->port))
    return -1;
  if (st->path)
    if (GWEN_DB_SetCharValue(db, GWEN_DB_FLAGS_OVERWRITE_VARS, "path", st->path))
      return -1;
  if (st->userName)
    if (GWEN_DB_SetCharValue(db, GWEN_DB_FLAGS_OVERWRITE_VARS, "userName", st->userName))
      return -1;
  if (st->password)
    if (GWEN_DB_SetCharValue(db, GWEN_DB_FLAGS_OVERWRITE_VARS, "password", st->password))
      return -1;
  if (st->vars)
    if (GWEN_DB_AddGroupChildren(st->vars, GWEN_DB_GetGroup(db, GWEN_DB_FLAGS_DEFAULT, "vars")))
      return -1;
  if (st->url)
    if (GWEN_DB_SetCharValue(db, GWEN_DB_FLAGS_OVERWRITE_VARS, "url", st->url))
      return -1;
  return 0;
}


int GWEN_Url_ReadDb(GWEN_URL *st, GWEN_DB_NODE *db) {
  assert(st);
  assert(db);
  GWEN_Url_SetProtocol(st, GWEN_DB_GetCharValue(db, "protocol", 0, 0));
  GWEN_Url_SetServer(st, GWEN_DB_GetCharValue(db, "server", 0, 0));
  GWEN_Url_SetPort(st, GWEN_DB_GetIntValue(db, "port", 0, 0));
  GWEN_Url_SetPath(st, GWEN_DB_GetCharValue(db, "path", 0, 0));
  GWEN_Url_SetUserName(st, GWEN_DB_GetCharValue(db, "userName", 0, 0));
  GWEN_Url_SetPassword(st, GWEN_DB_GetCharValue(db, "password", 0, 0));
  if (1) { /* for local vars */
    GWEN_DB_NODE *dbT;

    dbT=GWEN_DB_GetGroup(db, GWEN_PATH_FLAGS_NAMEMUSTEXIST, "vars");
    if (dbT) {
      if (st->vars)
        GWEN_DB_Group_free(st->vars);
      st->vars=GWEN_DB_Group_dup(dbT);
    }
  }
  GWEN_Url_SetUrl(st, GWEN_DB_GetCharValue(db, "url", 0, 0));
  return 0;
}


GWEN_URL *GWEN_Url_fromDb(GWEN_DB_NODE *db) {
  GWEN_URL *st;

  assert(db);
  st=GWEN_Url_new();
  GWEN_Url_ReadDb(st, db);
  st->_modified=0;
  return st;
}




const char *GWEN_Url_GetProtocol(const GWEN_URL *st) {
  assert(st);
  return st->protocol;
}


void GWEN_Url_SetProtocol(GWEN_URL *st, const char *d) {
  assert(st);
  if (st->protocol)
    free(st->protocol);
  if (d && *d)
    st->protocol=strdup(d);
  else
    st->protocol=0;
  st->_modified=1;
}




const char *GWEN_Url_GetServer(const GWEN_URL *st) {
  assert(st);
  return st->server;
}


void GWEN_Url_SetServer(GWEN_URL *st, const char *d) {
  assert(st);
  if (st->server)
    free(st->server);
  if (d && *d)
    st->server=strdup(d);
  else
    st->server=0;
  st->_modified=1;
}




int GWEN_Url_GetPort(const GWEN_URL *st) {
  assert(st);
  return st->port;
}


void GWEN_Url_SetPort(GWEN_URL *st, int d) {
  assert(st);
  st->port=d;
  st->_modified=1;
}




const char *GWEN_Url_GetPath(const GWEN_URL *st) {
  assert(st);
  return st->path;
}


void GWEN_Url_SetPath(GWEN_URL *st, const char *d) {
  assert(st);
  if (st->path)
    free(st->path);
  if (d && *d)
    st->path=strdup(d);
  else
    st->path=0;
  st->_modified=1;
}




const char *GWEN_Url_GetUserName(const GWEN_URL *st) {
  assert(st);
  return st->userName;
}


void GWEN_Url_SetUserName(GWEN_URL *st, const char *d) {
  assert(st);
  if (st->userName)
    free(st->userName);
  if (d && *d)
    st->userName=strdup(d);
  else
    st->userName=0;
  st->_modified=1;
}




const char *GWEN_Url_GetPassword(const GWEN_URL *st) {
  assert(st);
  return st->password;
}


void GWEN_Url_SetPassword(GWEN_URL *st, const char *d) {
  assert(st);
  if (st->password)
    free(st->password);
  if (d && *d)
    st->password=strdup(d);
  else
    st->password=0;
  st->_modified=1;
}




GWEN_DB_NODE *GWEN_Url_GetVars(const GWEN_URL *st) {
  assert(st);
  return st->vars;
}


void GWEN_Url_SetVars(GWEN_URL *st, GWEN_DB_NODE *d) {
  assert(st);
  if (st->vars)
    GWEN_DB_Group_free(st->vars);
  if (d)
    st->vars=GWEN_DB_Group_dup(d);
  else
    st->vars=0;
  st->_modified=1;
}




const char *GWEN_Url_GetUrl(const GWEN_URL *st) {
  assert(st);
  return st->url;
}


void GWEN_Url_SetUrl(GWEN_URL *st, const char *d) {
  assert(st);
  if (st->url)
    free(st->url);
  if (d && *d)
    st->url=strdup(d);
  else
    st->url=0;
  st->_modified=1;
}




int GWEN_Url_IsModified(const GWEN_URL *st) {
  assert(st);
  return st->_modified;
}


void GWEN_Url_SetModified(GWEN_URL *st, int i) {
  assert(st);
  st->_modified=i;
}


void GWEN_Url_Attach(GWEN_URL *st) {
  assert(st);
  st->_usage++;
}
GWEN_URL *GWEN_Url_List2__freeAll_cb(GWEN_URL *st, GWEN_UNUSED void *user_data) {
  GWEN_Url_free(st);
  return 0;
}


void GWEN_Url_List2_freeAll(GWEN_URL_LIST2 *stl) {
  if (stl) {
    GWEN_Url_List2_ForEach(stl, GWEN_Url_List2__freeAll_cb, 0);
    GWEN_Url_List2_free(stl);
  }
}


GWEN_URL_LIST *GWEN_Url_List_dup(const GWEN_URL_LIST *stl) {
  if (stl) {
    GWEN_URL_LIST *nl;
    GWEN_URL *e;

    nl=GWEN_Url_List_new();
    e=GWEN_Url_List_First(stl);
    while(e) {
      GWEN_URL *ne;

      ne=GWEN_Url_dup(e);
      assert(ne);
      GWEN_Url_List_Add(ne, nl);
      e=GWEN_Url_List_Next(e);
    } /* while (e) */
    return nl;
  }
  else
    return 0;
}




