list.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * UNIXODBC module
  3. *
  4. * Copyright (C) 2005-2006 Marco Lorrai
  5. * Copyright (C) 2008 1&1 Internet AG
  6. *
  7. * This file is part of Kamailio, a free SIP server.
  8. *
  9. * Kamailio is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version
  13. *
  14. * Kamailio is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. *
  24. * History:
  25. * --------
  26. * 2005-12-01 initial commit (chgen)
  27. * 2006-04-04 simplified link list (sgupta)
  28. * 2006-05-05 removed static allocation of 1k per column data (sgupta)
  29. */
  30. #include "../../dprint.h"
  31. #include "../../mem/mem.h"
  32. #include "list.h"
  33. /*!
  34. * \brief Create a list
  35. * \param start start of the list
  36. * \param link inserted element
  37. * \param n number of values
  38. * \param value inserted value
  39. * \return 0 on success, -1 on failure
  40. */
  41. int db_unixodbc_list_insert(list** start, list** link, int n, strn* value)
  42. {
  43. int i = 0;
  44. list* nlink;
  45. if (!(*start)) *link = NULL;
  46. nlink=(list*)pkg_malloc(sizeof(list));
  47. if(!nlink) {
  48. LM_ERR("no more pkg memory (1)\n");
  49. return -1;
  50. }
  51. nlink->rownum = n;
  52. nlink->next = NULL;
  53. nlink->lengths = (unsigned long*)pkg_malloc(sizeof(unsigned long)*n);
  54. if(!nlink->lengths) {
  55. LM_ERR("no more pkg memory (2)\n");
  56. pkg_free(nlink);
  57. return -1;
  58. }
  59. for(i=0; i<n; i++)
  60. nlink->lengths[i] = value[i].buflen;
  61. nlink->data = (char**)pkg_malloc(sizeof(char*)*n);
  62. if(!nlink->data) {
  63. LM_ERR("no more pkg memory (3)\n");
  64. pkg_free( nlink->lengths );
  65. pkg_free(nlink);
  66. return -1;
  67. }
  68. for(i=0; i<n; i++) {
  69. nlink->data[i] = pkg_malloc(sizeof(char) * nlink->lengths[i]);
  70. if(!nlink->data[i]) {
  71. LM_ERR("no more pkg memory (4)\n");
  72. pkg_free( nlink->lengths );
  73. pkg_free( nlink->data );
  74. pkg_free(nlink);
  75. return -1;
  76. }
  77. memcpy(nlink->data[i], value[i].s, nlink->lengths[i]);
  78. }
  79. if (!(*start)) {
  80. *link = nlink;
  81. *start = *link;
  82. } else {
  83. (*link)->next = nlink;
  84. *link = (*link)->next;
  85. }
  86. return 0;
  87. }
  88. /*!
  89. * \brief Destroy a list
  90. * \param link list element(s)
  91. */
  92. void db_unixodbc_list_destroy(list *start)
  93. {
  94. int i = 0;
  95. while(start) {
  96. list* temp = start;
  97. start = start->next;
  98. for(i = 0; i < temp->rownum; i++)
  99. pkg_free( temp->data[i] );
  100. pkg_free(temp->data);
  101. pkg_free(temp->lengths);
  102. pkg_free(temp);
  103. }
  104. }