Asterisk - The Open Source Telephony Project  18.5.0
chanvars.c
Go to the documentation of this file.
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 
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 
98 {
99  struct varshead *head;
100 
101  head = ast_calloc(1, sizeof(*head));
102  if (!head) {
103  return NULL;
104  }
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 }
void * __ast_calloc(size_t nmemb, size_t size, const char *file, int lineno, const char *func) attribute_malloc
Definition: astmm.c:1635
Asterisk main include file. File version handling, generic pbx functions.
void ast_var_list_destroy(struct varshead *head)
Definition: chanvars.c:109
char * ast_var_find(const struct varshead *head, const char *name)
Definition: chanvars.c:85
String manipulation functions.
Channel Variables.
const char * ast_var_name(const struct ast_var_t *var)
Definition: chanvars.c:60
#define var
Definition: ast_expr2f.c:614
const char * ast_var_full_name(const struct ast_var_t *var)
Definition: chanvars.c:75
struct varshead * ast_var_list_clone(struct varshead *head)
Definition: chanvars.c:124
#define NULL
Definition: resample.c:96
int value
Definition: syslog.c:37
struct ast_var_t * _ast_var_assign(const char *name, const char *value, const char *file, int lineno, const char *function)
Definition: chanvars.c:36
struct varshead * ast_var_list_create(void)
Definition: chanvars.c:97
Utility functions.
#define AST_LIST_REMOVE_HEAD(head, field)
Removes and returns the head entry from a list.
Definition: linkedlists.h:832
const char * ast_var_value(const struct ast_var_t *var)
Definition: chanvars.c:80
char name[0]
Definition: chanvars.h:31
char * value
Definition: chanvars.h:30
#define AST_LIST_TRAVERSE(head, var, field)
Loops over (traverses) the entries in a list.
Definition: linkedlists.h:490
static const char name[]
Definition: cdr_mysql.c:74
#define ast_free(a)
Definition: astmm.h:182
#define ast_calloc(num, len)
A wrapper for calloc()
Definition: astmm.h:204
struct ast_var_t::@249 entries
#define ast_var_assign(name, value)
Definition: chanvars.h:40
void ast_var_delete(struct ast_var_t *var)
Definition: chanvars.c:55
#define AST_LIST_HEAD_INIT_NOLOCK(head)
Initializes a list head structure.
Definition: linkedlists.h:680
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:401
#define AST_VAR_LIST_TRAVERSE(head, var)
Definition: chanvars.h:49
static void AST_VAR_LIST_INSERT_TAIL(struct varshead *head, struct ast_var_t *var)
Definition: chanvars.h:51