km_flat_id.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * $Id$
  3. *
  4. * Flatstore module connection identifier
  5. *
  6. * Copyright (C) 2004 FhG Fokus
  7. *
  8. * This file is part of Kamailio, a free SIP server.
  9. *
  10. * Kamailio is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version
  14. *
  15. * Kamailio is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. */
  24. #include <string.h>
  25. #include "../../dprint.h"
  26. #include "../../mem/mem.h"
  27. #include "km_flat_id.h"
  28. /*
  29. * Create a new connection identifier
  30. */
  31. struct flat_id* new_flat_id(char* dir, char* table)
  32. {
  33. struct flat_id* ptr;
  34. char* t;
  35. int t_len;
  36. if (!dir || !table) {
  37. LM_ERR("invalid parameter(s)\n");
  38. return 0;
  39. }
  40. ptr = (struct flat_id*)pkg_malloc(sizeof(struct flat_id));
  41. if (!ptr) {
  42. LM_ERR("no pkg memory left\n");
  43. return 0;
  44. }
  45. memset(ptr, 0, sizeof(struct flat_id));
  46. /* alloc memory for the table name */
  47. t_len = strlen(table);
  48. t = (char*)pkg_malloc(t_len+1);
  49. if (!t) {
  50. LM_ERR("no pkg memory left\n");
  51. pkg_free(ptr);
  52. return 0;
  53. }
  54. memset(t, 0, t_len);
  55. ptr->dir.s = dir;
  56. ptr->dir.len = strlen(dir);
  57. memcpy(t, table, t_len);
  58. t[t_len] = '\0';
  59. ptr->table.s = t;
  60. ptr->table.len = t_len;
  61. return ptr;
  62. }
  63. /*
  64. * Compare two connection identifiers
  65. */
  66. unsigned char cmp_flat_id(struct flat_id* id1, struct flat_id* id2)
  67. {
  68. if (!id1 || !id2) return 0;
  69. if (id1->dir.len != id2->dir.len) return 0;
  70. if (id1->table.len != id2->table.len) return 0;
  71. if (memcmp(id1->dir.s, id2->dir.s, id1->dir.len)) return 0;
  72. if (memcmp(id1->table.s, id2->table.s, id1->table.len)) return 0;
  73. return 1;
  74. }
  75. /*
  76. * Free a connection identifier
  77. */
  78. void free_flat_id(struct flat_id* id)
  79. {
  80. if (!id) return;
  81. if (id->table.s)
  82. pkg_free(id->table.s);
  83. pkg_free(id);
  84. }