dabfdda51bf307615fb3b0314e107a8cb224807b
[asterisk/asterisk.git] / chanvars.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Channel Variables
5  * 
6  * Copyright (C) 2002, Mark Spencer
7  *
8  * Mark Spencer <markster@linux-support.net>
9  *
10  * This program is free software, distributed under the terms of
11  * the GNU General Public License
12  */
13
14 #include <stdlib.h>
15 #include <string.h>
16
17 #include <asterisk/chanvars.h>
18 #include <asterisk/logger.h>
19
20 struct ast_var_t *ast_var_assign(const char *name, const char *value)
21 {
22         int i;
23         struct ast_var_t *var;
24         
25         var = malloc(sizeof(struct ast_var_t));
26
27         if (var == NULL)
28         {
29                 ast_log(LOG_WARNING, "Out of memory\n");
30                 return NULL;
31         }
32         
33         i = strlen(value);
34         var->value = malloc(i + 1);
35         if (var->value == NULL)
36         {
37                 ast_log(LOG_WARNING, "Out of memory\n");
38                 free(var);
39                 return NULL;
40         }
41
42         strncpy(var->value, value, i);
43         var->value[i] = '\0';
44         
45         i = strlen(name);
46         var->name = malloc(i + 1);
47         if (var->name == NULL)
48         {
49                 ast_log(LOG_WARNING, "Out of memory\n");
50                 free(var->value);
51                 free(var);
52                 return NULL;
53         }
54
55         strncpy(var->name, name, i); 
56         var->name[i] = '\0';
57
58         return var;
59 }       
60         
61 void ast_var_delete(struct ast_var_t *var)
62 {
63         if (var == NULL) return;
64
65         if (var->name != NULL) free(var->name);
66         if (var->value != NULL) free(var->value);
67
68         free(var);
69 }
70
71 char *ast_var_name(struct ast_var_t *var)
72 {
73         char *name;
74
75         if (var == NULL)
76                 return NULL;
77         if (var->name == NULL)
78                 return NULL;
79         /* Return the name without the initial underscores */
80         if ((strlen(var->name) > 0) && (var->name[0] == '_')) {
81                 if ((strlen(var->name) > 1) && (var->name[1] == '_'))
82                         name = (char*)&(var->name[2]);
83                 else
84                         name = (char*)&(var->name[1]);
85         } else
86                 name = var->name;
87         return name;
88 }
89
90 char *ast_var_full_name(struct ast_var_t *var)
91 {
92         return (var != NULL ? var->name : NULL);
93 }
94
95 char *ast_var_value(struct ast_var_t *var)
96 {
97         return (var != NULL ? var->value : NULL);
98 }
99
100