db_uri.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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_uri.h"
  32. #include "../../dprint.h"
  33. #include "../../mem/mem.h"
  34. #include "../../ut.h"
  35. #include <string.h>
  36. /* compare s1 & s2 with a function f (which should return 0 if ==);
  37. * s1 & s2 can be null
  38. * return 0 if match, 1 if not */
  39. #define CMP_STR(s1, s2, f) \
  40. ((s1) != (s2)) && ((s1) == 0 || (s2) == 0 || (f)((s1), (s2)) != 0)
  41. unsigned char db_uri_cmp(db_uri_t* uri1, db_uri_t* uri2)
  42. {
  43. if (!uri1 || !uri2) return 0;
  44. if (CMP_STR(uri1->scheme.s, uri2->scheme.s, strcmp)) return 0;
  45. if (uri1->cmp) {
  46. return uri1->cmp(uri1, uri2);
  47. } else {
  48. /* No driver specific comparison function, compare bodies
  49. * byte-wise
  50. */
  51. if (CMP_STR(uri1->body.s, uri2->body.s, strcmp)) return 0;
  52. }
  53. return 1;
  54. }
  55. /*
  56. * Create a new database URI
  57. */
  58. db_uri_t* db_uri(const char* uri)
  59. {
  60. char* colon;
  61. int len;
  62. db_uri_t* newp;
  63. char *turi;
  64. newp = (db_uri_t*)pkg_malloc(sizeof(db_uri_t));
  65. if (newp == NULL) goto error;
  66. memset(newp, '\0', sizeof(db_uri_t));
  67. if (db_gen_init(&newp->gen) < 0) goto error;
  68. len = strlen(uri);
  69. turi = (char*)uri;
  70. colon = q_memchr(turi, ':', len);
  71. if (colon == NULL) {
  72. newp->scheme.s = pkg_malloc(len + 1);
  73. if (newp->scheme.s == NULL) goto error;
  74. memcpy(newp->scheme.s, uri, len);
  75. newp->scheme.len = len;
  76. } else {
  77. newp->scheme.len = colon - uri;
  78. newp->scheme.s = pkg_malloc(newp->scheme.len + 1);
  79. if (newp->scheme.s == NULL) goto error;
  80. memcpy(newp->scheme.s, uri, colon - uri);
  81. newp->body.len = len - newp->scheme.len - 1;
  82. newp->body.s = pkg_malloc(newp->body.len + 1);
  83. if (newp->body.s == NULL) goto error;
  84. memcpy(newp->body.s, colon + 1, newp->body.len);
  85. newp->body.s[newp->body.len] = '\0';
  86. }
  87. newp->scheme.s[newp->scheme.len] = '\0';
  88. /* Call db_uri function if the driver has it */
  89. if (db_drv_call(&newp->scheme, "db_uri", newp, 0) < 0) goto error;
  90. return newp;
  91. error:
  92. ERR("db_uri: Error while creating db_uri structure\n");
  93. if (newp) {
  94. db_gen_free(&newp->gen);
  95. if (newp->body.s) pkg_free(newp->body.s);
  96. if (newp->scheme.s) pkg_free(newp->scheme.s);
  97. pkg_free(newp);
  98. }
  99. return 0;
  100. }
  101. /*
  102. * Free a connection identifier
  103. */
  104. void db_uri_free(db_uri_t* uri)
  105. {
  106. if (uri == NULL) return;
  107. db_gen_free(&uri->gen);
  108. if (uri->body.s) pkg_free(uri->body.s);
  109. if (uri->scheme.s) pkg_free(uri->scheme.s);
  110. pkg_free(uri);
  111. }
  112. /** @} */