Debian Codegraph @

main/chanvars.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 1999 - 2005, Digium, Inc.
5 *
6 * Mark Spencer <[email protected]>
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
19 /*! \file
20 *
21 * \brief Channel Variables
22 *
23 * \author Mark Spencer <[email protected]>
24 */
25
26 /*** MODULEINFO
27 <support_level>core</support_level>
28 ***/
29
30 #include "asterisk.h"
31
32 #include "asterisk/chanvars.h"
33 #include "asterisk/strings.h"
34 #include "asterisk/utils.h"
35
36 struct ast_var_t *_ast_var_assign(const char *name, const char *value, const char *file, int lineno, const char *function)
37 {
38 struct ast_var_t *var;
39 int name_len = strlen(name) + 1;
40 int value_len = strlen(value) + 1;
41
42 var = __ast_calloc(sizeof(*var) + name_len + value_len, sizeof(char),
43 file, lineno, function);
44 if (!var) {
45 return NULL;
46 }
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
52 return var;
53 }
54
55 void ast_var_delete(struct ast_var_t *var)
56 {
57 ast_free(var);
58 }
59
60 const char *ast_var_name(const struct ast_var_t *var)
61 {
62 const char *name;
63
64 if (var == NULL || (name = var->name) == NULL)
65 return NULL;
66 /* Return the name without the initial underscores */
67 if (name[0] == '_') {
68 name++;
69 if (name[0] == '_')
70 name++;
71 }
72 return name;
73 }
74
75 const char *ast_var_full_name(const struct ast_var_t *var)
76 {
77 return (var ? var->name : NULL);
78 }
79
80 const char *ast_var_value(const struct ast_var_t *var)
81 {
82 return (var ? var->value : NULL);
83 }
84
85 char *ast_var_find(const struct varshead *head, const char *name)
86 {
87 struct ast_var_t *var;
88
89 AST_LIST_TRAVERSE(head, var, entries) {
90 if (!strcmp(name, var->name)) {
91 return var->value;
92 }
93 }
94 return NULL;
95 }
96
97 struct varshead *ast_var_list_create(void)
98 {
99 struct varshead *head;
100
101 head = ast_calloc(1, sizeof(*head));
102 if (!head) {
103 return NULL;
104 }
105 AST_LIST_HEAD_INIT_NOLOCK(head);
106 return head;
107 }
108
109 void ast_var_list_destroy(struct varshead *head)
110 {
111 struct ast_var_t *var;
112
113 if (!head) {
114 return;
115 }
116
117 while ((var = AST_LIST_REMOVE_HEAD(head, entries))) {
118 ast_var_delete(var);
119 }
120
121 ast_free(head);
122 }
123
124 struct varshead *ast_var_list_clone(struct varshead *head)
125 {
126 struct varshead *clone;
127 struct ast_var_t *var, *newvar;
128
129 if (!head) {
130 return NULL;
131 }
132
133 clone = ast_var_list_create();
134 if (!clone) {
135 return NULL;
136 }
137
138 AST_VAR_LIST_TRAVERSE(head, var) {
139 newvar = ast_var_assign(var->name, var->value);
140 if (!newvar) {
141 ast_var_list_destroy(clone);
142 return NULL;
143 }
144 AST_VAR_LIST_INSERT_TAIL(clone, newvar);
145 }
146
147 return clone;
148 }