chanvars.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2005, Digium, Inc.
  5. *
  6. * Mark Spencer <markster@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. *
  20. * \brief Channel Variables
  21. *
  22. * \author Mark Spencer <markster@digium.com>
  23. */
  24. /*** MODULEINFO
  25. <support_level>core</support_level>
  26. ***/
  27. #include "asterisk.h"
  28. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  29. #include "asterisk/chanvars.h"
  30. #include "asterisk/strings.h"
  31. #include "asterisk/utils.h"
  32. #ifdef __AST_DEBUG_MALLOC
  33. struct ast_var_t *_ast_var_assign(const char *name, const char *value, const char *file, int lineno, const char *function)
  34. #else
  35. struct ast_var_t *ast_var_assign(const char *name, const char *value)
  36. #endif
  37. {
  38. struct ast_var_t *var;
  39. int name_len = strlen(name) + 1;
  40. int value_len = strlen(value) + 1;
  41. #ifdef __AST_DEBUG_MALLOC
  42. if (!(var = __ast_calloc(sizeof(*var) + name_len + value_len, sizeof(char), file, lineno, function))) {
  43. #else
  44. if (!(var = ast_calloc(sizeof(*var) + name_len + value_len, sizeof(char)))) {
  45. #endif
  46. return NULL;
  47. }
  48. ast_copy_string(var->name, name, name_len);
  49. var->value = var->name + name_len;
  50. ast_copy_string(var->value, value, value_len);
  51. return var;
  52. }
  53. void ast_var_delete(struct ast_var_t *var)
  54. {
  55. ast_free(var);
  56. }
  57. const char *ast_var_name(const struct ast_var_t *var)
  58. {
  59. const char *name;
  60. if (var == NULL || (name = var->name) == NULL)
  61. return NULL;
  62. /* Return the name without the initial underscores */
  63. if (name[0] == '_') {
  64. name++;
  65. if (name[0] == '_')
  66. name++;
  67. }
  68. return name;
  69. }
  70. const char *ast_var_full_name(const struct ast_var_t *var)
  71. {
  72. return (var ? var->name : NULL);
  73. }
  74. const char *ast_var_value(const struct ast_var_t *var)
  75. {
  76. return (var ? var->value : NULL);
  77. }
  78. char *ast_var_find(const struct varshead *head, const char *name)
  79. {
  80. struct ast_var_t *var;
  81. AST_LIST_TRAVERSE(head, var, entries) {
  82. if (!strcmp(name, var->name)) {
  83. return var->value;
  84. }
  85. }
  86. return NULL;
  87. }
  88. struct varshead *ast_var_list_create(void)
  89. {
  90. struct varshead *head;
  91. head = ast_calloc(1, sizeof(*head));
  92. if (!head) {
  93. return NULL;
  94. }
  95. AST_LIST_HEAD_INIT_NOLOCK(head);
  96. return head;
  97. }
  98. void ast_var_list_destroy(struct varshead *head)
  99. {
  100. struct ast_var_t *var;
  101. if (!head) {
  102. return;
  103. }
  104. while ((var = AST_LIST_REMOVE_HEAD(head, entries))) {
  105. ast_var_delete(var);
  106. }
  107. ast_free(head);
  108. }
  109. struct varshead *ast_var_list_clone(struct varshead *head)
  110. {
  111. struct varshead *clone;
  112. struct ast_var_t *var, *newvar;
  113. if (!head) {
  114. return NULL;
  115. }
  116. clone = ast_var_list_create();
  117. if (!clone) {
  118. return NULL;
  119. }
  120. AST_VAR_LIST_TRAVERSE(head, var) {
  121. newvar = ast_var_assign(var->name, var->value);
  122. if (!newvar) {
  123. ast_var_list_destroy(clone);
  124. return NULL;
  125. }
  126. AST_VAR_LIST_INSERT_TAIL(clone, newvar);
  127. }
  128. return clone;
  129. }