attr.c

Go to the documentation of this file.
00001 /*
00002  * $Id: attr.c 4518 2008-07-28 15:39:28Z henningw $
00003  *
00004  * Copyright (C) 2006 Voice Sistem SRL
00005  *
00006  * This file is part of Kamailio, a free SIP server.
00007  *
00008  * Kamailio is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * Kamailio is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program; if not, write to the Free Software
00020  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00021  *
00022  *
00023  * History:
00024  * ---------
00025  *  2006-09-08  first version (bogdan)
00026  */
00027 
00028 /*!
00029  * \file 
00030  * \brief MI :: Attributes
00031  * \ingroup mi
00032  */
00033 
00034 
00035 #include <stdio.h>
00036 #include <string.h>
00037 #include <errno.h>
00038 
00039 #include "../mem/mem.h"
00040 #include "../dprint.h"
00041 #include "attr.h"
00042 #include "fmt.h"
00043 
00044 
00045 extern char *mi_ap_buf;
00046 extern int  mi_ap_buf_len;
00047 
00048 
00049 struct mi_attr *add_mi_attr(struct mi_node *node, int flags,
00050                   char *name, int name_len, char *value, int value_len)
00051 {
00052    struct mi_attr *new, *p;
00053    int size_mem, name_pos, value_pos;
00054 
00055    if(!node)
00056       return NULL;
00057 
00058    if (!name) name_len=0;
00059    if (!name_len) name=0;
00060    if (!value) value_len=0;
00061    if (!value_len) value=0;
00062 
00063    if(!name && !value)
00064       return NULL;
00065 
00066    size_mem = sizeof(struct mi_attr);
00067    value_pos = name_pos = 0;
00068 
00069    if(name && (flags & MI_DUP_NAME)){
00070       name_pos = size_mem;
00071       size_mem += name_len;
00072    }
00073    if(value && (flags & MI_DUP_VALUE)){
00074       value_pos = size_mem;
00075       size_mem += value_len;
00076    }
00077 
00078    new = (struct mi_attr *)pkg_malloc(size_mem);
00079    if (!new) {
00080       LM_ERR("no more pkg mem (%d)\n",size_mem);
00081       return NULL;
00082    }
00083    memset(new,0,size_mem);
00084 
00085    if (name) {
00086       new->name.len = name_len;
00087       if(flags & MI_DUP_NAME){
00088          new->name.s = ((char *)new) + name_pos;
00089          strncpy(new->name.s, name, name_len);
00090       } else{
00091          new->name.s = name;
00092       }
00093    }
00094 
00095    if (value) {
00096       new->value.len = value_len;
00097       if(flags & MI_DUP_VALUE){
00098          new->value.s = ((char *)new) + value_pos;
00099          strncpy(new->value.s, value, value_len);
00100       }else{
00101          new->value.s = value;
00102       }
00103    }
00104    if(flags & MI_DUP_NAME){
00105       name_pos = size_mem;
00106       size_mem += name_len * sizeof(char);
00107    }
00108    if(flags & MI_DUP_VALUE){
00109       value_pos = size_mem;
00110       size_mem += value_len * sizeof(char);
00111    }
00112 
00113    if(!(node->attributes)){
00114       new->next = NULL;
00115       return (node->attributes = new);
00116    }
00117 
00118    for(p = node->attributes ; p->next ; p = p->next);
00119 
00120    new->next = NULL;
00121    p->next = new;
00122 
00123    return new;
00124 }
00125 
00126 
00127 
00128 struct mi_attr *addf_mi_attr(struct mi_node *node, int flags,
00129                      char *name, int name_len, char *fmt_val, ...)
00130 {
00131    va_list ap;
00132    char *p;
00133    int  len = 0;
00134 
00135    va_start(ap, fmt_val);
00136    p = mi_print_fmt( fmt_val, ap, &len);
00137    va_end(ap);
00138    if (p==NULL)
00139       return 0;
00140    return add_mi_attr(node, flags|MI_DUP_VALUE, name, name_len, p, len);
00141 }
00142 
00143 
00144 
00145 struct mi_attr *get_mi_attr_by_name(struct mi_node *node, char *name, int len)
00146 {
00147    struct mi_attr *head;
00148 
00149    if(!node || !name || !(node->attributes))
00150       return NULL;
00151 
00152    for(head = node->attributes ; head->next ; head = head->next)
00153       if(len == head->name.len 
00154       && !strncasecmp(name, head->name.s, head->name.len))
00155          return head;
00156 
00157    return NULL;
00158 }
00159 
00160 
00161 void del_mi_attr_list(struct mi_node *node)
00162 {
00163    struct mi_attr *p, *head;
00164 
00165    if(!node || !(node->attributes))
00166       return;
00167 
00168    for(head = node->attributes; head ;){
00169       p = head->next;
00170       pkg_free(head);
00171       head = p;
00172    }
00173 
00174    node->attributes = NULL;
00175 }
00176 

Generated on Thu May 17 12:00:24 2012 for Kamailio - The Open Source SIP Server by  doxygen 1.5.6