modules_k/db_unixodbc/list.c
e3649ef3
 /* 
  * $Id$ 
  *
  * UNIXODBC module
  *
  * Copyright (C) 2005-2006 Marco Lorrai
4c7d0961
  * Copyright (C) 2008 1&1 Internet AG
e3649ef3
  *
27642a08
  * This file is part of Kamailio, a free SIP server.
e3649ef3
  *
27642a08
  * Kamailio is free software; you can redistribute it and/or modify
e3649ef3
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version
  *
27642a08
  * Kamailio is distributed in the hope that it will be useful,
e3649ef3
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License 
  * along with this program; if not, write to the Free Software 
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  *
  *
  * History:
  * --------
  *  2005-12-01  initial commit (chgen)
696836d9
  *  2006-04-04  simplified link list (sgupta)
8e0bd7a1
  *  2006-05-05  removed static allocation of 1k per column data (sgupta)
e3649ef3
  */
 
f4d9af07
 #include "../../dprint.h"
 #include "../../mem/mem.h"
e3649ef3
 #include "list.h"
 
b6af1943
 
 /*!
  * \brief Create a list
  * \param start start of the list
  * \param link inserted element
  * \param n number of values
  * \param value inserted value
  * \return 0 on success, -1 on failure
  */
4c7d0961
 int db_unixodbc_list_insert(list** start, list** link, int n, strn* value)
e3649ef3
 {
696836d9
 	int i = 0;
f4d9af07
 
696836d9
 	if(!(*start)) {
 		*link = (list*)pkg_malloc(sizeof(list));
 		if(!(*link)) {
de7fe5e9
 			LM_ERR("no more pkg memory (1)\n");
f4d9af07
 			return -1;
 		}
8e0bd7a1
 		(*link)->rownum = n;
696836d9
 		(*link)->next = NULL;
8e0bd7a1
 
 		(*link)->lengths = (unsigned long*)pkg_malloc(sizeof(unsigned long)*n);
 		if(!(*link)->lengths) {
de7fe5e9
 			LM_ERR("no more pkg memory (2)\n");
696836d9
 			pkg_free(*link);
 			*link = NULL;
e3649ef3
 			return -1;
 		}
f4d9af07
 		for(i=0; i<n; i++)
8e0bd7a1
 			(*link)->lengths[i] = strlen(value[i].s) + 1;
 
 		(*link)->data = (char**)pkg_malloc(sizeof(char*)*n);
 		if(!(*link)->data) {
de7fe5e9
 			LM_ERR("no more pkg memory (3)\n");
8e0bd7a1
 			pkg_free( (*link)->lengths );
 			pkg_free(*link);
 			*link = NULL;
 			return -1;
 		}
 
 		for(i=0; i<n; i++) {
 			(*link)->data[i] = pkg_malloc(sizeof(char) * (*link)->lengths[i]);
 			if(!(*link)->data[i]) {
de7fe5e9
 				LM_ERR("no more pkg memory (4)\n");
8e0bd7a1
 				pkg_free( (*link)->lengths );
 				pkg_free( (*link)->data );
 				pkg_free(*link);
 				*link = NULL;
 				return -1;
 			}
 			strncpy((*link)->data[i], value[i].s, (*link)->lengths[i]);
 		}
696836d9
 	
 		*start = *link;
3ad0e6fa
 		return 0;
 	}
 	else
696836d9
 	{
 		list* nlink;
 		nlink=(list*)pkg_malloc(sizeof(list));
 		if(!nlink) {
de7fe5e9
 			LM_ERR("no more pkg memory (5)\n");
696836d9
 			return -1;
 		}
8e0bd7a1
 		nlink->rownum = n;
 
 		nlink->lengths = (unsigned long*)pkg_malloc(sizeof(unsigned long)*n);
 		if(!nlink->lengths) {
de7fe5e9
 			LM_ERR("no more pkg memory (6)\n");
696836d9
 			pkg_free(nlink);
8e0bd7a1
 			nlink = NULL;
696836d9
 			return -1;
 		}
 		for(i=0; i<n; i++)
8e0bd7a1
 			nlink->lengths[i] = strlen(value[i].s) + 1;
 
 		nlink->data = (char**)pkg_malloc(sizeof(char*)*n);
 		if(!nlink->data) {
de7fe5e9
 			LM_ERR("no more pkg memory (7)\n");
8e0bd7a1
 			pkg_free( nlink->lengths );
 			pkg_free(nlink);
 			nlink = NULL;
 			return -1;
 		}
 
 		for(i=0; i<n; i++) {
 			nlink->data[i] = pkg_malloc(sizeof(char) * nlink->lengths[i]);
 			if(!nlink->data[i]) {
de7fe5e9
 				LM_ERR("no more pkg memory (8)\n");
8e0bd7a1
 				pkg_free( nlink->lengths );
 				pkg_free( nlink->data );
 				pkg_free(nlink);
 				nlink = NULL;
 				return -1;
 			}
 			strncpy(nlink->data[i], value[i].s, nlink->lengths[i]);
 		}
e3649ef3
 
696836d9
 		nlink->next = NULL;
 		(*link)->next = nlink;
 		*link = (*link)->next;
e3649ef3
 
696836d9
 		return 0;
 	}
e3649ef3
 }
 
696836d9
 
b6af1943
 /*!
  * \brief Destroy a list
  * \param link list element(s)
  */
4c7d0961
 void db_unixodbc_list_destroy(list *start)
e3649ef3
 {
8e0bd7a1
 	int i = 0;
 
696836d9
 	while(start) {
 		list* temp = start;
 		start = start->next;
8e0bd7a1
 		for(i = 0; i < temp->rownum; i++)
 			pkg_free( temp->data[i] );
696836d9
 		pkg_free(temp->data);
8e0bd7a1
 		pkg_free(temp->lengths);
696836d9
 		pkg_free(temp);
f4d9af07
 	}
e3649ef3
 }