list.c

Go to the documentation of this file.
00001 /* 
00002  * $Id: list.c 5422 2009-01-05 17:23:48Z henningw $ 
00003  *
00004  * UNIXODBC module
00005  *
00006  * Copyright (C) 2005-2006 Marco Lorrai
00007  * Copyright (C) 2008 1&1 Internet AG
00008  *
00009  * This file is part of Kamailio, a free SIP server.
00010  *
00011  * Kamailio is free software; you can redistribute it and/or modify
00012  * it under the terms of the GNU General Public License as published by
00013  * the Free Software Foundation; either version 2 of the License, or
00014  * (at your option) any later version
00015  *
00016  * Kamailio is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  * GNU General Public License for more details.
00020  *
00021  * You should have received a copy of the GNU General Public License 
00022  * along with this program; if not, write to the Free Software 
00023  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00024  *
00025  *
00026  * History:
00027  * --------
00028  *  2005-12-01  initial commit (chgen)
00029  *  2006-04-04  simplified link list (sgupta)
00030  *  2006-05-05  removed static allocation of 1k per column data (sgupta)
00031  */
00032 
00033 #include "../../dprint.h"
00034 #include "../../mem/mem.h"
00035 #include "list.h"
00036 
00037 
00038 /*!
00039  * \brief Create a list
00040  * \param start start of the list
00041  * \param link inserted element
00042  * \param n number of values
00043  * \param value inserted value
00044  * \return 0 on success, -1 on failure
00045  */
00046 int db_unixodbc_list_insert(list** start, list** link, int n, strn* value)
00047 {
00048    int i = 0;
00049 
00050    if(!(*start)) {
00051       *link = (list*)pkg_malloc(sizeof(list));
00052       if(!(*link)) {
00053          LM_ERR("no more pkg memory (1)\n");
00054          return -1;
00055       }
00056       (*link)->rownum = n;
00057       (*link)->next = NULL;
00058 
00059       (*link)->lengths = (unsigned long*)pkg_malloc(sizeof(unsigned long)*n);
00060       if(!(*link)->lengths) {
00061          LM_ERR("no more pkg memory (2)\n");
00062          pkg_free(*link);
00063          *link = NULL;
00064          return -1;
00065       }
00066       for(i=0; i<n; i++)
00067          (*link)->lengths[i] = strlen(value[i].s) + 1;
00068 
00069       (*link)->data = (char**)pkg_malloc(sizeof(char*)*n);
00070       if(!(*link)->data) {
00071          LM_ERR("no more pkg memory (3)\n");
00072          pkg_free( (*link)->lengths );
00073          pkg_free(*link);
00074          *link = NULL;
00075          return -1;
00076       }
00077 
00078       for(i=0; i<n; i++) {
00079          (*link)->data[i] = pkg_malloc(sizeof(char) * (*link)->lengths[i]);
00080          if(!(*link)->data[i]) {
00081             LM_ERR("no more pkg memory (4)\n");
00082             pkg_free( (*link)->lengths );
00083             pkg_free( (*link)->data );
00084             pkg_free(*link);
00085             *link = NULL;
00086             return -1;
00087          }
00088          strncpy((*link)->data[i], value[i].s, (*link)->lengths[i]);
00089       }
00090    
00091       *start = *link;
00092       return 0;
00093    }
00094    else
00095    {
00096       list* nlink;
00097       nlink=(list*)pkg_malloc(sizeof(list));
00098       if(!nlink) {
00099          LM_ERR("no more pkg memory (5)\n");
00100          return -1;
00101       }
00102       nlink->rownum = n;
00103 
00104       nlink->lengths = (unsigned long*)pkg_malloc(sizeof(unsigned long)*n);
00105       if(!nlink->lengths) {
00106          LM_ERR("no more pkg memory (6)\n");
00107          pkg_free(nlink);
00108          nlink = NULL;
00109          return -1;
00110       }
00111       for(i=0; i<n; i++)
00112          nlink->lengths[i] = strlen(value[i].s) + 1;
00113 
00114       nlink->data = (char**)pkg_malloc(sizeof(char*)*n);
00115       if(!nlink->data) {
00116          LM_ERR("no more pkg memory (7)\n");
00117          pkg_free( nlink->lengths );
00118          pkg_free(nlink);
00119          nlink = NULL;
00120          return -1;
00121       }
00122 
00123       for(i=0; i<n; i++) {
00124          nlink->data[i] = pkg_malloc(sizeof(char) * nlink->lengths[i]);
00125          if(!nlink->data[i]) {
00126             LM_ERR("no more pkg memory (8)\n");
00127             pkg_free( nlink->lengths );
00128             pkg_free( nlink->data );
00129             pkg_free(nlink);
00130             nlink = NULL;
00131             return -1;
00132          }
00133          strncpy(nlink->data[i], value[i].s, nlink->lengths[i]);
00134       }
00135 
00136       nlink->next = NULL;
00137       (*link)->next = nlink;
00138       *link = (*link)->next;
00139 
00140       return 0;
00141    }
00142 }
00143 
00144 
00145 /*!
00146  * \brief Destroy a list
00147  * \param link list element(s)
00148  */
00149 void db_unixodbc_list_destroy(list *start)
00150 {
00151    int i = 0;
00152 
00153    while(start) {
00154       list* temp = start;
00155       start = start->next;
00156       for(i = 0; i < temp->rownum; i++)
00157          pkg_free( temp->data[i] );
00158       pkg_free(temp->data);
00159       pkg_free(temp->lengths);
00160       pkg_free(temp);
00161    }
00162 }
00163 

Generated on Wed May 23 18:00:28 2012 for Kamailio - The Open Source SIP Server by  doxygen 1.5.6