lib/kmi/tree.c

Go to the documentation of this file.
00001 /*
00002  * $Id: tree.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 
00034 #include <string.h>
00035 #include <stdio.h>
00036 #include <errno.h>
00037 #include "../../dprint.h"
00038 #include "mi_mem.h"
00039 #include "tree.h"
00040 #include "fmt.h"
00041 
00042 
00043 static int use_shm = 0;
00044 
00045 struct mi_root *init_mi_tree(unsigned int code, char *reason, int reason_len)
00046 {
00047         struct mi_root *root;
00048 
00049         if (use_shm)
00050                 root = (struct mi_root *)shm_malloc(sizeof(struct mi_root));
00051         else
00052                 root = (struct mi_root *)mi_malloc(sizeof(struct mi_root));
00053         if (!root) {
00054                 LM_ERR("no more pkg mem\n");
00055                 return NULL;
00056         }
00057 
00058         memset(root,0,sizeof(struct mi_root));
00059         root->node.next = root->node.last = &root->node;
00060 
00061         if (reason && reason_len) {
00062                 root->reason.s = reason;
00063                 root->reason.len = reason_len;
00064         }
00065         root->code = code;
00066 
00067         return root;
00068 }
00069 
00070 
00071 static void free_mi_node(struct mi_node *parent)
00072 {
00073         struct mi_node *p, *q;
00074 
00075         for(p = parent->kids ; p ; ){
00076                 q = p;
00077                 p = p->next;
00078                 free_mi_node(q);
00079         }
00080 
00081         if (use_shm) {
00082                 shm_free(parent);
00083         } else {
00084                 del_mi_attr_list(parent);
00085                 mi_free(parent);
00086         }
00087 }
00088 
00089 void free_mi_tree(struct mi_root *parent)
00090 {
00091         struct mi_node *p, *q;
00092 
00093         for(p = parent->node.kids ; p ; ){
00094                 q = p;
00095                 p = p->next;
00096                 free_mi_node(q);
00097         }
00098 
00099         if (use_shm)
00100                 shm_free(parent);
00101         else
00102                 mi_free(parent);
00103 }
00104 
00105 
00106 static inline struct mi_node *create_mi_node(char *name, int name_len,
00107                                                                         char *value, int value_len, int flags)
00108 {
00109         struct mi_node *new;
00110         int size_mem;
00111         int name_pos;
00112         int value_pos;
00113 
00114         if (!name) name_len=0;
00115         if (!name_len) name=0;
00116         if (!value) value_len=0;
00117         if (!value_len) value=0;
00118 
00119         if (!name && !value)
00120                 return NULL;
00121 
00122         size_mem = sizeof(struct mi_node);
00123         value_pos = name_pos = 0;
00124 
00125         if (name && (flags & MI_DUP_NAME)){
00126                 name_pos = size_mem;
00127                 size_mem += name_len;
00128         }
00129         if (value && (flags & MI_DUP_VALUE)){
00130                 value_pos = size_mem;
00131                 size_mem += value_len;
00132         }
00133 
00134         if (use_shm)
00135                 new = (struct mi_node *)shm_malloc(size_mem);
00136         else
00137                 new = (struct mi_node *)mi_malloc(size_mem);
00138         if(!new) {
00139                 LM_ERR("no more pkg mem\n");
00140                 return NULL;
00141         }
00142         memset(new,0,size_mem);
00143 
00144         if (name) {
00145                 new->name.len = name_len;
00146                 if(flags & MI_DUP_NAME){
00147                         new->name.s = ((char *)new) + name_pos;
00148                         strncpy(new->name.s, name, name_len);
00149                 } else{
00150                         new->name.s = name;
00151                 }
00152         }
00153 
00154         if (value) {
00155                 new->value.len = value_len;
00156                 if(flags & MI_DUP_VALUE){
00157                         new->value.s = ((char *)new) + value_pos;
00158                         strncpy(new->value.s, value, value_len);
00159                 }else{
00160                         new->value.s = value;
00161                 }
00162         }
00163         new->last = new;
00164 
00165         return new;
00166 }
00167 
00168 
00169 static inline struct mi_node *add_next(struct mi_node *brother,
00170                         char *name, int name_len, char *value, int value_len, int flags)
00171 {
00172         struct mi_node *new;
00173 
00174         if(!brother)
00175                 return NULL;
00176         
00177         new = create_mi_node(name, name_len, value, value_len, flags);
00178         if(!new)
00179                 return NULL;
00180 
00181         brother->last->next = new;
00182         brother->last = new;
00183 
00184         return new;
00185 }
00186 
00187 
00188 struct mi_node *add_mi_node_sibling( struct mi_node *brother, int flags,
00189                                                 char *name, int name_len, char *value, int value_len)
00190 {
00191         return add_next(brother, name, name_len, value, value_len, flags);
00192 }
00193 
00194 
00195 struct mi_node *addf_mi_node_sibling(struct mi_node *brother, int flags,
00196                                                         char *name, int name_len, char *fmt_val, ...)
00197 {
00198         va_list ap;
00199         char *p;
00200         int  len;
00201 
00202         va_start(ap, fmt_val);
00203         p = mi_print_fmt( fmt_val, ap, &len);
00204         va_end(ap);
00205         if (p==NULL)
00206                 return 0;
00207         return add_mi_node_sibling( brother, flags|MI_DUP_VALUE,
00208                 name, name_len, p, len);
00209 }
00210 
00211 
00212 struct mi_node *add_mi_node_child( struct mi_node *parent, int flags,
00213                                                 char *name, int name_len, char *value, int value_len)
00214 {
00215         if(parent->kids){
00216                 return add_next(parent->kids, name, name_len, value, value_len, flags);
00217         }else{
00218                 parent->kids = create_mi_node(name, name_len, value, value_len, flags);
00219                 return parent->kids;
00220         }
00221 }
00222 
00223 
00224 struct mi_node *addf_mi_node_child(struct mi_node *parent, int flags,
00225                                                         char *name, int name_len, char *fmt_val, ...)
00226 {
00227         va_list ap;
00228         char *p;
00229         int  len;
00230 
00231         va_start(ap, fmt_val);
00232         p = mi_print_fmt( fmt_val, ap, &len);
00233         va_end(ap);
00234         if (p==NULL)
00235                 return 0;
00236         return add_mi_node_child( parent, flags|MI_DUP_VALUE,
00237                 name, name_len, p, len);
00238 }
00239 
00240 
00241 static int clone_mi_node(struct mi_node *org, struct mi_node *parent)
00242 {
00243         struct mi_node *p, *q;
00244 
00245         for(p = org->kids ; p ; p=p->next){
00246                 q = add_mi_node_child( parent, MI_DUP_VALUE|MI_DUP_NAME,
00247                         p->name.s, p->name.len, p->value.s, p->value.len);
00248                 if (q==NULL)
00249                         return -1;
00250                 if (clone_mi_node( p, q)!=0)
00251                         return -1;
00252         }
00253         return 0;
00254 }
00255 
00256 
00257 struct mi_root* clone_mi_tree(struct mi_root *org, int shm)
00258 {
00259         struct mi_root *root;
00260 
00261         use_shm = shm?1:0;
00262 
00263         root = init_mi_tree( org->code, org->reason.s, org->reason.len);
00264         if (root==NULL)
00265                 goto done;
00266 
00267         if (clone_mi_node( &(org->node), &(root->node) )!=0 ) {
00268                 free_mi_tree(root);
00269                 root = NULL;
00270                 goto done;
00271         }
00272 
00273 done:
00274         use_shm=0;
00275         return root;
00276 }
00277 
00278 
00279 
00280 void free_shm_mi_tree(struct mi_root *parent)
00281 {
00282         use_shm = 1;
00283         free_mi_tree(parent);
00284         use_shm = 0;
00285 }