db_fld.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2001-2005 FhG FOKUS
  5. * Copyright (C) 2006-2007 iptelorg GmbH
  6. *
  7. * This file is part of ser, a free SIP server.
  8. *
  9. * ser 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. * For a license to use the ser software under conditions
  15. * other than those described here, or to purchase support for this
  16. * software, please contact iptel.org by e-mail at the following addresses:
  17. * [email protected]
  18. *
  19. * ser is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software
  26. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  27. */
  28. /** \ingroup DB_API
  29. * @{
  30. */
  31. #include "db_fld.h"
  32. #include "../../mem/mem.h"
  33. #include "../../dprint.h"
  34. #include <string.h>
  35. char* db_fld_str[] = {
  36. "DB_NONE",
  37. "DB_INT",
  38. "DB_FLOAT",
  39. "DB_DOUBLE",
  40. "DB_CSTR",
  41. "DB_STR",
  42. "DB_DATETIME",
  43. "DB_BLOB",
  44. "DB_BITMAP"
  45. };
  46. int db_fld_init(db_fld_t* fld)
  47. {
  48. int i;
  49. for(i = 0; !DB_FLD_LAST(fld[i]); i++) {
  50. if (db_gen_init(&fld[i].gen) < 0) return -1;
  51. }
  52. return 0;
  53. }
  54. void db_fld_close(db_fld_t* fld)
  55. {
  56. int i;
  57. for(i = 0; !DB_FLD_LAST(fld[i]); i++) {
  58. db_gen_free(&fld[i].gen);
  59. }
  60. }
  61. db_fld_t* db_fld(size_t n)
  62. {
  63. int i;
  64. db_fld_t* newp;
  65. newp = (db_fld_t*)pkg_malloc(sizeof(db_fld_t) * n);
  66. if (newp == NULL) {
  67. ERR("db_fld: No memory left\n");
  68. return NULL;
  69. }
  70. memset(newp, '\0', sizeof(db_fld_t) * n);
  71. for(i = 0; i < n; i++) {
  72. if (db_gen_init(&newp[i].gen) < 0) goto error;
  73. }
  74. return newp;
  75. error:
  76. if (newp) {
  77. while(i >= 0) {
  78. db_gen_free(&newp[i].gen);
  79. i--;
  80. }
  81. pkg_free(newp);
  82. }
  83. return NULL;
  84. }
  85. db_fld_t* db_fld_copy(db_fld_t* fld)
  86. {
  87. int i, n;
  88. db_fld_t* newp;
  89. for(n = 0; fld[n].name; n++);
  90. n++; /* We need to copy the terminating element too */
  91. newp = (db_fld_t*)pkg_malloc(sizeof(db_fld_t) * n);
  92. if (newp == NULL) {
  93. ERR("db_fld: No memory left\n");
  94. return NULL;
  95. }
  96. memcpy(newp, fld, sizeof(db_fld_t) * n);
  97. for(i = 0; i < n; i++) {
  98. if (db_gen_init(&newp[i].gen) < 0) goto error;
  99. }
  100. return newp;
  101. error:
  102. ERR("db_fld_copy() failed\n");
  103. if (newp) {
  104. /* Free everything allocated in this function so far */
  105. while(i >= 0) {
  106. db_gen_free(&newp[i].gen);
  107. i--;
  108. }
  109. pkg_free(newp);
  110. }
  111. return NULL;
  112. }
  113. void db_fld_free(db_fld_t* fld)
  114. {
  115. int i;
  116. if (DB_FLD_EMPTY(fld)) return;
  117. for(i = 0; !DB_FLD_LAST(fld[i]); i++) {
  118. db_gen_free(&fld[i].gen);
  119. }
  120. pkg_free(fld);
  121. }
  122. /** @} */