list.c 3.8 KB

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