chanvars.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2005, Digium, Inc.
00005  *
00006  * Mark Spencer <markster@digium.com>
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 /*! \file
00020  *
00021  * \brief Channel Variables
00022  *
00023  * \author Mark Spencer <markster@digium.com>
00024  */
00025 
00026 /*** MODULEINFO
00027    <support_level>core</support_level>
00028  ***/
00029 
00030 #include "asterisk.h"
00031 
00032 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 424965 $")
00033 
00034 #include "asterisk/chanvars.h"
00035 #include "asterisk/strings.h"
00036 #include "asterisk/utils.h"
00037 
00038 #ifdef MALLOC_DEBUG
00039 struct ast_var_t *_ast_var_assign(const char *name, const char *value, const char *file, int lineno, const char *function)
00040 #else
00041 struct ast_var_t *ast_var_assign(const char *name, const char *value)
00042 #endif
00043 {
00044    struct ast_var_t *var;
00045    int name_len = strlen(name) + 1;
00046    int value_len = strlen(value) + 1;
00047 
00048 #ifdef MALLOC_DEBUG
00049    if (!(var = __ast_calloc(sizeof(*var) + name_len + value_len, sizeof(char), file, lineno, function))) {
00050 #else
00051    if (!(var = ast_calloc(sizeof(*var) + name_len + value_len, sizeof(char)))) {
00052 #endif
00053       return NULL;
00054    }
00055 
00056    ast_copy_string(var->name, name, name_len);
00057    var->value = var->name + name_len;
00058    ast_copy_string(var->value, value, value_len);
00059 
00060    return var;
00061 }
00062 
00063 void ast_var_delete(struct ast_var_t *var)
00064 {
00065    ast_free(var);
00066 }
00067 
00068 const char *ast_var_name(const struct ast_var_t *var)
00069 {
00070    const char *name;
00071 
00072    if (var == NULL || (name = var->name) == NULL)
00073       return NULL;
00074    /* Return the name without the initial underscores */
00075    if (name[0] == '_') {
00076       name++;
00077       if (name[0] == '_')
00078          name++;
00079    }
00080    return name;
00081 }
00082 
00083 const char *ast_var_full_name(const struct ast_var_t *var)
00084 {
00085    return (var ? var->name : NULL);
00086 }
00087 
00088 const char *ast_var_value(const struct ast_var_t *var)
00089 {
00090    return (var ? var->value : NULL);
00091 }
00092 
00093 char *ast_var_find(const struct varshead *head, const char *name)
00094 {
00095    struct ast_var_t *var;
00096 
00097    AST_LIST_TRAVERSE(head, var, entries) {
00098       if (!strcmp(name, var->name)) {
00099          return var->value;
00100       }
00101    }
00102    return NULL;
00103 }
00104 
00105 struct varshead *ast_var_list_create(void)
00106 {
00107    struct varshead *head;
00108 
00109    head = ast_calloc(1, sizeof(*head));
00110    if (!head) {
00111       return NULL;
00112    }
00113    AST_LIST_HEAD_INIT_NOLOCK(head);
00114    return head;
00115 }
00116 
00117 void ast_var_list_destroy(struct varshead *head)
00118 {
00119    struct ast_var_t *var;
00120 
00121    if (!head) {
00122       return;
00123    }
00124 
00125    while ((var = AST_LIST_REMOVE_HEAD(head, entries))) {
00126       ast_var_delete(var);
00127    }
00128 
00129    ast_free(head);
00130 }
00131 
00132 struct varshead *ast_var_list_clone(struct varshead *head)
00133 {
00134    struct varshead *clone;
00135    struct ast_var_t *var, *newvar;
00136 
00137    if (!head) {
00138       return NULL;
00139    }
00140 
00141    clone = ast_var_list_create();
00142    if (!clone) {
00143       return NULL;
00144    }
00145 
00146    AST_VAR_LIST_TRAVERSE(head, var) {
00147       newvar = ast_var_assign(var->name, var->value);
00148       if (!newvar) {
00149          ast_var_list_destroy(clone);
00150          return NULL;
00151       }
00152       AST_VAR_LIST_INSERT_TAIL(clone, newvar);
00153    }
00154 
00155    return clone;
00156 }

Generated on Thu Apr 16 06:27:30 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6