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 
00035 #include <stdio.h>
00036 #include <string.h>
00037 #include <errno.h>
00038 
00039 #include "../../dprint.h"
00040 #include "mi_mem.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 *)mi_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 
00105         if(!(node->attributes)){
00106                 new->next = NULL;
00107                 return (node->attributes = new);
00108         }
00109 
00110         for(p = node->attributes ; p->next ; p = p->next);
00111 
00112         new->next = NULL;
00113         p->next = new;
00114 
00115         return new;
00116 }
00117 
00118 
00119 
00120 struct mi_attr *addf_mi_attr(struct mi_node *node, int flags,
00121                                                         char *name, int name_len, char *fmt_val, ...)
00122 {
00123         va_list ap;
00124         char *p;
00125         int  len = 0;
00126 
00127         va_start(ap, fmt_val);
00128         p = mi_print_fmt( fmt_val, ap, &len);
00129         va_end(ap);
00130         if (p==NULL)
00131                 return 0;
00132         return add_mi_attr(node, flags|MI_DUP_VALUE, name, name_len, p, len);
00133 }
00134 
00135 
00136 
00137 struct mi_attr *get_mi_attr_by_name(struct mi_node *node, char *name, int len)
00138 {
00139         struct mi_attr *head;
00140 
00141         if(!node || !name || !(node->attributes))
00142                 return NULL;
00143 
00144         for(head = node->attributes ; head->next ; head = head->next)
00145                 if(len == head->name.len 
00146                 && !strncasecmp(name, head->name.s, head->name.len))
00147                         return head;
00148 
00149         return NULL;
00150 }
00151 
00152 
00153 void del_mi_attr_list(struct mi_node *node)
00154 {
00155         struct mi_attr *p, *head;
00156 
00157         if(!node || !(node->attributes))
00158                 return;
00159 
00160         for(head = node->attributes; head ;){
00161                 p = head->next;
00162                 mi_free(head);
00163                 head = p;
00164         }
00165 
00166         node->attributes = NULL;
00167 }
00168